diff options
author | Ed Bartosh <ed.bartosh@linux.intel.com> | 2015-08-30 20:47:00 +0300 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2015-08-30 21:38:11 +0100 |
commit | 3229d37993e315c9ca1902849746b9f50f35845c (patch) | |
tree | 0c2ec328e119575a2881f8e3dc85790cf90be9db /scripts/lib/wic | |
parent | d19f6900a07a718660fcd75d36a3facf048ce157 (diff) | |
download | openembedded-core-3229d37993e315c9ca1902849746b9f50f35845c.tar.gz openembedded-core-3229d37993e315c9ca1902849746b9f50f35845c.tar.bz2 openembedded-core-3229d37993e315c9ca1902849746b9f50f35845c.zip |
wic: add BitbakeVars class
Moved code of getting bitbake variables into separate class.
Created singleton object of this class in the module namespace.
Preserved existing API get_bitbake_var.
Signed-off-by: Ed Bartosh <ed.bartosh@linux.intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'scripts/lib/wic')
-rw-r--r-- | scripts/lib/wic/utils/oe/misc.py | 91 |
1 files changed, 53 insertions, 38 deletions
diff --git a/scripts/lib/wic/utils/oe/misc.py b/scripts/lib/wic/utils/oe/misc.py index 9c8f52dd0d..3537a2e711 100644 --- a/scripts/lib/wic/utils/oe/misc.py +++ b/scripts/lib/wic/utils/oe/misc.py @@ -124,48 +124,63 @@ def exec_native_cmd(cmd_and_args, native_sysroot, catch=3): BOOTDD_EXTRA_SPACE = 16384 -_BITBAKE_VARS = defaultdict(dict) +class BitbakeVars(defaultdict): + """ + Container for Bitbake variables. + """ + def __init__(self): + defaultdict.__init__(self, dict) + + def get_var(self, var, image=None): + """ + Get bitbake variable value lazy way, i.e. run + 'bitbake -e' only when variable is requested. + """ + if image not in self: + # Get bitbake -e output + cmd = "bitbake -e" + if image: + cmd += " %s" % image + + log_level = msger.get_loglevel() + msger.set_loglevel('normal') + ret, lines = _exec_cmd(cmd) + msger.set_loglevel(log_level) + + if ret: + print "Couldn't get '%s' output." % cmd + print "Bitbake failed with error:\n%s\n" % lines + return + + # Parse bitbake -e output + for line in lines.split('\n'): + if "=" not in line: + continue + try: + key, val = line.split("=") + except ValueError: + continue + key = key.strip() + val = val.strip() + if key.replace('_', '').isalnum(): + self[image][key] = val.strip('"') + + # Make first image a default set of variables + images = [key for key in self if key] + if len(images) == 1: + self[None] = self[image] + + return self[image].get(var) + +# Create BB_VARS singleton +BB_VARS = BitbakeVars() def get_bitbake_var(var, image=None): """ - Get bitbake variable value lazy way, i.e. run - 'bitbake -e' only when variable is requested. + Provide old get_bitbake_var API by wrapping + get_var method of BB_VARS singleton. """ - if image not in _BITBAKE_VARS: - # Get bitbake -e output - cmd = "bitbake -e" - if image: - cmd += " %s" % image - - log_level = msger.get_loglevel() - msger.set_loglevel('normal') - ret, lines = _exec_cmd(cmd) - msger.set_loglevel(log_level) - - if ret: - print "Couldn't get '%s' output." % cmd - print "Bitbake failed with error:\n%s\n" % lines - return - - # Parse bitbake -e output - for line in lines.split('\n'): - if "=" not in line: - continue - try: - key, val = line.split("=") - except ValueError: - continue - key = key.strip() - val = val.strip() - if key.replace('_', '').isalnum(): - _BITBAKE_VARS[image][key] = val.strip('"') - - # Make first image a default set of variables - images = [key for key in _BITBAKE_VARS if key] - if len(images) == 1: - _BITBAKE_VARS[None] = _BITBAKE_VARS[image] - - return _BITBAKE_VARS[image].get(var) + return BB_VARS.get_var(var, image) def parse_sourceparams(sourceparams): """ |