File: genorig.py

package info (click to toggle)
lvm2 2.02.111-2.2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 8,856 kB
  • sloc: ansic: 101,481; sh: 15,369; makefile: 1,843; python: 777; ruby: 332; awk: 20
file content (116 lines) | stat: -rwxr-xr-x 3,484 bytes parent folder | download | duplicates (9)
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
#!/usr/bin/env python

import os, os.path, re, shutil, sys

class Changelog(list):
    _rules = r"""
^
(?P<source>
    \w[-+0-9a-z.]+
)
\ 
\(
(?P<version>
    [^\(\)\ \t]+
)
\)
\s+
(?P<distribution>
    [-+0-9a-zA-Z.]+
)
\;
"""
    _re = re.compile(_rules, re.X)

    class Entry(object):
        __slot__ = 'distribution', 'source', 'version'

        def __init__(self, distribution, source, version):
            self.distribution, self.source, self.version = distribution, source, version

    def __init__(self, dir):
        f = file(os.path.join(dir, "debian/changelog"))
        while True:
            line = f.readline()
            if not line:
                break
            match = self._re.match(line)
            if not match:
                continue
            self.append(self.Entry(match.group('distribution'), match.group('source'), match.group('version')))

class GenOrig(object):
    log = sys.stdout.write

    def __init__(self, root, orig, input_tar, version):
        self.orig, self.input_tar, self.version = orig, input_tar, version

        changelog = Changelog(root)
        self.source = changelog[0].source

    def __call__(self):
        import tempfile
        self.dir = tempfile.mkdtemp(prefix = 'genorig', dir = 'debian')
        try:
            self.orig_dir = "%s-%s" % (self.source, self.version)
            self.orig_tar = "%s_%s.orig.tar.gz" % (self.source, self.version)

            self.do_upstream()
            self.do_orig()
        finally:
            shutil.rmtree(self.dir)

    def do_upstream(self):
        self.log("Extracting tarball %s\n" % self.input_tar)
        match = re.match(r'(^|.*/)(?P<dir>[^/]+)\.(t|tar\.)(?P<extension>(gz|bz2))$', self.input_tar)
        if not match:
            raise RuntimeError("Can't identify name of tarball")
        cmdline = ['tar -xf', self.input_tar, '-C', self.dir]
        extension = match.group('extension')
        if extension == 'bz2':
            cmdline.append('-j')
        elif extension == 'gz':
            cmdline.append('-z')
        if os.spawnv(os.P_WAIT, '/bin/sh', ['sh', '-c', ' '.join(cmdline)]):
            raise RuntimeError("Can't extract tarball")
        os.rename(os.path.join(self.dir, match.group('dir')), os.path.join(self.dir, self.orig_dir))

    def do_orig(self):
        self.log("Generating tarball %s\n" % self.orig_tar)
        out = os.path.join(self.orig, self.orig_tar)

        try:
            os.mkdir(self.orig)
        except OSError: pass
        try:
            os.stat(out)
        except OSError: pass
        else:
            raise RuntimeError("Destination already exists (%s)" % out)

        cmdline = ['tar -czf', out, '-C', self.dir, self.orig_dir]
        try:
            if os.spawnv(os.P_WAIT, '/bin/sh', ['sh', '-c', ' '.join(cmdline)]):
                raise RuntimeError("Can't patch source")
            os.chmod(out, 0644)
        except:
            try:
                os.unlink(out)
            except OSError:
                pass
            raise

if __name__ == '__main__':
    from optparse import OptionParser
    p = OptionParser(usage = "%prog TAR VERSION")
    options, args = p.parse_args(sys.argv)

    if len(args) < 2:
        raise RuntimeError("Need more arguments")

    root = os.path.realpath(os.path.join(sys.path[0], '..', '..'))
    orig = os.path.realpath(os.path.join(root, '..', 'orig'))
    input_tar = args[1]
    version = args[2]

    GenOrig(root, orig, input_tar, version)()