TipsAndTricks/Tinfoil

From Yocto Project
Revision as of 01:22, 31 May 2017 by PaulEggleton (talk | contribs) (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 -...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search
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 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.

Let's take a look at a simple example which reads a configuration value - TMPDIR 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:

#!/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.


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).