1
0
mirror of https://github.com/openSUSE/osc.git synced 2025-01-11 16:36:14 +01:00

python3 compatibility: import proper modules

Some modules (httplib, StringIO, ...) were renamed in python3. This
patch try to import the proper symbols from python3 and then fallback to
python2 in a case ImportError will appear.

There is one exception, python 2.7 got the io module with StringIO, but
it allow unicode arguments only. Therefor the old module is poked before
new one.
This commit is contained in:
Michal Vyskocil 2013-04-09 12:36:42 +02:00 committed by Adrian Schröter
parent c612e8d47e
commit 87d354e1a0
8 changed files with 91 additions and 46 deletions

View File

@ -14,7 +14,11 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
import ConfigParser try:
import configparser
except ImportError:
#python 2.x
import ConfigParser as configparser
import re import re
# inspired from http://code.google.com/p/iniparse/ - although their implementation is # inspired from http://code.google.com/p/iniparse/ - although their implementation is
@ -106,9 +110,9 @@ class SectionLine(Line):
def _add_option(self, optname, value = None, line = None, sep = '='): def _add_option(self, optname, value = None, line = None, sep = '='):
if value is None and line is None: if value is None and line is None:
raise ConfigParser.Error('Either value or line must be passed in') raise configparser.Error('Either value or line must be passed in')
elif value and line: elif value and line:
raise ConfigParser.Error('value and line are mutually exclusive') raise configparser.Error('value and line are mutually exclusive')
if value is not None: if value is not None:
line = '%s%s%s' % (optname, sep, value) line = '%s%s%s' % (optname, sep, value)
@ -182,7 +186,7 @@ class OptionLine(Line):
self.format(line) self.format(line)
def format(self, line): def format(self, line):
mo = ConfigParser.ConfigParser.OPTCRE.match(line.strip()) mo = configparser.ConfigParser.OPTCRE.match(line.strip())
key, val = mo.group('option', 'value') key, val = mo.group('option', 'value')
self.frmt = line.replace(key.strip(), '%s', 1) self.frmt = line.replace(key.strip(), '%s', 1)
pos = val.find(' ;') pos = val.find(' ;')
@ -195,7 +199,7 @@ class OptionLine(Line):
return self.value return self.value
class OscConfigParser(ConfigParser.SafeConfigParser): class OscConfigParser(configparser.SafeConfigParser):
""" """
OscConfigParser() behaves like a normal ConfigParser() object. The OscConfigParser() behaves like a normal ConfigParser() object. The
only differences is that it preserves the order+format of configuration entries only differences is that it preserves the order+format of configuration entries
@ -204,7 +208,7 @@ class OscConfigParser(ConfigParser.SafeConfigParser):
class. class.
""" """
def __init__(self, defaults={}): def __init__(self, defaults={}):
ConfigParser.SafeConfigParser.__init__(self, defaults) configparser.SafeConfigParser.__init__(self, defaults)
self._sections = ConfigLineOrder() self._sections = ConfigLineOrder()
# XXX: unfortunately we have to override the _read() method from the ConfigParser() # XXX: unfortunately we have to override the _read() method from the ConfigParser()
@ -245,7 +249,7 @@ class OscConfigParser(ConfigParser.SafeConfigParser):
if value: if value:
#cursect[optname] = "%s\n%s" % (cursect[optname], value) #cursect[optname] = "%s\n%s" % (cursect[optname], value)
#self.set(cursect, optname, "%s\n%s" % (self.get(cursect, optname), value)) #self.set(cursect, optname, "%s\n%s" % (self.get(cursect, optname), value))
if cursect == ConfigParser.DEFAULTSECT: if cursect == configparser.DEFAULTSECT:
self._defaults[optname] = "%s\n%s" % (self._defaults[optname], value) self._defaults[optname] = "%s\n%s" % (self._defaults[optname], value)
else: else:
# use the raw value here (original version uses raw=False) # use the raw value here (original version uses raw=False)
@ -258,7 +262,7 @@ class OscConfigParser(ConfigParser.SafeConfigParser):
sectname = mo.group('header') sectname = mo.group('header')
if sectname in self._sections: if sectname in self._sections:
cursect = self._sections[sectname] cursect = self._sections[sectname]
elif sectname == ConfigParser.DEFAULTSECT: elif sectname == configparser.DEFAULTSECT:
cursect = self._defaults cursect = self._defaults
else: else:
#cursect = {'__name__': sectname} #cursect = {'__name__': sectname}
@ -270,7 +274,7 @@ class OscConfigParser(ConfigParser.SafeConfigParser):
optname = None optname = None
# no section header in the file? # no section header in the file?
elif cursect is None: elif cursect is None:
raise ConfigParser.MissingSectionHeaderError(fpname, lineno, line) raise configparser.MissingSectionHeaderError(fpname, lineno, line)
# an option line? # an option line?
else: else:
mo = self.OPTCRE.match(line) mo = self.OPTCRE.match(line)
@ -287,7 +291,7 @@ class OscConfigParser(ConfigParser.SafeConfigParser):
if optval == '""': if optval == '""':
optval = '' optval = ''
optname = self.optionxform(optname.rstrip()) optname = self.optionxform(optname.rstrip())
if cursect == ConfigParser.DEFAULTSECT: if cursect == configparser.DEFAULTSECT:
self._defaults[optname] = optval self._defaults[optname] = optval
else: else:
self._sections[cursect]._add_option(optname, line=line) self._sections[cursect]._add_option(optname, line=line)
@ -297,7 +301,7 @@ class OscConfigParser(ConfigParser.SafeConfigParser):
# raised at the end of the file and will contain a # raised at the end of the file and will contain a
# list of all bogus lines # list of all bogus lines
if not e: if not e:
e = ConfigParser.ParsingError(fpname) e = configparser.ParsingError(fpname)
e.append(lineno, repr(line)) e.append(lineno, repr(line))
# if any parsing errors occurred, raise an exception # if any parsing errors occurred, raise an exception
if e: if e:
@ -313,7 +317,7 @@ class OscConfigParser(ConfigParser.SafeConfigParser):
fp.write(str(self)) fp.write(str(self))
fp.write('\n') fp.write('\n')
else: else:
ConfigParser.SafeConfigParser.write(self, fp) configparser.SafeConfigParser.write(self, fp)
# XXX: simplify! # XXX: simplify!
def __str__(self): def __str__(self):

View File

@ -29,7 +29,11 @@ except:
# if rpm-python isn't installed (we might be on a debian system): # if rpm-python isn't installed (we might be on a debian system):
RPMError = None RPMError = None
from httplib import HTTPException, BadStatusLine try:
from http.client import HTTPException, BadStatusLine
except ImportError:
#python 2.x
from httplib import HTTPException, BadStatusLine
from urllib2 import URLError, HTTPError from urllib2 import URLError, HTTPError
# the good things are stolen from Matt Mackall's mercurial # the good things are stolen from Matt Mackall's mercurial

View File

@ -9,7 +9,11 @@ import os
import re import re
import sys import sys
import shutil import shutil
import urlparse try:
from urllib.parse import urlsplit
except ImportError:
#python 2.x
from urlparse import urlsplit
from tempfile import NamedTemporaryFile, mkdtemp from tempfile import NamedTemporaryFile, mkdtemp
from osc.fetch import * from osc.fetch import *
from osc.core import get_buildinfo, store_read_apiurl, store_read_project, store_read_package, meta_exists, quote_plus, get_buildconfig, is_package_dir from osc.core import get_buildinfo, store_read_apiurl, store_read_project, store_read_package, meta_exists, quote_plus, get_buildconfig, is_package_dir
@ -491,7 +495,7 @@ def main(apiurl, opts, argv):
opts.local_package = True opts.local_package = True
if opts.local_package: if opts.local_package:
pacname = os.path.splitext(build_descr)[0] pacname = os.path.splitext(build_descr)[0]
apihost = urlparse.urlsplit(apiurl)[1] apihost = urlsplit(apiurl)[1]
if not build_root: if not build_root:
try: try:
build_root = config['build-root'] % {'repo': repo, 'arch': arch, build_root = config['build-root'] % {'repo': repo, 'arch': arch,

View File

@ -9,7 +9,11 @@ from . import conf
from . import oscerr from . import oscerr
import sys import sys
import time import time
import urlparse try:
from urllib.parse import urlsplit
except ImportError:
#python 2.x
from urlparse import urlsplit
from optparse import SUPPRESS_HELP from optparse import SUPPRESS_HELP
@ -5552,7 +5556,7 @@ Please submit there instead, or use --nodevelproject to force direct submission.
package = os.path.splitext(descr)[0] package = os.path.splitext(descr)[0]
else: else:
package = store_read_package('.') package = store_read_package('.')
apihost = urlparse.urlsplit(self.get_api_url())[1] apihost = urlsplit(self.get_api_url())[1]
buildroot = os.environ.get('OSC_BUILD_ROOT', conf.config['build-root']) \ buildroot = os.environ.get('OSC_BUILD_ROOT', conf.config['build-root']) \
% {'repo': repository, 'arch': arch, 'project': project, 'package': package, 'apihost': apihost} % {'repo': repository, 'arch': arch, 'project': project, 'package': package, 'apihost': apihost}
if not os.path.isdir(buildroot): if not os.path.isdir(buildroot):

View File

@ -35,15 +35,23 @@ The configuration dictionary could look like this:
""" """
import base64 import base64
import cookielib
import httplib
import os import os
import re import re
import sys import sys
import StringIO
import urllib import urllib
import urllib2 import urllib2
import urlparse
try:
from http.cookiejar import LWPCookieJar, CookieJar
from http.client import HTTPConnection, HTTPResponse
from io import StringIO
from urllib.parse import urlsplit
except ImportError:
#python 2.x
from cookielib import LWPCookieJar, CookieJar
from httplib import HTTPConnection, HTTPResponse
from StringIO import StringIO
from urlparse import urlsplit
from . import OscConfigParser from . import OscConfigParser
from osc import oscerr from osc import oscerr
@ -358,10 +366,10 @@ cookiejar = None
def parse_apisrv_url(scheme, apisrv): def parse_apisrv_url(scheme, apisrv):
if apisrv.startswith('http://') or apisrv.startswith('https://'): if apisrv.startswith('http://') or apisrv.startswith('https://'):
return urlparse.urlsplit(apisrv)[0:2] return urlsplit(apisrv)[0:2]
elif scheme != None: elif scheme != None:
# the split/join is needed to get a proper url (e.g. without a trailing slash) # the split/join is needed to get a proper url (e.g. without a trailing slash)
return urlparse.urlsplit(urljoin(scheme, apisrv))[0:2] return urlsplit(urljoin(scheme, apisrv))[0:2]
else: else:
msg = 'invalid apiurl \'%s\' (specify the protocol (http:// or https://))' % apisrv msg = 'invalid apiurl \'%s\' (specify the protocol (http:// or https://))' % apisrv
raise urllib2.URLError(msg) raise urllib2.URLError(msg)
@ -518,7 +526,7 @@ def init_basicauth(config):
# a logger object or such # a logger object or such
def new_method(*args, **kwargs): def new_method(*args, **kwargs):
stdout = sys.stdout stdout = sys.stdout
sys.stdout = StringIO.StringIO() sys.stdout = StringIO()
meth(*args, **kwargs) meth(*args, **kwargs)
hdr = sys.stdout.getvalue() hdr = sys.stdout.getvalue()
sys.stdout = stdout sys.stdout = stdout
@ -532,8 +540,8 @@ def init_basicauth(config):
return new_method return new_method
if config['http_debug'] and not config['http_full_debug']: if config['http_debug'] and not config['http_full_debug']:
httplib.HTTPConnection.send = filterhdrs(httplib.HTTPConnection.send, True, 'Cookie', 'Authorization') HTTPConnection.send = filterhdrs(HTTPConnection.send, True, 'Cookie', 'Authorization')
httplib.HTTPResponse.begin = filterhdrs(httplib.HTTPResponse.begin, False, 'header: Set-Cookie.*\n') HTTPResponse.begin = filterhdrs(HTTPResponse.begin, False, 'header: Set-Cookie.*\n')
if sys.version_info < (2, 6): if sys.version_info < (2, 6):
# HTTPS proxy is not supported in old urllib2. It only leads to an error # HTTPS proxy is not supported in old urllib2. It only leads to an error
@ -551,7 +559,7 @@ def init_basicauth(config):
cookie_file = os.path.expanduser(config['cookiejar']) cookie_file = os.path.expanduser(config['cookiejar'])
global cookiejar global cookiejar
cookiejar = cookielib.LWPCookieJar(cookie_file) cookiejar = LWPCookieJar(cookie_file)
try: try:
cookiejar.load(ignore_discard=True) cookiejar.load(ignore_discard=True)
except IOError: except IOError:
@ -560,7 +568,7 @@ def init_basicauth(config):
os.chmod(cookie_file, 0600) os.chmod(cookie_file, 0600)
except: except:
#print 'Unable to create cookiejar file: \'%s\'. Using RAM-based cookies.' % cookie_file #print 'Unable to create cookiejar file: \'%s\'. Using RAM-based cookies.' % cookie_file
cookiejar = cookielib.CookieJar() cookiejar = CookieJar()
def get_configParser(conffile=None, force_read=False): def get_configParser(conffile=None, force_read=False):
@ -682,7 +690,7 @@ def write_initial_config(conffile, entries, custom_template=''):
else: else:
config['passx'] = base64.b64encode(config['pass'].encode('bz2')) config['passx'] = base64.b64encode(config['pass'].encode('bz2'))
sio = StringIO.StringIO(conf_template.strip() % config) sio = StringIO(conf_template.strip() % config)
cp = OscConfigParser.OscConfigParser(DEFAULTS) cp = OscConfigParser.OscConfigParser(DEFAULTS)
cp.readfp(sio) cp.readfp(sio)
write_config(conffile, cp) write_config(conffile, cp)

View File

@ -16,11 +16,14 @@ import os.path
import sys import sys
import urllib2 import urllib2
from urllib import pathname2url, quote_plus, urlencode, unquote from urllib import pathname2url, quote_plus, urlencode, unquote
from urlparse import urlsplit, urlunsplit try:
from cStringIO import StringIO from urllib.parse import urlsplit, urlunsplit, urlparse
from io import StringIO
except ImportError:
#python 2.x
from urlparse import urlsplit, urlunsplit, urlparse
from cStringIO import StringIO
import shutil import shutil
from . import oscerr
from . import conf
import subprocess import subprocess
import re import re
import socket import socket
@ -30,6 +33,9 @@ try:
except ImportError: except ImportError:
import cElementTree as ET import cElementTree as ET
from . import oscerr
from . import conf
# python 2.6 don't have memoryview # python 2.6 don't have memoryview
try: try:
memoryview memoryview
@ -298,7 +304,6 @@ class Serviceinfo:
def addDownloadUrl(self, serviceinfo_node, url_string): def addDownloadUrl(self, serviceinfo_node, url_string):
from urlparse import urlparse
url = urlparse( url_string ) url = urlparse( url_string )
protocol = url.scheme protocol = url.scheme
host = url.netloc host = url.netloc
@ -5164,8 +5169,7 @@ def streamfile(url, http_meth = http_GET, bufsize=8192, data=None, progress_obj=
cl = int(cl) cl = int(cl)
if progress_obj: if progress_obj:
import urlparse basename = os.path.basename(urlsplit(url)[2])
basename = os.path.basename(urlparse.urlsplit(url)[2])
progress_obj.start(basename=basename, text=text, size=cl) progress_obj.start(basename=basename, text=text, size=cl)
data = f.read(bufsize) data = f.read(bufsize)
read = len(data) read = len(data)
@ -6546,9 +6550,14 @@ def get_user_projpkgs(apiurl, user, role=None, exclude_projects=[], proj=True, p
return res return res
def raw_input(*args): def raw_input(*args):
import __builtin__
try: try:
return __builtin__.raw_input(*args) import builtins
except ImportError:
#python 2.7
import __builtin__ as builtins
try:
return builtins.raw_input(*args)
except EOFError: except EOFError:
# interpret ctrl-d as user abort # interpret ctrl-d as user abort
raise oscerr.UserAbort() raise oscerr.UserAbort()

View File

@ -7,12 +7,18 @@ import M2Crypto.httpslib
from M2Crypto.SSL.Checker import SSLVerificationError from M2Crypto.SSL.Checker import SSLVerificationError
from M2Crypto import m2, SSL from M2Crypto import m2, SSL
import M2Crypto.m2urllib2 import M2Crypto.m2urllib2
import urlparse
import socket import socket
import urllib import urllib
import httplib
import sys import sys
try:
from urllib.parse import urlparse
from http.client import HTTPSConnection
except ImportError:
#python 2.x
from urlparse import urlparse
from httplib import HTTPSConnection
class TrustedCertStore: class TrustedCertStore:
_tmptrusted = {} _tmptrusted = {}
@ -182,7 +188,7 @@ class myHTTPSHandler(M2Crypto.m2urllib2.HTTPSHandler):
# Our change: Check to see if we're using a proxy. # Our change: Check to see if we're using a proxy.
# Then create an appropriate ssl-aware connection. # Then create an appropriate ssl-aware connection.
full_url = req.get_full_url() full_url = req.get_full_url()
target_host = urlparse.urlparse(full_url)[1] target_host = urlparse(full_url)[1]
if (target_host != host): if (target_host != host):
h = myProxyHTTPSConnection(host = host, appname = self.appname, ssl_context = self.ctx) h = myProxyHTTPSConnection(host = host, appname = self.appname, ssl_context = self.ctx)
@ -249,7 +255,7 @@ class myHTTPSConnection(M2Crypto.httpslib.HTTPSConnection):
def getPort(self): def getPort(self):
return self.port return self.port
class myProxyHTTPSConnection(M2Crypto.httpslib.ProxyHTTPSConnection, httplib.HTTPSConnection): class myProxyHTTPSConnection(M2Crypto.httpslib.ProxyHTTPSConnection, HTTPSConnection):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.appname = kwargs.pop('appname', 'generic') self.appname = kwargs.pop('appname', 'generic')
M2Crypto.httpslib.ProxyHTTPSConnection.__init__(self, *args, **kwargs) M2Crypto.httpslib.ProxyHTTPSConnection.__init__(self, *args, **kwargs)
@ -261,7 +267,7 @@ class myProxyHTTPSConnection(M2Crypto.httpslib.ProxyHTTPSConnection, httplib.HTT
def endheaders(self, *args, **kwargs): def endheaders(self, *args, **kwargs):
if self._proxy_auth is None: if self._proxy_auth is None:
self._proxy_auth = self._encode_auth() self._proxy_auth = self._encode_auth()
httplib.HTTPSConnection.endheaders(self, *args, **kwargs) HTTPSConnection.endheaders(self, *args, **kwargs)
# broken in m2crypto: port needs to be an int # broken in m2crypto: port needs to be an int
def putrequest(self, method, url, skip_host=0, skip_accept_encoding=0): def putrequest(self, method, url, skip_host=0, skip_accept_encoding=0):

View File

@ -16,9 +16,15 @@
import os import os
import re import re
import sys import sys
import StringIO
import stat import stat
#XXX: python 2.7 contains io.StringIO, which needs unicode instead of str
#therefor try to import old stuff before new one here
try:
from cStringIO import StringIO
except ImportError:
from io import StringIO
# workaround for python24 # workaround for python24
if not hasattr(os, 'SEEK_SET'): if not hasattr(os, 'SEEK_SET'):
os.SEEK_SET = 0 os.SEEK_SET = 0
@ -49,10 +55,10 @@ class ArHdr:
def __str__(self): def __str__(self):
return '%16s %d' % (self.file, self.size) return '%16s %d' % (self.file, self.size)
class ArFile(StringIO.StringIO): class ArFile(StringIO):
"""Represents a file which resides in the archive""" """Represents a file which resides in the archive"""
def __init__(self, fn, uid, gid, mode, buf): def __init__(self, fn, uid, gid, mode, buf):
StringIO.StringIO.__init__(self, buf) StringIO.__init__(self, buf)
self.name = fn self.name = fn
self.uid = uid self.uid = uid
self.gid = gid self.gid = gid