]> piware.de Git - bin.git/blob - syncpackage
postinst-setup: bump postgresql version
[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         if not l.startswith(' '):
42             break
43         fname = l.split()[2]
44         if not fname.endswith('.dsc'):
45             files.append(fname)
46
47     f.close()
48     return files
49
50 #
51 # entry point
52 #
53
54 if len(sys.argv) != 3:
55     print 'Usage: syncpackage <.dsc URL or path> <target release>'
56     sys.exit (1)
57
58 (dscurl, release) = sys.argv[1:]
59 dscname = os.path.basename(dscurl)
60 basepath = os.path.dirname(dscurl)
61 (srcpkg, new_ver) = dscname.split('_')
62 new_ver = new_ver[:-4] # strip off '.dsc'
63
64 cur_ver = cur_version(srcpkg, release)
65
66 retrieve_file(dscurl)
67 files = dsc_getfiles(dscname)
68
69 # do we need the orig.tar.gz?
70 need_orig = True
71 if cur_ver.find('-') > 0 and new_ver.find('-') > 0 and \
72     cur_ver.split('-')[0] == new_ver.split('-')[0]:
73     need_orig = False
74     #files = [f for f in files if not f.endswith('orig.tar.gz')]
75
76 print 'Source %s: current version %s, new version %s' % (srcpkg, cur_ver, new_ver)
77 print 'needs orig.tar.gz', need_orig
78 print 'Files:', files
79 for f in files:
80     retrieve_file(os.path.join(basepath, f))
81
82 uidx = cur_ver.find('ubuntu')
83 if uidx > 0:
84     cur_ver = cur_ver[:uidx]
85     print 'WARNING! Overwriting modified Ubuntu version, setting current version to', cur_ver
86
87 uidx = cur_ver.find('build')
88 if uidx > 0:
89     cur_ver = cur_ver[:uidx]
90
91 orig_arg = ''
92 if need_orig:
93     orig_arg = '-sa'
94
95 # extract package, build Source
96 assert subprocess.call(['dpkg-source', '-x', dscname]) == 0
97 os.chdir(srcpkg + '-' + new_ver.rsplit('-', 1)[0])
98 assert subprocess.call("dpkg-genchanges -q -S %s -v%s -e\"$(getent passwd $(id -u)|cut -f5 -d:|cut -f1 -d,) <$DEBEMAIL>\" | \
99         sed 's/^Distribution:.*$/Distribution: %s/; 1 i\Origin: debian/unstable' > ../%s_%s_source.changes" % 
100         (orig_arg, cur_ver, release, srcpkg, new_ver), shell=True) == 0
101 os.chdir('..')
102 shutil.rmtree(srcpkg + '-' + new_ver.split('-')[0], True)
103 assert subprocess.call("debsign %s_%s_source.changes" % (srcpkg, new_ver), shell=True) == 0