more generic git.archive implementation
authorJF Ding <jian-feng.ding@intel.com>
Thu, 9 Feb 2012 11:45:09 +0000 (19:45 +0800)
committerJF Ding <jian-feng.ding@intel.com>
Thu, 9 Feb 2012 11:45:09 +0000 (19:45 +0800)
gitbuildsys/cmd_build.py
gitbuildsys/git.py

index 64b22f50bf7d87a560fa01189790e6356a45d0ea..1246cb0b60c0128f8334677daf1a2a2eedc2d540 100644 (file)
@@ -110,7 +110,7 @@ def do(opts, args):
 
     tarball = '%s/%s-%s-tizen.tar.bz2' % (workdir, name, version)
     msger.info('archive git tree to tar ball: %s' % tarball)
-    mygit.archive_tar("%s-%s/" % (name, version), tarball)
+    mygit.archive("%s-%s/" % (name, version), tarball)
 
     for f in glob.glob('packaging/*'):
         shutil.copy(f, workdir)
index 9f61a680852983662db44d2db7d950ca52529668..642dd7284590389902d88d54d814ee4f45c92111 100644 (file)
@@ -22,7 +22,7 @@ import os
 import runner
 import errors
 import msger
-from utils import Workdir
+from utils import Workdir, strip_end
 
 class Git:
     def __init__(self, path):
@@ -120,34 +120,58 @@ class Git:
         else:
             return (br in self.get_branches()[1])
 
-    def archive_tar(self, prefix, tarname, treeish = None):
-        """Archive git tree to tar ball
-          @prefix: tarball topdir 
-          @output: output tarball name
+    def archive(self, prefix, tarfname, treeish='HEAD'):
+        """Archive git tree from 'treeish', detect archive type
+        from the extname of output filename.
+
+          @prefix: tarball topdir
+          @tarfname: output tarball name
           @treeish: commit ID archive from
         """
-        filetypes = ['.tar.gz', '.tar.bz2', '.tgz']
-        tarfile = None
-        compress = None
-        for type in filetypes:
-           if tarname.endswith(type):
-               tarfile = "%s.tar" % tarname.replace(type, '')
-               compress = type
-               break
-        else:
-            raise errors.GitError("Can't support tarball type, "\
-                                  "supported types: %s" % ', '.join(filetypes))
 
-        if treeish is None:
-            treeish = 'HEAD'
+        filetypes = {
+                '.tar.gz': ('tar', 'gz'),
+                '.tgz': ('tar', 'gz'),
+                '.tar.bz2': ('tar', 'bz2'),
+                '.tbz2': ('tar', 'bz2'),
+                '.zip': ('zip', ''),
+        }
+
+        zipcmds = {
+                'gz': 'gzip',
+                'bz2': 'bzip2 -f',
+        }
+
+        for extname in filetypes:
+           if tarfname.endswith(extname):
+               fmt, compress = filetypes[extname]
+
+               barename = strip_end(tarfname, extname)
+               tarname = '%s.%s' % (barename, fmt)
+
+               if compress:
+                   zipcmd = zipcmds[compress]
+                   finalname = '%s.%s' % (tarname, compress)
+               else:
+                   zipcmd = None
+                   finalname = tarname
 
-        options = [ treeish, '--output=%s' % tarfile, \
-                    '--prefix=%s' % prefix ]
+               break
+
+        else:
+            raise errors.GitError("Cannot detect archive type from filename, "\
+                                  "supported ext-names: %s" \
+                                  % ', '.join(filetypes.keys()))
+
+        options = [ treeish,
+                    '--format=%s' % fmt,
+                    '--output=%s' % tarname,
+                    '--prefix=%s' % prefix
+                  ]
         self._exec_git('archive', options)
 
-        if compress == '.tar.bz2':
-            runner.quiet('bzip2 -f %s' % tarfile)
+        if zipcmd:
+            runner.quiet('%s %s' % (zipcmd, tarname))
 
-        if compress == 'gz':
-            # TODO: implement later.
-            pass
+        if finalname != tarfname:
+            os.rename(finalname, tarfname)