aboutsummaryrefslogtreecommitdiffhomepage
path: root/gbp/pkg/archive.py
blob: cfb96b8653c145431e162611a3bed42e2acb2252 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# vim: set fileencoding=utf-8 :
#
# (C) 2017 Guido Günther <agx@sigxcpu.org>
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, please see
#    <http://www.gnu.org/licenses/>

from .compressor import Compressor


class Archive(object):
    # Supported archive formats
    Formats = ['tar', 'zip']

    # Map combined file extensions to archive and compression format
    Ext_aliases = {'tgz': ('tar', 'gzip'),
                   'tbz2': ('tar', 'bzip2'),
                   'tlz': ('tar', 'lzma'),
                   'txz': ('tar', 'xz')}

    @staticmethod
    def parse_filename(filename):
        """
        Given an filename return the basename (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}

        >>> Archive.parse_filename("abc.tar.gz")
        ('abc', 'tar', 'gzip')
        >>> Archive.parse_filename("abc.tar.bz2")
        ('abc', 'tar', 'bzip2')
        >>> Archive.parse_filename("abc.def.tbz2")
        ('abc.def', 'tar', 'bzip2')
        >>> Archive.parse_filename("abc.def.tar.xz")
        ('abc.def', 'tar', 'xz')
        >>> Archive.parse_filename("abc.zip")
        ('abc', 'zip', None)
        >>> Archive.parse_filename("abc.lzma")
        ('abc', None, 'lzma')
        >>> Archive.parse_filename("abc.tar.foo")
        ('abc.tar.foo', None, None)
        >>> Archive.parse_filename("abc")
        ('abc', None, None)
        """
        (base_name, archive_fmt, compression) = (filename, None, None)

        # Split filename into 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 Archive.Formats:
                base_name = ".".join(split[:-1])
                (archive_fmt, compression) = (split[-1], None)
            else:
                for (c, ext) in Compressor.Exts.items():
                    if ext == split[-1]:
                        base_name = ".".join(split[:-1])
                        compression = c
                        if len(split) > 2 and split[-2] in Archive.Formats:
                            base_name = ".".join(split[:-2])
                            archive_fmt = split[-2]

        return (base_name, archive_fmt, compression)