summaryrefslogtreecommitdiffstats
path: root/scripts/lib/image/engine.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/lib/image/engine.py')
-rw-r--r--scripts/lib/image/engine.py279
1 files changed, 279 insertions, 0 deletions
diff --git a/scripts/lib/image/engine.py b/scripts/lib/image/engine.py
new file mode 100644
index 0000000000..e794545e94
--- /dev/null
+++ b/scripts/lib/image/engine.py
@@ -0,0 +1,279 @@
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
22# This module implements the image creation engine used by 'wic' to
23# create images. The engine parses through the OpenEmbedded kickstart
24# (wks) file specified and generates images that can then be directly
25# written onto media.
26#
27# AUTHORS
28# Tom Zanussi <tom.zanussi (at] linux.intel.com>
29#
30
31import os
32import sys
33from abc import ABCMeta, abstractmethod
34import shlex
35import json
36import subprocess
37import shutil
38
39import os, sys, errno
40from wic import msger, creator
41from wic.utils import cmdln, misc, errors
42from wic.conf import configmgr
43from wic.plugin import pluginmgr
44from wic.__version__ import VERSION
45from wic.utils.oe.misc import *
46
47
48def verify_build_env():
49 """
50 Verify that the build environment is sane.
51
52 Returns True if it is, false otherwise
53 """
54 try:
55 builddir = os.environ["BUILDDIR"]
56 except KeyError:
57 print "BUILDDIR not found, exiting. (Did you forget to source oe-init-build-env?)"
58 sys.exit(1)
59
60 return True
61
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 = 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 (get_line_val(line, "IMAGE_ROOTFS")):
74 rootfs_dir = get_line_val(line, "IMAGE_ROOTFS")
75 continue
76 if (get_line_val(line, "STAGING_KERNEL_DIR")):
77 kernel_dir = get_line_val(line, "STAGING_KERNEL_DIR")
78 continue
79 if (get_line_val(line, "STAGING_DIR_NATIVE")):
80 native_sysroot = 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
87SCRIPTS_CANNED_IMAGE_DIR = "scripts/" + CANNED_IMAGE_DIR
88
89def build_canned_image_list(dl):
90 layers_path = get_bitbake_var("BBLAYERS")
91 canned_wks_layer_dirs = []
92
93 if layers_path is not None:
94 for layer_path in layers_path.split():
95 path = os.path.join(layer_path, SCRIPTS_CANNED_IMAGE_DIR)
96 canned_wks_layer_dirs.append(path)
97
98 path = os.path.join(dl, CANNED_IMAGE_DIR)
99 canned_wks_layer_dirs.append(path)
100
101 return canned_wks_layer_dirs
102
103def find_canned_image(scripts_path, wks_file):
104 """
105 Find a .wks file with the given name in the canned files dir.
106
107 Return False if not found
108 """
109 layers_canned_wks_dir = build_canned_image_list(scripts_path)
110
111 for canned_wks_dir in layers_canned_wks_dir:
112 for root, dirs, files in os.walk(canned_wks_dir):
113 for file in files:
114 if file.endswith("~") or file.endswith("#"):
115 continue
116 if file.endswith(".wks") and wks_file + ".wks" == file:
117 fullpath = os.path.join(canned_wks_dir, file)
118 return fullpath
119 return None
120
121
122def list_canned_images(scripts_path):
123 """
124 List the .wks files in the canned image dir, minus the extension.
125 """
126 layers_canned_wks_dir = build_canned_image_list(scripts_path)
127
128 for canned_wks_dir in layers_canned_wks_dir:
129 for root, dirs, files in os.walk(canned_wks_dir):
130 for file in files:
131 if file.endswith("~") or file.endswith("#"):
132 continue
133 if file.endswith(".wks"):
134 fullpath = os.path.join(canned_wks_dir, file)
135 f = open(fullpath, "r")
136 lines = f.readlines()
137 for line in lines:
138 desc = ""
139 idx = line.find("short-description:")
140 if idx != -1:
141 desc = line[idx + len("short-description:"):].strip()
142 break
143 basename = os.path.splitext(file)[0]
144 print " %s\t\t%s" % (basename.ljust(30), desc)
145
146
147def list_canned_image_help(scripts_path, fullpath):
148 """
149 List the help and params in the specified canned image.
150 """
151 f = open(fullpath, "r")
152 lines = f.readlines()
153 found = False
154 for line in lines:
155 if not found:
156 idx = line.find("long-description:")
157 if idx != -1:
158 print
159 print line[idx + len("long-description:"):].strip()
160 found = True
161 continue
162 if not line.strip():
163 break
164 idx = line.find("#")
165 if idx != -1:
166 print line[idx + len("#:"):].rstrip()
167 else:
168 break
169
170
171def list_source_plugins():
172 """
173 List the available source plugins i.e. plugins available for --source.
174 """
175 plugins = pluginmgr.get_source_plugins()
176
177 for plugin in plugins:
178 print " %s" % plugin
179
180
181def wic_create(args, wks_file, rootfs_dir, bootimg_dir, kernel_dir,
182 native_sysroot, scripts_path, image_output_dir, debug,
183 properties_file, properties=None):
184 """Create image
185
186 wks_file - user-defined OE kickstart file
187 rootfs_dir - absolute path to the build's /rootfs dir
188 bootimg_dir - absolute path to the build's boot artifacts directory
189 kernel_dir - absolute path to the build's kernel directory
190 native_sysroot - absolute path to the build's native sysroots dir
191 scripts_path - absolute path to /scripts dir
192 image_output_dir - dirname to create for image
193 properties_file - use values from this file if nonempty i.e no prompting
194 properties - use values from this string if nonempty i.e no prompting
195
196 Normally, the values for the build artifacts values are determined
197 by 'wic -e' from the output of the 'bitbake -e' command given an
198 image name e.g. 'core-image-minimal' and a given machine set in
199 local.conf. If that's the case, the variables get the following
200 values from the output of 'bitbake -e':
201
202 rootfs_dir: IMAGE_ROOTFS
203 kernel_dir: STAGING_KERNEL_DIR
204 native_sysroot: STAGING_DIR_NATIVE
205
206 In the above case, bootimg_dir remains unset and the
207 plugin-specific image creation code is responsible for finding the
208 bootimg artifacts.
209
210 In the case where the values are passed in explicitly i.e 'wic -e'
211 is not used but rather the individual 'wic' options are used to
212 explicitly specify these values.
213 """
214 try:
215 oe_builddir = os.environ["BUILDDIR"]
216 except KeyError:
217 print "BUILDDIR not found, exiting. (Did you forget to source oe-init-build-env?)"
218 sys.exit(1)
219
220 direct_args = list()
221 direct_args.insert(0, oe_builddir)
222 direct_args.insert(0, image_output_dir)
223 direct_args.insert(0, wks_file)
224 direct_args.insert(0, rootfs_dir)
225 direct_args.insert(0, bootimg_dir)
226 direct_args.insert(0, kernel_dir)
227 direct_args.insert(0, native_sysroot)
228 direct_args.insert(0, "direct")
229
230 if debug:
231 msger.set_loglevel('debug')
232
233 cr = creator.Creator()
234
235 cr.main(direct_args)
236
237 print "\nThe image(s) were created using OE kickstart file:\n %s" % wks_file
238
239
240def wic_list(args, scripts_path, properties_file):
241 """
242 Print the complete list of properties defined by the image, or the
243 possible values for a particular image property.
244 """
245 if len(args) < 1:
246 return False
247
248 if len(args) == 1:
249 if args[0] == "images":
250 list_canned_images(scripts_path)
251 return True
252 elif args[0] == "source-plugins":
253 list_source_plugins()
254 return True
255 elif args[0] == "properties":
256 return True
257 else:
258 return False
259
260 if len(args) == 2:
261 if args[0] == "properties":
262 wks_file = args[1]
263 print "print properties contained in wks file: %s" % wks_file
264 return True
265 elif args[0] == "property":
266 print "print property values for property: %s" % args[1]
267 return True
268 elif args[1] == "help":
269 wks_file = args[0]
270 fullpath = find_canned_image(scripts_path, wks_file)
271 if not fullpath:
272 print "No image named %s found, exiting. (Use 'wic list images' to list available images, or specify a fully-qualified OE kickstart (.wks) filename)\n" % wks_file
273 sys.exit(1)
274 list_canned_image_help(scripts_path, fullpath)
275 return True
276 else:
277 return False
278
279 return False