Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 2 | # ex:ts=4:sw=4:sts=4:et |
| 3 | # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- |
| 4 | # |
| 5 | # Copyright (c) 2013, Intel Corporation. |
| 6 | # All rights reserved. |
| 7 | # |
| 8 | # This program is free software; you can redistribute it and/or modify |
| 9 | # it under the terms of the GNU General Public License version 2 as |
| 10 | # published by the Free Software Foundation. |
| 11 | # |
| 12 | # This program is distributed in the hope that it will be useful, |
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | # GNU General Public License for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU General Public License along |
| 18 | # with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | # |
| 21 | # DESCRIPTION 'wic' is the OpenEmbedded Image Creator that users can |
| 22 | # use to generate bootable images. Invoking it without any arguments |
| 23 | # will display help screens for the 'wic' command and list the |
| 24 | # available 'wic' subcommands. Invoking a subcommand without any |
| 25 | # arguments will likewise display help screens for the specified |
| 26 | # subcommand. Please use that interface for detailed help. |
| 27 | # |
| 28 | # AUTHORS |
| 29 | # Tom Zanussi <tom.zanussi (at] linux.intel.com> |
| 30 | # |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 31 | __version__ = "0.2.0" |
| 32 | |
| 33 | # Python Standard Library modules |
| 34 | import os |
| 35 | import sys |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 36 | import argparse |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 37 | import logging |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 38 | |
| 39 | from collections import namedtuple |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 40 | from distutils import spawn |
| 41 | |
| 42 | # External modules |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 43 | scripts_path = os.path.dirname(os.path.realpath(__file__)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 44 | lib_path = scripts_path + '/lib' |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 45 | sys.path.insert(0, lib_path) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 46 | import scriptpath |
| 47 | scriptpath.add_oe_lib_path() |
| 48 | |
| 49 | # Check whether wic is running within eSDK environment |
| 50 | sdkroot = scripts_path |
| 51 | if os.environ.get('SDKTARGETSYSROOT'): |
| 52 | while sdkroot != '' and sdkroot != os.sep: |
| 53 | if os.path.exists(os.path.join(sdkroot, '.devtoolbase')): |
| 54 | # Set BUILDDIR for wic to work within eSDK |
| 55 | os.environ['BUILDDIR'] = sdkroot |
| 56 | # .devtoolbase only exists within eSDK |
| 57 | # If found, initialize bitbake path for eSDK environment and append to PATH |
| 58 | sdkroot = os.path.join(os.path.dirname(scripts_path), 'bitbake', 'bin') |
| 59 | os.environ['PATH'] += ":" + sdkroot |
| 60 | break |
| 61 | sdkroot = os.path.dirname(sdkroot) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 62 | |
| 63 | bitbake_exe = spawn.find_executable('bitbake') |
| 64 | if bitbake_exe: |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 65 | bitbake_path = scriptpath.add_bitbake_lib_path() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 66 | from bb import cookerdata |
| 67 | from bb.main import bitbake_main, BitBakeConfigParameters |
| 68 | else: |
| 69 | bitbake_main = None |
| 70 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 71 | from wic import WicError |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 72 | from wic.misc import get_bitbake_var, BB_VARS |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 73 | from wic import engine |
| 74 | from wic import help as hlp |
| 75 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 76 | |
| 77 | def wic_logger(): |
| 78 | """Create and convfigure wic logger.""" |
| 79 | logger = logging.getLogger('wic') |
| 80 | logger.setLevel(logging.INFO) |
| 81 | |
| 82 | handler = logging.StreamHandler() |
| 83 | |
| 84 | formatter = logging.Formatter('%(levelname)s: %(message)s') |
| 85 | handler.setFormatter(formatter) |
| 86 | |
| 87 | logger.addHandler(handler) |
| 88 | |
| 89 | return logger |
| 90 | |
| 91 | logger = wic_logger() |
| 92 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 93 | def rootfs_dir_to_args(krootfs_dir): |
| 94 | """ |
| 95 | Get a rootfs_dir dict and serialize to string |
| 96 | """ |
| 97 | rootfs_dir = '' |
| 98 | for key, val in krootfs_dir.items(): |
| 99 | rootfs_dir += ' ' |
| 100 | rootfs_dir += '='.join([key, val]) |
| 101 | return rootfs_dir.strip() |
| 102 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 103 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 104 | class RootfsArgAction(argparse.Action): |
| 105 | def __init__(self, **kwargs): |
| 106 | super().__init__(**kwargs) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 107 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 108 | def __call__(self, parser, namespace, value, option_string=None): |
| 109 | if not "rootfs_dir" in vars(namespace) or \ |
| 110 | not type(namespace.__dict__['rootfs_dir']) is dict: |
| 111 | namespace.__dict__['rootfs_dir'] = {} |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 112 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 113 | if '=' in value: |
| 114 | (key, rootfs_dir) = value.split('=') |
| 115 | else: |
| 116 | key = 'ROOTFS_DIR' |
| 117 | rootfs_dir = value |
| 118 | |
| 119 | namespace.__dict__['rootfs_dir'][key] = rootfs_dir |
| 120 | |
| 121 | |
| 122 | def wic_create_subcommand(options, usage_str): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 123 | """ |
| 124 | Command-line handling for image creation. The real work is done |
| 125 | by image.engine.wic_create() |
| 126 | """ |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 127 | if options.build_rootfs and not bitbake_main: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 128 | raise WicError("Can't build rootfs as bitbake is not in the $PATH") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 129 | |
| 130 | if not options.image_name: |
| 131 | missed = [] |
| 132 | for val, opt in [(options.rootfs_dir, 'rootfs-dir'), |
| 133 | (options.bootimg_dir, 'bootimg-dir'), |
| 134 | (options.kernel_dir, 'kernel-dir'), |
| 135 | (options.native_sysroot, 'native-sysroot')]: |
| 136 | if not val: |
| 137 | missed.append(opt) |
| 138 | if missed: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 139 | raise WicError("The following build artifacts are not specified: %s" % |
| 140 | ", ".join(missed)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 141 | |
| 142 | if options.image_name: |
| 143 | BB_VARS.default_image = options.image_name |
| 144 | else: |
| 145 | options.build_check = False |
| 146 | |
| 147 | if options.vars_dir: |
| 148 | BB_VARS.vars_dir = options.vars_dir |
| 149 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 150 | if options.build_check and not engine.verify_build_env(): |
| 151 | raise WicError("Couldn't verify build environment, exiting") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 152 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 153 | if options.debug: |
| 154 | logger.setLevel(logging.DEBUG) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 155 | |
| 156 | if options.image_name: |
| 157 | if options.build_rootfs: |
| 158 | argv = ["bitbake", options.image_name] |
| 159 | if options.debug: |
| 160 | argv.append("--debug") |
| 161 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 162 | logger.info("Building rootfs...\n") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 163 | if bitbake_main(BitBakeConfigParameters(argv), |
| 164 | cookerdata.CookerConfiguration()): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 165 | raise WicError("bitbake exited with error") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 166 | |
| 167 | rootfs_dir = get_bitbake_var("IMAGE_ROOTFS", options.image_name) |
| 168 | kernel_dir = get_bitbake_var("DEPLOY_DIR_IMAGE", options.image_name) |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 169 | bootimg_dir = get_bitbake_var("STAGING_DATADIR", options.image_name) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 170 | |
| 171 | native_sysroot = options.native_sysroot |
| 172 | if options.vars_dir and not native_sysroot: |
| 173 | native_sysroot = get_bitbake_var("RECIPE_SYSROOT_NATIVE", options.image_name) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 174 | else: |
| 175 | if options.build_rootfs: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 176 | raise WicError("Image name is not specified, exiting. " |
| 177 | "(Use -e/--image-name to specify it)") |
| 178 | native_sysroot = options.native_sysroot |
| 179 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 180 | if not options.vars_dir and (not native_sysroot or not os.path.isdir(native_sysroot)): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 181 | logger.info("Building wic-tools...\n") |
| 182 | if bitbake_main(BitBakeConfigParameters("bitbake wic-tools".split()), |
| 183 | cookerdata.CookerConfiguration()): |
| 184 | raise WicError("bitbake wic-tools failed") |
| 185 | native_sysroot = get_bitbake_var("RECIPE_SYSROOT_NATIVE", "wic-tools") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 186 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 187 | if not native_sysroot: |
| 188 | raise WicError("Unable to find the location of the native tools sysroot") |
| 189 | |
| 190 | wks_file = options.wks_file |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 191 | |
| 192 | if not wks_file.endswith(".wks"): |
| 193 | wks_file = engine.find_canned_image(scripts_path, wks_file) |
| 194 | if not wks_file: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 195 | raise WicError("No image named %s found, exiting. (Use 'wic list images' " |
| 196 | "to list available images, or specify a fully-qualified OE " |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 197 | "kickstart (.wks) filename)" % options.wks_file) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 198 | |
| 199 | if not options.image_name: |
| 200 | rootfs_dir = '' |
| 201 | if 'ROOTFS_DIR' in options.rootfs_dir: |
| 202 | rootfs_dir = options.rootfs_dir['ROOTFS_DIR'] |
| 203 | bootimg_dir = options.bootimg_dir |
| 204 | kernel_dir = options.kernel_dir |
| 205 | native_sysroot = options.native_sysroot |
| 206 | if rootfs_dir and not os.path.isdir(rootfs_dir): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 207 | raise WicError("--rootfs-dir (-r) not found, exiting") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 208 | if not os.path.isdir(bootimg_dir): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 209 | raise WicError("--bootimg-dir (-b) not found, exiting") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 210 | if not os.path.isdir(kernel_dir): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 211 | raise WicError("--kernel-dir (-k) not found, exiting") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 212 | if not os.path.isdir(native_sysroot): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 213 | raise WicError("--native-sysroot (-n) not found, exiting") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 214 | else: |
| 215 | not_found = not_found_dir = "" |
| 216 | if not os.path.isdir(rootfs_dir): |
| 217 | (not_found, not_found_dir) = ("rootfs-dir", rootfs_dir) |
| 218 | elif not os.path.isdir(kernel_dir): |
| 219 | (not_found, not_found_dir) = ("kernel-dir", kernel_dir) |
| 220 | elif not os.path.isdir(native_sysroot): |
| 221 | (not_found, not_found_dir) = ("native-sysroot", native_sysroot) |
| 222 | if not_found: |
| 223 | if not not_found_dir: |
| 224 | not_found_dir = "Completely missing artifact - wrong image (.wks) used?" |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 225 | logger.info("Build artifacts not found, exiting.") |
| 226 | logger.info(" (Please check that the build artifacts for the machine") |
| 227 | logger.info(" selected in local.conf actually exist and that they") |
| 228 | logger.info(" are the correct artifacts for the image (.wks file)).\n") |
| 229 | raise WicError("The artifact that couldn't be found was %s:\n %s", not_found, not_found_dir) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 230 | |
| 231 | krootfs_dir = options.rootfs_dir |
| 232 | if krootfs_dir is None: |
| 233 | krootfs_dir = {} |
| 234 | krootfs_dir['ROOTFS_DIR'] = rootfs_dir |
| 235 | |
| 236 | rootfs_dir = rootfs_dir_to_args(krootfs_dir) |
| 237 | |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 238 | logger.info("Creating image(s)...\n") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 239 | engine.wic_create(wks_file, rootfs_dir, bootimg_dir, kernel_dir, |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 240 | native_sysroot, options) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 241 | |
| 242 | |
| 243 | def wic_list_subcommand(args, usage_str): |
| 244 | """ |
| 245 | Command-line handling for listing available images. |
| 246 | The real work is done by image.engine.wic_list() |
| 247 | """ |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 248 | if not engine.wic_list(args, scripts_path): |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 249 | raise WicError("Bad list arguments, exiting") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 250 | |
| 251 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 252 | def wic_ls_subcommand(args, usage_str): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 253 | """ |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 254 | Command-line handling for list content of images. |
| 255 | The real work is done by engine.wic_ls() |
| 256 | """ |
| 257 | engine.wic_ls(args, args.native_sysroot) |
| 258 | |
| 259 | def wic_cp_subcommand(args, usage_str): |
| 260 | """ |
| 261 | Command-line handling for copying files/dirs to images. |
| 262 | The real work is done by engine.wic_cp() |
| 263 | """ |
| 264 | engine.wic_cp(args, args.native_sysroot) |
| 265 | |
| 266 | def wic_rm_subcommand(args, usage_str): |
| 267 | """ |
| 268 | Command-line handling for removing files/dirs from images. |
| 269 | The real work is done by engine.wic_rm() |
| 270 | """ |
| 271 | engine.wic_rm(args, args.native_sysroot) |
| 272 | |
| 273 | def wic_write_subcommand(args, usage_str): |
| 274 | """ |
| 275 | Command-line handling for writing images. |
| 276 | The real work is done by engine.wic_write() |
| 277 | """ |
| 278 | engine.wic_write(args, args.native_sysroot) |
| 279 | |
| 280 | def wic_help_subcommand(args, usage_str): |
| 281 | """ |
| 282 | Command-line handling for help subcommand to keep the current |
| 283 | structure of the function definitions. |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 284 | """ |
| 285 | pass |
| 286 | |
| 287 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 288 | def wic_help_topic_subcommand(usage_str, help_str): |
| 289 | """ |
| 290 | Display function for help 'sub-subcommands'. |
| 291 | """ |
| 292 | print(help_str) |
| 293 | return |
| 294 | |
| 295 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 296 | wic_help_topic_usage = """ |
| 297 | """ |
| 298 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 299 | helptopics = { |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 300 | "plugins": [wic_help_topic_subcommand, |
| 301 | wic_help_topic_usage, |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 302 | hlp.wic_plugins_help], |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 303 | "overview": [wic_help_topic_subcommand, |
| 304 | wic_help_topic_usage, |
| 305 | hlp.wic_overview_help], |
| 306 | "kickstart": [wic_help_topic_subcommand, |
| 307 | wic_help_topic_usage, |
| 308 | hlp.wic_kickstart_help], |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 309 | "create": [wic_help_topic_subcommand, |
| 310 | wic_help_topic_usage, |
| 311 | hlp.wic_create_help], |
| 312 | "ls": [wic_help_topic_subcommand, |
| 313 | wic_help_topic_usage, |
| 314 | hlp.wic_ls_help], |
| 315 | "cp": [wic_help_topic_subcommand, |
| 316 | wic_help_topic_usage, |
| 317 | hlp.wic_cp_help], |
| 318 | "rm": [wic_help_topic_subcommand, |
| 319 | wic_help_topic_usage, |
| 320 | hlp.wic_rm_help], |
| 321 | "write": [wic_help_topic_subcommand, |
| 322 | wic_help_topic_usage, |
| 323 | hlp.wic_write_help], |
| 324 | "list": [wic_help_topic_subcommand, |
| 325 | wic_help_topic_usage, |
| 326 | hlp.wic_list_help] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 330 | def wic_init_parser_create(subparser): |
| 331 | subparser.add_argument("wks_file") |
| 332 | |
| 333 | subparser.add_argument("-o", "--outdir", dest="outdir", default='.', |
| 334 | help="name of directory to create image in") |
| 335 | subparser.add_argument("-e", "--image-name", dest="image_name", |
| 336 | help="name of the image to use the artifacts from " |
| 337 | "e.g. core-image-sato") |
| 338 | subparser.add_argument("-r", "--rootfs-dir", action=RootfsArgAction, |
| 339 | help="path to the /rootfs dir to use as the " |
| 340 | ".wks rootfs source") |
| 341 | subparser.add_argument("-b", "--bootimg-dir", dest="bootimg_dir", |
| 342 | help="path to the dir containing the boot artifacts " |
| 343 | "(e.g. /EFI or /syslinux dirs) to use as the " |
| 344 | ".wks bootimg source") |
| 345 | subparser.add_argument("-k", "--kernel-dir", dest="kernel_dir", |
| 346 | help="path to the dir containing the kernel to use " |
| 347 | "in the .wks bootimg") |
| 348 | subparser.add_argument("-n", "--native-sysroot", dest="native_sysroot", |
| 349 | help="path to the native sysroot containing the tools " |
| 350 | "to use to build the image") |
| 351 | subparser.add_argument("-s", "--skip-build-check", dest="build_check", |
| 352 | action="store_false", default=True, help="skip the build check") |
| 353 | subparser.add_argument("-f", "--build-rootfs", action="store_true", help="build rootfs") |
| 354 | subparser.add_argument("-c", "--compress-with", choices=("gzip", "bzip2", "xz"), |
| 355 | dest='compressor', |
| 356 | help="compress image with specified compressor") |
| 357 | subparser.add_argument("-m", "--bmap", action="store_true", help="generate .bmap") |
| 358 | subparser.add_argument("--no-fstab-update" ,action="store_true", |
| 359 | help="Do not change fstab file.") |
| 360 | subparser.add_argument("-v", "--vars", dest='vars_dir', |
| 361 | help="directory with <image>.env files that store " |
| 362 | "bitbake variables") |
| 363 | subparser.add_argument("-D", "--debug", dest="debug", action="store_true", |
| 364 | default=False, help="output debug information") |
| 365 | return |
| 366 | |
| 367 | |
| 368 | def wic_init_parser_list(subparser): |
| 369 | subparser.add_argument("list_type", |
| 370 | help="can be 'images' or 'source-plugins' " |
| 371 | "to obtain a list. " |
| 372 | "If value is a valid .wks image file") |
| 373 | subparser.add_argument("help_for", default=[], nargs='*', |
| 374 | help="If 'list_type' is a valid .wks image file " |
| 375 | "this value can be 'help' to show the help information " |
| 376 | "defined inside the .wks file") |
| 377 | return |
| 378 | |
| 379 | def imgtype(arg): |
| 380 | """ |
| 381 | Custom type for ArgumentParser |
| 382 | Converts path spec to named tuple: (image, partition, path) |
| 383 | """ |
| 384 | image = arg |
| 385 | part = path = None |
| 386 | if ':' in image: |
| 387 | image, part = image.split(':') |
| 388 | if '/' in part: |
| 389 | part, path = part.split('/', 1) |
| 390 | if not path: |
| 391 | path = '/' |
| 392 | |
| 393 | if not os.path.isfile(image): |
| 394 | err = "%s is not a regular file or symlink" % image |
| 395 | raise argparse.ArgumentTypeError(err) |
| 396 | |
| 397 | return namedtuple('ImgType', 'image part path')(image, part, path) |
| 398 | |
| 399 | def wic_init_parser_ls(subparser): |
| 400 | subparser.add_argument("path", type=imgtype, |
| 401 | help="image spec: <image>[:<vfat partition>[<path>]]") |
| 402 | subparser.add_argument("-n", "--native-sysroot", |
| 403 | help="path to the native sysroot containing the tools") |
| 404 | |
| 405 | def imgpathtype(arg): |
| 406 | img = imgtype(arg) |
| 407 | if img.part is None: |
| 408 | raise argparse.ArgumentTypeError("partition number is not specified") |
| 409 | return img |
| 410 | |
| 411 | def wic_init_parser_cp(subparser): |
| 412 | subparser.add_argument("src", |
| 413 | help="source spec") |
| 414 | subparser.add_argument("dest", type=imgpathtype, |
| 415 | help="image spec: <image>:<vfat partition>[<path>]") |
| 416 | subparser.add_argument("-n", "--native-sysroot", |
| 417 | help="path to the native sysroot containing the tools") |
| 418 | |
| 419 | def wic_init_parser_rm(subparser): |
| 420 | subparser.add_argument("path", type=imgpathtype, |
| 421 | help="path: <image>:<vfat partition><path>") |
| 422 | subparser.add_argument("-n", "--native-sysroot", |
| 423 | help="path to the native sysroot containing the tools") |
| 424 | |
| 425 | def expandtype(rules): |
| 426 | """ |
| 427 | Custom type for ArgumentParser |
| 428 | Converts expand rules to the dictionary {<partition>: size} |
| 429 | """ |
| 430 | if rules == 'auto': |
| 431 | return {} |
| 432 | result = {} |
| 433 | for rule in rules.split('-'): |
| 434 | try: |
| 435 | part, size = rule.split(':') |
| 436 | except ValueError: |
| 437 | raise argparse.ArgumentTypeError("Incorrect rule format: %s" % rule) |
| 438 | |
| 439 | if not part.isdigit(): |
| 440 | raise argparse.ArgumentTypeError("Rule '%s': partition number must be integer" % rule) |
| 441 | |
| 442 | # validate size |
| 443 | multiplier = 1 |
| 444 | for suffix, mult in [('K', 1024), ('M', 1024 * 1024), ('G', 1024 * 1024 * 1024)]: |
| 445 | if size.upper().endswith(suffix): |
| 446 | multiplier = mult |
| 447 | size = size[:-1] |
| 448 | break |
| 449 | if not size.isdigit(): |
| 450 | raise argparse.ArgumentTypeError("Rule '%s': size must be integer" % rule) |
| 451 | |
| 452 | result[int(part)] = int(size) * multiplier |
| 453 | |
| 454 | return result |
| 455 | |
| 456 | def wic_init_parser_write(subparser): |
| 457 | subparser.add_argument("image", |
| 458 | help="path to the wic image") |
| 459 | subparser.add_argument("target", |
| 460 | help="target file or device") |
| 461 | subparser.add_argument("-e", "--expand", type=expandtype, |
| 462 | help="expand rules: auto or <partition>:<size>[,<partition>:<size>]") |
| 463 | subparser.add_argument("-n", "--native-sysroot", |
| 464 | help="path to the native sysroot containing the tools") |
| 465 | |
| 466 | def wic_init_parser_help(subparser): |
| 467 | helpparsers = subparser.add_subparsers(dest='help_topic', help=hlp.wic_usage) |
| 468 | for helptopic in helptopics: |
| 469 | helpparsers.add_parser(helptopic, help=helptopics[helptopic][2]) |
| 470 | return |
| 471 | |
| 472 | |
| 473 | subcommands = { |
| 474 | "create": [wic_create_subcommand, |
| 475 | hlp.wic_create_usage, |
| 476 | hlp.wic_create_help, |
| 477 | wic_init_parser_create], |
| 478 | "list": [wic_list_subcommand, |
| 479 | hlp.wic_list_usage, |
| 480 | hlp.wic_list_help, |
| 481 | wic_init_parser_list], |
| 482 | "ls": [wic_ls_subcommand, |
| 483 | hlp.wic_ls_usage, |
| 484 | hlp.wic_ls_help, |
| 485 | wic_init_parser_ls], |
| 486 | "cp": [wic_cp_subcommand, |
| 487 | hlp.wic_cp_usage, |
| 488 | hlp.wic_cp_help, |
| 489 | wic_init_parser_cp], |
| 490 | "rm": [wic_rm_subcommand, |
| 491 | hlp.wic_rm_usage, |
| 492 | hlp.wic_rm_help, |
| 493 | wic_init_parser_rm], |
| 494 | "write": [wic_write_subcommand, |
| 495 | hlp.wic_write_usage, |
| 496 | hlp.wic_write_help, |
| 497 | wic_init_parser_write], |
| 498 | "help": [wic_help_subcommand, |
| 499 | wic_help_topic_usage, |
| 500 | hlp.wic_help_help, |
| 501 | wic_init_parser_help] |
| 502 | } |
| 503 | |
| 504 | |
| 505 | def init_parser(parser): |
| 506 | parser.add_argument("--version", action="version", |
| 507 | version="%(prog)s {version}".format(version=__version__)) |
| 508 | subparsers = parser.add_subparsers(dest='command', help=hlp.wic_usage) |
| 509 | for subcmd in subcommands: |
| 510 | subparser = subparsers.add_parser(subcmd, help=subcommands[subcmd][2]) |
| 511 | subcommands[subcmd][3](subparser) |
| 512 | |
| 513 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 514 | def main(argv): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 515 | parser = argparse.ArgumentParser( |
| 516 | description="wic version %s" % __version__) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 517 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 518 | init_parser(parser) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 519 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 520 | args = parser.parse_args(argv) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 521 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 522 | if "command" in vars(args): |
| 523 | if args.command == "help": |
| 524 | if args.help_topic is None: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 525 | parser.print_help() |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 526 | print() |
| 527 | print("Please specify a help topic") |
| 528 | elif args.help_topic in helptopics: |
| 529 | hlpt = helptopics[args.help_topic] |
| 530 | hlpt[0](hlpt[1], hlpt[2]) |
| 531 | return 0 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 532 | |
| 533 | return hlp.invoke_subcommand(args, parser, hlp.wic_help_usage, subcommands) |
| 534 | |
| 535 | |
| 536 | if __name__ == "__main__": |
| 537 | try: |
| 538 | sys.exit(main(sys.argv[1:])) |
| 539 | except WicError as err: |
Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 540 | print() |
| 541 | logger.error(err) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 542 | sys.exit(1) |