]> piware.de Git - bin.git/blob - syncpackage
initial checkin
[bin.git] / syncpackage
1 #!/usr/bin/python
2
3 import os, os.path, sys, urllib, subprocess, shutil
4
5 def retrieve_file(url):
6     '''Download file (by URL)  to the current directory.
7
8     If the file is already present, this function does nothing.'''
9
10     fname = os.path.basename(url)
11     if not os.path.exists(fname):
12         print 'downloading', url
13         urllib.urlretrieve(url, fname)
14
15 def cur_version(sourcepkg, release):
16     madison = subprocess.Popen(['apt-cache', 'madison', sourcepkg], stdout=subprocess.PIPE)
17     out = madison.communicate()[0]
18     assert (madison.returncode == 0)
19
20     for l in out.splitlines():
21         (pkg, version, aptsrc) = l.split('|')
22         if aptsrc.endswith('Sources') and aptsrc.find(release) > 0:
23             return version.strip()
24
25     raise Exception('apt-cache madison does not contain %s/%s' % (sourcepkg, release))
26
27 def dsc_getfiles(dsc):
28     '''Return list of files in a .dsc file (excluding the .dsc file itself).'''
29
30     f = open(dsc)
31     files = []
32
33     # skip until 'Files:'
34     for l in f:
35         if l.strip() == 'Files:':
36             break
37
38     for l in f:
39         if l.strip() == '':
40             break
41         fname = l.split()[2]
42         if not fname.endswith('.dsc'):
43             files.append(fname)
44
45     f.close()
46     return files
47
48 #
49 # entry point
50 #
51
52 if len(sys.argv) != 3:
53     print 'Usage: syncpackage <.dsc URL or path> <target release>'
54     sys.exit (1)
55
56 (dscurl, release) = sys.argv[1:]
57 dscname = os.path.basename(dscurl)
58 basepath = os.path.dirname(dscurl)
59 (srcpkg, new_ver) = dscname.split('_')
60 new_ver = new_ver[:-4] # strip off '.dsc'
61
62 cur_ver = cur_version(srcpkg, release)
63
64 retrieve_file(dscurl)
65 files = dsc_getfiles(dscname)
66
67 # do we need the orig.tar.gz?
68 need_orig = True
69 if cur_ver.find('-') > 0 and new_ver.find('-') > 0 and \
70     cur_ver.split('-')[0] == new_ver.split('-')[0]:
71     need_orig = False
72     #files = [f for f in files if not f.endswith('orig.tar.gz')]
73
74 print 'Source %s: current version %s, new version %s' % (srcpkg, cur_ver, new_ver)
75 print 'needs orig.tar.gz', need_orig
76 print 'Files:', files
77 for f in files:
78     retrieve_file(os.path.join(basepath, f))
79
80 uidx = cur_ver.find('ubuntu')
81 if uidx > 0:
82     cur_ver = cur_ver[:uidx]
83     print 'WARNING! Overwriting modified Ubuntu version, setting current version to', cur_ver
84
85 uidx = cur_ver.find('build')
86 if uidx > 0:
87     cur_ver = cur_ver[:uidx]
88
89 orig_arg = ''
90 if need_orig:
91     orig_arg = '-sa'
92
93 # extract package, build Source
94 assert subprocess.call(['dpkg-source', '-x', dscname]) == 0
95 os.chdir(srcpkg + '-' + new_ver.split('-')[0])
96 assert subprocess.call("dpkg-genchanges -q -S %s -v%s -e\"$(getent passwd $(id -u)|cut -f5 -d:|cut -f1 -d,) <$DEBEMAIL>\" | \
97         sed 's/^Distribution:.*$/Distribution: %s/; 1 i\Origin: debian/unstable' > ../%s_%s_source.changes" % 
98         (orig_arg, cur_ver, release, srcpkg, new_ver), shell=True) == 0
99 os.chdir('..')
100 shutil.rmtree(srcpkg + '-' + new_ver.split('-')[0], True)
101 assert subprocess.call("debsign %s_%s_source.changes" % (srcpkg, new_ver), shell=True) == 0