summaryrefslogtreecommitdiffstats
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
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>
-rw-r--r--scripts/lib/image/engine.py23
-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
-rwxr-xr-xscripts/wic21
5 files changed, 38 insertions, 103 deletions
diff --git a/scripts/lib/image/engine.py b/scripts/lib/image/engine.py
index 47950f8d15..92dcc5a4f9 100644
--- a/scripts/lib/image/engine.py
+++ b/scripts/lib/image/engine.py
@@ -60,29 +60,6 @@ def verify_build_env():
60 return True 60 return True
61 61
62 62
63def find_artifacts(image_name):
64 """
65 Gather the build artifacts for the current image (the image_name
66 e.g. core-image-minimal) for the current MACHINE set in local.conf
67 """
68 bitbake_env_lines = misc.get_bitbake_env_lines()
69
70 rootfs_dir = kernel_dir = bootimg_dir = native_sysroot = ""
71
72 for line in bitbake_env_lines.split('\n'):
73 if misc.get_line_val(line, "IMAGE_ROOTFS"):
74 rootfs_dir = misc.get_line_val(line, "IMAGE_ROOTFS")
75 continue
76 if misc.get_line_val(line, "DEPLOY_DIR_IMAGE"):
77 kernel_dir = misc.get_line_val(line, "DEPLOY_DIR_IMAGE")
78 continue
79 if misc.get_line_val(line, "STAGING_DIR_NATIVE"):
80 native_sysroot = misc.get_line_val(line, "STAGING_DIR_NATIVE")
81 continue
82
83 return (rootfs_dir, kernel_dir, bootimg_dir, native_sysroot)
84
85
86CANNED_IMAGE_DIR = "lib/image/canned-wks" # relative to scripts 63CANNED_IMAGE_DIR = "lib/image/canned-wks" # relative to scripts
87SCRIPTS_CANNED_IMAGE_DIR = "scripts/" + CANNED_IMAGE_DIR 64SCRIPTS_CANNED_IMAGE_DIR = "scripts/" + CANNED_IMAGE_DIR
88 65
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 """
diff --git a/scripts/wic b/scripts/wic
index a39ec95e0b..b75d122482 100755
--- a/scripts/wic
+++ b/scripts/wic
@@ -52,7 +52,7 @@ if bitbake_exe:
52else: 52else:
53 bitbake_main = None 53 bitbake_main = None
54 54
55from wic.utils.oe.misc import find_bitbake_env_lines, set_bitbake_env_lines 55from wic.utils.oe.misc import get_bitbake_var
56from wic.utils.errors import WicError 56from wic.utils.errors import WicError
57from image import engine 57from image import engine
58from image import help as hlp 58from image import help as hlp
@@ -141,12 +141,6 @@ def wic_create_subcommand(args, usage_str):
141 else: 141 else:
142 print "Done.\n" 142 print "Done.\n"
143 143
144 bitbake_env_lines = find_bitbake_env_lines(options.image_name)
145 if not bitbake_env_lines:
146 print "Couldn't get bitbake environment, exiting."
147 sys.exit(1)
148 set_bitbake_env_lines(bitbake_env_lines)
149
150 bootimg_dir = "" 144 bootimg_dir = ""
151 145
152 if options.image_name: 146 if options.image_name:
@@ -160,9 +154,10 @@ def wic_create_subcommand(args, usage_str):
160 cookerdata.CookerConfiguration()): 154 cookerdata.CookerConfiguration()):
161 sys.exit(1) 155 sys.exit(1)
162 156
163 (rootfs_dir, kernel_dir, bootimg_dir, native_sysroot) \ 157 rootfs_dir = get_bitbake_var("IMAGE_ROOTFS", options.image_name)
164 = engine.find_artifacts(options.image_name) 158 kernel_dir = get_bitbake_var("DEPLOY_DIR_IMAGE", options.image_name)
165 159 native_sysroot = get_bitbake_var("STAGING_DIR_NATIVE",
160 options.image_name)
166 else: 161 else:
167 if options.build_rootfs: 162 if options.build_rootfs:
168 print "Image name is not specified, exiting. (Use -e/--image-name to specify it)\n" 163 print "Image name is not specified, exiting. (Use -e/--image-name to specify it)\n"
@@ -244,12 +239,6 @@ def wic_list_subcommand(args, usage_str):
244 239
245 (options, args) = parser.parse_args(args) 240 (options, args) = parser.parse_args(args)
246 241
247 bitbake_env_lines = find_bitbake_env_lines(None)
248 if not bitbake_env_lines:
249 print "Couldn't get bitbake environment, exiting."
250 sys.exit(1)
251 set_bitbake_env_lines(bitbake_env_lines)
252
253 if not engine.wic_list(args, scripts_path, options.properties_file): 242 if not engine.wic_list(args, scripts_path, options.properties_file):
254 logging.error("Bad list arguments, exiting\n") 243 logging.error("Bad list arguments, exiting\n")
255 parser.print_help() 244 parser.print_help()