~mozillateam/thunderbird/thunderbird-beta.yakkety

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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/python

import os
import sys
import os.path
import glob
import shutil
import tarfile
import tempfile
import getopt

class FileExtractException(Exception):
    def __init__(self, info):
        self.info = info

    def __str__(self):
        return self.info

def extract_file_from_archive(filename, dest, tar=None):
    try:
        if not os.path.isdir(dest):
            os.makedirs(dest)

        temp = tempfile.mkdtemp()

        if tar == None:
            tf = tarfile.open(glob.glob(os.path.join(os.getcwd(), '*.bz2'))[0], 'r')
        else:
            tf = tarfile.open(tar, 'r')
        tf.extract(filename, temp)
        shutil.copyfile(os.path.join(temp, filename), os.path.join(dest, os.path.basename(filename)))

    except IndexError:
        raise FileExtractException("No valid tar file found")

    except KeyError:
        errstr = "File " + filename + " not found in archive"
        raise FileExtractException(errstr)

    except IOError as e:
        filename = ": '" + e.filename + "'" if e.filename != None else ""
        errstr = "IOError occurred whilst extracting file from archive: [Errno: " + str(e.errno) + "] " + e.strerror + filename
        raise FileExtractException(errstr)

    except:
        raise FileExtractException("Unexpected error")

    finally:
        if temp:
            shutil.rmtree(temp)

if __name__ == '__main__':
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'd:o:')
    except getopt.GetoptError, err:
        print str(err)
        exit(1)

    cwd = os.getcwd()
    dest = cwd
    tarball = None

    for o, a in opts:
        if o == "-d":
            dest = os.path.abspath(a)
        elif o == "-o":
            tarball = os.path.abspath(a)

    if len(args) != 1:
        print >> sys.stderr, "Need to specify one file"
        exit(1)

    filename = args[0]

    if os.path.exists(os.path.join(dest, filename)):
        exit(0)

    if filename.startswith('/'):
        print >> sys.stderr, "Input filename shouldn't be absolute"
        exit(1)

    if not os.path.isdir(dest):
        os.makedirs(dest)

    if tarball == None:
        try:
            extract_file_from_archive(filename, dest)

        except FileExtractException as e:
            print >> sys.stderr, str(e)
            exit(1)

        except:
            print >> sys.stderr, "Unexpected error"
            exit(1)
    else:
        try:
            temp = tempfile.mkdtemp()

            tb = tarfile.open(tarball, 'r')
            names = tb.getnames()

            if os.path.join(names[0], filename) in names:
                tb.extract(os.path.join(names[0], filename), temp)
                shutil.copyfile(os.path.join(temp, names[0], filename), os.path.join(dest, filename))
            else:
                bz2file = None
                for name in names:
                    (root, ext) = os.path.splitext(name)
                    if ext == '.bz2':
                        bz2file = name
                        break
                if bz2file:
                    tb.extract(bz2file, temp)
                else:
                    print >> sys.stderr, "File not found and no valid embedded tar file found in source tarball"
                    exit(1)

                extract_file_from_archive(filename, dest, os.path.join(temp, bz2file))

        except FileExtractException as e:
            print >> sys.stderr, str(e)
            exit(1)

        finally:
            if temp:
                shutil.rmtree(temp)