From cb02082ebe5aa0a30dd93b93875c7bb140dc3709 Mon Sep 17 00:00:00 2001 From: JF Ding Date: Fri, 2 Sep 2011 14:24:05 +0900 Subject: [PATCH] wrapper all subprocess call using msger.run all the output of external tools can be controlled --- mic/chroot.py | 7 +--- mic/configmgr.py | 22 +++++------ mic/imager/baseimager.py | 9 ++--- mic/imager/fs.py | 3 +- mic/imager/livecd.py | 7 ++-- mic/imager/liveusb.py | 21 +++++----- mic/imager/raw.py | 16 +++----- mic/kickstart/__init__.py | 9 ++--- mic/msger.py | 23 ++++++++++- mic/utils/fs_related.py | 85 +++++++++++++++++----------------------- mic/utils/misc.py | 3 -- mic/utils/rpmmisc.py | 13 ++---- plugins/backend/yumpkgmgr.py | 2 +- plugins/backend/zypppkgmgr.py | 4 +- plugins/imager/livecd_plugin.py | 3 +- plugins/imager/liveusb_plugin.py | 3 +- plugins/imager/raw_plugin.py | 5 ++- 17 files changed, 109 insertions(+), 126 deletions(-) diff --git a/mic/chroot.py b/mic/chroot.py index c3bcb4e..9b4ae37 100644 --- a/mic/chroot.py +++ b/mic/chroot.py @@ -83,11 +83,10 @@ def check_bind_mounts(chrootdir, bindmounts): return True def cleanup_mounts(chrootdir): - dev_null = os.open("/dev/null", os.O_WRONLY) umountcmd = misc.find_binary_path("umount") for point in BIND_MOUNTS: args = [ umountcmd, "-l", chrootdir + point ] - subprocess.call(args, stdout=dev_null, stderr=dev_null) + msger.run(args, True) abs_chrootdir = os.path.abspath(chrootdir) with open('/proc/mounts') as f: @@ -99,13 +98,11 @@ def cleanup_mounts(chrootdir): continue args = [ umountcmd, "-l", point ] - ret = subprocess.call(args, stdout=dev_null, stderr=dev_null) + ret = msger.run(args, True) if ret != 0: msger.warning("failed to unmount %s" % point) - os.close(dev_null) return ret - os.close(dev_null) return 0 def setup_chrootenv(chrootdir, bindmounts = None): diff --git a/mic/configmgr.py b/mic/configmgr.py index b1353d2..e6b077d 100644 --- a/mic/configmgr.py +++ b/mic/configmgr.py @@ -111,18 +111,16 @@ class ConfigMgr(object): if not ksconf: return - try: - ks = kickstart.read_kickstart(ksconf) - ksrepos = misc.get_repostrs_from_ks(ks) - msger.info("Retrieving repo metadata:") - repometadata = misc.get_metadata_from_repos(ksrepos, self.create['cachedir']) - msger.raw(" DONE") - - self.create['ks'] = ks - self.create['repomd'] = repometadata - self.create['name'] = os.path.splitext(os.path.basename(ksconf))[0] - except Exception, e: - raise errors.KsError("Unable to load kickstart file '%s': %s" % (ksconf, e)) + ks = kickstart.read_kickstart(ksconf) + ksrepos = misc.get_repostrs_from_ks(ks) + + msger.info("Retrieving repo metadata:") + repometadata = misc.get_metadata_from_repos(ksrepos, self.create['cachedir']) + msger.raw(" DONE") + + self.create['ks'] = ks + self.create['repomd'] = repometadata + self.create['name'] = os.path.splitext(os.path.basename(ksconf))[0] def setProperty(self, key, value): if not hasattr(self, key): diff --git a/mic/imager/baseimager.py b/mic/imager/baseimager.py index 225e06b..05bebae 100644 --- a/mic/imager/baseimager.py +++ b/mic/imager/baseimager.py @@ -862,7 +862,7 @@ class BaseImageCreator(object): pass def __run_post_scripts(self): - msger.info("Running scripts") + msger.info("Running scripts ...") for s in kickstart.get_post_scripts(self.ks): (fd, path) = tempfile.mkstemp(prefix = "ks-script-", dir = self._instroot + "/tmp") @@ -993,12 +993,12 @@ class BaseImageCreator(object): if self.__img_compression_method == "bz2": bzip2 = find_binary_path('bzip2') msger.info("Compressing %s with bzip2. Please wait..." % img_location) - rc = subprocess.call([bzip2, "-f", img_location]) + rc = msger.run([bzip2, "-f", img_location]) if rc: raise CreatorError("Failed to compress image %s with %s." % (img_location, self.__img_compression_method)) for bootimg in glob.glob(os.path.dirname(img_location) + "/*-boot.bin"): msger.info("Compressing %s with bzip2. Please wait..." % bootimg) - rc = subprocess.call([bzip2, "-f", bootimg]) + rc = msger.run([bzip2, "-f", bootimg]) if rc: raise CreatorError("Failed to compress image %s with %s." % (bootimg, self.__img_compression_method)) @@ -1012,8 +1012,7 @@ class BaseImageCreator(object): makedirs(destdir) # Ensure all data is flushed to _outdir - synccmd = find_binary_path("sync") - subprocess.call([synccmd]) + msger.run('sync', True) for f in os.listdir(self._outdir): shutil.move(os.path.join(self._outdir, f), diff --git a/mic/imager/fs.py b/mic/imager/fs.py index 52599f5..6475e92 100644 --- a/mic/imager/fs.py +++ b/mic/imager/fs.py @@ -18,7 +18,6 @@ # import os, sys -import subprocess from baseimager import BaseImageCreator from mic import msger @@ -41,7 +40,7 @@ class FsImageCreator(BaseImageCreator): msger.info("Copying %s to %s ..." % (self._instroot, destdir + "/" + self.name)) args = ['cp', "-af", self._instroot, destdir + "/" + self.name ] - subprocess.call(args) + msger.run(args) ignores = ["/dev/fd", "/dev/stdin", "/dev/stdout", "/dev/stderr", "/etc/mtab"] for exclude in ignores: diff --git a/mic/imager/livecd.py b/mic/imager/livecd.py index 70e05d7..a969ffb 100644 --- a/mic/imager/livecd.py +++ b/mic/imager/livecd.py @@ -20,7 +20,6 @@ import os, sys import glob import shutil -import subprocess from mic import kickstart, msger from mic.utils import fs_related, rpmmisc @@ -221,7 +220,7 @@ class LiveImageCreatorBase(LoopImageCreator): args.append(isodir) - if subprocess.call(args) != 0: + if msger.run(args) != 0: raise CreatorError("ISO creation failed!") """ It should be ok still even if you haven't isohybrid """ @@ -233,7 +232,7 @@ class LiveImageCreatorBase(LoopImageCreator): if isohybrid: args = [isohybrid, "-partok", iso ] - if subprocess.call(args) != 0: + if msger.run(args) != 0: raise CreatorError("Hybrid ISO creation failed!") self.__implant_md5sum(iso) @@ -249,7 +248,7 @@ class LiveImageCreatorBase(LoopImageCreator): implantisomd5 = "" return - subprocess.call([implantisomd5, iso], stdout=sys.stdout, stderr=sys.stderr) + msger.run([implantisomd5, iso]) def _stage_final_image(self): try: diff --git a/mic/imager/liveusb.py b/mic/imager/liveusb.py index c1ff2b7..129d988 100644 --- a/mic/imager/liveusb.py +++ b/mic/imager/liveusb.py @@ -19,7 +19,6 @@ import os import shutil -import subprocess import re from mic import msger @@ -91,7 +90,7 @@ class LiveUSBImageCreator(LiveCDImageCreator): overlaysuffix = "-%s-%s" % (diskmount.fslabel, diskmount.uuid) args = ['cp', "-Rf", isodir + "/isolinux", usbmnt + "/syslinux"] - rc = subprocess.call(args) + rc = msger.run(args) if rc: raise CreatorError("Can't copy isolinux directory %s" % (isodir + "/isolinux/*")) @@ -107,7 +106,7 @@ class LiveUSBImageCreator(LiveCDImageCreator): path = os.path.join(syslinux_path, f) if os.path.isfile(path): args = ['cp', path, usbmnt + "/syslinux/"] - rc = subprocess.call(args) + rc = msger.run(args) if rc: raise CreatorError("Can't copy syslinux file %s" % (path)) else: @@ -131,7 +130,7 @@ class LiveUSBImageCreator(LiveCDImageCreator): args = ['dd', "if=/dev/zero", "of=" + usbmnt + "/LiveOS/" + overfile, "count=%d" % overlaysizemb, "bs=1M"] else: args = ['dd', "if=/dev/null", "of=" + usbmnt + "/LiveOS/" + overfile, "count=1", "bs=1M", "seek=%d" % overlaysizemb] - rc = subprocess.call(args) + rc = msger.run(args) if rc: raise CreatorError("Can't create overlay file") text = text.replace("liveimg", "liveimg overlay=" + usblabel) @@ -141,11 +140,11 @@ class LiveUSBImageCreator(LiveCDImageCreator): msger.info("Initializing swap file") swapfile = usbmnt + "/LiveOS/" + "swap.img" args = ['dd', "if=/dev/zero", "of=" + swapfile, "count=%d" % swapsizemb, "bs=1M"] - rc = subprocess.call(args) + rc = msger.run(args) if rc: raise CreatorError("Can't create swap file") args = ["mkswap", "-f", swapfile] - rc = subprocess.call(args) + rc = msger.run(args) if rc: raise CreatorError("Can't mkswap on swap file") @@ -156,7 +155,7 @@ class LiveUSBImageCreator(LiveCDImageCreator): args = ['dd', "if=/dev/zero", "of=" + homefile, "count=%d" % homesizemb, "bs=1M"] else: args = ['dd', "if=/dev/null", "of=" + homefile, "count=1", "bs=1M", "seek=%d" % homesizemb] - rc = subprocess.call(args) + rc = msger.run(args) if rc: raise CreatorError("Can't create home file") @@ -165,13 +164,13 @@ class LiveUSBImageCreator(LiveCDImageCreator): args = [mkfscmd, "-F", "-j", homefile] else: args = [mkfscmd, homefile] - rc = subprocess.call(args, stdout=sys.stdout, stderr=sys.stderr) + rc = msger.run(args) if rc: raise CreatorError("Can't mke2fs home file") if fstype == "ext2" or fstype == "ext3": tune2fs = fs_related.find_binary_path("tune2fs") args = [tune2fs, "-c0", "-i0", "-ouser_xattr,acl", homefile] - rc = subprocess.call(args, stdout=sys.stdout, stderr=sys.stderr) + rc = msger.run(args) if rc: raise CreatorError("Can't tune2fs home file") @@ -190,7 +189,7 @@ class LiveUSBImageCreator(LiveCDImageCreator): fd = open(syslinuxcfg, "w") fd.write(text) fd.close() - rc = subprocess.call(args) + rc = msger.run(args) if rc: raise CreatorError("Can't install boot loader.") @@ -209,7 +208,7 @@ class LiveUSBImageCreator(LiveCDImageCreator): outimg = "%s/%s.usbimg" % (self._outdir, self.name) args = ['dd', "if=" + mbrfile, "of=" + outimg, "seek=0", "conv=notrunc", "bs=1", "count=%d" % (mbrsize)] - rc = subprocess.call(args) + rc = msger.run(args) if rc: raise CreatorError("Can't set MBR.") diff --git a/mic/imager/raw.py b/mic/imager/raw.py index 7e6fbac..124d367 100644 --- a/mic/imager/raw.py +++ b/mic/imager/raw.py @@ -20,7 +20,6 @@ import os import stat import shutil -import subprocess import urlgrabber.progress as progress @@ -61,13 +60,14 @@ class RawImageCreator(BaseImageCreator): self._dep_checks.extend(["sync", "kpartx", "parted", "extlinux"]) def configure(self, repodata = None): + import subprocess def chroot(): - os.chroot(self._instroot) os.chdir("/") if os.path.exists(self._instroot + "/usr/bin/Xorg"): subprocess.call(["/bin/chmod", "u+s", "/usr/bin/Xorg"], preexec_fn = chroot) + BaseImageCreator.configure(self, repodata) def _get_fstab(self): @@ -275,17 +275,13 @@ class RawImageCreator(BaseImageCreator): #Set MBR mbrsize = os.stat("%s/usr/share/syslinux/mbr.bin" % self._instroot)[stat.ST_SIZE] - ddcmd = fs_related.find_binary_path("dd") - rc = subprocess.call([ddcmd, "if=%s/usr/share/syslinux/mbr.bin" % self._instroot, "of=" + loopdev]) + rc = msger.run(['dd', "if=%s/usr/share/syslinux/mbr.bin" % self._instroot, "of=" + loopdev]) if rc != 0: raise MountError("Unable to set MBR to %s" % loopdev) #Set Bootable flag parted = fs_related.find_binary_path("parted") - dev_null = os.open("/dev/null", os.O_WRONLY) - rc = subprocess.call([parted, "-s", loopdev, "set", "%d" % (bootdevnum + 1), "boot", "on"], - stdout = dev_null, stderr = dev_null) - os.close(dev_null) + rc = msger.run([parted, "-s", loopdev, "set", "%d" % (bootdevnum + 1), "boot", "on"], True) #XXX disabled return code check because parted always fails to #reload part table with loop devices. Annoying because we can't #distinguish this failure from real partition failures :-( @@ -294,10 +290,10 @@ class RawImageCreator(BaseImageCreator): #Ensure all data is flushed to disk before doing syslinux install - subprocess.call(["sync"]) + msger.run('sync', True) fullpathsyslinux = fs_related.find_binary_path("extlinux") - rc = subprocess.call([fullpathsyslinux, "-i", "%s/boot/extlinux" % self._instroot]) + rc = msger.run([fullpathsyslinux, "-i", "%s/boot/extlinux" % self._instroot]) if rc != 0: raise MountError("Unable to install syslinux bootloader to %sp%d" % (loopdev, (bootdevnum + 1))) diff --git a/mic/kickstart/__init__.py b/mic/kickstart/__init__.py index 8454421..d537af1 100644 --- a/mic/kickstart/__init__.py +++ b/mic/kickstart/__init__.py @@ -2,6 +2,7 @@ # kickstart.py : Apply kickstart configuration to a system # # Copyright 2007, Red Hat Inc. +# Copyright 2009, 2010, 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 @@ -400,8 +401,7 @@ class MoblinRepoConfig(KickstartConfig): fd.write("\n") def __create_repo_file(self, repo, repodir): - if not os.path.exists(self.path(repodir)): - fs.makedirs(self.path(repodir)) + fs.makedirs(self.path(repodir)) f = open(self.path(repodir + "/" + repo.name + ".repo"), "w") self.__create_repo_section(repo, "base", f) if repo.debuginfo: @@ -417,12 +417,9 @@ class MoblinRepoConfig(KickstartConfig): self.__create_repo_file(repo, "/etc/zypp/repos.d") """ Import repo gpg keys """ if repodata: - dev_null = os.open("/dev/null", os.O_WRONLY) for repo in repodata: if repo['repokey']: - subprocess.call([fs.find_binary_path("rpm"), "--root=%s" % self.instroot, "--import", repo['repokey']], - stdout = dev_null, stderr = dev_null) - os.close(dev_null) + msger.run(['rpm', "--root=%s" % self.instroot, "--import", repo['repokey']], True) class RPMMacroConfig(KickstartConfig): """A class to apply the specified rpm macros to the filesystem""" diff --git a/mic/msger.py b/mic/msger.py index b24c38d..c429e37 100644 --- a/mic/msger.py +++ b/mic/msger.py @@ -21,7 +21,7 @@ import os,sys import re -__ALL__ = ['set_mode', 'get_loglevel', 'set_loglevel', 'raw' 'debug', 'verbose', 'info', 'warning', 'error', 'ask', 'pause'] +__ALL__ = ['set_mode', 'get_loglevel', 'set_loglevel', 'raw' 'debug', 'verbose', 'info', 'warning', 'error', 'ask', 'pause', 'run'] # COLORs in ANSI INFO_COLOR = 32 # green @@ -184,3 +184,24 @@ def pause(msg=None): if not msg: msg = 'press to continue ...' raw_input(msg) + +def run(cmdln_or_args, q=False): + """ q: quiet? """ + + from subprocess import * + if isinstance(cmdln_or_args, list): + cmdln = ' '.join(cmdln_or_args) + else: + cmdln = cmdln_or_args + + p = Popen(cmdln, stdout=PIPE, stderr=PIPE, shell=True) + out = p.communicate()[0].strip() + if out and not q: + msg = 'running command: "%s"\n' % cmdln + msg += ' +----------------\n' + for line in out.splitlines(): + msg += ' | %s\n' % line + msg += ' +----------------' + verbose(msg) + + return p.returncode diff --git a/mic/utils/fs_related.py b/mic/utils/fs_related.py index ec368f8..24fdc3a 100644 --- a/mic/utils/fs_related.py +++ b/mic/utils/fs_related.py @@ -100,27 +100,23 @@ def mksquashfs(in_img, out_img): if not sys.stdout.isatty(): args.append("-no-progress") - ret = subprocess.call(args, stdout=sys.stdout, stderr=sys.stderr) + ret = msger.run(args) if ret != 0: raise SquashfsError("'%s' exited with error (%d)" % (string.join(args, " "), ret)) def resize2fs(fs, size): - dev_null = os.open("/dev/null", os.O_WRONLY) - try: - resize2fs = find_binary_path("resize2fs") - return subprocess.call([resize2fs, fs, "%sK" % (size / 1024,)], - stdout = dev_null, stderr = dev_null) - finally: - os.close(dev_null) + resize2fs = find_binary_path("resize2fs") + return msger.run([resize2fs, fs, "%sK" % (size / 1024,)], True) def my_fuser(file): ret = False fuser = find_binary_path("fuser") if not os.path.exists(file): return ret + dev_null = os.open("/dev/null", os.O_WRONLY) - rc = subprocess.call([fuser, "-s", file], stderr=dev_null) + rc = msger.run([fuser, "-s", file], True) if rc == 0: fuser_proc = subprocess.Popen([fuser, file], stdout=subprocess.PIPE, stderr=dev_null) pids = fuser_proc.communicate()[0].strip().split() @@ -172,12 +168,12 @@ class BindChrootMount: return makedirs(self.dest) - rc = subprocess.call([self.mountcmd, "--bind", self.src, self.dest]) + rc = msger.run([self.mountcmd, "--bind", self.src, self.dest]) if rc != 0: raise MountError("Bind-mounting '%s' to '%s' failed" % (self.src, self.dest)) if self.option: - rc = subprocess.call([self.mountcmd, "--bind", "-o", "remount,%s" % self.option, self.dest]) + rc = msger.run([self.mountcmd, "--bind", "-o", "remount,%s" % self.option, self.dest]) if rc != 0: raise MountError("Bind-remounting '%s' failed" % self.dest) self.mounted = True @@ -187,7 +183,7 @@ class BindChrootMount: return if self.ismounted(): - subprocess.call([self.umountcmd, "-l", self.dest]) + msger.run([self.umountcmd, "-l", self.dest]) self.mounted = False class LoopbackMount: @@ -205,7 +201,7 @@ class LoopbackMount: def lounsetup(self): if self.losetup: - rc = subprocess.call([self.losetupcmd, "-d", self.loopdev]) + rc = msger.run([self.losetupcmd, "-d", self.loopdev]) self.losetup = False self.loopdev = None @@ -223,7 +219,7 @@ class LoopbackMount: self.loopdev = losetupOutput.split()[0] - rc = subprocess.call([self.losetupcmd, self.loopdev, self.lofile]) + rc = msger.run([self.losetupcmd, self.loopdev, self.lofile]) if rc != 0: raise MountError("Failed to allocate loop device for '%s'" % self.lofile) @@ -346,7 +342,7 @@ class LoopbackDisk(Disk): device = losetupOutput.split()[0] msger.debug("Losetup add %s mapping to %s" % (device, self.lofile)) - rc = subprocess.call([self.losetupcmd, device, self.lofile]) + rc = msger.run([self.losetupcmd, device, self.lofile]) if rc != 0: raise MountError("Failed to allocate loop device for '%s'" % self.lofile) @@ -356,7 +352,7 @@ class LoopbackDisk(Disk): if self.device is None: return msger.debug("Losetup remove %s" % self.device) - rc = subprocess.call([self.losetupcmd, "-d", self.device]) + rc = msger.run([self.losetupcmd, "-d", self.device]) self.device = None @@ -438,9 +434,8 @@ class DiskMount(Mount): def unmount(self): if self.mounted: msger.debug("Unmounting directory %s" % self.mountdir) - synccmd = find_binary_path("sync") - subprocess.call([synccmd]) # sync the data on this mount point - rc = subprocess.call([self.umountcmd, "-l", self.mountdir]) + msger.run('sync', True) # sync the data on this mount point + rc = msger.run([self.umountcmd, "-l", self.mountdir]) if rc == 0: self.mounted = False else: @@ -476,7 +471,7 @@ class DiskMount(Mount): if self.fstype: args.extend(["-t", self.fstype]) - rc = subprocess.call(args) + rc = msger.run(args) if rc != 0: raise MountError("Failed to mount '%s' to '%s' with command '%s'. Retval: %s" % (self.disk.device, self.mountdir, " ".join(args), rc)) @@ -506,13 +501,12 @@ class ExtDiskMount(DiskMount): if self.skipformat: msger.debug("Skip filesystem format.") return - msger.debug("Formating %s filesystem on %s" % (self.fstype, self.disk.device)) - rc = subprocess.call([self.mkfscmd, - "-F", "-L", self.fslabel, - "-m", "1", "-b", str(self.blocksize), - self.disk.device], stdout=sys.stdout, - stderr=sys.stderr) - # str(self.disk.size / self.blocksize)]) + + msger.info("Formating %s filesystem on %s" % (self.fstype, self.disk.device)) + rc = msger.run([self.mkfscmd, + "-F", "-L", self.fslabel, + "-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)) @@ -526,9 +520,7 @@ class ExtDiskMount(DiskMount): self.uuid = self.__parse_field(out, "Filesystem UUID") msger.debug("Tuning filesystem on %s" % self.disk.device) - subprocess.call([self.tune2fs, "-c0", "-i0", "-Odir_index", - "-ouser_xattr,acl", self.disk.device], - stdout=sys.stdout, stderr=sys.stderr) + msger.run([self.tune2fs, "-c0", "-i0", "-Odir_index", "-ouser_xattr,acl", self.disk.device]) def __resize_filesystem(self, size = None): current_size = os.stat(self.disk.lofile)[stat.ST_SIZE] @@ -564,8 +556,8 @@ class ExtDiskMount(DiskMount): DiskMount.mount(self, options) def __fsck(self): - msger.debug("Checking filesystem %s" % self.disk.lofile) - subprocess.call(["/sbin/e2fsck", "-f", "-y", self.disk.lofile], stdout=sys.stdout, stderr=sys.stderr) + msger.info("Checking filesystem %s" % self.disk.lofile) + msger.run(["/sbin/e2fsck", "-f", "-y", self.disk.lofile]) def __get_size_from_filesystem(self): dev_null = os.open("/dev/null", os.O_WRONLY) @@ -619,8 +611,7 @@ class VfatDiskMount(DiskMount): msger.debug("Skip filesystem format.") return msger.debug("Formating %s filesystem on %s" % (self.fstype, self.disk.device)) - blah = [self.mkfscmd, "-n", self.fslabel, "-i", self.uuid, self.disk.device] - rc = subprocess.call(blah) + rc = msger.run([self.mkfscmd, "-n", self.fslabel, "-i", self.uuid, self.disk.device]) if rc != 0: raise MountError("Error creating %s filesystem on disk %s" % (self.fstype,self.disk.device)) msger.debug("Tuning filesystem on %s" % self.disk.device) @@ -660,7 +651,7 @@ class VfatDiskMount(DiskMount): def __fsck(self): msger.debug("Checking filesystem %s" % self.disk.lofile) - subprocess.call([self.fsckcmd, "-y", self.disk.lofile]) + msger.run([self.fsckcmd, "-y", self.disk.lofile]) def __get_size_from_filesystem(self): return self.disk.size @@ -709,7 +700,7 @@ class BtrfsDiskMount(DiskMount): # disable selinux, selinux will block write if os.path.exists("/usr/sbin/setenforce"): - subprocess.call(["/usr/sbin/setenforce", "0"]) + msger.run(["/usr/sbin/setenforce", "0"]) def __parse_field(self, output, field): for line in output.split(" "): @@ -723,7 +714,7 @@ class BtrfsDiskMount(DiskMount): msger.debug("Skip filesystem format.") return msger.debug("Formating %s filesystem on %s" % (self.fstype, self.disk.device)) - rc = subprocess.call([self.mkfscmd, "-L", self.fslabel, self.disk.device]) + rc = msger.run([self.mkfscmd, "-L", self.fslabel, self.disk.device]) if rc != 0: raise MountError("Error creating %s filesystem on disk %s" % (self.fstype,self.disk.device)) @@ -770,7 +761,7 @@ class BtrfsDiskMount(DiskMount): def __fsck(self): msger.debug("Checking filesystem %s" % self.disk.lofile) - subprocess.call([self.btrfsckcmd, self.disk.lofile]) + msger.run([self.btrfsckcmd, self.disk.lofile]) def __get_size_from_filesystem(self): return self.disk.size @@ -822,7 +813,7 @@ class DeviceMapperSnapshot(object): self.cowloop.device) args = [self.dmsetupcmd, "create", self.__name, "--table", table] - if subprocess.call(args) != 0: + if msger.run(args) != 0: self.cowloop.cleanup() self.imgloop.cleanup() raise SnapshotError("Could not create snapshot device using: " + @@ -835,7 +826,7 @@ class DeviceMapperSnapshot(object): return time.sleep(2) - rc = subprocess.call([self.dmsetupcmd, "remove", self.__name]) + rc = msger.run([self.dmsetupcmd, "remove", self.__name]) if not ignore_errors and rc != 0: raise SnapshotError("Could not remove snapshot device") @@ -916,30 +907,24 @@ def load_module(module): break if not found: msger.info("Loading %s..." % module) - dev_null = os.open("/dev/null", os.O_WRONLY) - modprobecmd = find_binary_path("modprobe") - modprobe = subprocess.Popen([modprobecmd, module], - stdout=dev_null, stderr=dev_null) - os.waitpid(modprobe.pid, 0) - os.close(dev_null) + msger.run(['modprobe', module], True) def myurlgrab(url, filename, proxies, progress_obj = None): g = URLGrabber() if progress_obj is None: progress_obj = TextProgress() + if url.startswith("file:///"): file = url.replace("file://", "") if not os.path.exists(file): raise CreatorError("URLGrabber error: can't find file %s" % file) - copycmd = find_binary_path("cp") - subprocess.call([copycmd, "-f", file, filename]) + msger.run(['cp', "-f", file, filename]) else: try: filename = g.urlgrab(url = url, filename = filename, ssl_verify_host = False, ssl_verify_peer = False, proxies = proxies, http_headers = (('Pragma', 'no-cache'),), progress_obj = progress_obj) except URLGrabError, e: - raise CreatorError("URLGrabber error: %s: %s" % (e, url)) - except: raise CreatorError("URLGrabber error: %s" % url) + return filename diff --git a/mic/utils/misc.py b/mic/utils/misc.py index 5e0af7d..3082fe7 100644 --- a/mic/utils/misc.py +++ b/mic/utils/misc.py @@ -417,9 +417,6 @@ def get_metadata_from_repo(baseurl, proxies, cachedir, reponame, filename): return get_uncompressed_data_from_url(url,filename_tmp,proxies) def get_metadata_from_repos(repostrs, cachedir): - if not cachedir: - CreatorError("No cache dir defined.") - my_repo_metadata = [] for repostr in repostrs: reponame = None diff --git a/mic/utils/rpmmisc.py b/mic/utils/rpmmisc.py index 6340269..e375f21 100644 --- a/mic/utils/rpmmisc.py +++ b/mic/utils/rpmmisc.py @@ -19,7 +19,6 @@ import os, sys, re import rpm -import subprocess from mic import msger class RPMInstallCallback: @@ -361,13 +360,7 @@ def getBaseArch(): def checkRpmIntegrity(bin_rpm, package): argv = [bin_rpm, "--checksig", "--nogpg", package] - dev_null = os.open("/dev/null", os.O_WRONLY) - try: - ret = subprocess.call(argv, stdout = dev_null, stderr = dev_null) - finally: - os.close(dev_null) - - return ret + return msger.run(argv, True) def checkSig(ts, package): """ Takes a transaction set and a package, check it's sigs, @@ -477,7 +470,6 @@ def checkRepositoryEULA(name, repo): return savepath def _pager_file(savepath): - import subprocess if os.path.splitext(savepath)[1].upper() in ('.HTM', '.HTML'): pagers = ('w3m', 'links', 'lynx', 'less', 'more') @@ -486,8 +478,9 @@ def checkRepositoryEULA(name, repo): file_showed = False for pager in pagers: + cmd = "%s %s" % (pager, savepath) try: - subprocess.call([pager, savepath]) + os.system(cmd) except OSError: continue else: diff --git a/plugins/backend/yumpkgmgr.py b/plugins/backend/yumpkgmgr.py index d39b418..f36c0b4 100644 --- a/plugins/backend/yumpkgmgr.py +++ b/plugins/backend/yumpkgmgr.py @@ -340,7 +340,7 @@ class Yum(BackendPlugin, yum.YumBase): total_count = len(dlpkgs) cached_count = 0 - msger.info("\nChecking packages cache and packages integrity...") + msger.info("\nChecking packages cache and packages integrity ...") for po in dlpkgs: local = po.localPkg() if not os.path.exists(local): diff --git a/plugins/backend/zypppkgmgr.py b/plugins/backend/zypppkgmgr.py index 72e9f18..b9e0f88 100644 --- a/plugins/backend/zypppkgmgr.py +++ b/plugins/backend/zypppkgmgr.py @@ -367,7 +367,7 @@ class Zypp(BackendPlugin): total_count = len(dlpkgs) cached_count = 0 localpkgs = self.localpkgs.keys() - msger.info("Checking packages cache and packages integrity...") + msger.info("Checking packages cache and packages integrity ...") for po in dlpkgs: """ Check if it is cached locally """ if po.name() in localpkgs: @@ -383,7 +383,7 @@ class Zypp(BackendPlugin): msger.info("%d packages to be installed, %d packages gotten from cache, %d packages to be downloaded" % (total_count, cached_count, download_count)) try: if download_count > 0: - msger.info("Downloading packages...") + msger.info("Downloading packages ...") self.downloadPkgs(dlpkgs, download_count) self.installPkgs(dlpkgs) diff --git a/plugins/imager/livecd_plugin.py b/plugins/imager/livecd_plugin.py index e9f192d..f48137d 100644 --- a/plugins/imager/livecd_plugin.py +++ b/plugins/imager/livecd_plugin.py @@ -18,7 +18,6 @@ # import os -import subprocess import shutil import tempfile @@ -120,6 +119,8 @@ class LiveCDPlugin(ImagerPlugin): @classmethod def do_pack(cls, base_on): + import subprocess + def __mkinitrd(instance): kernelver = instance._get_kernel_versions().values()[0][0] args = [ "/usr/libexec/mkliveinitrd", "/boot/initrd-%s.img" % kernelver, "%s" % kernelver ] diff --git a/plugins/imager/liveusb_plugin.py b/plugins/imager/liveusb_plugin.py index 2641218..c1b4b09 100644 --- a/plugins/imager/liveusb_plugin.py +++ b/plugins/imager/liveusb_plugin.py @@ -18,7 +18,6 @@ # import os -import subprocess import shutil import tempfile @@ -119,6 +118,8 @@ class LiveUSBPlugin(ImagerPlugin): @classmethod def do_pack(cls, base_on): + import subprocess + def __mkinitrd(instance): kernelver = instance._get_kernel_versions().values()[0][0] args = [ "/usr/libexec/mkliveinitrd", "/boot/initrd-%s.img" % kernelver, "%s" % kernelver ] diff --git a/plugins/imager/raw_plugin.py b/plugins/imager/raw_plugin.py index b380fde..6d5b79e 100644 --- a/plugins/imager/raw_plugin.py +++ b/plugins/imager/raw_plugin.py @@ -18,7 +18,6 @@ # import os -import subprocess import shutil import re import tempfile @@ -86,6 +85,8 @@ class RawPlugin(ImagerPlugin): @classmethod def do_chroot(cls, target): + import subprocess + img = target imgsize = misc.get_file_size(img) * 1024L * 1024L partedcmd = fs_related.find_binary_path("parted") @@ -192,7 +193,7 @@ class RawPlugin(ImagerPlugin): args = ['dd', "if=%s" % srcloop.partitions[0]['device'], "of=%s" % image] msger.info("`dd` image ...") - rc = subprocess.call(args) + rc = msger.run(args) srcloop.cleanup() shutil.rmtree(srcmnt, ignore_errors = True) -- 2.7.4