Skip to contentSkip to conversation

wic refactoring (almost done)

PATCH New Ed Bartosh 1 participant 12 messages Feb 15, 2017

lore
Share & tools
lore

Ed Bartosh

author
#0 lore raw

cover wic refactoring (almost done)

Hi,

This patchset takes care of logging and error handling:
- remove custom msger module in favor of standard python logging
- raise WicError exception instead of calling sys.exit

This is the one of the last changes for this time.

I'm thinking about refactoring wic plugin system, but haven't decided yet if it makes sense to do right now or not. Other than that I'm happy with the state of the wic codebase for now. There are still many things to improve, but it looks and feels much better now.

The following changes since commit 10d1f08f71a623ca6d45cf0ae1afa2e880591a09:

wic: direct: move creation of PartitionedImage to __init__ (2017-02-10 16:44:03 +0200)

are available in the git repository at:

git://git.yoctoproject.org/poky-contrib ed/wic/wip
http://git.yoctoproject.org/cgit.cgi/poky-contrib/log/?h=ed/wic/wip

Ed Bartosh (11):
wic: setup logging in the main wic module
wic: use wic logger in core modules
wic: use wic logger in imager direct plugin
wic: use wic logger in wic source plugins
wic: remove msger module
wic: move errors module
wic: raise WicError in main module
wic: raise WicError in core modules
wic: raise WicError in wic plugins
wic: raise WicError instead of ImageError and CreatorError
wic: move WicError to lib/wic/__init__.py

 scripts/lib/wic/__init__.py                        |  20 ++
 scripts/lib/wic/engine.py                          |  28 ++-
 scripts/lib/wic/help.py                            |   8 +-
 scripts/lib/wic/ksparser.py                        |   7 +-
 scripts/lib/wic/msger.py                           | 209 ---------------------
 scripts/lib/wic/partition.py                       |  68 +++----
 scripts/lib/wic/plugin.py                          |  24 +--
 scripts/lib/wic/pluginbase.py                      |  12 +-
 scripts/lib/wic/plugins/imager/direct.py           |  87 ++++-----
 scripts/lib/wic/plugins/source/bootimg-efi.py      |  52 ++---
 .../lib/wic/plugins/source/bootimg-partition.py    |  25 +--
 scripts/lib/wic/plugins/source/bootimg-pcbios.py   |  41 ++--
 scripts/lib/wic/plugins/source/fsimage.py          |  14 +-
 .../lib/wic/plugins/source/isoimage-isohybrid.py   |  79 ++++----
 scripts/lib/wic/plugins/source/rawcopy.py          |  12 +-
 scripts/lib/wic/plugins/source/rootfs.py           |  21 ++-
 .../lib/wic/plugins/source/rootfs_pcbios_ext.py    |  54 +++---
 scripts/lib/wic/utils/errors.py                    |  29 ---
 scripts/lib/wic/utils/misc.py                      |  34 ++--
 scripts/lib/wic/utils/runner.py                    |  12 +-
 scripts/wic                                        | 104 +++++-----
 21 files changed, 378 insertions(+), 562 deletions(-)
 delete mode 100644 scripts/lib/wic/msger.py
 delete mode 100644 scripts/lib/wic/utils/errors.py

--
Regards,
Ed

author
#1 lore raw

PATCH 1/11 wic: setup logging in the main wic module

Set up wic logger using standerd logging module.
This is going to replace custom msger module.

---
 scripts/wic | 77 +++++++++++++++++++++++++++++++++++--------------------------
 1 file changed, 44 insertions(+), 33 deletions(-)
scripts/wic +44 -33
index 1b7d7df..f3e93da 100755
@@ -56,6 +56,23 @@ from wic.utils.errors import WicError
 from wic import engine
 from wic import help as hlp
 
+
+def wic_logger():
+    """Create and convfigure wic logger."""
+    logger = logging.getLogger('wic')
+    logger.setLevel(logging.INFO)
+
+    handler = logging.StreamHandler()
+
+    formatter = logging.Formatter('%(levelname)s: %(message)s')
+    handler.setFormatter(formatter)
+
+    logger.addHandler(handler)
+
+    return logger
+
+logger = wic_logger()
+
 def rootfs_dir_to_args(krootfs_dir):
     """
     Get a rootfs_dir dict and serialize to string
@@ -123,12 +140,12 @@ def wic_create_subcommand(args, usage_str):
     (options, args) = parser.parse_args(args)
 
     if len(args) != 1:
-        logging.error("Wrong number of arguments, exiting\n")
+        logger.error("Wrong number of arguments, exiting\n")
         parser.print_help()
         sys.exit(1)
 
     if options.build_rootfs and not bitbake_main:
-        logging.error("Can't build roofs as bitbake is not in the $PATH")
+        logger.error("Can't build roofs as bitbake is not in the $PATH")
         sys.exit(1)
 
     if not options.image_name:
@@ -140,8 +157,8 @@ def wic_create_subcommand(args, usage_str):
             if not val:
                 missed.append(opt)
         if missed:
-            print("The following build artifacts are not specified:")
-            print("  " + ", ".join(missed))
+            logger.error("The following build artifacts are not specified: %s",
+                         ", ".join(missed))
             sys.exit(1)
 
     if options.image_name:
@@ -152,23 +169,22 @@ def wic_create_subcommand(args, usage_str):
     if options.vars_dir:
         BB_VARS.vars_dir = options.vars_dir
 
-    if options.build_check:
-        print("Checking basic build environment...")
-        if not engine.verify_build_env():
-            print("Couldn't verify build environment, exiting\n")
-            sys.exit(1)
-        else:
-            print("Done.\n")
+    if options.build_check and not engine.verify_build_env():
+        logger.error("Couldn't verify build environment, exiting\n")
+        sys.exit(1)
 
     bootimg_dir = ""
 
+    if options.debug:
+        logger.setLevel(logging.DEBUG)
+
     if options.image_name:
         if options.build_rootfs:
             argv = ["bitbake", options.image_name]
             if options.debug:
                 argv.append("--debug")
 
-            print("Building rootfs...\n")
+            logger.info("Building rootfs...\n")
             if bitbake_main(BitBakeConfigParameters(argv),
                             cookerdata.CookerConfiguration()):
                 sys.exit(1)
@@ -179,18 +195,18 @@ def wic_create_subcommand(args, usage_str):
                                          "wic-tools", cache=False)
     else:
         if options.build_rootfs:
-            print("Image name is not specified, exiting. (Use -e/--image-name to specify it)\n")
+            logger.error("Image name is not specified, exiting. (Use -e/--image-name to specify it)\n")
             sys.exit(1)
         native_sysroot = options.native_sysroot
 
     if not native_sysroot or not os.path.isdir(native_sysroot):
-        print("Building wic-tools...\n")
+        logger.info("Building wic-tools...\n")
         if bitbake_main(BitBakeConfigParameters("bitbake wic-tools".split()),
                         cookerdata.CookerConfiguration()):
             sys.exit(1)
         native_sysroot = get_bitbake_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
         if not native_sysroot:
-            print("Unable to find the location of the native tools sysroot to use\n")
+            logger.info("Unable to find the location of the native tools sysroot to use\n")
             sys.exit(1)
 
     wks_file = args[0]
@@ -198,7 +214,7 @@ def wic_create_subcommand(args, usage_str):
     if not wks_file.endswith(".wks"):
         wks_file = engine.find_canned_image(scripts_path, wks_file)
         if not wks_file:
-            print("No image named %s found, exiting.  (Use 'wic list images' "\
+            logger.error("No image named %s found, exiting.  (Use 'wic list images' "\
                   "to list available images, or specify a fully-qualified OE "\
                   "kickstart (.wks) filename)\n" % args[0])
             sys.exit(1)
@@ -211,16 +227,16 @@ def wic_create_subcommand(args, usage_str):
         kernel_dir = options.kernel_dir
         native_sysroot = options.native_sysroot
         if rootfs_dir and not os.path.isdir(rootfs_dir):
-            print("--roofs-dir (-r) not found, exiting\n")
+            logger.error("--roofs-dir (-r) not found, exiting\n")
             sys.exit(1)
         if not os.path.isdir(bootimg_dir):
-            print("--bootimg-dir (-b) not found, exiting\n")
+            logger.error("--bootimg-dir (-b) not found, exiting\n")
             sys.exit(1)
         if not os.path.isdir(kernel_dir):
-            print("--kernel-dir (-k) not found, exiting\n")
+            logger.error("--kernel-dir (-k) not found, exiting\n")
             sys.exit(1)
         if not os.path.isdir(native_sysroot):
-            print("--native-sysroot (-n) not found, exiting\n")
+            logger.error("--native-sysroot (-n) not found, exiting\n")
             sys.exit(1)
     else:
         not_found = not_found_dir = ""
@@ -233,12 +249,11 @@ def wic_create_subcommand(args, usage_str):
         if not_found:
             if not not_found_dir:
                 not_found_dir = "Completely missing artifact - wrong image (.wks) used?"
-            print("Build artifacts not found, exiting.")
-            print("  (Please check that the build artifacts for the machine")
-            print("   selected in local.conf actually exist and that they")
-            print("   are the correct artifacts for the image (.wks file)).\n")
-            print("The artifact that couldn't be found was %s:\n  %s" % \
-                (not_found, not_found_dir))
+            logger.error("Build artifacts not found, exiting.")
+            logger.info("  (Please check that the build artifacts for the machine")
+            logger.info("   selected in local.conf actually exist and that they")
+            logger.info("   are the correct artifacts for the image (.wks file)).\n")
+            logger.info("The artifact that couldn't be found was %s:\n  %s", not_found, not_found_dir)
             sys.exit(1)
 
     krootfs_dir = options.rootfs_dir
@@ -248,7 +263,7 @@ def wic_create_subcommand(args, usage_str):
 
     rootfs_dir = rootfs_dir_to_args(krootfs_dir)
 
-    print("Creating image(s)...\n")
+    logger.info("Creating image(s)...\n")
     engine.wic_create(wks_file, rootfs_dir, bootimg_dir, kernel_dir,
                       native_sysroot, options)
 
@@ -262,7 +277,7 @@ def wic_list_subcommand(args, usage_str):
     args = parser.parse_args(args)[1]
 
     if not engine.wic_list(args, scripts_path):
-        logging.error("Bad list arguments, exiting\n")
+        logger.error("Bad list arguments, exiting\n")
         parser.print_help()
         sys.exit(1)
 
@@ -299,10 +314,6 @@ subcommands = {
 }
 
 
-def start_logging(loglevel):
-    logging.basicConfig(filename='wic.log', filemode='w', level=loglevel)
-
-
 def main(argv):
     parser = optparse.OptionParser(version="wic version %s" % __version__,
                                    usage=hlp.wic_usage)
@@ -324,6 +335,6 @@ if __name__ == "__main__":
     try:
         sys.exit(main(sys.argv[1:]))
     except WicError as err:
-        print("ERROR:", err, file=sys.stderr)
+        logger.error(err)
         sys.exit(1)
 
Signature · 1 line
2.1.4
author
#2 lore raw

PATCH 2/11 wic: use wic logger in core modules

In reply to Ed Bartosh

Replaced msger with wic logger in the core wic modules.

---
 scripts/lib/wic/engine.py       | 19 ++++++-----
 scripts/lib/wic/help.py         |  8 +++--
 scripts/lib/wic/ksparser.py     |  7 ++--
 scripts/lib/wic/partition.py    | 72 +++++++++++++++++++++++------------------
 scripts/lib/wic/plugin.py       | 19 +++++------
 scripts/lib/wic/pluginbase.py   | 12 ++++---
 scripts/lib/wic/utils/misc.py   | 32 ++++++++++--------
 scripts/lib/wic/utils/runner.py | 12 ++++---
 8 files changed, 104 insertions(+), 77 deletions(-)
scripts/lib/wic/engine.py +11 -8
index 1aa8f65..b64714c 100644
@@ -28,6 +28,7 @@
 # Tom Zanussi <tom.zanussi (at] linux.intel.com>
 #
 
+import logging
 import os
 import sys
 
@@ -35,6 +36,7 @@ from wic import msger
 from wic.plugin import pluginmgr
 from wic.utils.misc import get_bitbake_var
 
+logger = logging.getLogger('wic')
 
 def verify_build_env():
     """
@@ -43,7 +45,7 @@ def verify_build_env():
     Returns True if it is, false otherwise
     """
     if not os.environ.get("BUILDDIR"):
-        print("BUILDDIR not found, exiting. (Did you forget to source oe-init-build-env?)")
+        logger.error("BUILDDIR not found, exiting. (Did you forget to source oe-init-build-env?)")
         sys.exit(1)
 
     return True
@@ -179,7 +181,7 @@ def wic_create(wks_file, rootfs_dir, bootimg_dir, kernel_dir,
     try:
         oe_builddir = os.environ["BUILDDIR"]
     except KeyError:
-        print("BUILDDIR not found, exiting. (Did you forget to source oe-init-build-env?)")
+        logger.error("BUILDDIR not found, exiting. (Did you forget to source oe-init-build-env?)")
         sys.exit(1)
 
     if options.debug:
@@ -191,14 +193,15 @@ def wic_create(wks_file, rootfs_dir, bootimg_dir, kernel_dir,
     pname = 'direct'
     plugin_class = pluginmgr.get_plugins('imager').get(pname)
     if not plugin_class:
-        msger.error('Unknown plugin: %s' % pname)
+        logger.error('Unknown plugin: %s', pname)
+        sys.exit(1)
 
     plugin = plugin_class(wks_file, rootfs_dir, bootimg_dir, kernel_dir,
                           native_sysroot, oe_builddir, options)
 
     plugin.do_create()
 
-    print("\nThe image(s) were created using OE kickstart file:\n  %s" % wks_file)
+    logger.info("The image(s) were created using OE kickstart file:\n  %s", wks_file)
 
 
 def wic_list(args, scripts_path):
@@ -218,10 +221,10 @@ def wic_list(args, scripts_path):
         wks_file = args[0]
         fullpath = find_canned_image(scripts_path, wks_file)
         if not fullpath:
-            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)
+            logger.error("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)
             sys.exit(1)
         list_canned_image_help(scripts_path, fullpath)
         return True
scripts/lib/wic/help.py +5 -3
index 1bd411d..9b980e7 100644
@@ -30,8 +30,10 @@ import logging
 
 from wic.plugin import pluginmgr, PLUGIN_TYPES
 
+logger = logging.getLogger('wic')
+
 def subcommand_error(args):
-    logging.info("invalid subcommand %s", args[0])
+    logger.info("invalid subcommand %s", args[0])
 
 
 def display_help(subcommand, subcommands):
@@ -81,13 +83,13 @@ def invoke_subcommand(args, parser, main_command_usage, subcommands):
     Should use argparse, but has to work in 2.6.
     """
     if not args:
-        logging.error("No subcommand specified, exiting")
+        logger.error("No subcommand specified, exiting")
         parser.print_help()
         return 1
     elif args[0] == "help":
         wic_help(args, main_command_usage, subcommands)
     elif args[0] not in subcommands:
-        logging.error("Unsupported subcommand %s, exiting\n", args[0])
+        logger.error("Unsupported subcommand %s, exiting\n", args[0])
         parser.print_help()
         return 1
     else:
scripts/lib/wic/ksparser.py +5 -2
index d9fa805..c840d89 100644
@@ -27,12 +27,15 @@
 
 import os
 import shlex
+import logging
+
 from argparse import ArgumentParser, ArgumentError, ArgumentTypeError
 
-from wic import msger
 from wic.engine import find_canned
 from wic.partition import Partition
 
+logger = logging.getLogger('wic')
+
 class KickStartError(Exception):
     """Custom exception."""
     pass
@@ -167,7 +170,7 @@ class KickStart():
 
         self._parse(parser, confpath)
         if not self.bootloader:
-            msger.warning('bootloader config not specified, using defaults')
+            logger.warning('bootloader config not specified, using defaults\n')
             self.bootloader = bootloader.parse_args([])
 
     def _parse(self, parser, confpath):
scripts/lib/wic/partition.py +40 -32

146 lines not on this page · raw message

scripts/lib/wic/plugin.py +10 -9
index f04a034..70d3377 100644
@@ -17,8 +17,8 @@
 
 import os
 import sys
+import logging
 
-from wic import msger
 from wic import pluginbase
 from wic.utils import errors
 from wic.utils.misc import get_bitbake_var
@@ -30,6 +30,8 @@ PLUGIN_TYPES = ["imager", "source"]
 PLUGIN_DIR = "/lib/wic/plugins" # relative to scripts
 SCRIPTS_PLUGIN_DIR = "scripts" + PLUGIN_DIR
 
+logger = logging.getLogger('wic')
+
 class PluginMgr():
     plugin_dirs = {}
 
@@ -91,17 +93,16 @@ class PluginMgr():
                 if mod and mod != '__init__':
                     if mod in sys.modules:
                         #self.plugin_dirs[pdir] = True
-                        msger.warning("Module %s already exists, skip" % mod)
+                        logger.warning("Module %s already exists, skip", mod)
                     else:
                         try:
                             pymod = __import__(mod)
                             self.plugin_dirs[pdir] = True
-                            msger.debug("Plugin module %s:%s imported"\
-                                        % (mod, pymod.__file__))
+                            logger.debug("Plugin module %s:%s imported",
+                                         mod, pymod.__file__)
                         except ImportError as err:
-                            msg = 'Failed to load plugin %s/%s: %s' \
-                                % (os.path.basename(pdir), mod, err)
-                            msger.warning(msg)
+                            logger.warning('Failed to load plugin %s/%s: %s',
+                                           os.path.basename(pdir), mod, err)
 
             del sys.path[0]
 
@@ -140,8 +141,8 @@ class PluginMgr():
             if _source_name == source_name:
                 for _method_name in methods:
                     if not hasattr(klass, _method_name):
-                        msger.warning("Unimplemented %s source interface for: %s"\
-                                      % (_method_name, _source_name))
+                        logger.warning("Unimplemented %s source interface for: %s",
+                                       _method_name, _source_name)
                         return None
                     func = getattr(klass, _method_name)
                     methods[_method_name] = func
scripts/lib/wic/pluginbase.py +7 -5
index 2f747a9..aea9c02 100644
@@ -17,9 +17,11 @@
 
 __all__ = ['ImagerPlugin', 'SourcePlugin', 'get_plugins']
 
+import logging
+
 from collections import defaultdict
 
-from wic import msger
+logger = logging.getLogger('wic')
 
 class PluginMeta(type):
     plugins = defaultdict(dict)
@@ -49,7 +51,7 @@ class SourcePlugin(PluginMeta("Plugin", (), {})):
         disk image.  This provides a hook to allow finalization of a
         disk image e.g. to write an MBR to it.
         """
-        msger.debug("SourcePlugin: do_install_disk: disk: %s" % disk_name)
+        logger.debug("SourcePlugin: do_install_disk: disk: %s", disk_name)
 
     @classmethod
     def do_stage_partition(cls, part, source_params, creator, cr_workdir,
@@ -66,7 +68,7 @@ class SourcePlugin(PluginMeta("Plugin", (), {})):
         Not that get_bitbake_var() allows you to acces non-standard
         variables that you might want to use for this.
         """
-        msger.debug("SourcePlugin: do_stage_partition: part: %s" % part)
+        logger.debug("SourcePlugin: do_stage_partition: part: %s", part)
 
     @classmethod
     def do_configure_partition(cls, part, source_params, creator, cr_workdir,
@@ -77,7 +79,7 @@ class SourcePlugin(PluginMeta("Plugin", (), {})):
         custom configuration files for a partition, for example
         syslinux or grub config files.
         """
-        msger.debug("SourcePlugin: do_configure_partition: part: %s" % part)
+        logger.debug("SourcePlugin: do_configure_partition: part: %s", part)
 
     @classmethod
     def do_prepare_partition(cls, part, source_params, creator, cr_workdir,
@@ -87,7 +89,7 @@ class SourcePlugin(PluginMeta("Plugin", (), {})):
         Called to do the actual content population for a partition i.e. it
         'prepares' the partition to be incorporated into the image.
         """
-        msger.debug("SourcePlugin: do_prepare_partition: part: %s" % part)
+        logger.debug("SourcePlugin: do_prepare_partition: part: %s", part)
 
 def get_plugins(typen):
     return PluginMeta.plugins.get(typen)
scripts/lib/wic/utils/misc.py +18 -14
index edb9c5b..b7b835a 100644
@@ -26,14 +26,17 @@
 #
 """Miscellaneous functions."""
 
+import logging
 import os
 import re
+
 from collections import defaultdict
 from distutils import spawn
 
-from wic import msger
 from wic.utils import runner
 
+logger = logging.getLogger('wic')
+
 # executable -> recipe pairs for exec_native_cmd
 NATIVE_RECIPES = {"bmaptool": "bmap-tools",
                   "grub-mkimage": "grub-efi",
@@ -61,9 +64,9 @@ def _exec_cmd(cmd_and_args, as_shell=False, catch=3):
 
     Need to execute as_shell if the command uses wildcards
     """
-    msger.debug("_exec_cmd: %s" % cmd_and_args)
+    logger.debug("_exec_cmd: %s", cmd_and_args)
     args = cmd_and_args.split()
-    msger.debug(args)
+    logger.debug(args)
 
     if as_shell:
         ret, out = runner.runtool(cmd_and_args, catch)
@@ -71,11 +74,12 @@ def _exec_cmd(cmd_and_args, as_shell=False, catch=3):
         ret, out = runner.runtool(args, catch)
     out = out.strip()
     if ret != 0:
-        msger.error("_exec_cmd: %s returned '%s' instead of 0\noutput: %s" % \
+        logger.error("_exec_cmd: %s returned '%s' instead of 0\noutput: %s" % \
                     (cmd_and_args, ret, out))
+        sys.exit(1)
 
-    msger.debug("_exec_cmd: output for %s (rc = %d): %s" % \
-                (cmd_and_args, ret, out))
+    logger.debug("_exec_cmd: output for %s (rc = %d): %s",
+                 cmd_and_args, ret, out)
 
     return ret, out
 
@@ -97,7 +101,7 @@ def exec_native_cmd(cmd_and_args, native_sysroot, catch=3, pseudo=""):
     """
     # The reason -1 is used is because there may be "export" commands.
     args = cmd_and_args.split(';')[-1].split()
-    msger.debug(args)
+    logger.debug(args)
 
     if pseudo:
         cmd_and_args = pseudo + cmd_and_args
@@ -106,7 +110,7 @@ def exec_native_cmd(cmd_and_args, native_sysroot, catch=3, pseudo=""):
         (native_sysroot, native_sysroot, native_sysroot)
     native_cmd_and_args = "export PATH=%s:$PATH;%s" % \
                            (native_paths, cmd_and_args)
-    msger.debug("exec_native_cmd: %s" % cmd_and_args)
+    logger.debug("exec_native_cmd: %s", cmd_and_args)
 
     # If the command isn't in the native sysroot say we failed.
     if spawn.find_executable(args[0], native_paths):
@@ -127,7 +131,7 @@ def exec_native_cmd(cmd_and_args, native_sysroot, catch=3, pseudo=""):
         else:
             msg += "Wic failed to find a recipe to build native %s. Please "\
                    "file a bug against wic.\n" % prog
-        msger.error(msg)
+        logger.error(msg)
 
     return ret, out
 
@@ -184,14 +188,14 @@ class BitbakeVars(defaultdict):
                 if image:
                     cmd += " %s" % image
 
-                log_level = msger.get_loglevel()
-                msger.set_loglevel('normal')
+                log_level = logger.getEffectiveLevel()
+                logger.setLevel(logging.INFO)
                 ret, lines = _exec_cmd(cmd)
-                msger.set_loglevel(log_level)
+                logger.setLevel(log_level)
 
                 if ret:
-                    print("Couldn't get '%s' output." % cmd)
-                    print("Bitbake failed with error:\n%s\n" % lines)
+                    logger.error("Couldn't get '%s' output.", cmd)
+                    logger.error("Bitbake failed with error:\n%s\n", lines)
                     return
 
                 # Parse bitbake -e output
scripts/lib/wic/utils/runner.py +8 -4
index db536ba..d27dcc7 100644
@@ -15,10 +15,12 @@
 # with this program; if not, write to the Free Software Foundation, Inc., 59
 # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
+import logging
 import os
 import subprocess
+import sys
 
-from wic import msger
+logger = logging.getLogger('wic')
 
 def runtool(cmdln_or_args, catch=1):
     """ wrapper for most of the subprocess calls
@@ -70,7 +72,8 @@ def runtool(cmdln_or_args, catch=1):
     except OSError as err:
         if err.errno == 2:
             # [Errno 2] No such file or directory
-            msger.error('Cannot run command: %s, lost dependency?' % cmd)
+            logger.error('Cannot run command: %s, lost dependency?', cmd)
+            sys.exit(1)
         else:
             raise # relay
     finally:
@@ -80,7 +83,7 @@ def runtool(cmdln_or_args, catch=1):
     return (process.returncode, out)
 
 def show(cmdln_or_args):
-    # show all the message using msger.verbose
+    """Show all messages using logger.debug."""
 
     rcode, out = runtool(cmdln_or_args, catch=3)
 
@@ -99,7 +102,8 @@ def show(cmdln_or_args):
             msg += '\n  | %s' % line
         msg += '\n  +----------------'
 
-    msger.verbose(msg)
+    logger.debug(msg)
+
     return rcode
 
 def outs(cmdln_or_args, catch=1):
Signature · 1 line
2.1.4
author
#3 lore raw

PATCH 3/11 wic: use wic logger in imager direct plugin

In reply to Ed Bartosh

Replaced msger with wic logger in the direct plugin.

---
 scripts/lib/wic/plugins/imager/direct.py | 83 +++++++++++++++++---------------
 1 file changed, 43 insertions(+), 40 deletions(-)
scripts/lib/wic/plugins/imager/direct.py +43 -40
index 481d24d..2e2d26a 100644
@@ -23,14 +23,16 @@
 # AUTHORS
 # Tom Zanussi <tom.zanussi (at] linux.intel.com>
 #
+
+import logging
 import os
 import shutil
-import uuid
+import sys
 import tempfile
+import uuid
 
 from time import strftime
 
-from wic import msger
 from wic.filemap import sparse_copy
 from wic.ksparser import KickStart, KickStartError
 from wic.plugin import pluginmgr
@@ -38,6 +40,8 @@ from wic.pluginbase import ImagerPlugin
 from wic.utils.errors import ImageError
 from wic.utils.misc import get_bitbake_var, exec_cmd, exec_native_cmd
 
+logger = logging.getLogger('wic')
+
 class DirectPlugin(ImagerPlugin):
     """
     Install a system into a file containing a partitioned disk image.
@@ -54,7 +58,8 @@ class DirectPlugin(ImagerPlugin):
         try:
             self.ks = KickStart(wks_file)
         except KickStartError as err:
-            msger.error(str(err))
+            logger.error(str(err))
+            sys.exit(1)
 
         # parse possible 'rootfs=name' items
         self.rootfs_dir = dict(rdir.split('=') for rdir in rootfs_dir.split(' '))
@@ -204,12 +209,12 @@ class DirectPlugin(ImagerPlugin):
         full_path = self._image.path
         # Generate .bmap
         if self.bmap:
-            msger.debug("Generating bmap file for %s" % disk_name)
+            logger.debug("Generating bmap file for %s", disk_name)
             exec_native_cmd("bmaptool create %s -o %s.bmap" % (full_path, full_path),
                             self.native_sysroot)
         # Compress the image
         if self.compressor:
-            msger.debug("Compressing disk %s with %s" % (disk_name, self.compressor))
+            logger.debug("Compressing disk %s with %s", disk_name, self.compressor)
             exec_cmd("%s %s" % (self.compressor, full_path))
 
     def print_info(self):
@@ -239,7 +244,7 @@ class DirectPlugin(ImagerPlugin):
         msg += '  KERNEL_DIR:                   %s\n' % self.kernel_dir
         msg += '  NATIVE_SYSROOT:               %s\n' % self.native_sysroot
 
-        msger.info(msg)
+        logger.info(msg)
 
     @property
     def rootdev(self):
@@ -342,7 +347,7 @@ class PartitionedImage():
         partition on the disk. The 'ptable_format' parameter defines the
         partition table format and may be "msdos". """
 
-        msger.debug("Assigning %s partitions to disks" % self.ptable_format)
+        logger.debug("Assigning %s partitions to disks", self.ptable_format)
 
         # Go through partitions in the order they are added in .ks file
         for num in range(len(self.partitions)):
@@ -389,10 +394,10 @@ class PartitionedImage():
                     # to move forward to the next alignment point
                     align_sectors = (part.align * 1024 // self.sector_size) - align_sectors
 
-                    msger.debug("Realignment for %s%s with %s sectors, original"
-                                " offset %s, target alignment is %sK." %
-                                (part.disk, self.numpart, align_sectors,
-                                 self.offset, part.align))
+                    logger.debug("Realignment for %s%s with %s sectors, original"
+                                 " offset %s, target alignment is %sK.",
+                                 part.disk, self.numpart, align_sectors,
+                                 self.offset, part.align)
 
                     # increase the offset so we actually start the partition on right alignment
                     self.offset += align_sectors
@@ -413,11 +418,10 @@ class PartitionedImage():
                         part.type = 'logical'
                         part.num = self.realpart + 1
 
-            msger.debug("Assigned %s to %s%d, sectors range %d-%d size %d "
-                        "sectors (%d bytes)." \
-                            % (part.mountpoint, part.disk, part.num,
-                               part.start, self.offset - 1,
-                               part.size_sec, part.size_sec * self.sector_size))
+            logger.debug("Assigned %s to %s%d, sectors range %d-%d size %d "
+                         "sectors (%d bytes).", part.mountpoint, part.disk,
+                         part.num, part.start, self.offset - 1, part.size_sec,
+                         part.size_sec * self.sector_size)
 
         # Once all the partitions have been layed out, we can calculate the
         # minumim disk size
@@ -432,8 +436,8 @@ class PartitionedImage():
 
         # Start is included to the size so we need to substract one from the end.
         end = start + size - 1
-        msger.debug("Added '%s' partition, sectors %d-%d, size %d sectors" %
-                    (parttype, start, end, size))
+        logger.debug("Added '%s' partition, sectors %d-%d, size %d sectors",
+                     parttype, start, end, size)
 
         cmd = "parted -s %s unit s mkpart %s" % (device, parttype)
         if fstype:
@@ -443,20 +447,20 @@ class PartitionedImage():
         return exec_native_cmd(cmd, self.native_sysroot)
 
     def create(self):
-        msger.debug("Creating sparse file %s" % self.path)
+        logger.debug("Creating sparse file %s", self.path)
         with open(self.path, 'w') as sparse:
             os.ftruncate(sparse.fileno(), self.min_size)
 
-        msger.debug("Initializing partition table for %s" % self.path)
+        logger.debug("Initializing partition table for %s", self.path)
         exec_native_cmd("parted -s %s mklabel %s" %
                         (self.path, self.ptable_format), self.native_sysroot)
 
-        msger.debug("Set disk identifier %x" % self.identifier)
+        logger.debug("Set disk identifier %x", self.identifier)
         with open(self.path, 'r+b') as img:
             img.seek(0x1B8)
             img.write(self.identifier.to_bytes(4, 'little'))
 
-        msger.debug("Creating partitions")
+        logger.debug("Creating partitions")
 
         for part in self.partitions:
             if part.num == 0:
@@ -494,39 +498,39 @@ class PartitionedImage():
             # even number of sectors.
             if part.mountpoint == "/boot" and part.fstype in ["vfat", "msdos"] \
                and part.size_sec % 2:
-                msger.debug("Subtracting one sector from '%s' partition to " \
-                            "get even number of sectors for the partition" % \
-                            part.mountpoint)
+                logger.debug("Subtracting one sector from '%s' partition to "
+                             "get even number of sectors for the partition",
+                             part.mountpoint)
                 part.size_sec -= 1
 
             self._create_partition(self.path, part.type,
                                    parted_fs_type, part.start, part.size_sec)
 
             if part.part_type:
-                msger.debug("partition %d: set type UID to %s" % \
-                            (part.num, part.part_type))
+                logger.debug("partition %d: set type UID to %s",
+                             part.num, part.part_type)
                 exec_native_cmd("sgdisk --typecode=%d:%s %s" % \
                                          (part.num, part.part_type,
                                           self.path), self.native_sysroot)
 
             if part.uuid and self.ptable_format == "gpt":
-                msger.debug("partition %d: set UUID to %s" % \
-                            (part.num, part.uuid))
+                logger.debug("partition %d: set UUID to %s",
+                             part.num, part.uuid)
                 exec_native_cmd("sgdisk --partition-guid=%d:%s %s" % \
                                 (part.num, part.uuid, self.path),
                                 self.native_sysroot)
 
             if part.label and self.ptable_format == "gpt":
-                msger.debug("partition %d: set name to %s" % \
-                            (part.num, part.label))
+                logger.debug("partition %d: set name to %s",
+                             part.num, part.label)
                 exec_native_cmd("parted -s %s name %d %s" % \
                                 (self.path, part.num, part.label),
                                 self.native_sysroot)
 
             if part.active:
                 flag_name = "legacy_boot" if self.ptable_format == 'gpt' else "boot"
-                msger.debug("Set '%s' flag for partition '%s' on disk '%s'" % \
-                            (flag_name, part.num, self.path))
+                logger.debug("Set '%s' flag for partition '%s' on disk '%s'",
+                             flag_name, part.num, self.path)
                 exec_native_cmd("parted -s %s set %d %s on" % \
                                 (self.path, part.num, flag_name),
                                 self.native_sysroot)
@@ -540,8 +544,8 @@ class PartitionedImage():
             # isn't necessary).
             if parted_fs_type == "fat16":
                 if self.ptable_format == 'msdos':
-                    msger.debug("Disable 'lba' flag for partition '%s' on disk '%s'" % \
-                                (part.num, self.path))
+                    logger.debug("Disable 'lba' flag for partition '%s' on disk '%s'",
+                                 part.num, self.path)
                     exec_native_cmd("parted -s %s set %d lba off" % \
                                     (self.path, part.num),
                                     self.native_sysroot)
@@ -552,7 +556,7 @@ class PartitionedImage():
             os.remove(image)
 
     def assemble(self):
-        msger.debug("Installing partitions")
+        logger.debug("Installing partitions")
 
         for part in self.partitions:
             source = part.source_file
@@ -560,10 +564,9 @@ class PartitionedImage():
                 # install source_file contents into a partition
                 sparse_copy(source, self.path, part.start * self.sector_size)
 
-                msger.debug("Installed %s in partition %d, sectors %d-%d, "
-                            "size %d sectors" % \
-                            (source, part.num, part.start,
-                             part.start + part.size_sec - 1, part.size_sec))
+                logger.debug("Installed %s in partition %d, sectors %d-%d, "
+                             "size %d sectors", source, part.num, part.start,
+                             part.start + part.size_sec - 1, part.size_sec)
 
                 partimage = self.path + '.p%d' % part.num
                 os.rename(source, partimage)
Signature · 1 line
2.1.4
author
#4 lore raw

PATCH 4/11 wic: use wic logger in wic source plugins

In reply to Ed Bartosh

Replaced msger with wic logger in wic source plugins.

---
 scripts/lib/wic/plugins/source/bootimg-efi.py      | 60 ++++++++------
 .../lib/wic/plugins/source/bootimg-partition.py    | 28 ++++---
 scripts/lib/wic/plugins/source/bootimg-pcbios.py   | 41 +++++----
 scripts/lib/wic/plugins/source/fsimage.py          | 16 ++--
 .../lib/wic/plugins/source/isoimage-isohybrid.py   | 96 +++++++++++++---------
 scripts/lib/wic/plugins/source/rawcopy.py          | 14 ++--
 scripts/lib/wic/plugins/source/rootfs.py           | 24 +++---
 .../lib/wic/plugins/source/rootfs_pcbios_ext.py    | 56 +++++++------
 8 files changed, 200 insertions(+), 135 deletions(-)
scripts/lib/wic/plugins/source/bootimg-efi.py +36 -24
index 95316c8..e4c8451 100644
@@ -24,15 +24,18 @@
 # Tom Zanussi <tom.zanussi (at] linux.intel.com>
 #
 
+import logging
 import os
 import shutil
+import sys
 
-from wic import msger
 from wic.engine import get_custom_config
 from wic.pluginbase import SourcePlugin
 from wic.utils.misc import (exec_cmd, exec_native_cmd, get_bitbake_var,
                             BOOTDD_EXTRA_SPACE)
 
+logger = logging.getLogger('wic')
+
 class BootimgEFIPlugin(SourcePlugin):
     """
     Create EFI boot partition.
@@ -53,11 +56,12 @@ class BootimgEFIPlugin(SourcePlugin):
             if custom_cfg:
                 # Use a custom configuration for grub
                 grubefi_conf = custom_cfg
-                msger.debug("Using custom configuration file "
-                            "%s for grub.cfg" % configfile)
+                logger.debug("Using custom configuration file "
+                             "%s for grub.cfg", configfile)
             else:
-                msger.error("configfile is specified but failed to "
-                            "get it from %s." % configfile)
+                logger.error("configfile is specified but failed to "
+                             "get it from %s.", configfile)
+                sys.exit(1)
 
         if not custom_cfg:
             # Create grub configuration using parameters from wks file
@@ -75,8 +79,8 @@ class BootimgEFIPlugin(SourcePlugin):
                 % (kernel, creator.rootdev, bootloader.append)
             grubefi_conf += "}\n"
 
-        msger.debug("Writing grubefi config %s/hdd/boot/EFI/BOOT/grub.cfg" \
-                        % cr_workdir)
+        logger.debug("Writing grubefi config %s/hdd/boot/EFI/BOOT/grub.cfg",
+                     cr_workdir)
         cfg = open("%s/hdd/boot/EFI/BOOT/grub.cfg" % cr_workdir, "w")
         cfg.write(grubefi_conf)
         cfg.close()
@@ -104,15 +108,16 @@ class BootimgEFIPlugin(SourcePlugin):
             # obviously we need to have a common common deploy var
             bootimg_dir = get_bitbake_var("DEPLOY_DIR_IMAGE")
             if not bootimg_dir:
-                msger.error("Couldn't find DEPLOY_DIR_IMAGE, exiting\n")
+                logger.error("Couldn't find DEPLOY_DIR_IMAGE, exiting\n")
+                sys.exit(1)
 
             cp_cmd = "cp %s/%s %s" % (bootimg_dir, initrd, hdddir)
             exec_cmd(cp_cmd, True)
         else:
-            msger.debug("Ignoring missing initrd")
+            logger.debug("Ignoring missing initrd")
 
-        msger.debug("Writing systemd-boot config %s/hdd/boot/loader/loader.conf" \
-                        % cr_workdir)
+        logger.debug("Writing systemd-boot config "
+                     "%s/hdd/boot/loader/loader.conf", cr_workdir)
         cfg = open("%s/hdd/boot/loader/loader.conf" % cr_workdir, "w")
         cfg.write(loader_conf)
         cfg.close()
@@ -124,11 +129,12 @@ class BootimgEFIPlugin(SourcePlugin):
             if custom_cfg:
                 # Use a custom configuration for systemd-boot
                 boot_conf = custom_cfg
-                msger.debug("Using custom configuration file "
-                            "%s for systemd-boots's boot.conf" % configfile)
+                logger.debug("Using custom configuration file "
+                             "%s for systemd-boots's boot.conf", configfile)
             else:
-                msger.error("configfile is specified but failed to "
-                            "get it from %s." % configfile)
+                logger.error("configfile is specified but failed to "
+                             "get it from %s.", configfile)
+                sys.exit(1)
 
         if not custom_cfg:
             # Create systemd-boot configuration using parameters from wks file
@@ -143,8 +149,8 @@ class BootimgEFIPlugin(SourcePlugin):
             if initrd:
                 boot_conf += "initrd /%s\n" % initrd
 
-        msger.debug("Writing systemd-boot config %s/hdd/boot/loader/entries/boot.conf" \
-                        % cr_workdir)
+        logger.debug("Writing systemd-boot config "
+                     "%s/hdd/boot/loader/entries/boot.conf", cr_workdir)
         cfg = open("%s/hdd/boot/loader/entries/boot.conf" % cr_workdir, "w")
         cfg.write(boot_conf)
         cfg.close()
@@ -168,9 +174,11 @@ class BootimgEFIPlugin(SourcePlugin):
             elif source_params['loader'] == 'systemd-boot':
                 cls.do_configure_systemdboot(hdddir, creator, cr_workdir, source_params)
             else:
-                msger.error("unrecognized bootimg-efi loader: %s" % source_params['loader'])
+                logger.error("unrecognized bootimg-efi loader: %s", source_params['loader'])
+                sys.exit(1)
         except KeyError:
-            msger.error("bootimg-efi requires a loader, none specified")
+            logger.error("bootimg-efi requires a loader, none specified")
+            sys.exit(1)
 
 
     @classmethod
@@ -185,7 +193,8 @@ class BootimgEFIPlugin(SourcePlugin):
         if not bootimg_dir:
             bootimg_dir = get_bitbake_var("DEPLOY_DIR_IMAGE")
             if not bootimg_dir:
-                msger.error("Couldn't find DEPLOY_DIR_IMAGE, exiting\n")
+                logger.error("Couldn't find DEPLOY_DIR_IMAGE, exiting\n")
+                sys.exit(1)
             # just so the result notes display it
             creator.bootimg_dir = bootimg_dir
 
@@ -212,9 +221,12 @@ class BootimgEFIPlugin(SourcePlugin):
                     cp_cmd = "cp %s/%s %s/EFI/BOOT/%s" % (bootimg_dir, mod, hdddir, mod[8:])
                     exec_cmd(cp_cmd, True)
             else:
-                msger.error("unrecognized bootimg-efi loader: %s" % source_params['loader'])
+                logger.error("unrecognized bootimg-efi loader: %s",
+                             source_params['loader'])
+                sys.exit(1)
         except KeyError:
-            msger.error("bootimg-efi requires a loader, none specified")
+            logger.error("bootimg-efi requires a loader, none specified")
+            sys.exit(1)
 
         startup = os.path.join(bootimg_dir, "startup.nsh")
         if os.path.exists(startup):
@@ -232,8 +244,8 @@ class BootimgEFIPlugin(SourcePlugin):
 
         blocks += extra_blocks
 
-        msger.debug("Added %d extra blocks to %s to get to %d total blocks" % \
-                    (extra_blocks, part.mountpoint, blocks))
+        logger.debug("Added %d extra blocks to %s to get to %d total blocks",
+                     extra_blocks, part.mountpoint, blocks)
 
         # dosfs image, created by mkdosfs
         bootimg = "%s/boot.img" % cr_workdir
scripts/lib/wic/plugins/source/bootimg-partition.py +17 -11
index e0d9a50..b486915 100644
@@ -23,15 +23,18 @@
 # Maciej Borzecki <maciej.borzecki (at] open-rnd.pl>
 #
 
+import logging
 import os
 import re
+import sys
 
 from glob import glob
 
-from wic import msger
 from wic.pluginbase import SourcePlugin
 from wic.utils.misc import exec_cmd, get_bitbake_var
 
+logger = logging.getLogger('wic')
+
 class BootimgPartitionPlugin(SourcePlugin):
     """
     Create an image of boot partition, copying over files
@@ -78,16 +81,18 @@ class BootimgPartitionPlugin(SourcePlugin):
         if not bootimg_dir:
             bootimg_dir = get_bitbake_var("DEPLOY_DIR_IMAGE")
             if not bootimg_dir:
-                msger.error("Couldn't find DEPLOY_DIR_IMAGE, exiting\n")
+                logger.error("Couldn't find DEPLOY_DIR_IMAGE, exiting\n")
+                sys.exit(1)
 
-        msger.debug('Bootimg dir: %s' % bootimg_dir)
+        logger.debug('Bootimg dir: %s', bootimg_dir)
 
         boot_files = get_bitbake_var("IMAGE_BOOT_FILES")
 
         if not boot_files:
-            msger.error('No boot files defined, IMAGE_BOOT_FILES unset')
+            logger.error('No boot files defined, IMAGE_BOOT_FILES unset')
+            sys.exit(1)
 
-        msger.debug('Boot files: %s' % boot_files)
+        logger.debug('Boot files: %s', boot_files)
 
         # list of tuples (src_name, dst_name)
         deploy_files = []
@@ -95,11 +100,12 @@ class BootimgPartitionPlugin(SourcePlugin):
             if ';' in src_entry:
                 dst_entry = tuple(src_entry.split(';'))
                 if not dst_entry[0] or not dst_entry[1]:
-                    msger.error('Malformed boot file entry: %s' % (src_entry))
+                    logger.error('Malformed boot file entry: %s', src_entry)
+                    sys.exit(1)
             else:
                 dst_entry = (src_entry, src_entry)
 
-            msger.debug('Destination entry: %r' % (dst_entry,))
+            logger.debug('Destination entry: %r', dst_entry)
             deploy_files.append(dst_entry)
 
         for deploy_entry in deploy_files:
@@ -117,7 +123,7 @@ class BootimgPartitionPlugin(SourcePlugin):
 
                 srcs = glob(os.path.join(bootimg_dir, src))
 
-                msger.debug('Globbed sources: %s' % (', '.join(srcs)))
+                logger.debug('Globbed sources: %s', ', '.join(srcs))
                 for entry in srcs:
                     entry_dst_name = entry_name_fn(entry)
                     install_task.append((entry,
@@ -129,12 +135,12 @@ class BootimgPartitionPlugin(SourcePlugin):
 
             for task in install_task:
                 src_path, dst_path = task
-                msger.debug('Install %s as %s' % (os.path.basename(src_path),
-                                                  dst_path))
+                logger.debug('Install %s as %s',
+                             os.path.basename(src_path), dst_path)
                 install_cmd = "install -m 0644 -D %s %s" \
                               % (src_path, dst_path)
                 exec_cmd(install_cmd)
 
-        msger.debug('Prepare boot partition using rootfs in %s' % (hdddir))
+        logger.debug('Prepare boot partition using rootfs in %s', hdddir)
         part.prepare_rootfs(cr_workdir, oe_builddir, hdddir,
                             native_sysroot)
scripts/lib/wic/plugins/source/bootimg-pcbios.py +24 -17

91 lines not on this page · raw message

scripts/lib/wic/plugins/source/fsimage.py +10 -6
index 9193892..35fb78b 100644
@@ -15,12 +15,15 @@
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 #
 
+import logging
 import os
+import sys
 
-from wic import msger
 from wic.pluginbase import SourcePlugin
 from wic.utils.misc import get_bitbake_var
 
+logger = logging.getLogger('wic')
+
 class FSImagePlugin(SourcePlugin):
     """
     Add an already existing filesystem image to the partition layout.
@@ -58,16 +61,17 @@ class FSImagePlugin(SourcePlugin):
         if not bootimg_dir:
             bootimg_dir = get_bitbake_var("DEPLOY_DIR_IMAGE")
             if not bootimg_dir:
-                msger.error("Couldn't find DEPLOY_DIR_IMAGE, exiting\n")
+                logger.error("Couldn't find DEPLOY_DIR_IMAGE, exiting\n")
+                sys.exit(1)
 
-        msger.debug('Bootimg dir: %s' % bootimg_dir)
+        logger.debug('Bootimg dir: %s', bootimg_dir)
 
         if 'file' not in source_params:
-            msger.error("No file specified\n")
-            return
+            logger.error("No file specified\n")
+            sys.exit(1)
 
         src = os.path.join(bootimg_dir, source_params['file'])
 
 
-        msger.debug('Preparing partition using image %s' % (src))
+        logger.debug('Preparing partition using image %s', src)
         part.prepare_rootfs_from_fs_image(cr_workdir, src, "")
scripts/lib/wic/plugins/source/isoimage-isohybrid.py +58 -38

238 lines not on this page · raw message

scripts/lib/wic/plugins/source/rawcopy.py +9 -5
index 4e42e3e..c5c3be3 100644
@@ -15,13 +15,16 @@
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 #
 
+import logging
 import os
+import sys
 
-from wic import msger
 from wic.pluginbase import SourcePlugin
 from wic.utils.misc import exec_cmd, get_bitbake_var
 from wic.filemap import sparse_copy
 
+logger = logging.getLogger('wic')
+
 class RawCopyPlugin(SourcePlugin):
     """
     Populate partition content from raw image file.
@@ -59,13 +62,14 @@ class RawCopyPlugin(SourcePlugin):
         if not bootimg_dir:
             bootimg_dir = get_bitbake_var("DEPLOY_DIR_IMAGE")
             if not bootimg_dir:
-                msger.error("Couldn't find DEPLOY_DIR_IMAGE, exiting\n")
+                logger.error("Couldn't find DEPLOY_DIR_IMAGE, exiting\n")
+                sys.exit(1)
 
-        msger.debug('Bootimg dir: %s' % bootimg_dir)
+        logger.debug('Bootimg dir: %s', bootimg_dir)
 
         if 'file' not in source_params:
-            msger.error("No file specified\n")
-            return
+            logger.error("No file specified\n")
+            sys.exit(1)
 
         src = os.path.join(bootimg_dir, source_params['file'])
         dst = os.path.join(cr_workdir, "%s.%s" % (source_params['file'], part.lineno))
scripts/lib/wic/plugins/source/rootfs.py +14 -10
index 9d959fa..cfc90f6 100644
@@ -25,12 +25,15 @@
 # Joao Henrique Ferreira de Freitas <joaohf (at] gmail.com>
 #
 
+import logging
 import os
+import sys
 
-from wic import msger
 from wic.pluginbase import SourcePlugin
 from wic.utils.misc import get_bitbake_var
 
+logger = logging.getLogger('wic')
+
 class RootfsPlugin(SourcePlugin):
     """
     Populate partition content from a rootfs directory.
@@ -45,10 +48,10 @@ class RootfsPlugin(SourcePlugin):
 
         image_rootfs_dir = get_bitbake_var("IMAGE_ROOTFS", rootfs_dir)
         if not os.path.isdir(image_rootfs_dir):
-            msg = "No valid artifact IMAGE_ROOTFS from image named"
-            msg += " %s has been found at %s, exiting.\n" % \
-                (rootfs_dir, image_rootfs_dir)
-            msger.error(msg)
+            logger.error("No valid artifact IMAGE_ROOTFS from image named %s "
+                         "has been found at %s, exiting.\n",
+                         rootfs_dir, image_rootfs_dir)
+            sys.exit(1)
 
         return image_rootfs_dir
 
@@ -63,8 +66,9 @@ class RootfsPlugin(SourcePlugin):
         """
         if part.rootfs_dir is None:
             if not 'ROOTFS_DIR' in krootfs_dir:
-                msg = "Couldn't find --rootfs-dir, exiting"
-                msger.error(msg)
+                logger.error("Couldn't find --rootfs-dir, exiting")
+                sys.exit(1)
+
             rootfs_dir = krootfs_dir['ROOTFS_DIR']
         else:
             if part.rootfs_dir in krootfs_dir:
@@ -72,9 +76,9 @@ class RootfsPlugin(SourcePlugin):
             elif part.rootfs_dir:
                 rootfs_dir = part.rootfs_dir
             else:
-                msg = "Couldn't find --rootfs-dir=%s connection"
-                msg += " or it is not a valid path, exiting"
-                msger.error(msg % part.rootfs_dir)
+                logger.error("Couldn't find --rootfs-dir=%s connection or "
+                             "it is not a valid path, exiting", part.rootfs_dir)
+                sys.exit(1)
 
         real_rootfs_dir = cls.__get_rootfs_dir(rootfs_dir)
 
scripts/lib/wic/plugins/source/rootfs_pcbios_ext.py +32 -24

119 lines not on this page · raw message

Signature · 1 line
2.1.4
author
#5 lore raw

PATCH 5/11 wic: remove msger module

In reply to Ed Bartosh

Removed custom logger module msger as it's replaced by wic logger.

---
 scripts/lib/wic/engine.py |   4 -
 scripts/lib/wic/msger.py  | 209 ----------------------------------------------
 2 files changed, 213 deletions(-)
 delete mode 100644 scripts/lib/wic/msger.py
scripts/lib/wic/engine.py +0 -4
index b64714c..2ccd510 100644
@@ -32,7 +32,6 @@ import logging
 import os
 import sys
 
-from wic import msger
 from wic.plugin import pluginmgr
 from wic.utils.misc import get_bitbake_var
 
@@ -184,9 +183,6 @@ def wic_create(wks_file, rootfs_dir, bootimg_dir, kernel_dir,
         logger.error("BUILDDIR not found, exiting. (Did you forget to source oe-init-build-env?)")
         sys.exit(1)
 
-    if options.debug:
-        msger.set_loglevel('debug')
-
     if not os.path.exists(options.outdir):
         os.makedirs(options.outdir)
 
scripts/lib/wic/msger.py deleted +0 -209

209 lines not on this page · raw message

Signature · 1 line
2.1.4
author
#6 lore raw

PATCH 6/11 wic: move errors module

In reply to Ed Bartosh

Moved from lib/wic/utils/ to lib/wic as this is a core module.

---
 scripts/lib/wic/{utils => }/errors.py               | 0
 scripts/lib/wic/plugin.py                           | 4 ++--
 scripts/lib/wic/plugins/imager/direct.py            | 2 +-
 scripts/lib/wic/plugins/source/bootimg-pcbios.py    | 2 +-
 scripts/lib/wic/plugins/source/rootfs_pcbios_ext.py | 2 +-
 scripts/wic                                         | 3 +--
 6 files changed, 6 insertions(+), 7 deletions(-)
 rename scripts/lib/wic/{utils => }/errors.py (100%)
scripts/lib/wic/errors.py renamed from scripts/lib/wic/utils/errors.py +0 -0
similarity index 100%
rename from scripts/lib/wic/utils/errors.py
rename to scripts/lib/wic/errors.py
scripts/lib/wic/plugin.py +2 -2
index 70d3377..31311ad 100644
@@ -19,8 +19,8 @@ import os
 import sys
 import logging
 
+from wic.errors import CreatorError
 from wic import pluginbase
-from wic.utils import errors
 from wic.utils.misc import get_bitbake_var
 
 __ALL__ = ['PluginMgr', 'pluginmgr']
@@ -110,7 +110,7 @@ class PluginMgr():
         """ the return value is dict of name:class pairs """
 
         if ptype not in PLUGIN_TYPES:
-            raise errors.CreatorError('%s is not valid plugin type' % ptype)
+            raise CreatorError('%s is not valid plugin type' % ptype)
 
         plugins_dir = self._build_plugin_dir_list(self.plugin_dir, ptype)
 
scripts/lib/wic/plugins/imager/direct.py +1 -1
index 2e2d26a..e51c8be 100644
@@ -33,11 +33,11 @@ import uuid
 
 from time import strftime
 
+from wic.errors import ImageError
 from wic.filemap import sparse_copy
 from wic.ksparser import KickStart, KickStartError
 from wic.plugin import pluginmgr
 from wic.pluginbase import ImagerPlugin
-from wic.utils.errors import ImageError
 from wic.utils.misc import get_bitbake_var, exec_cmd, exec_native_cmd
 
 logger = logging.getLogger('wic')
scripts/lib/wic/plugins/source/bootimg-pcbios.py +1 -1
index 590d3d6..8f53fa2 100644
@@ -29,8 +29,8 @@ import os
 import sys
 
 from wic.engine import get_custom_config
+from wic.errors import ImageError
 from wic.utils import runner
-from wic.utils.errors import ImageError
 from wic.pluginbase import SourcePlugin
 from wic.utils.misc import (exec_cmd, exec_native_cmd,
                             get_bitbake_var, BOOTDD_EXTRA_SPACE)
scripts/lib/wic/plugins/source/rootfs_pcbios_ext.py +1 -1
index ed5fbdf..850aa5a 100644
@@ -23,9 +23,9 @@ import os
 import re
 import sys
 
+from wic.errors import ImageError
 from wic.utils import runner
 from wic.utils.misc import get_bitbake_var, exec_cmd, exec_native_cmd
-from wic.utils.errors import ImageError
 from wic.pluginbase import SourcePlugin
 
 logger = logging.getLogger('wic')
scripts/wic +1 -2
index f3e93da..b12c743 100755
@@ -51,8 +51,8 @@ if bitbake_exe:
 else:
     bitbake_main = None
 
+from wic.errors import WicError
 from wic.utils.misc import get_bitbake_var, BB_VARS
-from wic.utils.errors import WicError
 from wic import engine
 from wic import help as hlp
 
@@ -337,4 +337,3 @@ if __name__ == "__main__":
     except WicError as err:
         logger.error(err)
         sys.exit(1)
-
Signature · 1 line
2.1.4
author
#8 lore raw

PATCH 8/11 wic: raise WicError in core modules

In reply to Ed Bartosh

Replaced sys.exit with raising WicError in the core wic modules.

---
 scripts/lib/wic/engine.py       | 21 ++++++++-----------
 scripts/lib/wic/partition.py    | 46 +++++++++++++++++++----------------------
 scripts/lib/wic/utils/misc.py   |  6 +++---
 scripts/lib/wic/utils/runner.py |  6 +++---
 4 files changed, 36 insertions(+), 43 deletions(-)
scripts/lib/wic/engine.py +9 -12
index 2ccd510..38a68ed 100644
@@ -30,8 +30,8 @@
 
 import logging
 import os
-import sys
 
+from wic.errors import WicError
 from wic.plugin import pluginmgr
 from wic.utils.misc import get_bitbake_var
 
@@ -44,8 +44,7 @@ def verify_build_env():
     Returns True if it is, false otherwise
     """
     if not os.environ.get("BUILDDIR"):
-        logger.error("BUILDDIR not found, exiting. (Did you forget to source oe-init-build-env?)")
-        sys.exit(1)
+        raise WicError("BUILDDIR not found, exiting. (Did you forget to source oe-init-build-env?)")
 
     return True
 
@@ -180,8 +179,7 @@ def wic_create(wks_file, rootfs_dir, bootimg_dir, kernel_dir,
     try:
         oe_builddir = os.environ["BUILDDIR"]
     except KeyError:
-        logger.error("BUILDDIR not found, exiting. (Did you forget to source oe-init-build-env?)")
-        sys.exit(1)
+        raise WicError("BUILDDIR not found, exiting. (Did you forget to source oe-init-build-env?)")
 
     if not os.path.exists(options.outdir):
         os.makedirs(options.outdir)
@@ -189,8 +187,7 @@ def wic_create(wks_file, rootfs_dir, bootimg_dir, kernel_dir,
     pname = 'direct'
     plugin_class = pluginmgr.get_plugins('imager').get(pname)
     if not plugin_class:
-        logger.error('Unknown plugin: %s', pname)
-        sys.exit(1)
+        raise WicError('Unknown plugin: %s' % pname)
 
     plugin = plugin_class(wks_file, rootfs_dir, bootimg_dir, kernel_dir,
                           native_sysroot, oe_builddir, options)
@@ -217,11 +214,11 @@ def wic_list(args, scripts_path):
         wks_file = args[0]
         fullpath = find_canned_image(scripts_path, wks_file)
         if not fullpath:
-            logger.error("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)
-            sys.exit(1)
+            raise WicError("No image named %s found, exiting. "
+                           "(Use 'wic list images' to list available images, "
+                           "or specify a fully-qualified OE kickstart (.wks) "
+                           "filename)" % wks_file)
+
         list_canned_image_help(scripts_path, fullpath)
         return True
 
scripts/lib/wic/partition.py +21 -25

86 lines not on this page · raw message

scripts/lib/wic/utils/misc.py +3 -3
index b7b835a..94fdab2 100644
@@ -33,6 +33,7 @@ import re
 from collections import defaultdict
 from distutils import spawn
 
+from wic.errors import WicError
 from wic.utils import runner
 
 logger = logging.getLogger('wic')
@@ -74,9 +75,8 @@ def _exec_cmd(cmd_and_args, as_shell=False, catch=3):
         ret, out = runner.runtool(args, catch)
     out = out.strip()
     if ret != 0:
-        logger.error("_exec_cmd: %s returned '%s' instead of 0\noutput: %s" % \
-                    (cmd_and_args, ret, out))
-        sys.exit(1)
+        raise WicError("_exec_cmd: %s returned '%s' instead of 0\noutput: %s" % \
+                       (cmd_and_args, ret, out))
 
     logger.debug("_exec_cmd: output for %s (rc = %d): %s",
                  cmd_and_args, ret, out)
scripts/lib/wic/utils/runner.py +3 -3
index d27dcc7..5ede192 100644
@@ -18,7 +18,8 @@
 import logging
 import os
 import subprocess
-import sys
+
+from wic.errors import WicError
 
 logger = logging.getLogger('wic')
 
@@ -72,8 +73,7 @@ def runtool(cmdln_or_args, catch=1):
     except OSError as err:
         if err.errno == 2:
             # [Errno 2] No such file or directory
-            logger.error('Cannot run command: %s, lost dependency?', cmd)
-            sys.exit(1)
+            raise WicError('Cannot run command: %s, lost dependency?' % cmd)
         else:
             raise # relay
     finally:
Signature · 1 line
2.1.4
author
#11 lore raw

PATCH 11/11 wic: move WicError to lib/wic/__init__.py

In reply to Ed Bartosh

Removed unused exceptions from error.py Moved definition of WicError to lib/wic/__init__.py

---
 scripts/lib/wic/__init__.py                        | 20 +++++++++++++++
 scripts/lib/wic/engine.py                          |  2 +-
 scripts/lib/wic/errors.py                          | 29 ----------------------
 scripts/lib/wic/partition.py                       |  2 +-
 scripts/lib/wic/plugin.py                          |  3 +--
 scripts/lib/wic/plugins/imager/direct.py           |  2 +-
 scripts/lib/wic/plugins/source/bootimg-efi.py      |  2 +-
 .../lib/wic/plugins/source/bootimg-partition.py    |  2 +-
 scripts/lib/wic/plugins/source/bootimg-pcbios.py   |  2 +-
 scripts/lib/wic/plugins/source/fsimage.py          |  2 +-
 .../lib/wic/plugins/source/isoimage-isohybrid.py   |  2 +-
 scripts/lib/wic/plugins/source/rawcopy.py          |  2 +-
 scripts/lib/wic/plugins/source/rootfs.py           |  2 +-
 .../lib/wic/plugins/source/rootfs_pcbios_ext.py    |  2 +-
 scripts/lib/wic/utils/misc.py                      |  2 +-
 scripts/lib/wic/utils/runner.py                    |  2 +-
 scripts/wic                                        |  2 +-
 17 files changed, 35 insertions(+), 45 deletions(-)
 delete mode 100644 scripts/lib/wic/errors.py
scripts/lib/wic/__init__.py +20 -0
index e69de29..85876b1 100644
@@ -0,0 +1,20 @@
+#!/usr/bin/env python -tt
+#
+# Copyright (c) 2007 Red Hat, Inc.
+# Copyright (c) 2011 Intel, Inc.
+#
+# This program is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the Free
+# Software Foundation; version 2 of the License
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+# for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc., 59
+# Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+class WicError(Exception):
+    pass
scripts/lib/wic/engine.py +1 -1
index 38a68ed..c29924b 100644
@@ -31,7 +31,7 @@
 import logging
 import os
 
-from wic.errors import WicError
+from wic import WicError
 from wic.plugin import pluginmgr
 from wic.utils.misc import get_bitbake_var
 
scripts/lib/wic/errors.py deleted +0 -29
deleted file mode 100644
index d1b514d..0000000
@@ -1,29 +0,0 @@
-#!/usr/bin/env python -tt
-#
-# Copyright (c) 2007 Red Hat, Inc.
-# Copyright (c) 2011 Intel, Inc.
-#
-# This program is free software; you can redistribute it and/or modify it
-# under the terms of the GNU General Public License as published by the Free
-# Software Foundation; version 2 of the License
-#
-# This program is distributed in the hope that it will be useful, but
-# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
-# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
-# for more details.
-#
-# You should have received a copy of the GNU General Public License along
-# with this program; if not, write to the Free Software Foundation, Inc., 59
-# Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-
-class WicError(Exception):
-    pass
-
-class CreatorError(WicError):
-    pass
-
-class Usage(WicError):
-    pass
-
-class ImageError(WicError):
-    pass
scripts/lib/wic/partition.py +1 -1
index c5c439a..6a82299 100644
@@ -28,7 +28,7 @@ import logging
 import os
 import tempfile
 
-from wic.errors import WicError
+from wic import WicError
 from wic.utils.misc import exec_cmd, exec_native_cmd, get_bitbake_var
 from wic.plugin import pluginmgr
 
scripts/lib/wic/plugin.py +1 -2
index 0e98da5..ac55ecf 100644
@@ -19,8 +19,7 @@ import os
 import sys
 import logging
 
-from wic.errors import WicError
-from wic import pluginbase
+from wic import pluginbase, WicError
 from wic.utils.misc import get_bitbake_var
 
 __ALL__ = ['PluginMgr', 'pluginmgr']
scripts/lib/wic/plugins/imager/direct.py +1 -1
index 5b20ca9..6d1ed8c 100644
@@ -32,7 +32,7 @@ import uuid
 
 from time import strftime
 
-from wic.errors import WicError
+from wic import WicError
 from wic.filemap import sparse_copy
 from wic.ksparser import KickStart, KickStartError
 from wic.plugin import pluginmgr
scripts/lib/wic/plugins/source/bootimg-efi.py +1 -1
index 1f018fa..2d793a5 100644
@@ -28,7 +28,7 @@ import logging
 import os
 import shutil
 
-from wic.errors import WicError
+from wic import WicError
 from wic.engine import get_custom_config
 from wic.pluginbase import SourcePlugin
 from wic.utils.misc import (exec_cmd, exec_native_cmd, get_bitbake_var,
scripts/lib/wic/plugins/source/bootimg-partition.py +1 -1
index e9724a6..373e8d8 100644
@@ -29,7 +29,7 @@ import re
 
 from glob import glob
 
-from wic.errors import WicError
+from wic import WicError
 from wic.pluginbase import SourcePlugin
 from wic.utils.misc import exec_cmd, get_bitbake_var
 
scripts/lib/wic/plugins/source/bootimg-pcbios.py +1 -1
index c3a0e4e..74c75a0 100644
@@ -27,8 +27,8 @@
 import logging
 import os
 
+from wic import WicError
 from wic.engine import get_custom_config
-from wic.errors import WicError
 from wic.utils import runner
 from wic.pluginbase import SourcePlugin
 from wic.utils.misc import (exec_cmd, exec_native_cmd,
scripts/lib/wic/plugins/source/fsimage.py +1 -1
index 67fc6d1..8fbe44d 100644
@@ -18,7 +18,7 @@
 import logging
 import os
 
-from wic.errors import WicError
+from wic import WicError
 from wic.pluginbase import SourcePlugin
 from wic.utils.misc import get_bitbake_var
 
scripts/lib/wic/plugins/source/isoimage-isohybrid.py +1 -1
index df86acc..1ceba62 100644
@@ -26,7 +26,7 @@ import os
 import re
 import shutil
 
-from wic.errors import WicError
+from wic import WicError
 from wic.engine import get_custom_config
 from wic.pluginbase import SourcePlugin
 from wic.utils.misc import exec_cmd, exec_native_cmd, get_bitbake_var
scripts/lib/wic/plugins/source/rawcopy.py +1 -1
index 5612809..12348ae 100644
@@ -18,7 +18,7 @@
 import logging
 import os
 
-from wic.errors import WicError
+from wic import WicError
 from wic.pluginbase import SourcePlugin
 from wic.utils.misc import exec_cmd, get_bitbake_var
 from wic.filemap import sparse_copy
scripts/lib/wic/plugins/source/rootfs.py +1 -1
index e58469a..0ae62c7 100644
@@ -28,7 +28,7 @@
 import logging
 import os
 
-from wic.errors import WicError
+from wic import WicError
 from wic.pluginbase import SourcePlugin
 from wic.utils.misc import get_bitbake_var
 
scripts/lib/wic/plugins/source/rootfs_pcbios_ext.py +1 -1
index cf0eec2..94f80d0 100644
@@ -22,7 +22,7 @@ import logging
 import os
 import re
 
-from wic.errors import WicError
+from wic import WicError
 from wic.utils import runner
 from wic.utils.misc import get_bitbake_var, exec_cmd, exec_native_cmd
 from wic.pluginbase import SourcePlugin
scripts/lib/wic/utils/misc.py +1 -1
index 94fdab2..4b822f0 100644
@@ -33,7 +33,7 @@ import re
 from collections import defaultdict
 from distutils import spawn
 
-from wic.errors import WicError
+from wic import WicError
 from wic.utils import runner
 
 logger = logging.getLogger('wic')
scripts/lib/wic/utils/runner.py +1 -1
index 5ede192..56d7ea3 100644
@@ -19,7 +19,7 @@ import logging
 import os
 import subprocess
 
-from wic.errors import WicError
+from wic import WicError
 
 logger = logging.getLogger('wic')
 
scripts/wic +1 -1
index b8be5b8..e6780a1 100755
@@ -51,7 +51,7 @@ if bitbake_exe:
 else:
     bitbake_main = None
 
-from wic.errors import WicError
+from wic import WicError
 from wic.utils.misc import get_bitbake_var, BB_VARS
 from wic import engine
 from wic import help as hlp
Signature · 1 line
2.1.4
author
#9 lore raw

PATCH 9/11 wic: raise WicError in wic plugins

In reply to Ed Bartosh

Replaced sys.exit with raising WicError in wic plugins.

---
 scripts/lib/wic/plugins/imager/direct.py           |  6 +--
 scripts/lib/wic/plugins/source/bootimg-efi.py      | 32 +++++------
 .../lib/wic/plugins/source/bootimg-partition.py    | 11 ++--
 scripts/lib/wic/plugins/source/bootimg-pcbios.py   | 20 +++----
 scripts/lib/wic/plugins/source/fsimage.py          |  8 ++-
 .../lib/wic/plugins/source/isoimage-isohybrid.py   | 63 ++++++++--------------
 scripts/lib/wic/plugins/source/rawcopy.py          |  8 ++-
 scripts/lib/wic/plugins/source/rootfs.py           | 17 +++---
 .../lib/wic/plugins/source/rootfs_pcbios_ext.py    | 34 +++++-------
 9 files changed, 75 insertions(+), 124 deletions(-)
scripts/lib/wic/plugins/imager/direct.py +2 -4
index e51c8be..9c8a230 100644
@@ -27,13 +27,12 @@
 import logging
 import os
 import shutil
-import sys
 import tempfile
 import uuid
 
 from time import strftime
 
-from wic.errors import ImageError
+from wic.errors import ImageError, WicError
 from wic.filemap import sparse_copy
 from wic.ksparser import KickStart, KickStartError
 from wic.plugin import pluginmgr
@@ -58,8 +57,7 @@ class DirectPlugin(ImagerPlugin):
         try:
             self.ks = KickStart(wks_file)
         except KickStartError as err:
-            logger.error(str(err))
-            sys.exit(1)
+            raise WicError(str(err))
 
         # parse possible 'rootfs=name' items
         self.rootfs_dir = dict(rdir.split('=') for rdir in rootfs_dir.split(' '))
scripts/lib/wic/plugins/source/bootimg-efi.py +12 -20
index e4c8451..1f018fa 100644
@@ -27,8 +27,8 @@
 import logging
 import os
 import shutil
-import sys
 
+from wic.errors import WicError
 from wic.engine import get_custom_config
 from wic.pluginbase import SourcePlugin
 from wic.utils.misc import (exec_cmd, exec_native_cmd, get_bitbake_var,
@@ -59,9 +59,8 @@ class BootimgEFIPlugin(SourcePlugin):
                 logger.debug("Using custom configuration file "
                              "%s for grub.cfg", configfile)
             else:
-                logger.error("configfile is specified but failed to "
-                             "get it from %s.", configfile)
-                sys.exit(1)
+                raise WicError("configfile is specified but failed to "
+                               "get it from %s." % configfile)
 
         if not custom_cfg:
             # Create grub configuration using parameters from wks file
@@ -108,8 +107,7 @@ class BootimgEFIPlugin(SourcePlugin):
             # obviously we need to have a common common deploy var
             bootimg_dir = get_bitbake_var("DEPLOY_DIR_IMAGE")
             if not bootimg_dir:
-                logger.error("Couldn't find DEPLOY_DIR_IMAGE, exiting\n")
-                sys.exit(1)
+                raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting")
 
             cp_cmd = "cp %s/%s %s" % (bootimg_dir, initrd, hdddir)
             exec_cmd(cp_cmd, True)
@@ -132,9 +130,8 @@ class BootimgEFIPlugin(SourcePlugin):
                 logger.debug("Using custom configuration file "
                              "%s for systemd-boots's boot.conf", configfile)
             else:
-                logger.error("configfile is specified but failed to "
-                             "get it from %s.", configfile)
-                sys.exit(1)
+                raise WicError("configfile is specified but failed to "
+                               "get it from %s.", configfile)
 
         if not custom_cfg:
             # Create systemd-boot configuration using parameters from wks file
@@ -174,11 +171,9 @@ class BootimgEFIPlugin(SourcePlugin):
             elif source_params['loader'] == 'systemd-boot':
                 cls.do_configure_systemdboot(hdddir, creator, cr_workdir, source_params)
             else:
-                logger.error("unrecognized bootimg-efi loader: %s", source_params['loader'])
-                sys.exit(1)
+                raise WicError("unrecognized bootimg-efi loader: %s" % source_params['loader'])
         except KeyError:
-            logger.error("bootimg-efi requires a loader, none specified")
-            sys.exit(1)
+            raise WicError("bootimg-efi requires a loader, none specified")
 
 
     @classmethod
@@ -193,8 +188,7 @@ class BootimgEFIPlugin(SourcePlugin):
         if not bootimg_dir:
             bootimg_dir = get_bitbake_var("DEPLOY_DIR_IMAGE")
             if not bootimg_dir:
-                logger.error("Couldn't find DEPLOY_DIR_IMAGE, exiting\n")
-                sys.exit(1)
+                raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting")
             # just so the result notes display it
             creator.bootimg_dir = bootimg_dir
 
@@ -221,12 +215,10 @@ class BootimgEFIPlugin(SourcePlugin):
                     cp_cmd = "cp %s/%s %s/EFI/BOOT/%s" % (bootimg_dir, mod, hdddir, mod[8:])
                     exec_cmd(cp_cmd, True)
             else:
-                logger.error("unrecognized bootimg-efi loader: %s",
-                             source_params['loader'])
-                sys.exit(1)
+                raise WicError("unrecognized bootimg-efi loader: %s" %
+                               source_params['loader'])
         except KeyError:
-            logger.error("bootimg-efi requires a loader, none specified")
-            sys.exit(1)
+            raise WicError("bootimg-efi requires a loader, none specified")
 
         startup = os.path.join(bootimg_dir, "startup.nsh")
         if os.path.exists(startup):
scripts/lib/wic/plugins/source/bootimg-partition.py +4 -7
index b486915..e9724a6 100644
@@ -26,10 +26,10 @@
 import logging
 import os
 import re
-import sys
 
 from glob import glob
 
+from wic.errors import WicError
 from wic.pluginbase import SourcePlugin
 from wic.utils.misc import exec_cmd, get_bitbake_var
 
@@ -81,16 +81,14 @@ class BootimgPartitionPlugin(SourcePlugin):
         if not bootimg_dir:
             bootimg_dir = get_bitbake_var("DEPLOY_DIR_IMAGE")
             if not bootimg_dir:
-                logger.error("Couldn't find DEPLOY_DIR_IMAGE, exiting\n")
-                sys.exit(1)
+                raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting")
 
         logger.debug('Bootimg dir: %s', bootimg_dir)
 
         boot_files = get_bitbake_var("IMAGE_BOOT_FILES")
 
         if not boot_files:
-            logger.error('No boot files defined, IMAGE_BOOT_FILES unset')
-            sys.exit(1)
+            raise WicError('No boot files defined, IMAGE_BOOT_FILES unset')
 
         logger.debug('Boot files: %s', boot_files)
 
@@ -100,8 +98,7 @@ class BootimgPartitionPlugin(SourcePlugin):
             if ';' in src_entry:
                 dst_entry = tuple(src_entry.split(';'))
                 if not dst_entry[0] or not dst_entry[1]:
-                    logger.error('Malformed boot file entry: %s', src_entry)
-                    sys.exit(1)
+                    raise WicError('Malformed boot file entry: %s' % src_entry)
             else:
                 dst_entry = (src_entry, src_entry)
 
scripts/lib/wic/plugins/source/bootimg-pcbios.py +8 -12
index 8f53fa2..2ded2da 100644
@@ -26,10 +26,9 @@
 
 import logging
 import os
-import sys
 
 from wic.engine import get_custom_config
-from wic.errors import ImageError
+from wic.errors import ImageError, WicError
 from wic.utils import runner
 from wic.pluginbase import SourcePlugin
 from wic.utils.misc import (exec_cmd, exec_native_cmd,
@@ -57,14 +56,13 @@ class BootimgPcbiosPlugin(SourcePlugin):
         elif creator.ptable_format == 'gpt':
             mbrfile += "gptmbr.bin"
         else:
-            logger.error("Unsupported partition table: %s", creator.ptable_format)
-            sys.exit(1)
+            raise WicError("Unsupported partition table: %s" %
+                           creator.ptable_format)
 
         if not os.path.exists(mbrfile):
-            logger.error("Couldn't find %s.  If using the -e option, do you "
-                         "have the right MACHINE set in local.conf?  If not, "
-                         "is the bootimg_dir path correct?", mbrfile)
-            sys.exit(1)
+            raise WicError("Couldn't find %s.  If using the -e option, do you "
+                           "have the right MACHINE set in local.conf?  If not, "
+                           "is the bootimg_dir path correct?" % mbrfile)
 
         full_path = creator._full_path(workdir, disk_name, "direct")
         logger.debug("Installing MBR on disk %s as %s with size %s bytes",
@@ -152,11 +150,9 @@ class BootimgPcbiosPlugin(SourcePlugin):
         if not _has_syslinux(bootimg_dir):
             bootimg_dir = get_bitbake_var("STAGING_DATADIR", "wic-tools")
             if not bootimg_dir:
-                logger.error("Couldn't find STAGING_DATADIR, exiting\n")
-                sys.exit(1)
+                raise WicError("Couldn't find STAGING_DATADIR, exiting")
             if not _has_syslinux(bootimg_dir):
-                logger.error("Please build syslinux first\n")
-                sys.exit(1)
+                raise WicError("Please build syslinux first")
             # just so the result notes display it
             creator.bootimg_dir = bootimg_dir
 
scripts/lib/wic/plugins/source/fsimage.py +3 -5
index 35fb78b..67fc6d1 100644
@@ -17,8 +17,8 @@
 
 import logging
 import os
-import sys
 
+from wic.errors import WicError
 from wic.pluginbase import SourcePlugin
 from wic.utils.misc import get_bitbake_var
 
@@ -61,14 +61,12 @@ class FSImagePlugin(SourcePlugin):
         if not bootimg_dir:
             bootimg_dir = get_bitbake_var("DEPLOY_DIR_IMAGE")
             if not bootimg_dir:
-                logger.error("Couldn't find DEPLOY_DIR_IMAGE, exiting\n")
-                sys.exit(1)
+                raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting")
 
         logger.debug('Bootimg dir: %s', bootimg_dir)
 
         if 'file' not in source_params:
-            logger.error("No file specified\n")
-            sys.exit(1)
+            raise WicError("No file specified")
 
         src = os.path.join(bootimg_dir, source_params['file'])
 
scripts/lib/wic/plugins/source/isoimage-isohybrid.py +23 -40

152 lines not on this page · raw message

scripts/lib/wic/plugins/source/rawcopy.py +3 -5
index c5c3be3..5612809 100644
@@ -17,8 +17,8 @@
 
 import logging
 import os
-import sys
 
+from wic.errors import WicError
 from wic.pluginbase import SourcePlugin
 from wic.utils.misc import exec_cmd, get_bitbake_var
 from wic.filemap import sparse_copy
@@ -62,14 +62,12 @@ class RawCopyPlugin(SourcePlugin):
         if not bootimg_dir:
             bootimg_dir = get_bitbake_var("DEPLOY_DIR_IMAGE")
             if not bootimg_dir:
-                logger.error("Couldn't find DEPLOY_DIR_IMAGE, exiting\n")
-                sys.exit(1)
+                raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting")
 
         logger.debug('Bootimg dir: %s', bootimg_dir)
 
         if 'file' not in source_params:
-            logger.error("No file specified\n")
-            sys.exit(1)
+            raise WicError("No file specified")
 
         src = os.path.join(bootimg_dir, source_params['file'])
         dst = os.path.join(cr_workdir, "%s.%s" % (source_params['file'], part.lineno))
scripts/lib/wic/plugins/source/rootfs.py +7 -10
index cfc90f6..e58469a 100644
@@ -27,8 +27,8 @@
 
 import logging
 import os
-import sys
 
+from wic.errors import WicError
 from wic.pluginbase import SourcePlugin
 from wic.utils.misc import get_bitbake_var
 
@@ -48,10 +48,9 @@ class RootfsPlugin(SourcePlugin):
 
         image_rootfs_dir = get_bitbake_var("IMAGE_ROOTFS", rootfs_dir)
         if not os.path.isdir(image_rootfs_dir):
-            logger.error("No valid artifact IMAGE_ROOTFS from image named %s "
-                         "has been found at %s, exiting.\n",
-                         rootfs_dir, image_rootfs_dir)
-            sys.exit(1)
+            raise WicError("No valid artifact IMAGE_ROOTFS from image "
+                           "named %s has been found at %s, exiting." %
+                           (rootfs_dir, image_rootfs_dir))
 
         return image_rootfs_dir
 
@@ -66,8 +65,7 @@ class RootfsPlugin(SourcePlugin):
         """
         if part.rootfs_dir is None:
             if not 'ROOTFS_DIR' in krootfs_dir:
-                logger.error("Couldn't find --rootfs-dir, exiting")
-                sys.exit(1)
+                raise WicError("Couldn't find --rootfs-dir, exiting")
 
             rootfs_dir = krootfs_dir['ROOTFS_DIR']
         else:
@@ -76,9 +74,8 @@ class RootfsPlugin(SourcePlugin):
             elif part.rootfs_dir:
                 rootfs_dir = part.rootfs_dir
             else:
-                logger.error("Couldn't find --rootfs-dir=%s connection or "
-                             "it is not a valid path, exiting", part.rootfs_dir)
-                sys.exit(1)
+                raise WicError("Couldn't find --rootfs-dir=%s connection or "
+                               "it is not a valid path, exiting" % part.rootfs_dir)
 
         real_rootfs_dir = cls.__get_rootfs_dir(rootfs_dir)
 
scripts/lib/wic/plugins/source/rootfs_pcbios_ext.py +13 -21
index 850aa5a..4cc3a39 100644
@@ -21,9 +21,8 @@
 import logging
 import os
 import re
-import sys
 
-from wic.errors import ImageError
+from wic.errors import ImageError, WicError
 from wic.utils import runner
 from wic.utils.misc import get_bitbake_var, exec_cmd, exec_native_cmd
 from wic.pluginbase import SourcePlugin
@@ -99,10 +98,9 @@ class RootfsPlugin(SourcePlugin):
 
         image_rootfs_dir = get_bitbake_var("IMAGE_ROOTFS", rootfs_dir)
         if not os.path.isdir(image_rootfs_dir):
-            logger.error("No valid artifact IMAGE_ROOTFS from image named %s "
-                         "has been found at %s, exiting.\n",
-                         rootfs_dir, image_rootfs_dir)
-            sys.exit(1)
+            raise WicError("No valid artifact IMAGE_ROOTFS from image named %s "
+                           "has been found at %s, exiting." %
+                           (rootfs_dir, image_rootfs_dir))
 
         return image_rootfs_dir
 
@@ -160,14 +158,12 @@ class RootfsPlugin(SourcePlugin):
             logger.info("building syslinux-native...")
             exec_cmd("bitbake syslinux-native")
         if not is_exe(native_syslinux_nomtools):
-            logger.error("Couldn't find syslinux-nomtools (%s), exiting\n",
-                         native_syslinux_nomtools)
-            sys.exit(1)
+            raise WicError("Couldn't find syslinux-nomtools (%s), exiting" %
+                           native_syslinux_nomtools)
 
         if part.rootfs is None:
             if 'ROOTFS_DIR' not in krootfs_dir:
-                logger.error("Couldn't find --rootfs-dir, exiting")
-                sys.exit(1)
+                raise WicError("Couldn't find --rootfs-dir, exiting")
             rootfs_dir = krootfs_dir['ROOTFS_DIR']
         else:
             if part.rootfs in krootfs_dir:
@@ -175,9 +171,8 @@ class RootfsPlugin(SourcePlugin):
             elif part.rootfs:
                 rootfs_dir = part.rootfs
             else:
-                logger.error("Couldn't find --rootfs-dir=%s connection or "
-                             "it is not a valid path, exiting", part.rootfs)
-                sys.exit(1)
+                raise WicError("Couldn't find --rootfs-dir=%s connection or "
+                               "it is not a valid path, exiting" % part.rootfs)
 
         real_rootfs_dir = cls._get_rootfs_dir(rootfs_dir)
 
@@ -203,15 +198,12 @@ class RootfsPlugin(SourcePlugin):
         elif image_creator.ptable_format == 'gpt':
             mbrfile += "gptmbr.bin"
         else:
-            logger.error("Unsupported partition table: %s",
-                         image_creator.ptable_format)
-            sys.exit(1)
+            raise WicError("Unsupported partition table: %s" %
+                           image_creator.ptable_format)
 
         if not os.path.exists(mbrfile):
-            logger.error("Couldn't find %s. Has syslinux-native been baked?",
-                         mbrfile)
-            sys.exit(1)
-
+            raise WicError("Couldn't find %s. Has syslinux-native been baked?",
+                           mbrfile)
         full_path = disk.path
         logger.debug("Installing MBR on disk %s as %s with size %s bytes",
                      disk_name, full_path, disk.min_size)
Signature · 1 line
2.1.4
author
#7 lore raw

PATCH 7/11 wic: raise WicError in main module

In reply to Ed Bartosh

Replaced sys.exit with raising WicError in main module.

---
 scripts/wic | 56 +++++++++++++++++++++++---------------------------------
 1 file changed, 23 insertions(+), 33 deletions(-)
scripts/wic +23 -33
index b12c743..b8be5b8 100755
@@ -140,13 +140,11 @@ def wic_create_subcommand(args, usage_str):
     (options, args) = parser.parse_args(args)
 
     if len(args) != 1:
-        logger.error("Wrong number of arguments, exiting\n")
         parser.print_help()
-        sys.exit(1)
+        raise WicError("Wrong number of arguments, exiting")
 
     if options.build_rootfs and not bitbake_main:
-        logger.error("Can't build roofs as bitbake is not in the $PATH")
-        sys.exit(1)
+        raise WicError("Can't build roofs as bitbake is not in the $PATH")
 
     if not options.image_name:
         missed = []
@@ -157,9 +155,8 @@ def wic_create_subcommand(args, usage_str):
             if not val:
                 missed.append(opt)
         if missed:
-            logger.error("The following build artifacts are not specified: %s",
-                         ", ".join(missed))
-            sys.exit(1)
+            raise WicError("The following build artifacts are not specified: %s" %
+                           ", ".join(missed))
 
     if options.image_name:
         BB_VARS.default_image = options.image_name
@@ -170,8 +167,7 @@ def wic_create_subcommand(args, usage_str):
         BB_VARS.vars_dir = options.vars_dir
 
     if options.build_check and not engine.verify_build_env():
-        logger.error("Couldn't verify build environment, exiting\n")
-        sys.exit(1)
+        raise WicError("Couldn't verify build environment, exiting")
 
     bootimg_dir = ""
 
@@ -187,7 +183,7 @@ def wic_create_subcommand(args, usage_str):
             logger.info("Building rootfs...\n")
             if bitbake_main(BitBakeConfigParameters(argv),
                             cookerdata.CookerConfiguration()):
-                sys.exit(1)
+                raise WicError("bitbake exited with error")
 
         rootfs_dir = get_bitbake_var("IMAGE_ROOTFS", options.image_name)
         kernel_dir = get_bitbake_var("DEPLOY_DIR_IMAGE", options.image_name)
@@ -195,29 +191,28 @@ def wic_create_subcommand(args, usage_str):
                                          "wic-tools", cache=False)
     else:
         if options.build_rootfs:
-            logger.error("Image name is not specified, exiting. (Use -e/--image-name to specify it)\n")
-            sys.exit(1)
+            raise WicError("Image name is not specified, exiting. "
+                           "(Use -e/--image-name to specify it)")
         native_sysroot = options.native_sysroot
 
     if not native_sysroot or not os.path.isdir(native_sysroot):
         logger.info("Building wic-tools...\n")
         if bitbake_main(BitBakeConfigParameters("bitbake wic-tools".split()),
                         cookerdata.CookerConfiguration()):
-            sys.exit(1)
+            raise WicError("bitbake wic-tools failed")
         native_sysroot = get_bitbake_var("RECIPE_SYSROOT_NATIVE", "wic-tools")
         if not native_sysroot:
-            logger.info("Unable to find the location of the native tools sysroot to use\n")
-            sys.exit(1)
+            raise WicError("Unable to find the location of the native "
+                           "tools sysroot to use")
 
     wks_file = args[0]
 
     if not wks_file.endswith(".wks"):
         wks_file = engine.find_canned_image(scripts_path, wks_file)
         if not wks_file:
-            logger.error("No image named %s found, exiting.  (Use 'wic list images' "\
-                  "to list available images, or specify a fully-qualified OE "\
-                  "kickstart (.wks) filename)\n" % args[0])
-            sys.exit(1)
+            raise WicError("No image named %s found, exiting.  (Use 'wic list images' "
+                           "to list available images, or specify a fully-qualified OE "
+                           "kickstart (.wks) filename)" % args[0])
 
     if not options.image_name:
         rootfs_dir = ''
@@ -227,17 +222,13 @@ def wic_create_subcommand(args, usage_str):
         kernel_dir = options.kernel_dir
         native_sysroot = options.native_sysroot
         if rootfs_dir and not os.path.isdir(rootfs_dir):
-            logger.error("--roofs-dir (-r) not found, exiting\n")
-            sys.exit(1)
+            raise WicError("--roofs-dir (-r) not found, exiting")
         if not os.path.isdir(bootimg_dir):
-            logger.error("--bootimg-dir (-b) not found, exiting\n")
-            sys.exit(1)
+            raise WicError("--bootimg-dir (-b) not found, exiting")
         if not os.path.isdir(kernel_dir):
-            logger.error("--kernel-dir (-k) not found, exiting\n")
-            sys.exit(1)
+            raise WicError("--kernel-dir (-k) not found, exiting")
         if not os.path.isdir(native_sysroot):
-            logger.error("--native-sysroot (-n) not found, exiting\n")
-            sys.exit(1)
+            raise WicError("--native-sysroot (-n) not found, exiting")
     else:
         not_found = not_found_dir = ""
         if not os.path.isdir(rootfs_dir):
@@ -249,12 +240,11 @@ def wic_create_subcommand(args, usage_str):
         if not_found:
             if not not_found_dir:
                 not_found_dir = "Completely missing artifact - wrong image (.wks) used?"
-            logger.error("Build artifacts not found, exiting.")
+            logger.info("Build artifacts not found, exiting.")
             logger.info("  (Please check that the build artifacts for the machine")
             logger.info("   selected in local.conf actually exist and that they")
             logger.info("   are the correct artifacts for the image (.wks file)).\n")
-            logger.info("The artifact that couldn't be found was %s:\n  %s", not_found, not_found_dir)
-            sys.exit(1)
+            raise WicError("The artifact that couldn't be found was %s:\n  %s", not_found, not_found_dir)
 
     krootfs_dir = options.rootfs_dir
     if krootfs_dir is None:
@@ -277,9 +267,8 @@ def wic_list_subcommand(args, usage_str):
     args = parser.parse_args(args)[1]
 
     if not engine.wic_list(args, scripts_path):
-        logger.error("Bad list arguments, exiting\n")
         parser.print_help()
-        sys.exit(1)
+        raise WicError("Bad list arguments, exiting")
 
 
 def wic_help_topic_subcommand(args, usage_str):
@@ -326,7 +315,7 @@ def main(argv):
         if args[0] == "help":
             if len(args) == 1:
                 parser.print_help()
-                sys.exit(1)
+                raise WicError("help command requires parameter")
 
     return hlp.invoke_subcommand(args, parser, hlp.wic_help_usage, subcommands)
 
@@ -335,5 +324,6 @@ if __name__ == "__main__":
     try:
         sys.exit(main(sys.argv[1:]))
     except WicError as err:
+        print()
         logger.error(err)
         sys.exit(1)
Signature · 1 line
2.1.4
author
#10 lore raw

PATCH 10/11 wic: raise WicError instead of ImageError and CreatorError

In reply to Ed Bartosh

There is no need to raise special exceptions. Raising WicError should be enough.

---
 scripts/lib/wic/plugin.py                           | 4 ++--
 scripts/lib/wic/plugins/imager/direct.py            | 6 +++---
 scripts/lib/wic/plugins/source/bootimg-pcbios.py    | 4 ++--
 scripts/lib/wic/plugins/source/rootfs_pcbios_ext.py | 4 ++--
 4 files changed, 9 insertions(+), 9 deletions(-)
scripts/lib/wic/plugin.py +2 -2
index 31311ad..0e98da5 100644
@@ -19,7 +19,7 @@ import os
 import sys
 import logging
 
-from wic.errors import CreatorError
+from wic.errors import WicError
 from wic import pluginbase
 from wic.utils.misc import get_bitbake_var
 
@@ -110,7 +110,7 @@ class PluginMgr():
         """ the return value is dict of name:class pairs """
 
         if ptype not in PLUGIN_TYPES:
-            raise CreatorError('%s is not valid plugin type' % ptype)
+            raise WicError('%s is not valid plugin type' % ptype)
 
         plugins_dir = self._build_plugin_dir_list(self.plugin_dir, ptype)
 
scripts/lib/wic/plugins/imager/direct.py +3 -3
index 9c8a230..5b20ca9 100644
@@ -32,7 +32,7 @@ import uuid
 
 from time import strftime
 
-from wic.errors import ImageError, WicError
+from wic.errors import WicError
 from wic.filemap import sparse_copy
 from wic.ksparser import KickStart, KickStartError
 from wic.plugin import pluginmgr
@@ -355,8 +355,8 @@ class PartitionedImage():
                 # The --part-type can also be implemented for MBR partitions,
                 # in which case it would map to the 1-byte "partition type"
                 # filed at offset 3 of the partition entry.
-                raise ImageError("setting custom partition type is not " \
-                                 "implemented for msdos partitions")
+                raise WicError("setting custom partition type is not " \
+                               "implemented for msdos partitions")
 
             # Get the disk where the partition is located
             self.numpart += 1
scripts/lib/wic/plugins/source/bootimg-pcbios.py +2 -2
index 2ded2da..c3a0e4e 100644
@@ -28,7 +28,7 @@ import logging
 import os
 
 from wic.engine import get_custom_config
-from wic.errors import ImageError, WicError
+from wic.errors import WicError
 from wic.utils import runner
 from wic.pluginbase import SourcePlugin
 from wic.utils.misc import (exec_cmd, exec_native_cmd,
@@ -71,7 +71,7 @@ class BootimgPcbiosPlugin(SourcePlugin):
         rcode = runner.show(['dd', 'if=%s' % mbrfile,
                              'of=%s' % full_path, 'conv=notrunc'])
         if rcode != 0:
-            raise ImageError("Unable to set MBR to %s" % full_path)
+            raise WicError("Unable to set MBR to %s" % full_path)
 
     @classmethod
     def do_configure_partition(cls, part, source_params, creator, cr_workdir,
scripts/lib/wic/plugins/source/rootfs_pcbios_ext.py +2 -2
index 4cc3a39..cf0eec2 100644
@@ -22,7 +22,7 @@ import logging
 import os
 import re
 
-from wic.errors import ImageError, WicError
+from wic.errors import WicError
 from wic.utils import runner
 from wic.utils.misc import get_bitbake_var, exec_cmd, exec_native_cmd
 from wic.pluginbase import SourcePlugin
@@ -210,4 +210,4 @@ class RootfsPlugin(SourcePlugin):
 
         ret_code = runner.show(['dd', 'if=%s' % mbrfile, 'of=%s' % full_path, 'conv=notrunc'])
         if ret_code != 0:
-            raise ImageError("Unable to set MBR to %s" % full_path)
+            raise WicError("Unable to set MBR to %s" % full_path)
Signature · 1 line
2.1.4