From 7e7504abbdbb0f1509a8e19ed39f522d74d99d65 Mon Sep 17 00:00:00 2001 From: JF Ding Date: Fri, 2 Sep 2011 16:59:20 +0900 Subject: [PATCH] image type "fs" to check and confirm the exists dir --- mic/imager/fs.py | 20 +++++++------------- mic/msger.py | 9 +++++---- plugins/imager/fs_plugin.py | 18 +++++++++++++++--- 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/mic/imager/fs.py b/mic/imager/fs.py index 6475e92..d11f24a 100644 --- a/mic/imager/fs.py +++ b/mic/imager/fs.py @@ -29,22 +29,16 @@ class FsImageCreator(BaseImageCreator): self._fsopts = None def package(self, destdir = "."): - - destdir = os.path.abspath(os.path.expanduser(destdir)) - if not os.path.exists(destdir): - os.makedirs(destdir) + fsdir = os.path.join(destdir, self.name) if self._recording_pkgs: self._save_recording_pkgs(destdir) - msger.info("Copying %s to %s ..." % (self._instroot, destdir + "/" + self.name)) - - args = ['cp', "-af", self._instroot, destdir + "/" + self.name ] - msger.run(args) + msger.info("Copying %s to %s ..." % (self._instroot, fsdir)) + msger.run(['cp', "-af", self._instroot, fsdir]) - ignores = ["/dev/fd", "/dev/stdin", "/dev/stdout", "/dev/stderr", "/etc/mtab"] - for exclude in ignores: - if os.path.exists(destdir + "/" + self.name + exclude): - os.unlink(destdir + "/" + self.name + exclude) + for exclude in ["/dev/fd", "/dev/stdin", "/dev/stdout", "/dev/stderr", "/etc/mtab"]: + if os.path.exists(fsdir + exclude): + os.unlink(fsdir + exclude) - self.outimage.append(destdir + "/" + self.name) + self.outimage.append(fsdir) diff --git a/mic/msger.py b/mic/msger.py index c429e37..a6c500e 100644 --- a/mic/msger.py +++ b/mic/msger.py @@ -39,6 +39,7 @@ LOG_LEVELS = { 'normal': 1, 'verbose': 2, 'debug': 3, + 'never': 4, } LOG_LEVEL = 1 @@ -72,7 +73,7 @@ def _color_print(head, color, msg = None, stream = sys.stdout, level = 'normal') head = head.lstrip() newline = True - if msg: + if msg is not None: stream.write('%s%s' % (head, msg)) if newline: stream.write('\n') @@ -149,7 +150,7 @@ def error(msg): sys.exit(1) def ask(msg, default=True): - _color_print('Q', ASK_COLOR, '') + _color_print('\rQ', ASK_COLOR, '') try: if default: msg += '(Y/n) ' @@ -180,8 +181,8 @@ def ask(msg, default=True): def pause(msg=None): if INTERACTIVE: - _color_print('Q', ASK_COLOR, '') - if not msg: + _color_print('\rQ', ASK_COLOR, '') + if msg is None: msg = 'press to continue ...' raw_input(msg) diff --git a/plugins/imager/fs_plugin.py b/plugins/imager/fs_plugin.py index 70c9a98..25a3e62 100644 --- a/plugins/imager/fs_plugin.py +++ b/plugins/imager/fs_plugin.py @@ -17,6 +17,8 @@ # with the express permission of Red Hat, Inc. # +import os + from mic import configmgr, pluginmgr, chroot, msger from mic.utils import cmdln, errors from mic.imager import fs @@ -48,8 +50,7 @@ class FsPlugin(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 == createopts['pkgmgr']: pkgmgr = pcls break @@ -58,6 +59,17 @@ class FsPlugin(ImagerPlugin): raise CreatorError("Can't find backend plugin: %s" % createopts['pkgmgr']) creator = fs.FsImageCreator(createopts, pkgmgr) + + destdir = os.path.abspath(os.path.expanduser(createopts["outdir"])) + fsdir = os.path.join(destdir, creator.name) + + if not os.path.exists(destdir): + os.makedirs(destdir) + elif os.path.exists(fsdir): + if msger.ask('The target dir: %s already exists, need to delete it?' % fsdir): + import shutil + shutil.rmtree(fsdir) + try: creator.check_depend_tools() creator.mount(None, createopts["cachedir"]) @@ -72,7 +84,7 @@ class FsPlugin(ImagerPlugin): creator.configure(createopts["repomd"]) creator.unmount() - creator.package(createopts["outdir"]) + creator.package(destdir) outimage = creator.outimage creator.print_outimage_info() except errors.CreatorError, e: -- 2.7.4