diff options
author | Ed Bartosh <ed.bartosh@linux.intel.com> | 2017-02-03 22:26:06 +0200 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2017-02-05 09:22:17 +0000 |
commit | 71ce8d09e0f5912544936b0364990250c1381fed (patch) | |
tree | a26734dc16a71142d4a0130817e8e288ea9bfce1 /scripts/lib/wic | |
parent | 3e052dd58c30a6e3e3af45c8369a693628a23eea (diff) | |
download | poky-71ce8d09e0f5912544936b0364990250c1381fed.tar.gz |
wic: flatten imager class hierarchy
wic code is hard to follow due to deep and twiggy class
inheritance tree.
Flatten imager tree:
wic -> wic_create -> Creator -> DirectPlugin -> DirectImageCreator
to
wic -> wic_create -> DirectPlugin
by
removing Creator class and creator module
merging DirectImageCreator into DirectPlugin
Changed APIs to use the same parameters names.
Passed parsed command line options as an object down the stack.
(From OE-Core rev: 1e28d512341ce470c7afb256a01e597ab87170ca)
Signed-off-by: Ed Bartosh <ed.bartosh@linux.intel.com>
Signed-off-by: Ross Burton <ross.burton@intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'scripts/lib/wic')
-rw-r--r-- | scripts/lib/wic/creator.py | 106 | ||||
-rw-r--r-- | scripts/lib/wic/engine.py | 32 | ||||
-rw-r--r-- | scripts/lib/wic/plugins/imager/direct.py | 108 |
3 files changed, 55 insertions, 191 deletions
diff --git a/scripts/lib/wic/creator.py b/scripts/lib/wic/creator.py deleted file mode 100644 index 74db83cd30..0000000000 --- a/scripts/lib/wic/creator.py +++ /dev/null | |||
@@ -1,106 +0,0 @@ | |||
1 | #!/usr/bin/env python -tt | ||
2 | # | ||
3 | # Copyright (c) 2011 Intel, Inc. | ||
4 | # | ||
5 | # This program is free software; you can redistribute it and/or modify it | ||
6 | # under the terms of the GNU General Public License as published by the Free | ||
7 | # Software Foundation; version 2 of the License | ||
8 | # | ||
9 | # This program is distributed in the hope that it will be useful, but | ||
10 | # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
11 | # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
12 | # for more details. | ||
13 | # | ||
14 | # You should have received a copy of the GNU General Public License along | ||
15 | # with this program; if not, write to the Free Software Foundation, Inc., 59 | ||
16 | # Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
17 | |||
18 | import os, sys | ||
19 | from optparse import OptionParser, SUPPRESS_HELP | ||
20 | |||
21 | from wic import msger | ||
22 | from wic.utils import errors | ||
23 | from wic.plugin import pluginmgr | ||
24 | |||
25 | class Creator(): | ||
26 | """${name}: create an image | ||
27 | |||
28 | Usage: | ||
29 | ${name} SUBCOMMAND <ksfile> [OPTS] | ||
30 | |||
31 | ${command_list} | ||
32 | ${option_list} | ||
33 | """ | ||
34 | |||
35 | name = 'wic create(cr)' | ||
36 | |||
37 | def __init__(self, *args, **kwargs): | ||
38 | self._subcmds = {} | ||
39 | |||
40 | # get cmds from pluginmgr | ||
41 | # mix-in do_subcmd interface | ||
42 | for subcmd, klass in pluginmgr.get_plugins('imager').items(): | ||
43 | if not hasattr(klass, 'do_create'): | ||
44 | msger.warning("Unsupported subcmd: %s" % subcmd) | ||
45 | continue | ||
46 | |||
47 | func = getattr(klass, 'do_create') | ||
48 | self._subcmds[subcmd] = func | ||
49 | |||
50 | def get_optparser(self): | ||
51 | optparser = OptionParser() | ||
52 | optparser.add_option('-d', '--debug', action='store_true', | ||
53 | dest='debug', | ||
54 | help=SUPPRESS_HELP) | ||
55 | optparser.add_option('-v', '--verbose', action='store_true', | ||
56 | dest='verbose', | ||
57 | help=SUPPRESS_HELP) | ||
58 | optparser.add_option('', '--logfile', type='string', dest='logfile', | ||
59 | default=None, | ||
60 | help='Path of logfile') | ||
61 | optparser.add_option('-c', '--config', type='string', dest='config', | ||
62 | default=None, | ||
63 | help='Specify config file for wic') | ||
64 | optparser.add_option('-o', '--outdir', type='string', action='store', | ||
65 | dest='outdir', default=None, | ||
66 | help='Output directory') | ||
67 | optparser.add_option('', '--tmpfs', action='store_true', dest='enabletmpfs', | ||
68 | help='Setup tmpdir as tmpfs to accelerate, experimental' | ||
69 | ' feature, use it if you have more than 4G memory') | ||
70 | optparser.add_option('', '--bmap', action='store_true', help='generate .bmap') | ||
71 | return optparser | ||
72 | |||
73 | def postoptparse(self, options): | ||
74 | abspath = lambda pth: os.path.abspath(os.path.expanduser(pth)) | ||
75 | |||
76 | if options.verbose: | ||
77 | msger.set_loglevel('verbose') | ||
78 | if options.debug: | ||
79 | msger.set_loglevel('debug') | ||
80 | |||
81 | if options.logfile: | ||
82 | logfile_abs_path = abspath(options.logfile) | ||
83 | if os.path.isdir(logfile_abs_path): | ||
84 | raise errors.Usage("logfile's path %s should be file" | ||
85 | % options.logfile) | ||
86 | if not os.path.exists(os.path.dirname(logfile_abs_path)): | ||
87 | os.makedirs(os.path.dirname(logfile_abs_path)) | ||
88 | msger.set_interactive(False) | ||
89 | msger.set_logfile(logfile_abs_path) | ||
90 | |||
91 | def main(self, argv=None): | ||
92 | if argv is None: | ||
93 | argv = sys.argv | ||
94 | else: | ||
95 | argv = argv[:] # don't modify caller's list | ||
96 | |||
97 | pname = argv[0] | ||
98 | if pname not in self._subcmds: | ||
99 | msger.error('Unknown plugin: %s' % pname) | ||
100 | |||
101 | optparser = self.get_optparser() | ||
102 | options, args = optparser.parse_args(argv) | ||
103 | |||
104 | self.postoptparse(options) | ||
105 | |||
106 | return self._subcmds[pname](options, *args[1:]) | ||
diff --git a/scripts/lib/wic/engine.py b/scripts/lib/wic/engine.py index 7fb6f1317b..e27598d01a 100644 --- a/scripts/lib/wic/engine.py +++ b/scripts/lib/wic/engine.py | |||
@@ -31,7 +31,7 @@ | |||
31 | import os | 31 | import os |
32 | import sys | 32 | import sys |
33 | 33 | ||
34 | from wic import msger, creator | 34 | from wic import msger |
35 | from wic.plugin import pluginmgr | 35 | from wic.plugin import pluginmgr |
36 | from wic.utils.misc import get_bitbake_var | 36 | from wic.utils.misc import get_bitbake_var |
37 | 37 | ||
@@ -145,10 +145,10 @@ def list_source_plugins(): | |||
145 | print(" %s" % plugin) | 145 | print(" %s" % plugin) |
146 | 146 | ||
147 | 147 | ||
148 | def wic_create(wks_file, rootfs_dir, bootimg_dir, kernel_dir, | 148 | def wic_create(wks_file, rootfs_dir, bootimg_dir, kernel_dir, native_sysroot, |
149 | native_sysroot, scripts_path, image_output_dir, | 149 | scripts_path, options): |
150 | compressor, bmap, debug): | 150 | """ |
151 | """Create image | 151 | Create image |
152 | 152 | ||
153 | wks_file - user-defined OE kickstart file | 153 | wks_file - user-defined OE kickstart file |
154 | rootfs_dir - absolute path to the build's /rootfs dir | 154 | rootfs_dir - absolute path to the build's /rootfs dir |
@@ -157,8 +157,7 @@ def wic_create(wks_file, rootfs_dir, bootimg_dir, kernel_dir, | |||
157 | native_sysroot - absolute path to the build's native sysroots dir | 157 | native_sysroot - absolute path to the build's native sysroots dir |
158 | scripts_path - absolute path to /scripts dir | 158 | scripts_path - absolute path to /scripts dir |
159 | image_output_dir - dirname to create for image | 159 | image_output_dir - dirname to create for image |
160 | compressor - compressor utility to compress the image | 160 | options - wic command line options (debug, bmap, etc) |
161 | bmap - enable generation of .bmap | ||
162 | 161 | ||
163 | Normally, the values for the build artifacts values are determined | 162 | Normally, the values for the build artifacts values are determined |
164 | by 'wic -e' from the output of the 'bitbake -e' command given an | 163 | by 'wic -e' from the output of the 'bitbake -e' command given an |
@@ -184,20 +183,21 @@ def wic_create(wks_file, rootfs_dir, bootimg_dir, kernel_dir, | |||
184 | print("BUILDDIR not found, exiting. (Did you forget to source oe-init-build-env?)") | 183 | print("BUILDDIR not found, exiting. (Did you forget to source oe-init-build-env?)") |
185 | sys.exit(1) | 184 | sys.exit(1) |
186 | 185 | ||
187 | if debug: | 186 | if options.debug: |
188 | msger.set_loglevel('debug') | 187 | msger.set_loglevel('debug') |
189 | 188 | ||
190 | if not os.path.exists(image_output_dir): | 189 | if not os.path.exists(options.outdir): |
191 | os.makedirs(image_output_dir) | 190 | os.makedirs(options.outdir) |
192 | 191 | ||
193 | crobj = creator.Creator() | 192 | pname = 'direct' |
193 | plugin_class = pluginmgr.get_plugins('imager').get(pname) | ||
194 | if not plugin_class: | ||
195 | msger.error('Unknown plugin: %s' % pname) | ||
194 | 196 | ||
195 | cmdline = ["direct", native_sysroot, kernel_dir, bootimg_dir, rootfs_dir, | 197 | plugin = plugin_class(wks_file, rootfs_dir, bootimg_dir, kernel_dir, |
196 | wks_file, image_output_dir, oe_builddir, compressor or ""] | 198 | native_sysroot, scripts_path, oe_builddir, options) |
197 | if bmap: | ||
198 | cmdline.append('--bmap') | ||
199 | 199 | ||
200 | crobj.main(cmdline) | 200 | plugin.do_create() |
201 | 201 | ||
202 | print("\nThe image(s) were created using OE kickstart file:\n %s" % wks_file) | 202 | print("\nThe image(s) were created using OE kickstart file:\n %s" % wks_file) |
203 | 203 | ||
diff --git a/scripts/lib/wic/plugins/imager/direct.py b/scripts/lib/wic/plugins/imager/direct.py index b38e876758..ae420a687e 100644 --- a/scripts/lib/wic/plugins/imager/direct.py +++ b/scripts/lib/wic/plugins/imager/direct.py | |||
@@ -39,50 +39,6 @@ from wic.utils.errors import CreatorError, ImageError | |||
39 | from wic.utils.misc import get_bitbake_var, exec_cmd, exec_native_cmd | 39 | from wic.utils.misc import get_bitbake_var, exec_cmd, exec_native_cmd |
40 | from wic.utils.partitionedfs import Image | 40 | from wic.utils.partitionedfs import Image |
41 | 41 | ||
42 | class DirectPlugin(ImagerPlugin): | ||
43 | """ | ||
44 | Install a system into a file containing a partitioned disk image. | ||
45 | |||
46 | An image file is formatted with a partition table, each partition | ||
47 | created from a rootfs or other OpenEmbedded build artifact and dd'ed | ||
48 | into the virtual disk. The disk image can subsequently be dd'ed onto | ||
49 | media and used on actual hardware. | ||
50 | """ | ||
51 | |||
52 | name = 'direct' | ||
53 | |||
54 | @staticmethod | ||
55 | def do_create(opts, *args): | ||
56 | """ | ||
57 | Create direct image, called from creator as 'direct' cmd | ||
58 | """ | ||
59 | native_sysroot, kernel_dir, bootimg_dir, rootfs_dir, ksconf, \ | ||
60 | outdir, oe_builddir, compressor = args | ||
61 | |||
62 | try: | ||
63 | ksobj = KickStart(ksconf) | ||
64 | except KickStartError as err: | ||
65 | msger.error(str(err)) | ||
66 | |||
67 | name = "%s-%s" % (os.path.splitext(os.path.basename(ksconf))[0], | ||
68 | strftime("%Y%m%d%H%M")) | ||
69 | |||
70 | # parse possible 'rootfs=name' items | ||
71 | krootfs_dir = dict(rdir.split('=') for rdir in rootfs_dir.split(' ')) | ||
72 | |||
73 | creator = DirectImageCreator(name, ksobj, oe_builddir, outdir, | ||
74 | krootfs_dir, bootimg_dir, kernel_dir, | ||
75 | native_sysroot, compressor, opts.bmap) | ||
76 | try: | ||
77 | creator.create() | ||
78 | creator.assemble() | ||
79 | creator.finalize() | ||
80 | creator.print_info() | ||
81 | except errors.CreatorError: | ||
82 | raise | ||
83 | finally: | ||
84 | creator.cleanup() | ||
85 | |||
86 | class DiskImage(): | 42 | class DiskImage(): |
87 | """ | 43 | """ |
88 | A Disk backed by a file. | 44 | A Disk backed by a file. |
@@ -101,43 +57,57 @@ class DiskImage(): | |||
101 | 57 | ||
102 | self.created = True | 58 | self.created = True |
103 | 59 | ||
104 | class DirectImageCreator: | 60 | class DirectPlugin(ImagerPlugin): |
105 | """ | 61 | """ |
106 | Installs a system into a file containing a partitioned disk image. | 62 | Install a system into a file containing a partitioned disk image. |
107 | 63 | ||
108 | DirectImageCreator is an advanced ImageCreator subclass; an image | 64 | An image file is formatted with a partition table, each partition |
109 | file is formatted with a partition table, each partition created | 65 | created from a rootfs or other OpenEmbedded build artifact and dd'ed |
110 | from a rootfs or other OpenEmbedded build artifact and dd'ed into | 66 | into the virtual disk. The disk image can subsequently be dd'ed onto |
111 | the virtual disk. The disk image can subsequently be dd'ed onto | ||
112 | media and used on actual hardware. | 67 | media and used on actual hardware. |
113 | """ | 68 | """ |
69 | name = 'direct' | ||
114 | 70 | ||
115 | def __init__(self, name, ksobj, oe_builddir, outdir, | 71 | def __init__(self, wks_file, rootfs_dir, bootimg_dir, kernel_dir, |
116 | rootfs_dir, bootimg_dir, kernel_dir, | 72 | native_sysroot, scripts_path, oe_builddir, options): |
117 | native_sysroot, compressor, bmap=False): | 73 | try: |
118 | """ | 74 | self.ks = KickStart(wks_file) |
119 | Initialize a DirectImageCreator instance. | 75 | except KickStartError as err: |
76 | msger.error(str(err)) | ||
120 | 77 | ||
121 | This method takes the same arguments as ImageCreator.__init__() | 78 | # parse possible 'rootfs=name' items |
122 | """ | 79 | self.rootfs_dir = dict(rdir.split('=') for rdir in rootfs_dir.split(' ')) |
123 | self.name = name | 80 | self.bootimg_dir = bootimg_dir |
124 | self.outdir = outdir | 81 | self.kernel_dir = kernel_dir |
125 | self.workdir = tempfile.mkdtemp(dir=outdir, prefix='tmp.wic.') | 82 | self.native_sysroot = native_sysroot |
126 | self.ks = ksobj | 83 | self.oe_builddir = oe_builddir |
127 | 84 | ||
85 | self.outdir = options.outdir | ||
86 | self.compressor = options.compressor | ||
87 | self.bmap = options.bmap | ||
88 | |||
89 | self.name = "%s-%s" % (os.path.splitext(os.path.basename(wks_file))[0], | ||
90 | strftime("%Y%m%d%H%M")) | ||
91 | self.workdir = tempfile.mkdtemp(dir=self.outdir, prefix='tmp.wic.') | ||
128 | self._image = None | 92 | self._image = None |
129 | self._disks = {} | 93 | self._disks = {} |
130 | self._disk_format = "direct" | 94 | self._disk_format = "direct" |
131 | self._disk_names = [] | 95 | self._disk_names = [] |
132 | self.ptable_format = self.ks.bootloader.ptable | 96 | self.ptable_format = self.ks.bootloader.ptable |
133 | 97 | ||
134 | self.oe_builddir = oe_builddir | 98 | def do_create(self): |
135 | self.rootfs_dir = rootfs_dir | 99 | """ |
136 | self.bootimg_dir = bootimg_dir | 100 | Plugin entry point. |
137 | self.kernel_dir = kernel_dir | 101 | """ |
138 | self.native_sysroot = native_sysroot | 102 | try: |
139 | self.compressor = compressor | 103 | self.create() |
140 | self.bmap = bmap | 104 | self.assemble() |
105 | self.finalize() | ||
106 | self.print_info() | ||
107 | except errors.CreatorError: | ||
108 | raise | ||
109 | finally: | ||
110 | self.cleanup() | ||
141 | 111 | ||
142 | def _get_part_num(self, num, parts): | 112 | def _get_part_num(self, num, parts): |
143 | """calculate the real partition number, accounting for partitions not | 113 | """calculate the real partition number, accounting for partitions not |
@@ -359,7 +329,7 @@ class DirectImageCreator: | |||
359 | extension = "direct" + {"gzip": ".gz", | 329 | extension = "direct" + {"gzip": ".gz", |
360 | "bzip2": ".bz2", | 330 | "bzip2": ".bz2", |
361 | "xz": ".xz", | 331 | "xz": ".xz", |
362 | "": ""}.get(self.compressor) | 332 | None: ""}.get(self.compressor) |
363 | full_path = self._full_path(self.outdir, disk_name, extension) | 333 | full_path = self._full_path(self.outdir, disk_name, extension) |
364 | msg += ' %s\n\n' % full_path | 334 | msg += ' %s\n\n' % full_path |
365 | 335 | ||