Port syncpackage to Python 3

This commit is contained in:
Stefano Rivera 2019-09-04 17:15:50 -03:00
parent 3f5e56c75e
commit 2c8c4d7268
2 changed files with 21 additions and 21 deletions

View File

@ -41,6 +41,7 @@ if sys.version_info[0] >= 3:
'setup-packaging-environment', 'setup-packaging-environment',
'sponsor-patch', 'sponsor-patch',
'submittodebian', 'submittodebian',
'syncpackage',
] ]
data_files = [ data_files = [
('share/bash-completion/completions', glob.glob("bash_completion/*")), ('share/bash-completion/completions', glob.glob("bash_completion/*")),
@ -52,7 +53,6 @@ else:
scripts = [ scripts = [
'import-bug-from-debian', 'import-bug-from-debian',
'merge-changelog', 'merge-changelog',
'syncpackage',
'ubuntu-build', 'ubuntu-build',
'ubuntu-iso', 'ubuntu-iso',
'ubuntu-upload-permission', 'ubuntu-upload-permission',

View File

@ -1,4 +1,4 @@
#!/usr/bin/python #!/usr/bin/python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# #
# Copyright (C) 2008-2010 Martin Pitt <martin.pitt@canonical.com>, # Copyright (C) 2008-2010 Martin Pitt <martin.pitt@canonical.com>,
@ -27,7 +27,7 @@ import os
import shutil import shutil
import sys import sys
import textwrap import textwrap
import urllib import urllib.request
from lazr.restfulclient.errors import HTTPError from lazr.restfulclient.errors import HTTPError
@ -50,7 +50,7 @@ from ubuntutools import subprocess
def remove_signature(dscname): def remove_signature(dscname):
'''Removes the signature from a .dsc file if the .dsc file is signed.''' '''Removes the signature from a .dsc file if the .dsc file is signed.'''
dsc_file = open(dscname) dsc_file = codecs.open(dscname, encoding='utf-8')
if dsc_file.readline().strip() == "-----BEGIN PGP SIGNED MESSAGE-----": if dsc_file.readline().strip() == "-----BEGIN PGP SIGNED MESSAGE-----":
unsigned_file = [] unsigned_file = []
# search until begin of body found # search until begin of body found
@ -65,7 +65,7 @@ def remove_signature(dscname):
unsigned_file.append(line) unsigned_file.append(line)
dsc_file.close() dsc_file.close()
dsc_file = open(dscname, "w") dsc_file = codecs.open(dscname, "w", encoding='utf-8')
dsc_file.writelines(unsigned_file) dsc_file.writelines(unsigned_file)
dsc_file.close() dsc_file.close()
@ -78,7 +78,7 @@ def add_fixed_bugs(changes, bugs):
# Remove duplicates # Remove duplicates
bugs = set(str(bug) for bug in bugs) bugs = set(str(bug) for bug in bugs)
for i in xrange(len(changes)): for i in range(len(changes)):
if changes[i].startswith("Launchpad-Bugs-Fixed:"): if changes[i].startswith("Launchpad-Bugs-Fixed:"):
bugs.update(changes[i][22:].strip().split(" ")) bugs.update(changes[i][22:].strip().split(" "))
changes[i] = "Launchpad-Bugs-Fixed: %s" % (" ".join(bugs)) changes[i] = "Launchpad-Bugs-Fixed: %s" % (" ".join(bugs))
@ -137,7 +137,7 @@ def sync_dsc(src_pkg, debian_dist, release, name, email, bugs, ubuntu_mirror,
try: try:
src_pkg.pull() src_pkg.pull()
except DownloadError, e: except DownloadError as e:
Logger.error('Failed to download: %s', str(e)) Logger.error('Failed to download: %s', str(e))
sys.exit(1) sys.exit(1)
src_pkg.unpack() src_pkg.unpack()
@ -158,7 +158,7 @@ def sync_dsc(src_pkg, debian_dist, release, name, email, bugs, ubuntu_mirror,
# Download Ubuntu files (override Debian source tarballs) # Download Ubuntu files (override Debian source tarballs)
try: try:
ubu_pkg.pull() ubu_pkg.pull()
except DownloadError, e: except DownloadError as e:
Logger.error('Failed to download: %s', str(e)) Logger.error('Failed to download: %s', str(e))
sys.exit(1) sys.exit(1)
@ -169,7 +169,7 @@ def sync_dsc(src_pkg, debian_dist, release, name, email, bugs, ubuntu_mirror,
# read Debian distribution from debian/changelog if not specified # read Debian distribution from debian/changelog if not specified
if debian_dist is None: if debian_dist is None:
line = open("debian/changelog").readline() line = codecs.open("debian/changelog", encoding='utf-8').readline()
debian_dist = line.split(" ")[2].strip(";") debian_dist = line.split(" ")[2].strip(";")
if not fakesync: if not fakesync:
@ -187,8 +187,7 @@ def sync_dsc(src_pkg, debian_dist, release, name, email, bugs, ubuntu_mirror,
if not Logger.verbose: if not Logger.verbose:
cmd += ["-q"] cmd += ["-q"]
Logger.command(cmd + ['>', '../' + changes_filename]) Logger.command(cmd + ['>', '../' + changes_filename])
process = subprocess.Popen(cmd, stdout=subprocess.PIPE) changes = subprocess.check_output(cmd, encoding='utf-8')
changes = process.communicate()[0]
# Add additional bug numbers # Add additional bug numbers
if len(bugs) > 0: if len(bugs) > 0:
@ -200,7 +199,7 @@ def sync_dsc(src_pkg, debian_dist, release, name, email, bugs, ubuntu_mirror,
shutil.rmtree(directory, True) shutil.rmtree(directory, True)
# write changes file # write changes file
changes_file = open(changes_filename, "w") changes_file = codecs.open(changes_filename, "w", encoding='utf-8')
changes_file.writelines(changes) changes_file.writelines(changes)
changes_file.close() changes_file.close()
@ -274,7 +273,7 @@ def fetch_source_pkg(package, dist, version, component, ubuntu_release,
try: try:
debian_srcpkg = get_debian_srcpkg(package, dist) debian_srcpkg = get_debian_srcpkg(package, dist)
except (udtexceptions.PackageNotFoundException, except (udtexceptions.PackageNotFoundException,
udtexceptions.SeriesNotFoundException), e: udtexceptions.SeriesNotFoundException) as e:
Logger.error(str(e)) Logger.error(str(e))
sys.exit(1) sys.exit(1)
if version is None: if version is None:
@ -286,7 +285,7 @@ def fetch_source_pkg(package, dist, version, component, ubuntu_release,
ubuntu_version = Version(ubuntu_srcpkg.getVersion()) ubuntu_version = Version(ubuntu_srcpkg.getVersion())
except udtexceptions.PackageNotFoundException: except udtexceptions.PackageNotFoundException:
ubuntu_version = Version('~') ubuntu_version = Version('~')
except udtexceptions.SeriesNotFoundException, e: except udtexceptions.SeriesNotFoundException as e:
Logger.error(str(e)) Logger.error(str(e))
sys.exit(1) sys.exit(1)
if ubuntu_version >= version: if ubuntu_version >= version:
@ -388,7 +387,7 @@ def copy(src_pkg, release, bugs, sponsoree=None, simulate=False, force=False):
to_pocket=ubuntu_pocket, to_pocket=ubuntu_pocket,
include_binaries=False, include_binaries=False,
sponsored=sponsoree) sponsored=sponsoree)
except HTTPError, error: except HTTPError as error:
Logger.error("HTTP Error %s: %s", error.response.status, Logger.error("HTTP Error %s: %s", error.response.status,
error.response.reason) error.response.reason)
Logger.error(error.content) Logger.error(error.content)
@ -416,7 +415,7 @@ def is_blacklisted(query):
series = Launchpad.distributions['ubuntu'].current_series series = Launchpad.distributions['ubuntu'].current_series
lp_comments = series.getDifferenceComments(source_package_name=query) lp_comments = series.getDifferenceComments(source_package_name=query)
blacklisted = False blacklisted = False
comments = [u'%s\n -- %s %s' comments = ['%s\n -- %s %s'
% (c.body_text, c.comment_author.name, % (c.body_text, c.comment_author.name,
c.comment_date.strftime('%a, %d %b %Y %H:%M:%S +0000')) c.comment_date.strftime('%a, %d %b %Y %H:%M:%S +0000'))
for c in lp_comments] for c in lp_comments]
@ -430,9 +429,10 @@ def is_blacklisted(query):
# Old blacklist: # Old blacklist:
url = 'http://people.canonical.com/~ubuntu-archive/sync-blacklist.txt' url = 'http://people.canonical.com/~ubuntu-archive/sync-blacklist.txt'
with codecs.EncodedFile(urllib.urlopen(url), 'UTF-8') as f: with urllib.request.urlopen(url) as f:
applicable_lines = [] applicable_lines = []
for line in f: for line in f:
line = line.decode('utf-8')
if not line.strip(): if not line.strip():
applicable_lines = [] applicable_lines = []
continue continue
@ -475,7 +475,7 @@ def close_bugs(bugs, package, version, changes, sponsoree):
bug.newMessage(content=message) bug.newMessage(content=message)
break break
else: else:
Logger.error(u"Cannot find any tasks on LP: #%i to close.", bug.id) Logger.error("Cannot find any tasks on LP: #%i to close.", bug.id)
def parse(): def parse():
@ -686,9 +686,9 @@ def main():
"reasoning and subscribe ~ubuntu-archive."] "reasoning and subscribe ~ubuntu-archive."]
if blacklist_fail: if blacklist_fail:
Logger.error(u"Source package %s is blacklisted.", src_pkg.source) Logger.error("Source package %s is blacklisted.", src_pkg.source)
elif blacklisted == 'ALWAYS': elif blacklisted == 'ALWAYS':
Logger.normal(u"Source package %s is blacklisted.", src_pkg.source) Logger.normal("Source package %s is blacklisted.", src_pkg.source)
if messages: if messages:
for message in messages: for message in messages:
for line in textwrap.wrap(message): for line in textwrap.wrap(message):
@ -698,7 +698,7 @@ def main():
Logger.normal("Blacklist Comments:") Logger.normal("Blacklist Comments:")
for comment in comments: for comment in comments:
for line in textwrap.wrap(comment): for line in textwrap.wrap(comment):
Logger.normal(u" " + line) Logger.normal(" " + line)
if blacklist_fail: if blacklist_fail:
sys.exit(1) sys.exit(1)