summaryrefslogtreecommitdiffstats
path: root/scripts/lib/wic/utils/oe/misc.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/lib/wic/utils/oe/misc.py')
-rw-r--r--scripts/lib/wic/utils/oe/misc.py205
1 files changed, 205 insertions, 0 deletions
diff --git a/scripts/lib/wic/utils/oe/misc.py b/scripts/lib/wic/utils/oe/misc.py
new file mode 100644
index 0000000000..b0b5baab73
--- /dev/null
+++ b/scripts/lib/wic/utils/oe/misc.py
@@ -0,0 +1,205 @@
1# ex:ts=4:sw=4:sts=4:et
2# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
3#
4# Copyright (c) 2013, Intel Corporation.
5# All rights reserved.
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License version 2 as
9# published by the Free Software Foundation.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License along
17# with this program; if not, write to the Free Software Foundation, Inc.,
18# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19#
20# DESCRIPTION
21# This module provides a place to collect various wic-related utils
22# for the OpenEmbedded Image Tools.
23#
24# AUTHORS
25# Tom Zanussi <tom.zanussi (at] linux.intel.com>
26#
27
28from wic import msger
29from wic.utils import runner
30
31def __exec_cmd(cmd_and_args, as_shell = False, catch = 3):
32 """
33 Execute command, catching stderr, stdout
34
35 Need to execute as_shell if the command uses wildcards
36 """
37 msger.debug("__exec_cmd: %s" % cmd_and_args)
38 args = cmd_and_args.split()
39 msger.debug(args)
40
41 if (as_shell):
42 rc, out = runner.runtool(cmd_and_args, catch)
43 else:
44 rc, out = runner.runtool(args, catch)
45 out = out.strip()
46 msger.debug("__exec_cmd: output for %s (rc = %d): %s" % \
47 (cmd_and_args, rc, out))
48
49 return (rc, out)
50
51
52def exec_cmd(cmd_and_args, as_shell = False, catch = 3):
53 """
54 Execute command, catching stderr, stdout
55
56 Exits if rc non-zero
57 """
58 rc, out = __exec_cmd(cmd_and_args, as_shell, catch)
59
60 if rc != 0:
61 msger.error("exec_cmd: %s returned '%s' instead of 0" % (cmd_and_args, rc))
62
63 return out
64
65
66def exec_cmd_quiet(cmd_and_args, as_shell = False):
67 """
68 Execute command, catching nothing in the output
69
70 Exits if rc non-zero
71 """
72 return exec_cmd(cmd_and_args, as_shell, 0)
73
74
75def exec_native_cmd(cmd_and_args, native_sysroot, catch = 3):
76 """
77 Execute native command, catching stderr, stdout
78
79 Need to execute as_shell if the command uses wildcards
80
81 Always need to execute native commands as_shell
82 """
83 native_paths = \
84 "export PATH=%s/sbin:%s/usr/sbin:%s/usr/bin:$PATH" % \
85 (native_sysroot, native_sysroot, native_sysroot)
86 native_cmd_and_args = "%s;%s" % (native_paths, cmd_and_args)
87 msger.debug("exec_native_cmd: %s" % cmd_and_args)
88
89 args = cmd_and_args.split()
90 msger.debug(args)
91
92 rc, out = __exec_cmd(native_cmd_and_args, True, catch)
93
94 if rc == 127: # shell command-not-found
95 msger.error("A native (host) program required to build the image "
96 "was not found (see details above). Please make sure "
97 "it's installed and try again.")
98
99 return (rc, out)
100
101
102def exec_native_cmd_quiet(cmd_and_args, native_sysroot):
103 """
104 Execute native command, catching nothing in the output
105
106 Need to execute as_shell if the command uses wildcards
107
108 Always need to execute native commands as_shell
109 """
110 return exec_native_cmd(cmd_and_args, native_sysroot, 0)
111
112
113# kickstart doesn't support variable substution in commands, so this
114# is our current simplistic scheme for supporting that
115
116wks_vars = dict()
117
118def get_wks_var(key):
119 return wks_vars[key]
120
121def add_wks_var(key, val):
122 wks_vars[key] = val
123
124BOOTDD_EXTRA_SPACE = 16384
125IMAGE_EXTRA_SPACE = 10240
126IMAGE_OVERHEAD_FACTOR = 1.3
127
128__bitbake_env_lines = ""
129
130def set_bitbake_env_lines(bitbake_env_lines):
131 global __bitbake_env_lines
132 __bitbake_env_lines = bitbake_env_lines
133
134def get_bitbake_env_lines():
135 return __bitbake_env_lines
136
137def find_bitbake_env_lines(image_name):
138 """
139 If image_name is empty, plugins might still be able to use the
140 environment, so set it regardless.
141 """
142 if image_name:
143 bitbake_env_cmd = "bitbake -e %s" % image_name
144 else:
145 bitbake_env_cmd = "bitbake -e"
146 rc, bitbake_env_lines = __exec_cmd(bitbake_env_cmd)
147 if rc != 0:
148 print "Couldn't get '%s' output." % bitbake_env_cmd
149 return None
150
151 return bitbake_env_lines
152
153def find_artifact(bitbake_env_lines, variable):
154 """
155 Gather the build artifact for the current image (the image_name
156 e.g. core-image-minimal) for the current MACHINE set in local.conf
157 """
158 retval = ""
159
160 for line in bitbake_env_lines.split('\n'):
161 if (get_line_val(line, variable)):
162 retval = get_line_val(line, variable)
163 break
164
165 return retval
166
167def get_line_val(line, key):
168 """
169 Extract the value from the VAR="val" string
170 """
171 if line.startswith(key + "="):
172 stripped_line = line.split('=')[1]
173 stripped_line = stripped_line.replace('\"', '')
174 return stripped_line
175 return None
176
177def get_bitbake_var(key):
178 for line in __bitbake_env_lines.split('\n'):
179 if (get_line_val(line, key)):
180 val = get_line_val(line, key)
181 return val
182 return None
183
184def parse_sourceparams(sourceparams):
185 """
186 Split sourceparams string of the form key1=val1[,key2=val2,...]
187 into a dict. Also accepts valueless keys i.e. without =.
188
189 Returns dict of param key/val pairs (note that val may be None).
190 """
191 params_dict = {}
192
193 params = sourceparams.split(',')
194 if params:
195 for p in params:
196 if not p:
197 continue
198 if not '=' in p:
199 key = p
200 val = None
201 else:
202 key, val = p.split('=')
203 params_dict[key] = val
204
205 return params_dict