From 2c8c4d726827ff1cacd9f4ee92af870d73ff69e1 Mon Sep 17 00:00:00 2001 From: Stefano Rivera Date: Wed, 4 Sep 2019 17:15:50 -0300 Subject: [PATCH] Port syncpackage to Python 3 --- setup.py | 2 +- syncpackage | 40 ++++++++++++++++++++-------------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/setup.py b/setup.py index 6735b80..78d0394 100755 --- a/setup.py +++ b/setup.py @@ -41,6 +41,7 @@ if sys.version_info[0] >= 3: 'setup-packaging-environment', 'sponsor-patch', 'submittodebian', + 'syncpackage', ] data_files = [ ('share/bash-completion/completions', glob.glob("bash_completion/*")), @@ -52,7 +53,6 @@ else: scripts = [ 'import-bug-from-debian', 'merge-changelog', - 'syncpackage', 'ubuntu-build', 'ubuntu-iso', 'ubuntu-upload-permission', diff --git a/syncpackage b/syncpackage index ee48c64..43cd3be 100755 --- a/syncpackage +++ b/syncpackage @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 # -*- coding: utf-8 -*- # # Copyright (C) 2008-2010 Martin Pitt , @@ -27,7 +27,7 @@ import os import shutil import sys import textwrap -import urllib +import urllib.request from lazr.restfulclient.errors import HTTPError @@ -50,7 +50,7 @@ from ubuntutools import subprocess def remove_signature(dscname): '''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-----": unsigned_file = [] # search until begin of body found @@ -65,7 +65,7 @@ def remove_signature(dscname): unsigned_file.append(line) dsc_file.close() - dsc_file = open(dscname, "w") + dsc_file = codecs.open(dscname, "w", encoding='utf-8') dsc_file.writelines(unsigned_file) dsc_file.close() @@ -78,7 +78,7 @@ def add_fixed_bugs(changes, bugs): # Remove duplicates 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:"): bugs.update(changes[i][22:].strip().split(" ")) 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: src_pkg.pull() - except DownloadError, e: + except DownloadError as e: Logger.error('Failed to download: %s', str(e)) sys.exit(1) 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) try: ubu_pkg.pull() - except DownloadError, e: + except DownloadError as e: Logger.error('Failed to download: %s', str(e)) 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 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(";") if not fakesync: @@ -187,8 +187,7 @@ def sync_dsc(src_pkg, debian_dist, release, name, email, bugs, ubuntu_mirror, if not Logger.verbose: cmd += ["-q"] Logger.command(cmd + ['>', '../' + changes_filename]) - process = subprocess.Popen(cmd, stdout=subprocess.PIPE) - changes = process.communicate()[0] + changes = subprocess.check_output(cmd, encoding='utf-8') # Add additional bug numbers 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) # 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.close() @@ -274,7 +273,7 @@ def fetch_source_pkg(package, dist, version, component, ubuntu_release, try: debian_srcpkg = get_debian_srcpkg(package, dist) except (udtexceptions.PackageNotFoundException, - udtexceptions.SeriesNotFoundException), e: + udtexceptions.SeriesNotFoundException) as e: Logger.error(str(e)) sys.exit(1) if version is None: @@ -286,7 +285,7 @@ def fetch_source_pkg(package, dist, version, component, ubuntu_release, ubuntu_version = Version(ubuntu_srcpkg.getVersion()) except udtexceptions.PackageNotFoundException: ubuntu_version = Version('~') - except udtexceptions.SeriesNotFoundException, e: + except udtexceptions.SeriesNotFoundException as e: Logger.error(str(e)) sys.exit(1) if ubuntu_version >= version: @@ -388,7 +387,7 @@ def copy(src_pkg, release, bugs, sponsoree=None, simulate=False, force=False): to_pocket=ubuntu_pocket, include_binaries=False, sponsored=sponsoree) - except HTTPError, error: + except HTTPError as error: Logger.error("HTTP Error %s: %s", error.response.status, error.response.reason) Logger.error(error.content) @@ -416,7 +415,7 @@ def is_blacklisted(query): series = Launchpad.distributions['ubuntu'].current_series lp_comments = series.getDifferenceComments(source_package_name=query) blacklisted = False - comments = [u'%s\n -- %s %s' + comments = ['%s\n -- %s %s' % (c.body_text, c.comment_author.name, c.comment_date.strftime('%a, %d %b %Y %H:%M:%S +0000')) for c in lp_comments] @@ -430,9 +429,10 @@ def is_blacklisted(query): # Old blacklist: 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 = [] for line in f: + line = line.decode('utf-8') if not line.strip(): applicable_lines = [] continue @@ -475,7 +475,7 @@ def close_bugs(bugs, package, version, changes, sponsoree): bug.newMessage(content=message) break 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(): @@ -686,9 +686,9 @@ def main(): "reasoning and subscribe ~ubuntu-archive."] 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': - Logger.normal(u"Source package %s is blacklisted.", src_pkg.source) + Logger.normal("Source package %s is blacklisted.", src_pkg.source) if messages: for message in messages: for line in textwrap.wrap(message): @@ -698,7 +698,7 @@ def main(): Logger.normal("Blacklist Comments:") for comment in comments: for line in textwrap.wrap(comment): - Logger.normal(u" " + line) + Logger.normal(" " + line) if blacklist_fail: sys.exit(1)