From 8705fe23834ec9a164ca21e8c141fded97af28de Mon Sep 17 00:00:00 2001 From: Richard Purdie Date: Sat, 18 Jan 2014 14:26:07 +0000 Subject: bitbake: user-manual-metadata: Rework section about shell/python functions (Bitbake rev: c2bcb5364ff7c702bc1ec2726169f608b445f979) Signed-off-by: Richard Purdie --- bitbake/doc/user-manual/user-manual-metadata.xml | 61 +++++++++++++++++++----- 1 file changed, 49 insertions(+), 12 deletions(-) diff --git a/bitbake/doc/user-manual/user-manual-metadata.xml b/bitbake/doc/user-manual/user-manual-metadata.xml index 23b3fa67d3..55b59ebcc1 100644 --- a/bitbake/doc/user-manual/user-manual-metadata.xml +++ b/bitbake/doc/user-manual/user-manual-metadata.xml @@ -409,22 +409,59 @@ -
- Defining executable metadata - NOTE: This is only supported in .bb and .bbclass files. +
+ Functions + + + This is only supported in .bb + and .bbclass files. + + + + As with most languages, functions are the building blocks + that define operations. + Bitbake supports shell and Python functions. + An example shell function definition is: + + some_function () { + echo "Hello World" + } + + An example Python function definition is: -do_mytask () { - echo "Hello, world!" -} + python some_python_function () { + d.setVar("TEXT", "Hello World") + print d.getVar("TEXT", True) + } - This is essentially identical to setting a variable, except that this variable happens to be executable shell code. + In python functions, the "bb" and "os" modules are already + imported, there is no need to import those modules. + The datastore, "d" is also a global variable and always + available to these functions automatically. + + + + Bitbake will execute functions of this form using + the bb.build.exec_func(), which can also be + called from Python functions to execute other functions, + either shell or Python based. + Shell functions can only execute other shell functions. + + + + There is also a second way to declare python functions with + parameters which takes the form: -python do_printdate () { - import time - print time.strftime('%Y%m%d', time.gmtime()) -} + def some_python_function(arg1, arg2): + print arg1 + " " + arg2 - This is the similar to the previous, but flags it as Python so that BitBake knows it is Python code. + The difference is that the second form takes parameters, + the datastore is not available automatically + and must be passed as a parameter and these functions are + not called with the exec_func() but are + executed with direct Python function calls. + The "bb" and "os" modules are still automatically available + and there is no need to import them.
-- cgit v1.2.3-54-g00ecf