File: get-orig-source

package info (click to toggle)
tuxonice-userui 1.1%2Bdfsg1.gc3bdd83-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 636 kB
  • ctags: 1,853
  • sloc: ansic: 5,788; makefile: 91; sh: 63; python: 46
file content (50 lines) | stat: -rwxr-xr-x 1,925 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/python
import copy, getopt, re, subprocess, sys, tarfile, urllib2
from os.path import dirname

default_exclude = re.compile(r'^/debian(/|$)|/\.git').search

def main():
    opts, (target,) = getopt.getopt(sys.argv[1:], 'f:x:')
    opts = dict(opts)
    pkg, ver, rev = re.match(r"(.+)_(.+g([0-9a-z]{7,}))\.orig\.tar\.xz",
                             target).groups()
    prefix = pkg + '-' + ver
    try: # first try from local working copy
        src = tarfile.open(fileobj=subprocess.Popen(
            ("git", "archive", "--format=tar", "--prefix=./", rev),
            stdout=subprocess.PIPE, cwd=dirname(dirname(__file__)),
            ).stdout, mode="r|")
    except (OSError,           # git not installed
            tarfile.ReadError, # not a working copy, revision not found, etc.
            ):                 # fallback to given url
        if '-f' not in opts:
            raise
        url = opts['-f'] % rev
        sys.stderr.write("Fall back to %s\n" % url)
        src = tarfile.open(fileobj=urllib2.urlopen(url), mode="r|*")
    try:
        exclude = re.compile(opts['-x']).search
    except KeyError:
        exclude = lambda _: False
    strip = len(src.firstmember.name)
    with open(target, 'wb') as f:
        xz = subprocess.Popen(("xz", "-c"), stdin=subprocess.PIPE, stdout=f)
        try:
            dst = tarfile.open(fileobj=xz.stdin, mode="w|")
            for item in src:
                name = item.name[strip:]
                if default_exclude(name) or exclude(name):
                    continue
                new_item = copy.copy(item)
                new_item.name = prefix + name
                dst.addfile(new_item, src.extractfile(item)
                                      if item.isreg() else None)
            del src
            dst.close()
        finally:
            xz.stdin.close()
            xz.wait()

if __name__ == '__main__':
    sys.exit(main())