From c49f895f3f19b8ef4e64aa5969084c45c58db376 Mon Sep 17 00:00:00 2001 From: JF Ding Date: Mon, 5 Sep 2011 10:46:08 +0900 Subject: [PATCH] remove improper exception handlings --- mic/imager/baseimager.py | 6 ++++-- mic/imager/liveusb.py | 7 ++----- mic/imager/raw.py | 9 ++------- mic/utils/fs_related.py | 12 ++++-------- plugins/backend/yumpkgmgr.py | 6 +++--- plugins/imager/fs_plugin.py | 2 +- plugins/imager/livecd_plugin.py | 17 ++++++++--------- plugins/imager/liveusb_plugin.py | 18 ++++++++++-------- plugins/imager/loop_plugin.py | 11 +++++------ plugins/imager/raw_plugin.py | 16 +++++++--------- 10 files changed, 46 insertions(+), 58 deletions(-) diff --git a/mic/imager/baseimager.py b/mic/imager/baseimager.py index 9e3ef51..f1aba09 100644 --- a/mic/imager/baseimager.py +++ b/mic/imager/baseimager.py @@ -1021,10 +1021,12 @@ class BaseImageCreator(object): self.do_genchecksum(os.path.join(destdir, f)) def print_outimage_info(self): - msger.info("Your new image can be found here:") + msg = "Your new image can be found here:\n" self.outimage.sort() for file in self.outimage: - msger.raw(os.path.abspath(file)) + msg += ' %s\n' % os.path.abspath(file) + + msger.info(msg) def check_depend_tools(self): for tool in self._dep_checks: diff --git a/mic/imager/liveusb.py b/mic/imager/liveusb.py index 129d988..6ed65ee 100644 --- a/mic/imager/liveusb.py +++ b/mic/imager/liveusb.py @@ -63,10 +63,7 @@ class LiveUSBImageCreator(LiveCDImageCreator): usbloop.add_partition(usbimgsize/1024/1024, "/dev/sdb", "/", fstype, boot=True) - try: - usbloop.mount() - except MountError, e: - raise CreatorError("Failed mount disks : %s" % e) + usbloop.mount() try: fs_related.makedirs(usbmnt + "/LiveOS") @@ -100,7 +97,7 @@ class LiveUSBImageCreator(LiveCDImageCreator): syslinux_path = "/usr/lib/syslinux" else: raise CreatorError("syslinux not installed : " - "cannot find syslinux installation path") + "cannot find syslinux installation path") for f in ("isolinux.bin", "vesamenu.c32"): path = os.path.join(syslinux_path, f) diff --git a/mic/imager/raw.py b/mic/imager/raw.py index 124d367..a1d72ef 100644 --- a/mic/imager/raw.py +++ b/mic/imager/raw.py @@ -21,7 +21,7 @@ import os import stat import shutil -import urlgrabber.progress as progress +from urlgrabber import progress from mic import kickstart, msger from mic.utils import fs_related @@ -169,11 +169,7 @@ class RawImageCreator(BaseImageCreator): for p in parts: self.__instloop.add_partition(int(p.size), p.disk, p.mountpoint, p.fstype, fsopts = p.fsopts, boot = p.active) - try: - self.__instloop.mount() - except MountError, e: - raise CreatorError("Failed mount disks : %s" % e) - + self.__instloop.mount() self._create_mkinitrd_config() def _get_required_packages(self): @@ -288,7 +284,6 @@ class RawImageCreator(BaseImageCreator): if rc != 0 and 1 == 0: raise MountError("Unable to set bootable flag to %sp%d" % (loopdev, (bootdevnum + 1))) - #Ensure all data is flushed to disk before doing syslinux install msger.run('sync', True) diff --git a/mic/utils/fs_related.py b/mic/utils/fs_related.py index 24fdc3a..ba8e236 100644 --- a/mic/utils/fs_related.py +++ b/mic/utils/fs_related.py @@ -102,8 +102,7 @@ def mksquashfs(in_img, out_img): ret = msger.run(args) if ret != 0: - raise SquashfsError("'%s' exited with error (%d)" % - (string.join(args, " "), ret)) + raise SquashfsError("'%s' exited with error (%d)" % (' '.join(args), ret)) def resize2fs(fs, size): resize2fs = find_binary_path("resize2fs") @@ -201,7 +200,7 @@ class LoopbackMount: def lounsetup(self): if self.losetup: - rc = msger.run([self.losetupcmd, "-d", self.loopdev]) + msger.run([self.losetupcmd, "-d", self.loopdev]) self.losetup = False self.loopdev = None @@ -355,8 +354,6 @@ class LoopbackDisk(Disk): rc = msger.run([self.losetupcmd, "-d", self.device]) self.device = None - - class SparseLoopbackDisk(LoopbackDisk): """A Disk backed by a sparse file via the loop module.""" def __init__(self, lofile, size): @@ -508,7 +505,7 @@ class ExtDiskMount(DiskMount): "-m", "1", "-b", str(self.blocksize), self.disk.device]) # str(self.disk.size / self.blocksize)]) if rc != 0: - raise MountError("Error creating %s filesystem on disk %s" % (self.fstype,self.disk.device)) + raise MountError("Error creating %s filesystem on disk %s" % (self.fstype, self.disk.device)) dev_null = os.open("/dev/null", os.O_WRONLY) try: @@ -816,8 +813,7 @@ class DeviceMapperSnapshot(object): if msger.run(args) != 0: self.cowloop.cleanup() self.imgloop.cleanup() - raise SnapshotError("Could not create snapshot device using: " + - string.join(args, " ")) + raise SnapshotError("Could not create snapshot device using: " + ' '.join(args)) self.__created = True diff --git a/plugins/backend/yumpkgmgr.py b/plugins/backend/yumpkgmgr.py index 1028c55..6dcc15d 100644 --- a/plugins/backend/yumpkgmgr.py +++ b/plugins/backend/yumpkgmgr.py @@ -91,11 +91,10 @@ def getRPMCallback(): fmt = self._makefmt(percent) msg = fmt % ("Installing") if msg != self.lastmsg: - sys.stdout.write(msg) - sys.stdout.flush() + msger.info(msg) self.lastmsg = msg if self.total_installed == self.total_actions: - sys.stdout.write("\n") + msger.raw() elif what == rpm.RPMCALLBACK_UNINST_START: pass @@ -372,6 +371,7 @@ class Yum(BackendPlugin, yum.YumBase): cb.tsInfo = self.tsInfo cb.filelog = False + msger.warning('\nCaution, do NOT interrupt the installation, else mic cannot finish the cleanup.') ret = self.runTransaction(cb) self._cleanupRpmdbLocks(self.conf.installroot) return ret diff --git a/plugins/imager/fs_plugin.py b/plugins/imager/fs_plugin.py index 25a3e62..ca8b8ba 100644 --- a/plugins/imager/fs_plugin.py +++ b/plugins/imager/fs_plugin.py @@ -87,7 +87,7 @@ class FsPlugin(ImagerPlugin): creator.package(destdir) outimage = creator.outimage creator.print_outimage_info() - except errors.CreatorError, e: + except errors.CreatorError: raise finally: creator.cleanup() diff --git a/plugins/imager/livecd_plugin.py b/plugins/imager/livecd_plugin.py index f48137d..56610d6 100644 --- a/plugins/imager/livecd_plugin.py +++ b/plugins/imager/livecd_plugin.py @@ -49,20 +49,19 @@ class LiveCDPlugin(ImagerPlugin): cfgmgr.setProperty("ksconf", ksconf) creatoropts = cfgmgr.create - if creatoropts['arch'].startswith('arm'): + if creatoropts['arch'] and creatoropts['arch'].startswith('arm'): msger.warning('livecd cannot support arm images, Quit') return # try to find the pkgmgr pkgmgr = None - plgmgr = pluginmgr.PluginMgr() - for (key, pcls) in plgmgr.get_plugins('backend').iteritems(): + for (key, pcls) in pluginmgr.PluginMgr().get_plugins('backend').iteritems(): if key == creatoropts['pkgmgr']: pkgmgr = pcls break if not pkgmgr: - raise errors.CreatorError("Can't find backend %s" % pkgmgr) + raise errors.CreatorError("Can't find package manager: %s" % creatoropts['pkgmgr']) creator = livecd.LiveCDImageCreator(creatoropts, pkgmgr) try: @@ -76,7 +75,7 @@ class LiveCDPlugin(ImagerPlugin): creator.print_outimage_info() outimage = creator.outimage - except errors.CreatorError, e: + except errors.CreatorError: raise finally: creator.cleanup() @@ -104,11 +103,11 @@ class LiveCDPlugin(ImagerPlugin): try: extloop.mount() - except errors.MountError, e: + except errors.MountError: extloop.cleanup() shutil.rmtree(extmnt, ignore_errors = True) shutil.rmtree(os_image_dir, ignore_errors = True) - raise errors.CreatorError("Failed to loopback mount '%s' : %s" %(os_image, e)) + raise try: chroot.chroot(extmnt, None, "/bin/env HOME=/root /bin/bash") @@ -160,9 +159,9 @@ class LiveCDPlugin(ImagerPlugin): imgloop = fs_related.DiskMount(fs_related.LoopbackDisk(img, 0), imgmnt) try: imgloop.mount() - except errors.MountError, e: + except errors.MountError: imgloop.cleanup() - raise errors.CreatorError("Failed to loopback mount '%s' : %s" %(img, e)) + raise # legacy LiveOS filesystem layout support, remove for F9 or F10 if os.path.exists(imgmnt + "/squashfs.img"): diff --git a/plugins/imager/liveusb_plugin.py b/plugins/imager/liveusb_plugin.py index c1b4b09..d230e18 100644 --- a/plugins/imager/liveusb_plugin.py +++ b/plugins/imager/liveusb_plugin.py @@ -52,18 +52,20 @@ class LiveUSBPlugin(ImagerPlugin): creatoropts = cfgmgr.create cfgmgr.setProperty("ksconf", args[0]) - if creatoropts['arch'].startswith('arm'): + if creatoropts['arch'] and creatoropts['arch'].startswith('arm'): msger.warning('liveusb cannot support arm images, Quit') return # try to find the pkgmgr pkgmgr = None - plgmgr = pluginmgr.PluginMgr() - for (key, pcls) in plgmgr.get_plugins('backend').iteritems(): + for (key, pcls) in pluginmgr.PluginMgr().get_plugins('backend').iteritems(): if key == creatoropts['pkgmgr']: pkgmgr = pcls break + if not pkgmgr: + raise errors.CreatorError("Can't find package manager: %s" % creatoropts['pkgmgr']) + creator = liveusb.LiveUSBImageCreator(creatoropts, pkgmgr) try: creator.check_depend_tools() @@ -76,7 +78,7 @@ class LiveUSBPlugin(ImagerPlugin): creator.print_outimage_info() outimage = creator.outimage - except errors.CreatorError, e: + except errors.CreatorError: raise finally: creator.cleanup() @@ -104,10 +106,10 @@ class LiveUSBPlugin(ImagerPlugin): try: extloop.mount() - except errors.MountError, e: + except errors.MountError: extloop.cleanup() shutil.rmtree(extmnt, ignore_errors = True) - raise errors.CreatorError("Failed to loopback mount '%s' : %s" %(os_image, e)) + raise try: chroot.chroot(extmnt, None, "/bin/env HOME=/root /bin/bash") @@ -163,9 +165,9 @@ class LiveUSBPlugin(ImagerPlugin): imgloop.add_partition(imgsize/1024/1024, "/dev/sdb", "/", "vfat", boot=False) try: imgloop.mount() - except errors.MountError, e: + except errors.MountError: imgloop.cleanup() - raise errors.CreatorError("Failed to loopback mount '%s' : %s" %(img, e)) + raise # legacy LiveOS filesystem layout support, remove for F9 or F10 if os.path.exists(imgmnt + "/squashfs.img"): diff --git a/plugins/imager/loop_plugin.py b/plugins/imager/loop_plugin.py index 2281ad0..c2d7ffa 100644 --- a/plugins/imager/loop_plugin.py +++ b/plugins/imager/loop_plugin.py @@ -51,14 +51,13 @@ class LoopPlugin(ImagerPlugin): # try to find the pkgmgr pkgmgr = None - plgmgr = pluginmgr.PluginMgr() - for (key, pcls) in plgmgr.get_plugins('backend').iteritems(): + for (key, pcls) in pluginmgr.PluginMgr().get_plugins('backend').iteritems(): if key == creatoropts['pkgmgr']: pkgmgr = pcls break if not pkgmgr: - raise errors.CreatorError("Can't find backend %s" % pkgmgr) + raise errors.CreatorError("Can't find package manager: %s" % creatoropts['pkgmgr']) creator = loop.LoopImageCreator(creatoropts, pkgmgr) try: @@ -68,7 +67,7 @@ class LoopPlugin(ImagerPlugin): creator.configure(creatoropts["repomd"]) creator.unmount() creator.package(creatoropts["outdir"]) - except errors.CreatorError, e: + except errors.CreatorError: raise finally: creator.cleanup() @@ -89,10 +88,10 @@ class LoopPlugin(ImagerPlugin): try: extloop.mount() - except errors.MountError, e: + except errors.MountError: extloop.cleanup() shutil.rmtree(extmnt, ignore_errors = True) - raise errors.CreatorError("Failed to loopback mount '%s' : %s" %(img, e)) + raise try: chroot.chroot(extmnt, None, "/bin/env HOME=/root /bin/bash") diff --git a/plugins/imager/raw_plugin.py b/plugins/imager/raw_plugin.py index 6d5b79e..146a7a9 100644 --- a/plugins/imager/raw_plugin.py +++ b/plugins/imager/raw_plugin.py @@ -54,14 +54,13 @@ class RawPlugin(ImagerPlugin): # try to find the pkgmgr pkgmgr = None - plgmgr = pluginmgr.PluginMgr() - for (key, pcls) in plgmgr.get_plugins('backend').iteritems(): + for (key, pcls) in pluginmgr.PluginMgr().get_plugins('backend').iteritems(): if key == creatoropts['pkgmgr']: pkgmgr = pcls break if not pkgmgr: - raise errors.CreatorError("Can't find backend %s" % pkgmgr) + raise errors.CreatorError("Can't find package manager: %s" % creatoropts['pkgmgr']) creator = raw.RawImageCreator(creatoropts, pkgmgr) try: @@ -75,7 +74,7 @@ class RawPlugin(ImagerPlugin): creator.print_outimage_info() outimage = creator.outimage - except errors.CreatorError, e: + except errors.CreatorError: raise finally: creator.cleanup() @@ -162,9 +161,9 @@ class RawPlugin(ImagerPlugin): try: imgloop.mount() - except errors.MountError, e: + except errors.MountError: imgloop.cleanup() - raise errors.CreatorError("Failed to loopback mount '%s' : %s" % (img, e)) + raise try: chroot.chroot(imgmnt, None, "/bin/env HOME=/root /bin/bash") @@ -184,10 +183,9 @@ class RawPlugin(ImagerPlugin): try: srcloop.mount() - except errors.MountError, e: + except errors.MountError: srcloop.cleanup() - raise errors.CreatorError("Failed to loopback mount '%s' : %s" % - (srcimg, e)) + raise image = os.path.join(tempfile.mkdtemp(dir = "/var/tmp", prefix = "tmp"), "target.img") args = ['dd', "if=%s" % srcloop.partitions[0]['device'], "of=%s" % image] -- 2.7.4