summaryrefslogtreecommitdiffstats
path: root/scripts/lib/wic
diff options
context:
space:
mode:
authorEd Bartosh <ed.bartosh@linux.intel.com>2015-06-26 21:27:31 +0300
committerRichard Purdie <richard.purdie@linuxfoundation.org>2015-07-02 23:08:36 +0100
commitaa470a1fcd09abbc570d3d885aff460603ef9925 (patch)
treef04cc1fe57fa3a1310ce8c7cab3cd213d5b7c6a1 /scripts/lib/wic
parent673343414f4c9db32e3e0c58f07c59954bab734c (diff)
downloadpoky-aa470a1fcd09abbc570d3d885aff460603ef9925.tar.gz
wic: Refactor getting bitbake variables
Wic gets bitbake variables by parsing output of 'bitbake -e' command. This implementation improves this procedure as it runs 'bitbake -e' only when API is called and does it only once, i.e. in a "lazy" way. As parsing results are cached 'bitbake -e' is run only once and results are parsed only once per requested set of variables. get_bitbake_var became the only API call. It replaces find_artifacts, find_artifact, find_bitbake_env_lines, get_bitbake_env_lines, set_bitbake_env_lines and get_line_val calls making API much more clear. (From OE-Core rev: 3abe23bd217315246ec2d98dc9c390b85cfe6a92) 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/plugins/source/rootfs.py9
-rw-r--r--scripts/lib/wic/plugins/source/rootfs_pcbios_ext.py6
-rw-r--r--scripts/lib/wic/utils/oe/misc.py82
3 files changed, 33 insertions, 64 deletions
diff --git a/scripts/lib/wic/plugins/source/rootfs.py b/scripts/lib/wic/plugins/source/rootfs.py
index 12fbf67a6e..a90712b247 100644
--- a/scripts/lib/wic/plugins/source/rootfs.py
+++ b/scripts/lib/wic/plugins/source/rootfs.py
@@ -29,7 +29,7 @@ import os
29 29
30from wic import msger 30from wic import msger
31from wic.pluginbase import SourcePlugin 31from wic.pluginbase import SourcePlugin
32from wic.utils.oe.misc import find_bitbake_env_lines, find_artifact 32from wic.utils.oe.misc import get_bitbake_var
33 33
34class RootfsPlugin(SourcePlugin): 34class RootfsPlugin(SourcePlugin):
35 """ 35 """
@@ -43,12 +43,7 @@ class RootfsPlugin(SourcePlugin):
43 if os.path.isdir(rootfs_dir): 43 if os.path.isdir(rootfs_dir):
44 return rootfs_dir 44 return rootfs_dir
45 45
46 bitbake_env_lines = find_bitbake_env_lines(rootfs_dir) 46 image_rootfs_dir = get_bitbake_var("IMAGE_ROOTFS", rootfs_dir)
47 if not bitbake_env_lines:
48 msg = "Couldn't get bitbake environment, exiting."
49 msger.error(msg)
50
51 image_rootfs_dir = find_artifact(bitbake_env_lines, "IMAGE_ROOTFS")
52 if not os.path.isdir(image_rootfs_dir): 47 if not os.path.isdir(image_rootfs_dir):
53 msg = "No valid artifact IMAGE_ROOTFS from image named" 48 msg = "No valid artifact IMAGE_ROOTFS from image named"
54 msg += " %s has been found at %s, exiting.\n" % \ 49 msg += " %s has been found at %s, exiting.\n" % \
diff --git a/scripts/lib/wic/plugins/source/rootfs_pcbios_ext.py b/scripts/lib/wic/plugins/source/rootfs_pcbios_ext.py
index 533eaa7bd1..76e7b033fb 100644
--- a/scripts/lib/wic/plugins/source/rootfs_pcbios_ext.py
+++ b/scripts/lib/wic/plugins/source/rootfs_pcbios_ext.py
@@ -59,11 +59,7 @@ class RootfsPlugin(SourcePlugin):
59 if os.path.isdir(rootfs_dir): 59 if os.path.isdir(rootfs_dir):
60 return rootfs_dir 60 return rootfs_dir
61 61
62 bitbake_env_lines = misc.find_bitbake_env_lines(rootfs_dir) 62 image_rootfs_dir = misc.get_bitbake_var("IMAGE_ROOTFS", rootfs_dir)
63 if not bitbake_env_lines:
64 msger.error("Couldn't get bitbake environment, exiting.")
65
66 image_rootfs_dir = misc.find_artifact(bitbake_env_lines, "IMAGE_ROOTFS")
67 if not os.path.isdir(image_rootfs_dir): 63 if not os.path.isdir(image_rootfs_dir):
68 msg = "No valid artifact IMAGE_ROOTFS from image named" 64 msg = "No valid artifact IMAGE_ROOTFS from image named"
69 msg += " %s has been found at %s, exiting.\n" % \ 65 msg += " %s has been found at %s, exiting.\n" % \
diff --git a/scripts/lib/wic/utils/oe/misc.py b/scripts/lib/wic/utils/oe/misc.py
index 2f916ddf45..1de6f46a38 100644
--- a/scripts/lib/wic/utils/oe/misc.py
+++ b/scripts/lib/wic/utils/oe/misc.py
@@ -25,6 +25,8 @@
25# Tom Zanussi <tom.zanussi (at] linux.intel.com> 25# Tom Zanussi <tom.zanussi (at] linux.intel.com>
26# 26#
27 27
28from collections import defaultdict
29
28from wic import msger 30from wic import msger
29from wic.utils import runner 31from wic.utils import runner
30 32
@@ -108,62 +110,38 @@ def add_wks_var(key, val):
108 110
109BOOTDD_EXTRA_SPACE = 16384 111BOOTDD_EXTRA_SPACE = 16384
110 112
111__bitbake_env_lines = "" 113_BITBAKE_VARS = defaultdict(dict)
112
113def set_bitbake_env_lines(bitbake_env_lines):
114 global __bitbake_env_lines
115 __bitbake_env_lines = bitbake_env_lines
116 114
117def get_bitbake_env_lines(): 115def get_bitbake_var(var, image=None):
118 return __bitbake_env_lines
119
120def find_bitbake_env_lines(image_name):
121 """
122 If image_name is empty, plugins might still be able to use the
123 environment, so set it regardless.
124 """ 116 """
125 if image_name: 117 Get bitbake variable value lazy way, i.e. run
126 bitbake_env_cmd = "bitbake -e %s" % image_name 118 'bitbake -e' only when variable is requested.
127 else:
128 bitbake_env_cmd = "bitbake -e"
129 rc, bitbake_env_lines = __exec_cmd(bitbake_env_cmd)
130 if rc != 0:
131 print "Couldn't get '%s' output." % bitbake_env_cmd
132 print "Bitbake failed with error:\n%s\n" % bitbake_env_lines
133 return None
134
135 return bitbake_env_lines
136
137def find_artifact(bitbake_env_lines, variable):
138 """ 119 """
139 Gather the build artifact for the current image (the image_name 120 if image not in _BITBAKE_VARS:
140 e.g. core-image-minimal) for the current MACHINE set in local.conf 121 # Get bitbake -e output
141 """ 122 cmd = "bitbake -e"
142 retval = "" 123 if image:
143 124 cmd += " %s" % image
144 for line in bitbake_env_lines.split('\n'): 125 rc, lines = __exec_cmd(cmd)
145 if get_line_val(line, variable): 126 if rc:
146 retval = get_line_val(line, variable) 127 print "Couldn't get '%s' output." % cmd
147 break 128 print "Bitbake failed with error:\n%s\n" % lines
148 129 return
149 return retval 130
131 # Parse bitbake -e output
132 for line in lines.split('\n'):
133 if "=" not in line:
134 continue
135 try:
136 key, val = line.split("=")
137 except ValueError:
138 continue
139 key = key.strip()
140 val = val.strip()
141 if key.replace('_', '').isalnum():
142 _BITBAKE_VARS[image][key] = val.strip('"')
150 143
151def get_line_val(line, key): 144 return _BITBAKE_VARS[image].get(var)
152 """
153 Extract the value from the VAR="val" string
154 """
155 if line.startswith(key + "="):
156 stripped_line = line.split('=')[1]
157 stripped_line = stripped_line.replace('\"', '')
158 return stripped_line
159 return None
160
161def get_bitbake_var(key):
162 for line in __bitbake_env_lines.split('\n'):
163 if get_line_val(line, key):
164 val = get_line_val(line, key)
165 return val
166 return None
167 145
168def parse_sourceparams(sourceparams): 146def parse_sourceparams(sourceparams):
169 """ 147 """