From b5c98ff757dd13a1d416958f015f9f8f983c5ec5 Mon Sep 17 00:00:00 2001 From: Gui Chen Date: Fri, 30 Dec 2011 13:33:21 +0800 Subject: [PATCH] loop.py & loop_plugin.py: mountpoints check and code style Signed-off-by: Gui Chen --- mic/imager/loop.py | 34 +++++++++++++++++---------------- plugins/imager/loop_plugin.py | 44 +++++++++++++++++++++++++++---------------- 2 files changed, 46 insertions(+), 32 deletions(-) diff --git a/mic/imager/loop.py b/mic/imager/loop.py index c2f0178..0494777 100644 --- a/mic/imager/loop.py +++ b/mic/imager/loop.py @@ -51,7 +51,9 @@ def load_mountpoints(dpath): with open(os.path.join(dpath, '.mountpoints')) as f: for line in f.readlines(): try: - mps.append(tuple(line.strip().split(':'))) + mp, label, name, size, fstype = line.strip().split(':') + size = int(size) + mps.append((mp, label, name, size, fstype)) except: msger.warning("wrong format line in mountpoints mapping file") return mps @@ -70,8 +72,8 @@ class LoopImageCreator(BaseImageCreator): def __init__(self, creatoropts=None, pkgmgr=None, taring_to=None): """Initialize a LoopImageCreator instance. - This method takes the same arguments as ImageCreator.__init__() with - the addition of: + This method takes the same arguments as ImageCreator.__init__() + with the addition of: fslabel -- A string used as a label for any filesystems created. """ @@ -90,7 +92,7 @@ class LoopImageCreator(BaseImageCreator): allloops = [] for part in sorted(kickstart.get_partitions(self.ks), - key = lambda p: p.mountpoint): + key=lambda p: p.mountpoint): if part.fstype == "swap": continue @@ -99,22 +101,22 @@ class LoopImageCreator(BaseImageCreator): if mp == '/': # the base image if not label: - label = self.name + label = self.name else: mp = mp.rstrip('/') if not label: - msger.warning('no "label" specified for loop img at %s,' - ' use the mountpoint as the name' % mp) + msger.warning('no "label" specified for loop img at %s' + ', use the mountpoint as the name' % mp) label = mp.split('/')[-1] - imgname = misc.strip_end(label,'.img') + '.img' + imgname = misc.strip_end(label, '.img') + '.img' allloops.append({ 'mountpoint': mp, 'label': label, 'name': imgname, 'size': part.size or 4096L * 1024 * 1024, 'fstype': part.fstype or 'ext3', - 'loop': None, # to be created in _mount_instroot + 'loop': None, # to be created in _mount_instroot }) self._instloops = allloops @@ -135,7 +137,6 @@ class LoopImageCreator(BaseImageCreator): def _set_fstype(self, fstype): self.__fstype = fstype - def _set_image_size(self, imgsize): self.__image_size = imgsize @@ -159,9 +160,9 @@ class LoopImageCreator(BaseImageCreator): #filesystem label. In the case of ext3 it's 16 characters, but in the case #of ISO9660 it's 32 characters. # - #mke2fs silently truncates the label, but mkisofs aborts if the label is too - #long. So, for convenience sake, any string assigned to this attribute is - #silently truncated to FSLABEL_MAXLEN (32) characters. + #mke2fs silently truncates the label, but mkisofs aborts if the label is + #too long. So, for convenience sake, any string assigned to this attribute + #is silently truncated to FSLABEL_MAXLEN (32) characters. fslabel = property(__get_fslabel, __set_fslabel) def __get_image(self): @@ -226,7 +227,7 @@ class LoopImageCreator(BaseImageCreator): # # Helpers for subclasses # - def _resparse(self, size = None): + def _resparse(self, size=None): """Rebuild the filesystem image to be as sparse as possible. This method should be used by subclasses when staging the final image @@ -259,10 +260,11 @@ class LoopImageCreator(BaseImageCreator): if self.__imgdir is None: self.__imgdir = self._mkdtemp() + # # Actual implementation # - def _mount_instroot(self, base_on = None): + def _mount_instroot(self, base_on=None): if base_on and os.path.isfile(base_on): self.__imgdir = os.path.dirname(base_on) @@ -306,7 +308,7 @@ class LoopImageCreator(BaseImageCreator): loop['label']) try: - msger.verbose('Mounting image "%s" on "%s"' %(imgname, mp)) + msger.verbose('Mounting image "%s" on "%s"' % (imgname, mp)) fs.makedirs(mp) loop['loop'].mount() except MountError, e: diff --git a/plugins/imager/loop_plugin.py b/plugins/imager/loop_plugin.py index e4b8661..2fe004d 100644 --- a/plugins/imager/loop_plugin.py +++ b/plugins/imager/loop_plugin.py @@ -30,7 +30,9 @@ class LoopPlugin(ImagerPlugin): name = 'loop' @classmethod - @cmdln.option("--taring-to", dest="taring_to", type='string', default=None, help="Specify the filename for packaging all loop images into a single tarball") + @cmdln.option("--taring-to", dest="taring_to", type='string', default=None, + help="Specify the filename for packaging all loop images " + "into a single tarball") def do_create(self, subcmd, opts, *args): """${cmd_name}: create loop image @@ -60,10 +62,13 @@ class LoopPlugin(ImagerPlugin): ksconf = misc.save_ksconf_file(ksconf, creatoropts['release']) configmgr._ksconf = ksconf - - # Called After setting the configmgr._ksconf as the creatoropts['name'] is reset there. + + # Called After setting the configmgr._ksconf + # as the creatoropts['name'] is reset there. if creatoropts['release'] is not None: - creatoropts['outdir'] = "%s/%s/images/%s/" % (creatoropts['outdir'], creatoropts['release'], creatoropts['name']) + creatoropts['outdir'] = "%s/%s/images/%s/" % (creatoropts['outdir'], + creatoropts['release'], + creatoropts['name']) # try to find the pkgmgr pkgmgr = None @@ -74,7 +79,10 @@ class LoopPlugin(ImagerPlugin): if not pkgmgr: pkgmgrs = pluginmgr.get_plugins('backend').keys() - raise errors.CreatorError("Can't find package manager: %s (availables: %s)" % (creatoropts['pkgmgr'], ', '.join(pkgmgrs))) + raise errors.CreatorError("Can't find package manager: %s " + "(availables: %s)" \ + % (creatoropts['pkgmgr'], + ', '.join(pkgmgrs))) creator = LoopImageCreator(creatoropts, pkgmgr, opts.taring_to) @@ -88,7 +96,8 @@ class LoopPlugin(ImagerPlugin): imagefile = "%s.img" % os.path.join(creator.destdir, creator.name) if os.path.exists(imagefile): - if msger.ask('The target image: %s already exists, cleanup and continue?' % imagefile): + if msger.ask('The target image: %s already exists, cleanup ' \ + 'and continue?' % imagefile): os.unlink(imagefile) else: raise errors.Abort('Canceled') @@ -102,7 +111,9 @@ class LoopPlugin(ImagerPlugin): creator.package(creatoropts["outdir"]) if creatoropts['release'] is not None: - creator.release_output(ksconf, creatoropts['outdir'], creatoropts['release']) + creator.release_output(ksconf, + creatoropts['outdir'], + creatoropts['release']) creator.print_outimage_info() except errors.CreatorError: @@ -135,7 +146,7 @@ class LoopPlugin(ImagerPlugin): msger.error("Cannot support fstype: %s" % fstype) name = os.path.join(tmpdir, name) - size = int(size) * 1024L * 1024L + size = size * 1024L * 1024L loop = myDiskMount(fs_related.SparseLoopbackDisk(name, size), os.path.join(mntdir, mp.lstrip('/')), fstype, size, label) @@ -150,8 +161,7 @@ class LoopPlugin(ImagerPlugin): for lp in reversed(loops): chroot.cleanup_after_chroot("img", lp, None, mntdir) - shutil.rmtree(mntdir, ignore_errors = True) - shutil.rmtree(tmpdir, ignore_errors = True) + shutil.rmtree(tmpdir, ignore_errors=True) raise loops.append(loop) @@ -164,10 +174,10 @@ class LoopPlugin(ImagerPlugin): for loop in reversed(loops): chroot.cleanup_after_chroot("img", loop, None, mntdir) - shutil.rmtree(tmpdir, ignore_errors = True) + shutil.rmtree(tmpdir, ignore_errors=True) @classmethod - def do_chroot(cls, target):#chroot.py parse opts&args + def do_chroot(cls, target): import tarfile if tarfile.is_tarfile(target): LoopPlugin._do_chroot_tar(target) @@ -183,7 +193,8 @@ class LoopPlugin(ImagerPlugin): fstype = imgtype[:4] myDiskMount = fs_related.ExtDiskMount else: - raise errors.CreatorError("Unsupported filesystem type: %s" % imgtype) + raise errors.CreatorError("Unsupported filesystem type: %s" \ + % imgtype) extmnt = misc.mkdtemp() extloop = myDiskMount(fs_related.SparseLoopbackDisk(img, imgsize), @@ -196,19 +207,20 @@ class LoopPlugin(ImagerPlugin): except errors.MountError: extloop.cleanup() - shutil.rmtree(extmnt, ignore_errors = True) + shutil.rmtree(extmnt, ignore_errors=True) raise try: chroot.chroot(extmnt, None, "/bin/env HOME=/root /bin/bash") except: - raise errors.CreatorError("Failed to chroot to %s." %img) + raise errors.CreatorError("Failed to chroot to %s." % img) finally: chroot.cleanup_after_chroot("img", extloop, None, extmnt) @classmethod def do_unpack(cls, srcimg): - image = os.path.join(tempfile.mkdtemp(dir = "/var/tmp", prefix = "tmp"), "target.img") + image = os.path.join(tempfile.mkdtemp(dir="/var/tmp", prefix="tmp"), + "target.img") msger.info("Copying file system ...") shutil.copyfile(srcimg, image) return image -- 2.7.4