centralized interface to check existing images
authorGui Chen <gui.chen@intel.com>
Wed, 6 Jun 2012 03:41:11 +0000 (11:41 +0800)
committerJF Ding <jian-feng.ding@intel.com>
Wed, 6 Jun 2012 06:48:30 +0000 (14:48 +0800)
new api: ImagerPlugin::check_image_exists

this change will fix a bug for --pack-to handling

Signed-off-by: Gui Chen <gui.chen@intel.com>
mic/imager/baseimager.py
mic/pluginbase.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

index daa39d0..f23ec56 100644 (file)
@@ -100,14 +100,7 @@ class BaseImageCreator(object):
             if 'release' in createopts and createopts['release']:
                 self.name += '-' + createopts['release']
 
-                if os.path.exists(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")
-
-                    # pending FEA: save log by default for --release
+                # pending FEA: save log by default for --release
 
             if self.pack_to:
                 if '@NAME@' in self.pack_to:
index 99cd24a..9ec1ba0 100644 (file)
 # with this program; if not, write to the Free Software Foundation, Inc., 59
 # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
+import os
+import shutil
 from mic import msger
+from mic.utils import errors
 
 class _Plugin(object):
     class __metaclass__(type):
@@ -40,6 +43,34 @@ class _Plugin(object):
 class ImagerPlugin(_Plugin):
     mic_plugin_type = "imager"
 
+    @classmethod
+    def check_image_exists(self, destdir, apacking=None, images=[], release=None):
+        # if it's a packing file, reset images
+        if apacking:
+            images = [apacking]
+
+        # release option will override images
+        if release is not None:
+            images = [os.path.basename(destdir.rstrip('/'))]
+            destdir = os.path.dirname(destdir.rstrip('/'))
+
+        for name in images:
+            if not name:
+                continue
+
+            image = os.path.join(destdir, name)
+            if not os.path.exists(image):
+                continue
+
+            if msger.ask("Target image/dir: %s already exists, "
+                         "clean up and continue?" % image):
+                if os.path.isdir(image):
+                    shutil.rmtree(image)
+                else:
+                    os.unlink(image)
+            else:
+                raise errors.Abort("Cancled")
+
     def do_create(self):
         pass
 
index 59c610e..aceb653 100644 (file)
@@ -90,14 +90,10 @@ class FsPlugin(ImagerPlugin):
         if len(recording_pkgs) > 0:
             creator._recording_pkgs = recording_pkgs
 
-        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, cleanup and continue?' % fsdir):
-                    import shutil
-                    shutil.rmtree(fsdir)
-                else:
-                    raise errors.Abort('Canceled')
+        self.check_image_exists(creator.destdir,
+                                creator.pack_to,
+                                [creator.name],
+                                creatoropts['release'])
 
         try:
             creator.check_depend_tools()
index 7828e5c..5b9349f 100644 (file)
@@ -89,13 +89,10 @@ class LiveCDPlugin(ImagerPlugin):
         if len(recording_pkgs) > 0:
             creator._recording_pkgs = recording_pkgs
 
-        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, cleanup and continue?' % imagefile):
-                    os.unlink(imagefile)
-                else:
-                    raise errors.Abort('Canceled')
+        self.check_image_exists(creator.destdir,
+                                creator.pack_to,
+                                [creator.name + ".iso"],
+                                creatoropts['release'])
 
         try:
             creator.check_depend_tools()
index 45fd308..c5735aa 100644 (file)
@@ -91,14 +91,10 @@ class LiveUSBPlugin(ImagerPlugin):
         if len(recording_pkgs) > 0:
             creator._recording_pkgs = recording_pkgs
 
-        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, cleanup and continue?' % imagefile):
-                    os.unlink(imagefile)
-                else:
-                    raise errors.Abort('Canceled')
-
+        self.check_image_exists(creator.destdir,
+                                creator.pack_to,
+                                [creator.name + ".usbimg"],
+                                creatoropts['release'])
         try:
             creator.check_depend_tools()
             creator.mount(None, creatoropts["cachedir"])
index de9c431..52f2c42 100644 (file)
@@ -97,18 +97,10 @@ class LoopPlugin(ImagerPlugin):
         if len(recording_pkgs) > 0:
             creator._recording_pkgs = recording_pkgs
 
-        if creatoropts['release'] is None:
-            if creatoropts['pack_to']:
-                imagefile = "%s" % os.path.join(creator.destdir, creator.pack_to)
-            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, cleanup '
-                             'and continue?' % imagefile):
-                    os.unlink(imagefile)
-                else:
-                    raise errors.Abort('Canceled')
+        self.check_image_exists(creator.destdir,
+                                creator.pack_to,
+                                [creator.name + ".img"],
+                                creatoropts['release'])
 
         try:
             creator.check_depend_tools()
index 6fbb35f..f156606 100644 (file)
@@ -94,14 +94,12 @@ class RawPlugin(ImagerPlugin):
         if len(recording_pkgs) > 0:
             creator._recording_pkgs = recording_pkgs
 
-        if creatoropts['release'] is None:
-            for item in creator.get_diskinfo():
-                imagefile = "%s-%s.raw" % (os.path.join(creator.destdir, creator.name), item['name'])
-                if os.path.exists(imagefile):
-                    if msger.ask('The target image: %s already exists, cleanup and continue?' % imagefile):
-                       os.unlink(imagefile)
-                    else:
-                       raise errors.Abort('Canceled')
+        images = ["%s-%s.raw" % (creator.name, part['name'])
+                  for part in creator.get_diskinfo()]
+        self.check_image_exists(creator.destdir,
+                                creator.pack_to,
+                                images,
+                                creatoropts['release'])
 
         try:
             creator.check_depend_tools()