From 7f6b72e039efa5e2c3517b9336f9b29b4cb65c71 Mon Sep 17 00:00:00 2001 From: Markus Lehtonen Date: Tue, 15 May 2012 10:02:40 +0300 Subject: 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 --- gbp/pkg/__init__.py | 81 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 60 insertions(+), 21 deletions(-) (limited to 'gbp/pkg') diff --git a/gbp/pkg/__init__.py b/gbp/pkg/__init__.py index e68fc61..6663838 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. @@ -71,27 +131,6 @@ class PkgPolicy(object): raise NotImplementedError("Class needs to provide upstreamversion_re") 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" -- cgit v1.2.3