better handling for existing files checks
authorJF Ding <jian-feng.ding@intel.com>
Thu, 27 Oct 2011 09:02:41 +0000 (17:02 +0800)
committerJF Ding <jian-feng.ding@intel.com>
Thu, 27 Oct 2011 09:02:41 +0000 (17:02 +0800)
mic/imager/baseimager.py
mic/utils/errors.py
plugins/imager/fs_plugin.py
plugins/imager/livecd_plugin.py
plugins/imager/liveusb_plugin.py
plugins/imager/loop_plugin.py
plugins/imager/raw_plugin.py
tools/mic

index 5a7fb2b..31af6ef 100644 (file)
@@ -29,7 +29,7 @@ import rpm
 
 from mic import kickstart
 from mic import msger
-from mic.utils.errors import CreatorError
+from mic.utils.errors import CreatorError, Abort
 from mic.utils import misc, rpmmisc, runner, fs_related as fs
 
 class BaseImageCreator(object):
@@ -59,6 +59,9 @@ class BaseImageCreator(object):
         """
         self.pkgmgr = pkgmgr
 
+        self.__builddir = None
+        self.__bindmounts = []
+
         if createopts:
             # A pykickstart.KickstartParser instance."""
             self.ks = createopts['ks']
@@ -72,8 +75,10 @@ class BaseImageCreator(object):
 
                 # check whether destine dir exist
                 if os.path.exists(self.destdir):
-                    if msger.ask('Image dir: %s already exists, need to delete it?' % self.destdir):
+                    if msger.ask('Image dir: %s already exists, cleanup and continue?' % self.destdir):
                         shutil.rmtree(self.destdir, ignore_errors = True)
+                    else:
+                        raise Abort('Canceled')
 
                 # save log by default for --release
                 if not createopts['logfile']:
@@ -95,9 +100,6 @@ class BaseImageCreator(object):
             self.target_arch = "noarch"
             self._local_pkgs_path = None
 
-        self.__builddir = None
-        self.__bindmounts = []
-
         self._dep_checks = ["ls", "bash", "cp", "echo", "modprobe", "passwd"]
 
         #FIXME to be obsolete, make it configurable
index 1ede190..4cf06b9 100644 (file)
@@ -32,6 +32,9 @@ class Usage(CreatorError):
     def __str__(self):
         return self.keyword + str(self.msg) + ', please use "--help" for more info'
 
+class Abort(CreatorError):
+    keyword = ''
+
 class ConfigError(CreatorError):
     keyword = '<config>'
 
index 967cb54..344ef21 100644 (file)
@@ -78,9 +78,11 @@ class FsPlugin(ImagerPlugin):
         if creatoropts['release'] is None:
             fsdir = os.path.join(creator.destdir, creator.name)
             if os.path.exists(fsdir):
-                if msger.ask('The target dir: %s already exists, need to delete it?' % fsdir):
+                if msger.ask('The target dir: %s already exists, cleanup and continue?' % fsdir):
                     import shutil
                     shutil.rmtree(fsdir)
+                else:
+                    raise errors.Abort('Canceled')
 
         try:
             creator.check_depend_tools()
index a253023..fa1d058 100644 (file)
@@ -82,8 +82,10 @@ class LiveCDPlugin(ImagerPlugin):
         if creatoropts['release'] is None:
             imagefile = "%s.iso" % os.path.join(creator.destdir, creator.name)
             if os.path.exists(imagefile):
-                if msger.ask('The target image: %s already exists, need to delete it?' % imagefile):
+                if msger.ask('The target image: %s already exists, cleanup and continue?' % imagefile):
                     os.unlink(imagefile)
+                else:
+                    raise errors.Abort('Canceled')
 
         try:
             creator.check_depend_tools()
index 71833fc..67d204b 100644 (file)
@@ -84,8 +84,10 @@ class LiveUSBPlugin(ImagerPlugin):
         if creatoropts['release'] is None:
             imagefile = "%s.usbimg" % os.path.join(creator.destdir, creator.name)
             if os.path.exists(imagefile):
-                if msger.ask('The target image: %s already exists, need to delete it?' % imagefile):
+                if msger.ask('The target image: %s already exists, cleanup and continue?' % imagefile):
                     os.unlink(imagefile)
+                else:
+                    raise errors.Abort('Canceled')
 
         try:
             creator.check_depend_tools()
index 96660b8..96b585d 100644 (file)
@@ -82,8 +82,10 @@ class LoopPlugin(ImagerPlugin):
             else:
                 imagefile = "%s.img" % os.path.join(creator.destdir, creator.name)
             if os.path.exists(imagefile):
-                if msger.ask('The target image: %s already exists, need to delete it?' % imagefile):
+                if msger.ask('The target image: %s already exists, cleanup and continue?' % imagefile):
                     os.unlink(imagefile)
+                else:
+                    raise errors.Abort('Canceled')
 
         try:
             creator.check_depend_tools()
index 6276241..9e2040e 100644 (file)
@@ -81,8 +81,10 @@ class RawPlugin(ImagerPlugin):
         if creatoropts['release'] is None:
             imagefile = "%s-sda.raw" % os.path.join(creator.destdir, creator.name)
             if os.path.exists(imagefile):
-                if msger.ask('The target image: %s already exists, need to delete it?' % imagefile):
+                if msger.ask('The target image: %s already exists, cleanup and continue?' % imagefile):
                     os.unlink(imagefile)
+                else:
+                    raise errors.Abort('Canceled')
 
         try:
             creator.check_depend_tools()
index 7ec9aa7..85e6afe 100755 (executable)
--- a/tools/mic
+++ b/tools/mic
@@ -170,6 +170,9 @@ if __name__ == "__main__":
     except errors.Usage, usage:
         msger.error(str(usage))
 
+    except errors.Abort, msg:
+        msger.info(str(msg))
+
     except errors.CreatorError, err:
         if msger.get_loglevel() == 'debug':
             import traceback