TipsAndTricks/Tinfoil: Difference between revisions

From Yocto Project
Jump to navigationJump to search
(Created page with " {| style="color:black; background-color:#ffffcc" width="100%" cellpadding="10" class="wikitable" | '''NOTE:''' This is currently a draft, needs additional content & editing -...")
 
No edit summary
Line 6: Line 6:
= Writing simple utility scripts using Tinfoil =
= Writing simple utility scripts using Tinfoil =


The tinfoil API, which was introduced to BitBake a number of years ago and underwent a major rework in the 2.3 (pyro) release, provides a way for scripts to interact with the metadata and call into BitBake code. Some of the scripts that come with BitBake / OpenEmbedded make heavy use of it - <code>bitbake-layers</code>, <code>devtool</code>, <code>recipetool</code> and <code>oe-pkgdata-util</code> are examples.
The tinfoil API, which was introduced to BitBake a number of years ago and underwent a major rework in the 2.3 (pyro) release, provides a way for scripts to interact with the metadata and call into BitBake/OpenEmbedded code. Some of the scripts that come with BitBake / OpenEmbedded make heavy use of it - <code>bitbake-layers</code>, <code>devtool</code>, <code>recipetool</code> and <code>oe-pkgdata-util</code> are examples.


Let's take a look at a simple example which reads a configuration value - <code>TMPDIR</code> to be specific. This needs to be placed in the scripts directory to work as-is, although you can modify the import code to make it work from anywhere:
== Getting a variable value ==
 
Let's take a look at a simple example which reads a configuration-level variable value - <code>TMPDIR</code> to be specific. This script needs to be placed in the <code>scripts</code> subdirectory to work as-is, although you can modify the import code to make it work from anywhere:


<pre>
<pre>
Line 30: Line 32:
     print('TMPDIR is "%s"' % tmpdir)
     print('TMPDIR is "%s"' % tmpdir)
</pre>
</pre>


If you are querying multiple variable values this is much more efficient than calling out to "bitbake -e" and parsing its output.
If you are querying multiple variable values this is much more efficient than calling out to "bitbake -e" and parsing its output.


== Parsing recipes ==


Here's another example where we parse a couple of recipes:
Here's another example where we parse a couple of recipes:

Revision as of 01:28, 31 May 2017

NOTE: This is currently a draft, needs additional content & editing - PaulEggleton (talk) 18:22, 30 May 2017 (PDT)

Writing simple utility scripts using Tinfoil

The tinfoil API, which was introduced to BitBake a number of years ago and underwent a major rework in the 2.3 (pyro) release, provides a way for scripts to interact with the metadata and call into BitBake/OpenEmbedded code. Some of the scripts that come with BitBake / OpenEmbedded make heavy use of it - bitbake-layers, devtool, recipetool and oe-pkgdata-util are examples.

Getting a variable value

Let's take a look at a simple example which reads a configuration-level variable value - TMPDIR to be specific. This script needs to be placed in the scripts subdirectory to work as-is, although you can modify the import code to make it work from anywhere:

#!/usr/bin/env python3

import os
import sys

# Set up sys.path to let us import tinfoil
scripts_path = os.path.dirname(os.path.realpath(__file__))
lib_path = scripts_path + '/lib'
sys.path.insert(0, lib_path)
import scriptpath
scriptpath.add_bitbake_lib_path()

import bb.tinfoil

with bb.tinfoil.Tinfoil() as tinfoil:
    tinfoil.prepare(config_only=True)
    tmpdir = tinfoil.config_data.getVar('TMPDIR')
    print('TMPDIR is "%s"' % tmpdir)

If you are querying multiple variable values this is much more efficient than calling out to "bitbake -e" and parsing its output.

Parsing recipes

Here's another example where we parse a couple of recipes:

#!/usr/bin/env python3

import os
import sys

# Set up sys.path to let us import tinfoil
scripts_path = os.path.dirname(os.path.realpath(__file__))
lib_path = scripts_path + '/lib'
sys.path.insert(0, lib_path)
import scriptpath
scriptpath.add_bitbake_lib_path()

import bb.tinfoil

with bb.tinfoil.Tinfoil() as tinfoil:
    tinfoil.prepare(config_only=False)

    rd = tinfoil.parse_recipe('gzip')
    print('gzip SRC_URI = "%s"' % rd.getVar('SRC_URI'))

    rd = tinfoil.parse_recipe('virtual/kernel')
    print('Kernel recipe is %s' % rd.getVar('PN'))

Notice that in this second example we pass config_only=False - this is so tinfoil will load the data for all recipes enabled in your configuration, just as bitbake does every time you run a normal build. You don't have to specify config_only=False to parse a recipe file if you know the path to it, but it is required if you want to specify it simply by name and get the preferred version / provider.

The parse_recipe() function will accept recipe names, or providers such as virtual/kernel in the second call above. It returns a datastore (usually seen as d within recipes and other python code within OE).