ubuntu-dev-tools/syncpackage

473 lines
18 KiB
Plaintext
Raw Normal View History

2010-04-13 23:18:24 +02:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
2010-12-27 18:24:21 +02:00
# Copyright (C) 2008-2010 Martin Pitt <martin.pitt@canonical.com>,
# 2010 Benjamin Drung <bdrung@ubuntu.com>,
# 2010 Stefano Rivera <stefanor@ubuntu.com>
#
# ##################################################################
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 3.
2010-12-03 00:06:43 +01:00
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# See file /usr/share/common-licenses/GPL-3 for more details.
#
# ##################################################################
2010-04-13 23:18:24 +02:00
import debian.deb822
import debian.debian_support
import hashlib
import optparse
import os
import re
import shutil
import subprocess
import sys
import urllib
2010-04-13 23:18:24 +02:00
2010-12-27 21:09:24 +02:00
from ubuntutools.config import UDTConfig, ubu_email
from ubuntutools.requestsync.mail import getDebianSrcPkg \
as requestsync_mail_getDebianSrcPkg
from ubuntutools.requestsync.lp import getDebianSrcPkg, getUbuntuSrcPkg
2010-12-27 18:24:21 +02:00
from ubuntutools.logger import Logger
from ubuntutools.lp import udtexceptions
from ubuntutools.lp.lpapicache import Launchpad
class File(object):
2010-10-30 18:45:14 +02:00
def __init__(self, url, checksum, size):
self.url = url
self.name = os.path.basename(url)
self.checksum = checksum
self.size = size
2010-10-30 18:45:14 +02:00
def __repr__(self):
return self.name + " (" + self.checksum + " " + self.size + \
") source " + str(bool(self.is_source_file()))
2010-10-30 18:45:14 +02:00
def __eq__(self, other):
return self.name == other.name and self.checksum == other.checksum and \
self.size == other.size
2010-10-30 18:45:14 +02:00
def get_name(self):
return self.name
2010-10-30 18:45:14 +02:00
def is_source_file(self):
return re.match(".*\.orig.*\.tar\..*", self.name)
2010-12-27 18:24:21 +02:00
def download(self):
2010-10-30 18:45:14 +02:00
'''Download file (by URL) to the current directory.
2010-10-30 18:45:14 +02:00
If the file is already present, this function does nothing.'''
2010-10-30 18:45:14 +02:00
file_exists = os.path.exists(self.name)
2010-10-30 18:45:14 +02:00
if file_exists:
# Check for correct checksum
md5 = hashlib.md5()
md5.update(open(self.name).read())
file_exists = md5.hexdigest() == self.checksum
2010-10-30 18:45:14 +02:00
if not file_exists:
2010-12-27 18:24:21 +02:00
Logger.info('Downloading %s...', self.url)
2010-10-30 18:45:14 +02:00
try:
urllib.urlretrieve(self.url, self.name)
except IOError as err:
2010-12-27 18:24:21 +02:00
Logger.error('Failed to download %s [Errno %i]: %s.',
self.name, err.errno, err.strerror)
2010-10-30 18:45:14 +02:00
sys.exit(1)
class Version(debian.debian_support.Version):
2010-10-30 18:45:14 +02:00
def strip_epoch(self):
'''Removes the epoch from a Debian version string.
strip_epoch(1:1.52-1) will return "1.52-1" and strip_epoch(1.1.3-1) will
return "1.1.3-1".'''
parts = self.full_version.split(':')
if len(parts) > 1:
del parts[0]
version_without_epoch = ':'.join(parts)
return version_without_epoch
def get_related_debian_version(self):
related_debian_version = self.full_version
uidx = related_debian_version.find('ubuntu')
if uidx > 0:
related_debian_version = related_debian_version[:uidx]
uidx = related_debian_version.find('build')
if uidx > 0:
related_debian_version = related_debian_version[:uidx]
return Version(related_debian_version)
def is_modified_in_ubuntu(self):
return self.full_version.find('ubuntu') > 0
def remove_signature(dscname):
2010-10-30 18:45:14 +02:00
'''Removes the signature from a .dsc file if the .dsc file is signed.'''
2010-12-27 15:20:49 +01:00
dsc_file = open(dscname)
if dsc_file.readline().strip() == "-----BEGIN PGP SIGNED MESSAGE-----":
2010-10-30 18:45:14 +02:00
unsigned_file = []
# search until begin of body found
2010-12-27 15:20:49 +01:00
for line in dsc_file:
if line.strip() == "":
2010-10-30 18:45:14 +02:00
break
# search for end of body
2010-12-27 15:20:49 +01:00
for line in dsc_file:
if line.strip() == "":
2010-10-30 18:45:14 +02:00
break
unsigned_file.append(line)
2010-10-30 18:45:14 +02:00
2010-12-27 15:20:49 +01:00
dsc_file.close()
dsc_file = open(dscname, "w")
dsc_file.writelines(unsigned_file)
dsc_file.close()
2010-12-27 18:24:21 +02:00
def dsc_getfiles(dscurl):
2010-10-30 18:45:14 +02:00
'''Return list of files in a .dsc file (excluding the .dsc file itself).'''
2010-04-13 23:18:24 +02:00
2010-10-30 18:45:14 +02:00
basepath = os.path.dirname(dscurl)
dsc = debian.deb822.Dsc(urllib.urlopen(dscurl))
2010-04-13 23:18:24 +02:00
2010-10-30 18:45:14 +02:00
if 'Files' not in dsc:
2010-12-27 18:24:21 +02:00
Logger.error('No Files field found in the dsc file. Please check %s!',
os.path.basename(dscurl))
2010-10-30 18:45:14 +02:00
sys.exit(1)
2010-04-13 23:18:24 +02:00
2010-10-30 18:45:14 +02:00
files = []
2010-12-27 15:20:49 +01:00
for source_file in dsc['Files']:
url = os.path.join(basepath, source_file['name'])
if not source_file['name'].endswith('.dsc'):
files.append(File(url, source_file['md5sum'], source_file['size']))
2010-10-30 18:45:14 +02:00
return files
2010-04-13 23:18:24 +02:00
2010-12-27 15:20:49 +01:00
def add_fixed_bugs(changes, bugs):
'''Add additional Launchpad bugs to the list of fixed bugs in changes
file.'''
2010-10-30 18:45:14 +02:00
2010-12-27 15:20:49 +01:00
changes = [l for l in changes.split("\n") if l.strip() != ""]
2010-10-30 18:45:14 +02:00
# Remove duplicates
bugs = set(bugs)
for i in xrange(len(changes)):
if changes[i].startswith("Launchpad-Bugs-Fixed:"):
bugs.update(changes[i][22:].strip().split(" "))
changes[i] = "Launchpad-Bugs-Fixed: %s" % (" ".join(bugs))
break
elif i == len(changes) - 1:
# Launchpad-Bugs-Fixed entry does not exist in changes file
line = "Launchpad-Bugs-Fixed: %s" % (" ".join(bugs))
changes.append(line)
return "\n".join(changes + [""])
2010-12-27 18:24:21 +02:00
def sync_dsc(dscurl, debian_dist, release, name, email, bugs, keyid=None):
2010-10-30 18:45:14 +02:00
assert dscurl.endswith(".dsc")
dscname = os.path.basename(dscurl)
basepath = os.path.dirname(dscurl)
(srcpkg, new_ver) = dscname.split('_')
uploader = name + " <" + email + ">"
if os.path.exists(os.path.join(basepath, dscname)):
dscfile = dscurl
else:
try:
urllib.urlretrieve(dscurl, dscname)
except IOError as error:
2010-12-27 18:24:21 +02:00
Logger.error('Failed to download %s [Errno %i]: %s.',
dscname, error.errno, error.strerror)
2010-10-30 18:45:14 +02:00
sys.exit(1)
dscfile = debian.deb822.Dsc(file(dscname))
if "Version" not in dscfile:
2010-12-27 18:24:21 +02:00
Logger.error('No Version field found in the dsc file. Please check %s!',
dscname)
2010-10-30 18:45:14 +02:00
sys.exit(1)
new_ver = Version(dscfile["Version"])
try:
ubuntu_source = getUbuntuSrcPkg(srcpkg, release)
ubuntu_ver = Version(ubuntu_source.getVersion())
2010-12-27 15:20:49 +01:00
ubuntu_dsc = [f for f in ubuntu_source.sourceFileUrls()
if f.endswith(".dsc")]
2010-10-30 18:45:14 +02:00
assert len(ubuntu_dsc) == 1
ubuntu_dsc = ubuntu_dsc[0]
except udtexceptions.PackageNotFoundException:
ubuntu_ver = Version('~')
ubuntu_dsc = None
# No need to continue if version is not greater than current one
if new_ver <= ubuntu_ver:
2010-12-27 18:24:21 +02:00
Logger.error('%s version %s is not greater than already available %s',
srcpkg, new_ver, ubuntu_ver)
sys.exit(1)
2010-12-27 18:24:21 +02:00
Logger.debug('Source %s: current version %s, new version %s',
srcpkg, ubuntu_ver, new_ver)
2010-10-30 18:45:14 +02:00
2010-12-27 18:24:21 +02:00
files = dsc_getfiles(dscurl)
source_files = [f for f in files if f.is_source_file()]
2010-12-27 18:24:21 +02:00
Logger.debug('Files: %s', str([x.get_name() for x in files]))
Logger.debug('Source files: %s', str([x.get_name() for x in source_files]))
[f.download() for f in files]
2010-10-30 18:45:14 +02:00
if ubuntu_dsc is None:
ubuntu_files = None
else:
2010-12-27 18:24:21 +02:00
ubuntu_files = dsc_getfiles(ubuntu_dsc)
2010-10-30 18:45:14 +02:00
# do we need the orig.tar.gz?
need_orig = True
fakesync_files = []
if ubuntu_ver.upstream_version == new_ver.upstream_version:
# We need to check if all .orig*.tar.* tarballs exist in Ubuntu
need_orig = False
for source_file in source_files:
2010-12-27 15:20:49 +01:00
ubuntu_file = [f for f in ubuntu_files
if f.get_name() == source_file.get_name()]
2010-10-30 18:45:14 +02:00
if len(ubuntu_file) == 0:
# The source file does not exist in Ubuntu
2010-12-27 18:24:21 +02:00
Logger.info('%s does not exist in Ubuntu.',
source_file.get_name())
2010-10-30 18:45:14 +02:00
need_orig = True
elif not ubuntu_file[0] == source_file:
# The checksum of the files mismatch -> We need a fake sync
2010-12-27 18:24:21 +02:00
Logger.warn('The checksum of the file %s mismatch. '
'A fake sync is required.', source_file.get_name())
2010-10-30 18:45:14 +02:00
fakesync_files.append(ubuntu_file[0])
2010-12-27 18:24:21 +02:00
Logger.debug('Ubuntu version: %s', ubuntu_file[0])
Logger.debug('Debian version: %s', source_file)
Logger.debug('Needs source tarball: %s', str(need_orig))
2010-10-30 18:45:14 +02:00
cur_ver = ubuntu_ver.get_related_debian_version()
if ubuntu_ver.is_modified_in_ubuntu():
2010-12-27 18:24:21 +02:00
Logger.warn('Overwriting modified Ubuntu version %s, '
'setting current version to %s',
ubuntu_ver.full_version, cur_ver.full_version)
2010-10-30 18:45:14 +02:00
# extract package
cmd = ['dpkg-source', '-x', dscname]
env = os.environ
env['DEB_VENDOR'] = 'Ubuntu'
2010-12-27 18:24:21 +02:00
if not Logger.verbose:
2010-10-30 18:45:14 +02:00
cmd.insert(1, "-q")
2010-12-27 18:24:21 +02:00
Logger.command(cmd)
subprocess.check_call(cmd, env=env)
2010-10-30 18:45:14 +02:00
# Do a fake sync if required
if len(fakesync_files) > 0:
# Download Ubuntu files (override Debian source tarballs)
2010-12-27 18:24:21 +02:00
[f.download() for f in fakesync_files]
2010-10-30 18:45:14 +02:00
# change into package directory
directory = srcpkg + '-' + new_ver.upstream_version
2010-12-27 18:24:21 +02:00
Logger.command(('cd', directory))
2010-10-30 18:45:14 +02:00
os.chdir(directory)
# read Debian distribution from debian/changelog if not specified
if debian_dist is None:
line = open("debian/changelog").readline()
debian_dist = line.split(" ")[2].strip(";")
if len(fakesync_files) == 0:
# create the changes file
2010-12-27 15:20:49 +01:00
changes_filename = "%s_%s_source.changes" % \
(srcpkg, new_ver.strip_epoch())
2010-10-30 18:45:14 +02:00
cmd = ["dpkg-genchanges", "-S", "-v" + cur_ver.full_version,
"-DDistribution=" + release,
"-DOrigin=debian/" + debian_dist,
"-e" + uploader]
if need_orig:
cmd.append("-sa")
else:
cmd.append("-sd")
2010-12-27 18:24:21 +02:00
if not Logger.verbose:
2010-10-30 18:45:14 +02:00
cmd += ["-q"]
2010-12-27 18:24:21 +02:00
Logger.command(cmd + ['>', '../' + changes_filename])
2010-10-30 18:45:14 +02:00
changes = subprocess.Popen(cmd, stdout=subprocess.PIPE,
env={"DEB_VENDOR": "Ubuntu"}).communicate()[0]
# Add additional bug numbers
if len(bugs) > 0:
2010-12-27 15:20:49 +01:00
changes = add_fixed_bugs(changes, bugs)
2010-10-30 18:45:14 +02:00
# remove extracted (temporary) files
2010-12-27 18:24:21 +02:00
Logger.command(('cd', '..'))
2010-10-30 18:45:14 +02:00
os.chdir('..')
shutil.rmtree(directory, True)
# write changes file
2010-12-27 15:20:49 +01:00
changes_file = open(changes_filename, "w")
changes_file.writelines(changes)
changes_file.close()
2010-10-30 18:45:14 +02:00
# remove signature and sign package
remove_signature(dscname)
if keyid is not False:
2010-12-27 15:20:49 +01:00
cmd = ["debsign", changes_filename]
2010-10-30 18:45:14 +02:00
if not keyid is None:
cmd.insert(1, "-k" + keyid)
2010-12-27 18:24:21 +02:00
Logger.command(cmd)
2010-10-30 18:45:14 +02:00
subprocess.check_call(cmd)
else:
# Create fakesync changelog entry
new_ver = Version(new_ver.full_version + "fakesync1")
2010-12-27 15:20:49 +01:00
changes_filename = "%s_%s_source.changes" % \
(srcpkg, new_ver.strip_epoch())
2010-10-30 18:45:14 +02:00
if len(bugs) > 0:
message = "Fake sync due to mismatching orig tarball (LP: %s)." % \
(", ".join(["#" + str(b) for b in bugs]))
2010-10-30 18:45:14 +02:00
else:
message = "Fake sync due to mismatching orig tarball."
cmd = ["dch", "-v", new_ver.full_version, "-D", release, message]
env = {"DEBFULLNAME": name, "DEBEMAIL": email}
2010-12-27 18:24:21 +02:00
Logger.command(cmd)
2010-10-30 18:45:14 +02:00
subprocess.check_call(cmd, env=env)
# update the Maintainer field
cmd = ["update-maintainer"]
2010-12-27 18:24:21 +02:00
if not Logger.verbose:
2010-10-30 18:45:14 +02:00
cmd.append("-q")
2010-12-27 18:24:21 +02:00
Logger.command(cmd)
2010-10-30 18:45:14 +02:00
subprocess.check_call(cmd)
2010-12-03 00:06:43 +01:00
2010-10-30 18:45:14 +02:00
# Build source package
cmd = ["debuild", "--no-lintian", "-S", "-v" + cur_ver.full_version]
env = os.environ
env['DEB_VENDOR'] = 'Ubuntu'
if need_orig:
cmd += ['-sa']
if keyid:
cmd += ["-k" + keyid]
2010-12-27 18:24:21 +02:00
Logger.command(cmd)
returncode = subprocess.call(cmd, env=env)
if returncode != 0:
2010-12-27 18:24:21 +02:00
Logger.error('Source-only build with debuild failed. '
'Please check build log above.')
sys.exit(1)
def get_debian_dscurl(package, dist, release, version=None, component=None):
2010-10-30 18:45:14 +02:00
if dist is None:
dist = "unstable"
2010-10-30 18:45:14 +02:00
if type(version) == str:
version = Version(version)
if version is None or component is None:
debian_srcpkg = getDebianSrcPkg(package, dist)
try:
src_pkg = getUbuntuSrcPkg(package, release)
ubuntu_version = Version(src_pkg.getVersion())
2010-10-30 18:45:14 +02:00
except udtexceptions.PackageNotFoundException:
ubuntu_version = Version('~')
if ubuntu_version >= Version(debian_srcpkg.getVersion()):
# The LP importer is maybe out of date
debian_srcpkg = requestsync_mail_getDebianSrcPkg(package, dist)
if version is None:
version = Version(debian_srcpkg.getVersion())
if component is None:
component = debian_srcpkg.getComponent()
assert component in ("main", "contrib", "non-free")
if package.startswith("lib"):
group = package[0:4]
else:
group = package[0]
dsc_file = package + "_" + version.strip_epoch() + ".dsc"
dscurl = os.path.join("http://ftp.debian.org/debian/pool", component, group,
package, dsc_file)
return dscurl
2010-12-27 15:20:49 +01:00
def main():
2010-12-27 18:24:21 +02:00
usage = "%prog [options] <.dsc URL/path or package name>"
epilog = "See %s(1) for more info." % os.path.basename(sys.argv[0])
2010-10-30 18:45:14 +02:00
parser = optparse.OptionParser(usage=usage, epilog=epilog)
2010-12-27 21:09:24 +02:00
parser.add_option("-d", "--distribution",
2010-10-30 18:45:14 +02:00
dest="dist", default=None,
help="Debian distribution to sync from.")
2010-12-27 21:09:24 +02:00
parser.add_option("-r", "--release",
dest="release", default=None,
2010-10-30 18:45:14 +02:00
help="Specify target Ubuntu release.")
2010-12-27 21:09:24 +02:00
parser.add_option("-V", "--debian-version",
dest="debversion", default=None,
2010-10-30 18:45:14 +02:00
help="Specify the version to sync from.")
2010-12-27 21:09:24 +02:00
parser.add_option("-c", "--component",
dest="component", default=None,
2010-10-30 18:45:14 +02:00
help="Specify the Debian component to sync from.")
2010-12-27 21:09:24 +02:00
parser.add_option("-v", "--verbose",
dest="verbose", action="store_true", default=False,
help="Display more progress information.")
parser.add_option("-n", "--uploader-name",
dest="uploader_name", default=None,
2010-10-30 18:45:14 +02:00
help="Use UPLOADER_NAME as the name of the maintainer "
2010-12-27 21:09:24 +02:00
"for this upload.")
parser.add_option("-e", "--uploader-email",
dest="uploader_email", default=None,
2010-10-30 18:45:14 +02:00
help="Use UPLOADER_EMAIL as email address of the "
2010-12-27 21:09:24 +02:00
"maintainer for this upload.")
parser.add_option("-k", "--key",
dest="keyid", default=None,
2010-10-30 18:45:14 +02:00
help="Specify the key ID to be used for signing.")
2010-12-27 21:09:24 +02:00
parser.add_option('--dont-sign',
dest='keyid', action='store_false',
help='Do not sign the upload.')
2010-10-30 18:45:14 +02:00
parser.add_option("-b", "--bug", metavar="BUG",
2010-12-27 21:09:24 +02:00
dest="bugs", action="append", default=list(),
help="Mark Launchpad bug BUG as being fixed by this "
"upload.")
parser.add_option('--no-conf',
dest='no_conf', default=False, action='store_true',
help="Don't read config files or environment variables.")
2010-10-30 18:45:14 +02:00
(options, args) = parser.parse_args()
if len(args) == 0:
2010-12-27 18:24:21 +02:00
parser.error('No .dsc URL/path or package name specified.')
if len(args) > 1:
parser.error('Multiple .dsc URLs/paths or package names specified: '
+ ', '.join(args))
2010-10-30 18:45:14 +02:00
invalid_bug_numbers = [bug for bug in options.bugs if not bug.isdigit()]
2010-10-30 18:45:14 +02:00
if len(invalid_bug_numbers) > 0:
2010-12-27 18:24:21 +02:00
parser.error('Invalid bug number(s) specified: '
+ ', '.join(invalid_bug_numbers))
2010-12-27 21:09:24 +02:00
config = UDTConfig(options.no_conf)
if options.uploader_name is None:
2010-12-27 21:09:24 +02:00
options.uploader_name = ubu_email(export=False)[0]
if options.uploader_email is None:
2010-12-27 21:09:24 +02:00
options.uploader_email = ubu_email(export=False)[1]
2010-10-30 18:45:14 +02:00
Launchpad.login_anonymously()
if options.release is None:
options.release = Launchpad.distributions["ubuntu"].current_series.name
if args[0].endswith(".dsc"):
dscurl = args[0]
else:
if options.component not in (None, "main", "contrib", "non-free"):
2010-12-27 18:24:21 +02:00
parser.error('%s is not a valid Debian component. '
'It should be one of main, contrib, or non-free.'
% options.component)
2010-10-30 18:45:14 +02:00
dscurl = get_debian_dscurl(args[0], options.dist, options.release,
options.debversion, options.component)
2010-12-27 18:24:21 +02:00
Logger.verbose = options.verbose
Logger.debug('.dsc url: %s', dscurl)
sync_dsc(dscurl, options.dist, options.release, options.uploader_name,
options.uploader_email, options.bugs, options.keyid)
2010-12-27 15:20:49 +01:00
if __name__ == "__main__":
main()