add --compress-to option to support zip format in loop image
authorGui Chen <gui.chen@intel.com>
Mon, 14 May 2012 07:55:38 +0000 (15:55 +0800)
committerGui Chen <gui.chen@intel.com>
Mon, 14 May 2012 07:55:38 +0000 (15:55 +0800)
keep '--taring-to' to compatible with before

Signed-off-by: Gui Chen <gui.chen@intel.com>
mic/imager/loop.py
mic/utils/misc.py
plugins/imager/loop_plugin.py

index c85ef50..35bb413 100644 (file)
@@ -98,7 +98,7 @@ class LoopImageCreator(BaseImageCreator):
     will be created as a separated loop image.
     """
 
-    def __init__(self, creatoropts=None, pkgmgr=None, taring_to=None):
+    def __init__(self, creatoropts=None, pkgmgr=None, compress_to=None):
         """Initialize a LoopImageCreator instance.
 
         This method takes the same arguments as ImageCreator.__init__()
@@ -109,14 +109,18 @@ class LoopImageCreator(BaseImageCreator):
 
         BaseImageCreator.__init__(self, creatoropts, pkgmgr)
 
-        if taring_to:
-            if '@NAME@' in taring_to:
-                taring_to = taring_to.replace('@NAME@', self.name)
+        if compress_to:
+            if '@NAME@' in compress_to:
+                compress_to = compress_to.replace('@NAME@', self.name)
 
-            if not taring_to.endswith('.tar'):
-                taring_to += ".tar"
+            compress_imgdir_method = os.path.splitext(compress_to)[1]
+            if compress_imgdir_method in (".zip", ".tar"):
+                self.compress_imgdir_method = compress_imgdir_method[1:]
+            else:
+                self.compress_imgdir_method = "tar"
+                compress_to += ".tar"
 
-        self.taring_to = taring_to
+        self.compress_to = compress_to
 
         self.__fslabel = None
         self.fslabel = self.name
@@ -171,8 +175,8 @@ class LoopImageCreator(BaseImageCreator):
         else:
             self.__image_size = 0
 
-        if taring_to:
-            self._img_name = self.taring_to
+        if compress_to:
+            self._img_name = self.compress_to
         else:
             self._img_name = self.name + ".img"
 
@@ -361,13 +365,12 @@ class LoopImageCreator(BaseImageCreator):
             item['loop'].cleanup()
 
     def _stage_final_image(self):
-        if self.taring_to:
-            import tarfile
+        if self.compress_to:
 
             self._resparse(0)
 
-            tarfile_name = self.taring_to
-            mountfp_xml = os.path.splitext(tarfile_name)[0] + ".xml"
+            cfile_name = self.compress_to
+            mountfp_xml = os.path.splitext(cfile_name)[0] + ".xml"
 
             for item in self._instloops:
                 imgfile = os.path.join(self.__imgdir, item['name'])
@@ -376,13 +379,15 @@ class LoopImageCreator(BaseImageCreator):
                                 '-O ^huge_file,extents,uninit_bg %s ' \
                                 % imgfile)
 
-            msger.info("Tar all loop images together to %s" % tarfile_name)
-            tar = tarfile.open(os.path.join(self._outdir, tarfile_name), 'w')
-            for item in os.listdir(self.__imgdir):
-                fpath = os.path.join(self.__imgdir, item)
-                tar.add(fpath, item)
-
-            tar.close()
+            msger.info("Compress all loop images together to %s" % cfile_name)
+            dstfile = os.path.join(self._outdir, cfile_name)
+            if self.compress_imgdir_method == "tar":
+                misc.taring(dstfile, self.__imgdir)
+            elif self.compress_imgdir_method == "zip":
+                misc.ziping(dstfile, self.__imgdir)
+            else:
+                raise CreatorError("Unsupported compress type: %s" \
+                                   % self.compress_imgdir_method)
 
             # save mount points mapping file to xml
             save_mountpoints(os.path.join(self._outdir, mountfp_xml),
index 2c76637..5e0e077 100644 (file)
@@ -53,6 +53,23 @@ RPM_RE  = re.compile("(.*)\.(.*) (.*)-(.*)")
 RPM_FMT = "%(name)s.%(arch)s %(ver_rel)s"
 SRPM_RE = re.compile("(.*)-(\d+.*)-(\d+\.\d+).src.rpm")
 
+def taring(dstfile, targetdir):
+    import tarfile
+    wf = tarfile.open(dstfile, 'w')
+    for item in os.listdir(targetdir):
+        wf.add(os.path.join(targetdir, item), item)
+    wf.close()
+
+def ziping(dstfile, targetdir):
+    import zipfile
+    wf = zipfile.ZipFile(dstfile, 'w', compression=zipfile.ZIP_DEFLATED)
+    for item in os.listdir(targetdir):
+        fpath = os.path.join(targetdir, item)
+        if not os.path.isfile(fpath):
+            continue
+        wf.write(fpath, item, zipfile.ZIP_DEFLATED)
+    wf.close()
+
 def human_size(size):
     """Return human readable string for Bytes size
     """
index e6c6ee1..77e039c 100644 (file)
@@ -30,9 +30,12 @@ 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="compress_to", type='string',
+                  default=None, help="same with '--compress-to'")
+    @cmdln.option("--compress-to", dest="compress_to", type='string',
+                  default=None, help="Specify compress filename for all image "
+                  "output, compress type decided by file extension, '.zip' for "
+                  "zip format, '.tar' for tar format, default is tar format")
     def do_create(self, subcmd, opts, *args):
         """${cmd_name}: create loop image
 
@@ -89,14 +92,14 @@ class LoopPlugin(ImagerPlugin):
         if creatoropts['runtime']:
             rt_util.runmic_in_runtime(creatoropts['runtime'], creatoropts, ksconf, None)
 
-        creator = LoopImageCreator(creatoropts, pkgmgr, opts.taring_to)
+        creator = LoopImageCreator(creatoropts, pkgmgr, opts.compress_to)
 
         if len(recording_pkgs) > 0:
             creator._recording_pkgs = recording_pkgs
 
         if creatoropts['release'] is None:
-            if opts.taring_to:
-                imagefile = "%s.tar" % os.path.join(creator.destdir, opts.taring_to)
+            if opts.compress_to:
+                imagefile = "%s" % os.path.join(creator.destdir, creator.compress_to)
             else:
                 imagefile = "%s.img" % os.path.join(creator.destdir, creator.name)