From 3229d37993e315c9ca1902849746b9f50f35845c Mon Sep 17 00:00:00 2001 From: Ed Bartosh Date: Sun, 30 Aug 2015 20:47:00 +0300 Subject: 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 Signed-off-by: Richard Purdie --- scripts/lib/wic/utils/oe/misc.py | 91 +++++++++++++++++++++++----------------- 1 file changed, 53 insertions(+), 38 deletions(-) (limited to 'scripts') 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): """ -- cgit 1.2.3-korg