From 595e84191ead1a856d7cd2f881f5af78e5faebd4 Mon Sep 17 00:00:00 2001 From: Stephan Kulow Date: Sat, 20 Apr 2019 21:16:50 +0200 Subject: [PATCH] More porting to python3 Travis changed default python to 3.6, which has impact on the way flake8 checks. So make sure we run fine under flake8 of python 3 --- ReviewBot.py | 8 +++++- ToolBase.py | 10 ++++++-- compare_pkglist.py | 6 ++--- deptool.py | 2 +- factory-package-news/announcer.py | 6 ++--- factory-package-news/factory-package-news.py | 27 ++++++++++---------- fcc_submitter.py | 16 ++++++------ issue-diff.py | 8 +++++- k8s-secret.py | 12 ++++++--- legal-auto.py | 10 ++++---- osc-cycle.py | 12 ++++----- osc-staging.py | 8 +++++- suppkg_rebuild.py | 2 +- tests/api_tests.py | 9 ------- tests/comment_tests.py | 6 ++--- tests/pkgcache_tests.py | 4 ++- update_crawler.py | 6 ++--- 17 files changed, 87 insertions(+), 65 deletions(-) diff --git a/ReviewBot.py b/ReviewBot.py index 28303033..1bc51519 100644 --- a/ReviewBot.py +++ b/ReviewBot.py @@ -36,6 +36,12 @@ except ImportError: # python 2.x from urllib2 import HTTPError, URLError +try: + import __builtin__ + input = getattr(__builtin__, 'raw_input') +except (ImportError, AttributeError): + pass + from itertools import count class PackageLookup(object): @@ -867,7 +873,7 @@ class CommandLineInterface(cmdln.Cmdln): self.logger.info("sleeping %d minutes. Press enter to check now ..."%interval) signal.alarm(interval*60) try: - raw_input() + input() except ExTimeout: pass signal.alarm(0) diff --git a/ToolBase.py b/ToolBase.py index f7903a81..3bbedb0d 100644 --- a/ToolBase.py +++ b/ToolBase.py @@ -29,11 +29,17 @@ http_GET = osc.core.http_GET http_DELETE = osc.core.http_DELETE http_POST = osc.core.http_POST +try: + import __builtin__ + input = getattr(__builtin__, 'raw_input') +except (ImportError, AttributeError): + pass + # http://stackoverflow.com/questions/312443/how-do-you-split-a-list-into-evenly-sized-chunks-in-python def chunks(l, n): """ Yield successive n-sized chunks from l. """ - for i in xrange(0, len(l), n): + for i in range(0, len(l), n): yield l[i:i+n] class ToolBase(object): @@ -190,7 +196,7 @@ class CommandLineInterface(cmdln.Cmdln): logger.info("sleeping %d minutes. Press enter to check now ..."%interval) signal.alarm(interval*60) try: - raw_input() + input() except ExTimeout: pass signal.alarm(0) diff --git a/compare_pkglist.py b/compare_pkglist.py index 28d5de39..f686ba5c 100755 --- a/compare_pkglist.py +++ b/compare_pkglist.py @@ -138,13 +138,13 @@ class CompareList(object): return # get souce packages from target - print 'Gathering the package list from %s' % self.old_prj + print('Gathering the package list from %s' % self.old_prj) source = self.get_source_packages(self.old_prj) - print 'Gathering the package list from %s' % self.new_prj + print('Gathering the package list from %s' % self.new_prj) target = self.get_source_packages(self.new_prj) removed_packages = self.removed_pkglist(self.old_prj) if self.existin: - print 'Gathering the package list from %s' % self.existin + print('Gathering the package list from %s' % self.existin) existin_packages = self.get_source_packages(self.existin) if not self.removedonly: diff --git a/deptool.py b/deptool.py index 22c2b2f1..913cce0a 100755 --- a/deptool.py +++ b/deptool.py @@ -88,7 +88,7 @@ class DepTool(cmdln.Cmdln): if parser.has_option(name, 'priority'): repo.priority = parser.getint(name, 'priority') logger.debug("add repo %s" % name) - except Exception, e: + except Exception as e: logger.error(e) def _add_system_repo(self): diff --git a/factory-package-news/announcer.py b/factory-package-news/announcer.py index d51ab516..4568d1a0 100755 --- a/factory-package-news/announcer.py +++ b/factory-package-news/announcer.py @@ -67,7 +67,7 @@ logging.basicConfig(level=logging.DEBUG if options.debug format='%(asctime)s - %(module)s:%(lineno)d - %(levelname)s - %(message)s') if options.dump_config: - print yaml.dump(config_defaults, default_flow_style=False) + print(yaml.dump(config_defaults, default_flow_style=False)) sys.exit(0) config = _load_config(options.config) @@ -145,8 +145,8 @@ msg['Date'] = email.utils.formatdate(localtime=1) msg['Message-ID'] = email.utils.make_msgid() if options.dry: - print "sending ..." - print msg.as_string() + print("sending ...") + print(msg.as_string()) else: logger.info("announcing version {}".format(version)) s = smtplib.SMTP(options.relay) diff --git a/factory-package-news/factory-package-news.py b/factory-package-news/factory-package-news.py index 07d2829f..270efd6a 100755 --- a/factory-package-news/factory-package-news.py +++ b/factory-package-news/factory-package-news.py @@ -35,13 +35,13 @@ class ChangeLogger(cmdln.Cmdln): h = None try: h = self.ts.hdrFromFdno(fd) - except rpm.error, e: + except rpm.error as e: if str(e) == "public key not available": - print str(e) + print(str(e)) if str(e) == "public key not trusted": - print str(e) + print(str(e)) if str(e) == "error reading package header": - print str(e) + print(str(e)) h = None return h @@ -78,7 +78,7 @@ class ChangeLogger(cmdln.Cmdln): ): srpm = '%s-%s-%s.src.rpm'%('kernel-source', m.group('version'), m.group('release')) pkgdata[h['name']]['sourcerpm'] = srpm - print "%s -> %s"%(h['sourcerpm'], srpm) + print("%s -> %s"%(h['sourcerpm'], srpm)) if srpm in changelogs: changelogs[srpm]['packages'].append(h['name']) @@ -204,7 +204,7 @@ class ChangeLogger(cmdln.Cmdln): p1 = set(v1pkgs.keys()) p2 = set(v2pkgs.keys()) - print "Packages changed:" + print('Packages changed:') group = self._get_packages_grouped(v2pkgs, p1&p2) # pprint(p1&p2) # pprint(group) @@ -218,7 +218,7 @@ class ChangeLogger(cmdln.Cmdln): try: t1 = v1changelogs[srpm1]['changelogtime'][0] except IndexError: - print >>sys.stderr, srpm1, "doesn't have a changelog" + print("{} doesn't have a changelog".format(srpm1), file=sys.stderr) continue m = SRPM_RE.match(srpm) if m: @@ -226,17 +226,17 @@ class ChangeLogger(cmdln.Cmdln): else: name = srpm if len(v2changelogs[srpm]['changelogtime']) == 0: - print " %s ERROR: no changelog"%name + print(' {} ERROR: no changelog'.format(name)) continue if t1 == v2changelogs[srpm]['changelogtime'][0]: continue # no new changelog entry, probably just rebuilt pkgs = sorted(group[srpm]) details += "\n==== %s ====\n"%name if v1pkgs[pkgs[0]]['version'] != v2pkgs[pkgs[0]]['version']: - print " %s (%s -> %s)"%(name, v1pkgs[pkgs[0]]['version'], v2pkgs[pkgs[0]]['version']) - details += "Version update (%s -> %s)\n"%(v1pkgs[pkgs[0]]['version'], v2pkgs[pkgs[0]]['version']) + print(" %s (%s -> %s)"%(name, v1pkgs[pkgs[0]]['version'], v2pkgs[pkgs[0]]['version'])) + details += "Version update (%s -> %s)\n" % (v1pkgs[pkgs[0]]['version'], v2pkgs[pkgs[0]]['version']) else: - print " %s"%name + print(" %s" % name) if len(pkgs) > 1: details += "Subpackages: %s\n"%" ".join([p for p in pkgs if p != name]) for (i2, t2) in enumerate(v2changelogs[srpm]['changelogtime']): @@ -245,8 +245,8 @@ class ChangeLogger(cmdln.Cmdln): details += "\n" + v2changelogs[srpm]['changelogtext'][i2] details += '\n' - print "\n=== Details ===" - print details + print("\n=== Details ===") + print(details) def get_optparser(self): parser = cmdln.CmdlnOptionParser(self) @@ -266,4 +266,3 @@ class ChangeLogger(cmdln.Cmdln): if __name__ == "__main__": app = ChangeLogger() sys.exit( app.main() ) - diff --git a/fcc_submitter.py b/fcc_submitter.py index 74b2c408..9a601031 100755 --- a/fcc_submitter.py +++ b/fcc_submitter.py @@ -266,13 +266,13 @@ class FccSubmitter(object): logging.info('No build succeeded package in %s'%self.from_prj) return - print 'Build succeeded packages:' - print '-------------------------------------' + print('Build succeeded packages:') + print('-------------------------------------') for pkg in succeeded_packages: - print pkg + print(pkg) - print '-------------------------------------' - print "Found {} build succeded packages".format(len(succeeded_packages)) + print('-------------------------------------') + print("Found {} build succeded packages".format(len(succeeded_packages))) def get_deleted_packages(self, project): query = 'states=accepted&types=delete&project={}&view=collection' @@ -403,9 +403,9 @@ class FccSubmitter(object): print("Multi-specfile packages:") if ms_packages: for pkg in ms_packages: - print pkg + print(pkg) else: - print 'None' + print('None') @@ -415,7 +415,7 @@ def main(args): osc.conf.config['debug'] = args.debug if args.freeze: - print "freezing {}".format(FCC) + print('freezing {}'.format(FCC)) freezer = FccFreezer() freezer.freeze() else: diff --git a/issue-diff.py b/issue-diff.py index d839638d..36168f65 100755 --- a/issue-diff.py +++ b/issue-diff.py @@ -26,6 +26,12 @@ from osclib.core import package_list from osclib.git import CACHE_DIR from osclib.git import sync +try: + import __builtin__ + input = getattr(__builtin__, 'raw_input') +except (ImportError, AttributeError): + pass + # Issue summary can contain unicode characters and therefore a string containing # either summary or one in which ISSUE_SUMMARY is then placed must be unicode. # For example, translation-update-upstream contains bsc#877707 which has a @@ -106,7 +112,7 @@ def prompt_continue(change_count): else: print('No changes for which to create bug, continue? [y/b/s/n/?] (y): ', end='') - response = raw_input().lower() + response = input().lower() if response == '?': print('b = break; file bug if applicable, record in db, and stop\ns = skip package') elif response in allowed: diff --git a/k8s-secret.py b/k8s-secret.py index 411a1adf..81fc760d 100755 --- a/k8s-secret.py +++ b/k8s-secret.py @@ -7,6 +7,12 @@ from osclib.cache_manager import CacheManager import subprocess import sys +try: + import __builtin__ + input = getattr(__builtin__, 'raw_input') +except (ImportError, AttributeError): + pass + CACHE_DIR = CacheManager.directory('k8s-secret') SCRIPT_PATH = os.path.dirname(os.path.realpath(__file__)) @@ -15,10 +21,10 @@ def secret_create(cache_file): environment = {'OSCRC': cache_file} print('Username: ', end='') - environment['OBS_USER'] = raw_input() + environment['OBS_USER'] = input() print('Password: ', end='') - environment['OBS_PASS'] = raw_input() + environment['OBS_PASS'] = input() osc_init = os.path.join(SCRIPT_PATH, 'dist/ci/osc-init') subprocess.Popen([osc_init], env=environment).wait() @@ -37,7 +43,7 @@ def main(args): print(f.read()) print('Apply secret for {} [y/n] (y): '.format(args.prefix), end='') - response = raw_input().lower() + response = input().lower() if response != '' and response != 'y': return diff --git a/legal-auto.py b/legal-auto.py index 6a42aa47..b8cd7191 100755 --- a/legal-auto.py +++ b/legal-auto.py @@ -58,9 +58,9 @@ class LegalAuto(ReviewBot.ReviewBot): def retried_GET(self, url): try: return http_GET(url) - except HTTPError, e: + except HTTPError as e: if 500 <= e.code <= 599: - print 'Retrying {}'.format(url) + print('Retrying {}'.format(url)) time.sleep(1) return self.retried_GET(url) raise e @@ -109,7 +109,7 @@ class LegalAuto(ReviewBot.ReviewBot): url = osc.core.makeurl(self.legaldb, ['package', str(pack)]) report = REQ.get(url, headers=self.legaldb_headers).json() if report.get('priority', 0) != self.request_priority(): - print "Update priority %d" % self.request_priority() + print('Update priority {}'.format(self.request_priority())) url = osc.core.makeurl( self.legaldb, ['package', str(pack)], {'priority': self.request_priority()}) REQ.patch(url, headers=self.legaldb_headers) @@ -249,7 +249,7 @@ class LegalAuto(ReviewBot.ReviewBot): if match and match.group(1) == package: lpackage = package if package != lpackage: - print "SKIP", package, "it links to", lpackage + print("SKIP", package, "it links to", lpackage) skip = True break if skip: @@ -291,7 +291,7 @@ class LegalAuto(ReviewBot.ReviewBot): return None if not 'saved' in obj: return None - print "PKG", tproject, sproject, package, revision, obj['saved']['id'] + print("PKG", tproject, sproject, package, revision, obj['saved']['id']) self.pkg_cache[hkey] = obj['saved']['id'] return self.pkg_cache[hkey] diff --git a/osc-cycle.py b/osc-cycle.py index c106b3af..4609796a 100644 --- a/osc-cycle.py +++ b/osc-cycle.py @@ -15,24 +15,24 @@ def do_cycle(self, subcmd, opts, *args): """ if len(args) == 0: - print ("No packages were specified, no chain to draw") + print("No packages were specified, no chain to draw") apiurl = self.get_api_url() - print ("digraph depgraph {") + print("digraph depgraph {") args = [pkg.strip() for pkglist in args for pkg in pkglist.split(',') if pkg.strip()] for pkgname in args: try: deps = ET.fromstring(get_dependson(apiurl, opts.project, opts.repository, opts.arch, [pkgname])) pkg = deps.find('package') - print ("\"%s\"" % pkgname) + print("\"%s\"" % pkgname) for deps in pkg.findall('pkgdep'): if deps.text in args: - print ("\"%s\" -> \"%s\"" % (deps.text, pkgname)) + print("\"%s\" -> \"%s\"" % (deps.text, pkgname)) except: # Ignore packages that do not exist - print ("[color=red]") + print("[color=red]") continue - print ("}") + print("}") diff --git a/osc-staging.py b/osc-staging.py index 54894357..06a5a7c3 100644 --- a/osc-staging.py +++ b/osc-staging.py @@ -39,6 +39,12 @@ from osclib.request_splitter import RequestSplitter from osclib.supersede_command import SupersedeCommand from osclib.prio_command import PrioCommand +try: + import __builtin__ + input = getattr(__builtin__, 'raw_input') +except (ImportError, AttributeError): + pass + def _print_version(self): from osclib.common import VERSION @@ -632,7 +638,7 @@ def do_staging(self, subcmd, opts, *args): if opts.non_interactive: print('y') else: - response = raw_input().lower() + response = input().lower() if response != '' and response != 'y': print('Quit') return diff --git a/suppkg_rebuild.py b/suppkg_rebuild.py index fd0821b0..843b478a 100755 --- a/suppkg_rebuild.py +++ b/suppkg_rebuild.py @@ -99,7 +99,7 @@ class StagingHelper(object): """Main method""" rebuild_data = self.api.pseudometa_file_load('support_pkg_rebuild') if rebuild_data is None: - print "There is no support_pkg_rebuild file!" + print("There is no support_pkg_rebuild file!") return logging.info('Gathering support package list from %s' % self.project) diff --git a/tests/api_tests.py b/tests/api_tests.py index 5d712483..c3b31088 100644 --- a/tests/api_tests.py +++ b/tests/api_tests.py @@ -7,15 +7,6 @@ from obs import OBS from osclib.conf import Config from osclib.stagingapi import StagingAPI - -PY3 = sys.version_info[0] == 3 - -if PY3: - string_types = str, -else: - string_types = basestring, - - class TestApiCalls(unittest.TestCase): """ Tests for various api calls to ensure we return expected content diff --git a/tests/comment_tests.py b/tests/comment_tests.py index 37f6b45f..6b6605e8 100644 --- a/tests/comment_tests.py +++ b/tests/comment_tests.py @@ -19,8 +19,8 @@ class TestComment(unittest.TestCase): } def test_truncate(self): - comment = "string of text" - for i in xrange(len(comment) + 1): + comment = 'string of text' + for i in range(len(comment) + 1): truncated = self.api.truncate(comment, length=i) print(truncated) self.assertEqual(len(truncated), i) @@ -47,7 +47,7 @@ handle """.strip() - for i in xrange(len(comment) + len('...\n')): + for i in range(len(comment) + len('...\n')): truncated = self.api.truncate(comment, length=i) print('=' * 80) print(truncated) diff --git a/tests/pkgcache_tests.py b/tests/pkgcache_tests.py index 31c08fd5..9402f01d 100644 --- a/tests/pkgcache_tests.py +++ b/tests/pkgcache_tests.py @@ -1,3 +1,5 @@ +from __future__ import print_function + import os import shutil import unittest @@ -14,7 +16,7 @@ class TestPkgCache(unittest.TestCase): self.cache = PkgCache('/tmp/cache', force_clean=True) for fn in ('file_a', 'file_b', 'file_c'): with open(os.path.join('/tmp', fn), 'w') as f: - print >>f, fn + print(fn, file=f) def tearDown(self): """Clean the environment.""" diff --git a/update_crawler.py b/update_crawler.py index df1c6b85..e73c7d90 100755 --- a/update_crawler.py +++ b/update_crawler.py @@ -37,7 +37,7 @@ http_GET = osc.core.http_GET def chunks(l, n): """ Yield successive n-sized chunks from l. """ - for i in xrange(0, len(l), n): + for i in range(0, len(l), n): yield l[i:i+n] class UpdateCrawler(object): @@ -87,7 +87,7 @@ class UpdateCrawler(object): return http_GET(url) except HTTPError as e: if 500 <= e.code <= 599: - print 'Retrying {}'.format(url) + print('Retrying {}'.format(url)) time.sleep(1) return self.retried_GET(url) raise e @@ -169,7 +169,7 @@ class UpdateCrawler(object): def _submitrequest(self, src_project, src_package, rev, dst_project, dst_package, msg): res = 0 - print "creating submit request", src_project, src_package, rev, dst_project, dst_package + print("creating submit request", src_project, src_package, rev, dst_project, dst_package) if not self.dryrun: res = osc.core.create_submit_request(self.apiurl, src_project,