3 import os, os.path, sys, urllib, subprocess, shutil
5 def retrieve_file(url):
6 '''Download file (by URL) to the current directory.
8 If the file is already present, this function does nothing.'''
10 fname = os.path.basename(url)
11 if not os.path.exists(fname):
12 print 'downloading', url
13 urllib.urlretrieve(url, fname)
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)
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()
25 raise Exception('apt-cache madison does not contain %s/%s' % (sourcepkg, release))
27 def dsc_getfiles(dsc):
28 '''Return list of files in a .dsc file (excluding the .dsc file itself).'''
35 if l.strip() == 'Files:':
42 if not fname.endswith('.dsc'):
52 if len(sys.argv) != 3:
53 print 'Usage: syncpackage <.dsc URL or path> <target release>'
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'
62 cur_ver = cur_version(srcpkg, release)
65 files = dsc_getfiles(dscname)
67 # do we need the orig.tar.gz?
69 if cur_ver.find('-') > 0 and new_ver.find('-') > 0 and \
70 cur_ver.split('-')[0] == new_ver.split('-')[0]:
72 #files = [f for f in files if not f.endswith('orig.tar.gz')]
74 print 'Source %s: current version %s, new version %s' % (srcpkg, cur_ver, new_ver)
75 print 'needs orig.tar.gz', need_orig
78 retrieve_file(os.path.join(basepath, f))
80 uidx = cur_ver.find('ubuntu')
82 cur_ver = cur_ver[:uidx]
83 print 'WARNING! Overwriting modified Ubuntu version, setting current version to', cur_ver
85 uidx = cur_ver.find('build')
87 cur_ver = cur_ver[:uidx]
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
100 shutil.rmtree(srcpkg + '-' + new_ver.split('-')[0], True)
101 assert subprocess.call("debsign %s_%s_source.changes" % (srcpkg, new_ver), shell=True) == 0