aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMarkus Lehtonen <markus.lehtonen@linux.intel.com>2012-05-15 10:02:40 +0300
committerGuido Günther <agx@sigxcpu.org>2014-07-24 19:41:29 +0200
commit7f6b72e039efa5e2c3517b9336f9b29b4cb65c71 (patch)
treed50d9136847892a9de5fbdf7614a731ad4e8a380
parent36c8e5cd00fd600be63160b0ef9427a70ab45870 (diff)
Move get_compression() out of pkg.PkgPolicy class
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>
-rw-r--r--gbp/pkg/__init__.py81
-rwxr-xr-xgbp/scripts/buildpackage.py5
2 files changed, 63 insertions, 23 deletions
diff --git a/gbp/pkg/__init__.py b/gbp/pkg/__init__.py
index e68fc61a..66638380 100644
--- a/gbp/pkg/__init__.py
+++ b/gbp/pkg/__init__.py
@@ -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:
diff --git a/gbp/scripts/buildpackage.py b/gbp/scripts/buildpackage.py
index 0ef6e9c7..8d79e8be 100755
--- a/gbp/scripts/buildpackage.py
+++ b/gbp/scripts/buildpackage.py
@@ -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'