From 24ae4ba819c8224ca4672a3c2d87beb7bcb45b60 Mon Sep 17 00:00:00 2001 From: Stephan Kulow Date: Mon, 5 Nov 2018 15:46:50 +0100 Subject: [PATCH] Port more code to run under python3 (python2 compat kept) --- osclib/cache.py | 37 ++++++++++++++++++++++++------------- osclib/conf.py | 6 +++--- osclib/list_command.py | 9 +++++---- osclib/obslock.py | 4 +++- osclib/prio_command.py | 17 ++++++++++++----- osclib/repair_command.py | 9 +++++++-- osclib/stagingapi.py | 15 +++++++++++---- 7 files changed, 65 insertions(+), 32 deletions(-) diff --git a/osclib/cache.py b/osclib/cache.py index a5490174..44481bb7 100644 --- a/osclib/cache.py +++ b/osclib/cache.py @@ -7,9 +7,19 @@ import osc.core import re import shutil import sys -import urlparse -import urllib -from StringIO import StringIO + +try: + from urllib.parse import unquote + from urllib.parse import urlsplit, SplitResult + from urllib.request import URLError, HTTPError + from io import StringIO +except ImportError: + #python 2.x + from urlparse import urlsplit, SplitResult + from urllib import unquote + from urllib2 import URLError, HTTPError + from StringIO import StringIO + from osc import conf from osc.core import urlopen from osclib.cache_manager import CacheManager @@ -122,7 +132,7 @@ class Cache(object): @staticmethod def get(url): - url = urllib.unquote(url) + url = unquote(url) match, project = Cache.match(url) if match: path = Cache.path(url, project, include_file=True) @@ -176,7 +186,7 @@ class Cache(object): @staticmethod def put(url, data): - url = urllib.unquote(url) + url = unquote(url) match, project = Cache.match(url) if match: path = Cache.path(url, project, include_file=True, makedirs=True) @@ -189,6 +199,7 @@ class Cache(object): # be replaced with urlopen('file://...') to be consistent, but until # the need arrises StringIO has less overhead. text = data.read() + text.decode('utf-8') data = StringIO(text) if conf.config['debug']: print('CACHE_PUT', url, project, file=sys.stderr) @@ -200,7 +211,7 @@ class Cache(object): @staticmethod def delete(url): - url = urllib.unquote(url) + url = unquote(url) match, project = Cache.match(url) if match: path = Cache.path(url, project, include_file=True) @@ -220,9 +231,9 @@ class Cache(object): # Also delete version without query. This does not handle other # variations using different query strings. Handy for PUT with ?force=1. - o = urlparse.urlsplit(url) + o = urlsplit(url) if o.query != '': - url_plain = urlparse.SplitResult(o.scheme, o.netloc, o.path, '', o.fragment).geturl() + url_plain = SplitResult(o.scheme, o.netloc, o.path, '', o.fragment).geturl() Cache.delete(url_plain) @staticmethod @@ -250,9 +261,9 @@ class Cache(object): @staticmethod def spliturl(url): - o = urlparse.urlsplit(url) - apiurl = urlparse.SplitResult(o.scheme, o.netloc, '', '', '').geturl() - path = urlparse.SplitResult('', '', o.path, o.query, '').geturl() + o = urlsplit(url) + apiurl = SplitResult(o.scheme, o.netloc, '', '', '').geturl() + path = SplitResult('', '', o.path, o.query, '').geturl() return (apiurl, path) @staticmethod @@ -262,7 +273,7 @@ class Cache(object): parts = [Cache.CACHE_DIR] - o = urlparse.urlsplit(url) + o = urlsplit(url) parts.append(o.hostname) if project: @@ -273,7 +284,7 @@ class Cache(object): os.makedirs(directory) if include_file: - parts.append(hashlib.sha1(url).hexdigest()) + parts.append(hashlib.sha1(url.encode('utf-8')).hexdigest()) return os.path.join(*parts) return directory diff --git a/osclib/conf.py b/osclib/conf.py index a72965d0..7dd11b4b 100644 --- a/osclib/conf.py +++ b/osclib/conf.py @@ -1,6 +1,6 @@ from __future__ import print_function -from ConfigParser import ConfigParser +from osc import OscConfigParser from collections import OrderedDict import io import os @@ -235,7 +235,7 @@ class Config(object): extra sections. """ - cp = ConfigParser(defaults=defaults) + cp = OscConfigParser.OscConfigParser(defaults=defaults) cp.read(self.conf_file) if cp.has_section(section): return dict(cp.items(section)) @@ -246,7 +246,7 @@ class Config(object): from osclib.core import attribute_value_load config = attribute_value_load(apiurl, self.project, 'Config') if config: - cp = ConfigParser() + cp = OscConfigParser.OscConfigParser() config = '[remote]\n' + config cp.readfp(io.BytesIO(config)) return dict(cp.items('remote')) diff --git a/osclib/list_command.py b/osclib/list_command.py index 160cef75..01c03815 100644 --- a/osclib/list_command.py +++ b/osclib/list_command.py @@ -1,9 +1,10 @@ +from __future__ import print_function + from colorama import Fore from osc import oscerr from osclib.request_splitter import RequestSplitter from osclib.supersede_command import SupersedeCommand - class ListCommand: SOURCE_PROJECT_STRIP = [ 'SUSE:SLE-12:', @@ -34,7 +35,7 @@ class ListCommand: hide_source = self.api.project == 'openSUSE:Factory' for group in sorted(splitter.grouped.keys()): - print Fore.YELLOW + group + print(Fore.YELLOW + group) for request in splitter.grouped[group]['requests']: request_id = int(request.get('id')) @@ -58,13 +59,13 @@ class ListCommand: if message: line += '\n' + Fore.WHITE + message + Fore.RESET - print ' ', line + print(' ' + line) if len(splitter.other): non_ring_packages = [] for request in splitter.other: non_ring_packages.append(request.find('./action/target').get('package')) - print 'Not in a ring:', ' '.join(sorted(non_ring_packages)) + print('Not in a ring: ' + ' '.join(sorted(non_ring_packages))) # Print requests not handled by staging process to highlight them. splitter.stageable = False diff --git a/osclib/obslock.py b/osclib/obslock.py index 237d805c..e71cd3c0 100644 --- a/osclib/obslock.py +++ b/osclib/obslock.py @@ -1,3 +1,5 @@ +from __future__ import print_function + from datetime import datetime import time import warnings @@ -95,7 +97,7 @@ class OBSLock(object): stop = False if stop: - print 'Lock acquired by [%s] %s ago, reason <%s>. Try later.' % (user, delta, reason) + print('Lock acquired by [%s] %s ago, reason <%s>. Try later.' % (user, delta, reason)) exit(-1) self._write(self._signature()) diff --git a/osclib/prio_command.py b/osclib/prio_command.py index eff9bcde..b872aad4 100644 --- a/osclib/prio_command.py +++ b/osclib/prio_command.py @@ -1,6 +1,13 @@ +from __future__ import print_function + import json import osc -import urllib2 + +try: + from urllib.request import HTTPError +except ImportError: + #python 2.x + from urllib2 import HTTPError class PrioCommand(object): def __init__(self, api): @@ -22,12 +29,12 @@ class PrioCommand(object): if req.priority != priority: query = { 'cmd': 'setpriority', 'priority': priority } url = osc.core.makeurl(self.api.apiurl, ['request', reqid], query) - print reqid, message + print(reqid + ' ' + message) try: osc.core.http_POST(url, data=message) - print reqid, r['by'], priority - except urllib2.HTTPError as e: - print e + print(reqid + ' ' + r['by'] + ' ' + priority) + except HTTPError as e: + print(e) def perform(self, projects=None, priority=None): diff --git a/osclib/repair_command.py b/osclib/repair_command.py index ffb13edd..19a11c86 100644 --- a/osclib/repair_command.py +++ b/osclib/repair_command.py @@ -1,7 +1,12 @@ from __future__ import print_function import re -import urllib2 + +try: + from urllib.request import HTTPError +except ImportError: + #python 2.x + from urllib2 import HTTPError from osc import oscerr from osc.core import change_review_state @@ -37,7 +42,7 @@ class RepairCommand(object): staging_project = reviews[0] try: data = self.api.get_prj_pseudometa(staging_project) - except urllib2.HTTPError as e: + except HTTPError as e: if e.code == 404: data = None diff --git a/osclib/stagingapi.py b/osclib/stagingapi.py index 4ed1d976..69538cdf 100644 --- a/osclib/stagingapi.py +++ b/osclib/stagingapi.py @@ -1,4 +1,11 @@ -from cStringIO import StringIO +from __future__ import print_function + +try: + # python2 + from StringIO import StringIO +except ImportError: + from io import StringIO + from datetime import datetime import dateutil.parser import json @@ -156,7 +163,7 @@ class StagingAPI(object): return func(url) except HTTPError as e: if 500 <= e.code <= 599: - print 'Error {}, retrying {} in {}s'.format(e.code, url, retry_sleep_seconds) + print('Error {}, retrying {} in {}s'.format(e.code, url, retry_sleep_seconds)) time.sleep(retry_sleep_seconds) # increase sleep time up to one minute to avoid hammering # the server in case of real problems @@ -1606,10 +1613,10 @@ class StagingAPI(object): u = self.makeurl(['build', prj], query=query) try: - print "tried to trigger rebuild for project '%s' package '%s'" % (prj, pkg) + print("tried to trigger rebuild for project '%s' package '%s'" % (prj, pkg)) http_POST(u) except: - print "could not trigger rebuild for project '%s' package '%s'" % (prj, pkg) + print("could not trigger rebuild for project '%s' package '%s'" % (prj, pkg)) def _candidate_adi_project(self): """Decide a candidate name for an ADI project."""