Move get_compression() out of pkg.PkgPolicy class
authorMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Tue, 15 May 2012 07:02:40 +0000 (10:02 +0300)
committerGuido Günther <agx@sigxcpu.org>
Thu, 24 Jul 2014 17:41:29 +0000 (19:41 +0200)
Renames the function to parse_archive_filename() and changes it's
return values. Filename parsing is merely generic functionality, not
tied to any packaging policy.

The function now returns the base name of the file (that is, filename
without, archive and compression extensions), archive format and
compression method. Adds supported archive formats 'tar' and 'zip' and
file extension aliases, e.g. 'tgz'.

Signed-off-by: Markus Lehtonen <markus.lehtonen@linux.intel.com>
gbp/pkg/__init__.py
gbp/scripts/buildpackage.py

index e68fc61..6663838 100644 (file)
@@ -34,6 +34,66 @@ compressor_opts = { 'gzip'  : [ '-n', 'gz' ],
 compressor_aliases = { 'bz2' : 'bzip2',
                        'gz'  : 'gzip', }
 
+# Supported archive formats
+arhive_formats = [ 'tar', 'zip' ]
+
+# Map combined file extensions to arhive and compression format
+archive_ext_aliases = { 'tgz'   : ('tar', 'gzip'),
+                        'tbz2'  : ('tar', 'bzip2'),
+                        'tlz'   : ('tar', 'lzma'),
+                        'txz'   : ('tar', 'xz')}
+
+def parse_archive_filename(filename):
+    """
+    Given an filename return the basename (i.e. filename without the
+    archive and compression extensions), archive format and compression
+    method used.
+
+    @param filename: the name of the file
+    @type filename: string
+    @return: tuple containing basename, archive format and compression method
+    @rtype: C{tuple} of C{str}
+
+    >>> parse_archive_filename("abc.tar.gz")
+    ('abc', 'tar', 'gzip')
+    >>> parse_archive_filename("abc.tar.bz2")
+    ('abc', 'tar', 'bzip2')
+    >>> parse_archive_filename("abc.def.tbz2")
+    ('abc.def', 'tar', 'bzip2')
+    >>> parse_archive_filename("abc.def.tar.xz")
+    ('abc.def', 'tar', 'xz')
+    >>> parse_archive_filename("abc.zip")
+    ('abc', 'zip', None)
+    >>> parse_archive_filename("abc.lzma")
+    ('abc', None, 'lzma')
+    >>> parse_archive_filename("abc.tar.foo")
+    ('abc.tar.foo', None, None)
+    >>> parse_archive_filename("abc")
+    ('abc', None, None)
+    """
+    (base_name, archive_fmt, compression) = (filename, None, None)
+
+    # Split filename to pieces
+    split = filename.split(".")
+    if len(split) > 1:
+        if split[-1] in archive_ext_aliases:
+            base_name = ".".join(split[:-1])
+            (archive_fmt, compression) = archive_ext_aliases[split[-1]]
+        elif split[-1] in arhive_formats:
+            base_name = ".".join(split[:-1])
+            (archive_fmt, compression) = (split[-1], None)
+        else:
+            for (c, o) in compressor_opts.iteritems():
+                if o[1] == split[-1]:
+                    base_name = ".".join(split[:-1])
+                    compression = c
+                    if len(split) > 2 and split[-2] in arhive_formats:
+                        base_name = ".".join(split[:-2])
+                        archive_fmt = split[-2]
+
+    return (base_name, archive_fmt, compression)
+
+
 class PkgPolicy(object):
     """
     Common helpers for packaging policy.
@@ -72,27 +132,6 @@ class PkgPolicy(object):
         return True if cls.upstreamversion_re.match(version) else False
 
     @staticmethod
-    def get_compression(orig_file):
-        """
-        Given an orig file return the compression used
-
-        >>> PkgPolicy.get_compression("abc.tar.gz")
-        'gzip'
-        >>> PkgPolicy.get_compression("abc.tar.bz2")
-        'bzip2'
-        >>> PkgPolicy.get_compression("abc.tar.foo")
-        >>> PkgPolicy.get_compression("abc")
-        """
-        try:
-            ext = orig_file.rsplit('.',1)[1]
-        except IndexError:
-            return None
-        for (c, o) in compressor_opts.iteritems():
-            if o[1] == ext:
-                return c
-        return None
-
-    @staticmethod
     def has_orig(orig_file, dir):
         "Check if orig tarball exists in dir"
         try:
index 0ef6e9c..8d79e8b 100755 (executable)
@@ -37,7 +37,8 @@ from gbp.scripts.common.buildpackage import (index_name, wc_name,
                                              git_archive_submodules,
                                              git_archive_single, dump_tree,
                                              write_wc, drop_index)
-from gbp.pkg import (UpstreamSource, compressor_opts, compressor_aliases)
+from gbp.pkg import (UpstreamSource, compressor_opts, compressor_aliases,
+                     parse_archive_filename)
 
 def git_archive(repo, cp, output_dir, treeish, comp_type, comp_level, with_submodules):
     "create a compressed orig tarball in output_dir using git_archive"
@@ -318,7 +319,7 @@ def guess_comp_type(repo, comp_type, cp, tarball_dir):
             else:
                 commit = repo.pristine_tar_branch
             tarball = repo.get_commit_info(commit)['subject']
-            comp_type = du.DebianPkgPolicy.get_compression(tarball)
+            (base_name, archive_fmt, comp_type) = parse_archive_filename(tarball)
             gbp.log.debug("Determined compression type '%s'" % comp_type)
             if not comp_type:
                 comp_type = 'gzip'