4 sys.path.append("debian/lib/python")
12 from debian_linux.debian import Changelog, VersionLinux
13 from debian_linux.patches import PatchSeries
16 def __init__(self, input_files, override_version):
17 self.log = sys.stdout.write
19 self.input_files = input_files
21 changelog = Changelog(version = VersionLinux)[0]
22 source = changelog.source
23 version = changelog.version
26 version = VersionLinux('%s-undef' % override_version)
28 self.version_dfsg = version.linux_dfsg
29 if self.version_dfsg is None:
30 self.version_dfsg = '0'
32 self.log('Using source name %s, version %s, dfsg %s\n' % (source, version.upstream, self.version_dfsg))
34 self.orig = '%s-%s' % (source, version.upstream)
35 self.orig_tar = '%s_%s.orig.tar.gz' % (source, version.upstream)
36 self.tag = 'v' + version.upstream.replace('~', '-')
40 self.dir = tempfile.mkdtemp(prefix = 'genorig', dir = 'debian')
42 if os.path.isdir(self.input_files[0]):
43 self.upstream_export(self.input_files[0])
45 self.upstream_extract(self.input_files[0])
46 if len(self.input_files) > 1:
47 self.upstream_patch(self.input_files[1])
51 shutil.rmtree(self.dir)
53 def upstream_export(self, input_repo):
54 self.log("Exporting %s from %s\n" % (self.tag, input_repo))
56 archive_proc = subprocess.Popen(['git', 'archive', '--format=tar',
57 '--prefix=%s/' % self.orig, self.tag],
59 stdout=subprocess.PIPE)
60 extract_proc = subprocess.Popen(['tar', '-xf', '-'], cwd=self.dir,
61 stdin=archive_proc.stdout)
63 if extract_proc.wait():
64 raise RuntimeError("Can't extract tarball")
66 def upstream_extract(self, input_tar):
67 self.log("Extracting tarball %s\n" % input_tar)
68 match = re.match(r'(^|.*/)(?P<dir>linux-\d+\.\d+\.\d+(-\S+)?)\.tar(\.(?P<extension>(bz2|gz)))?$', input_tar)
70 raise RuntimeError("Can't identify name of tarball")
72 cmdline = ['tar', '-xf', input_tar, '-C', self.dir]
73 if match.group('extension') == 'bz2':
75 elif match.group('extension') == 'gz':
78 if subprocess.Popen(cmdline).wait():
79 raise RuntimeError("Can't extract tarball")
81 os.rename(os.path.join(self.dir, match.group('dir')), os.path.join(self.dir, self.orig))
83 def upstream_patch(self, input_patch):
84 self.log("Patching source with %s\n" % input_patch)
85 match = re.match(r'(^|.*/)patch-\d+\.\d+\.\d+(-\S+?)?(\.(?P<extension>(bz2|gz)))?$', input_patch)
87 raise RuntimeError("Can't identify name of patch")
89 if match.group('extension') == 'bz2':
90 cmdline.append('bzcat')
91 elif match.group('extension') == 'gz':
92 cmdline.append('zcat')
95 cmdline.append(input_patch)
96 cmdline.append('| (cd %s; patch -p1 -f -s -t --no-backup-if-mismatch)' % os.path.join(self.dir, self.orig))
97 if os.spawnv(os.P_WAIT, '/bin/sh', ['sh', '-c', ' '.join(cmdline)]):
98 raise RuntimeError("Can't patch source")
100 def debian_patch(self):
101 name = "orig-" + self.version_dfsg
102 self.log("Patching source with debian patch (series %s)\n" % name)
103 fp = file("debian/patches/series/" + name)
104 series = PatchSeries(name, "debian/patches", fp)
105 series(dir = os.path.join(self.dir, self.orig))
108 out = os.path.join("../orig", self.orig_tar)
114 raise RuntimeError("Destination already exists")
116 self.log("Generate tarball %s\n" % out)
117 cmdline = ['tar -czf', out, '-C', self.dir, self.orig]
119 if os.spawnv(os.P_WAIT, '/bin/sh', ['sh', '-c', ' '.join(cmdline)]):
120 raise RuntimeError("Can't patch source")
129 if __name__ == '__main__':
130 from optparse import OptionParser
131 parser = OptionParser(usage = "%prog [OPTION]... {TAR [PATCH] | REPO}")
132 parser.add_option("-V", "--override-version", dest = "override_version", help = "Override version", metavar = "VERSION")
133 options, args = parser.parse_args()
135 assert 1 <= len(args) <= 2
136 Main(args, options.override_version)()