Skip to content

Commit 0952c53

Browse files
abncrobinso
authored andcommitted
Update imports
- some import updates to facilitate easy porting to python3 - remove any usage that is not available in python3, eg: urllib2.__version__
1 parent a1b9138 commit 0952c53

5 files changed

Lines changed: 51 additions & 48 deletions

File tree

bin/bugzilla

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ import re
2222
import socket
2323
import sys
2424
import tempfile
25-
import urllib2
26-
import xmlrpclib
25+
26+
from urllib2 import HTTPError
27+
from xmlrpclib import Fault, ProtocolError
2728

2829
import bugzilla
2930

@@ -1169,12 +1170,12 @@ if __name__ == '__main__':
11691170
log.debug("", exc_info=True)
11701171
print("\nConnection lost/failed: %s" % str(e))
11711172
sys.exit(2)
1172-
except (xmlrpclib.Fault, urllib2.HTTPError):
1173+
except (Fault, HTTPError):
11731174
e = sys.exc_info()[1]
11741175
log.debug("", exc_info=True)
11751176
print("\nServer error: %s" % str(e))
11761177
sys.exit(3)
1177-
except xmlrpclib.ProtocolError:
1178+
except ProtocolError:
11781179
e = sys.exc_info()[1]
11791180
log.debug("", exc_info=True)
11801181
print("\nInvalid server response: %d %s" % (e.errcode, e.errmsg))

bugzilla/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
version = __version__
1414

1515
import logging
16-
import xmlrpclib
16+
from xmlrpclib import Fault, ServerProxy
1717

1818
log = logging.getLogger("bugzilla")
1919

@@ -33,7 +33,7 @@ class NovellBugzilla(Bugzilla34):
3333
def getBugzillaClassForURL(url):
3434
url = Bugzilla3.fix_url(url)
3535
log.debug("Detecting subclass for %s", url)
36-
s = xmlrpclib.ServerProxy(url)
36+
s = ServerProxy(url)
3737
rhbz = False
3838
bzversion = ''
3939
c = None
@@ -51,7 +51,7 @@ def getBugzillaClassForURL(url):
5151
extensions = s.Bugzilla.extensions()
5252
if extensions.get('extensions', {}).get('RedHat', False):
5353
rhbz = True
54-
except xmlrpclib.Fault:
54+
except Fault:
5555
pass
5656
log.debug("rhbz=%s", str(rhbz))
5757

@@ -60,7 +60,7 @@ def getBugzillaClassForURL(url):
6060
log.debug("Checking return value of Bugzilla.version()")
6161
r = s.Bugzilla.version()
6262
bzversion = r['version']
63-
except xmlrpclib.Fault:
63+
except Fault:
6464
pass
6565
log.debug("bzversion='%s'", str(bzversion))
6666

bugzilla/base.py

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99
# option) any later version. See http://www.gnu.org/copyleft/gpl.html for
1010
# the full text of the license.
1111

12-
import cookielib
1312
import os
14-
import StringIO
1513
import sys
16-
import urllib2
17-
import urlparse
18-
import xmlrpclib
14+
15+
from ConfigParser import SafeConfigParser
16+
from cookielib import LoadError, LWPCookieJar, MozillaCookieJar
17+
from urllib2 import Request, HTTPError, build_opener
18+
from urlparse import urlparse, parse_qsl
19+
from StringIO import StringIO
20+
from xmlrpclib import Binary, Fault, ProtocolError, ServerProxy, Transport
1921

2022
import pycurl
2123

@@ -75,7 +77,7 @@ def _decode_rfc2231_value(val):
7577

7678

7779
def _build_cookiejar(cookiefile):
78-
cj = cookielib.MozillaCookieJar(cookiefile)
80+
cj = MozillaCookieJar(cookiefile)
7981
if cookiefile is None:
8082
return cj
8183
if not os.path.exists(cookiefile):
@@ -90,17 +92,17 @@ def _build_cookiejar(cookiefile):
9092
try:
9193
cj.load()
9294
return cj
93-
except cookielib.LoadError:
95+
except LoadError:
9496
pass
9597

9698
try:
97-
cj = cookielib.LWPCookieJar(cookiefile)
99+
cj = LWPCookieJar(cookiefile)
98100
cj.load()
99-
except cookielib.LoadError:
101+
except LoadError:
100102
raise BugzillaError("cookiefile=%s not in LWP or Mozilla format" %
101103
cookiefile)
102104

103-
retcj = cookielib.MozillaCookieJar(cookiefile)
105+
retcj = MozillaCookieJar(cookiefile)
104106
for cookie in cj:
105107
retcj.set_cookie(cookie)
106108
retcj.save()
@@ -115,7 +117,7 @@ def _check_http_error(uri, request_body, response_data):
115117
import httplib
116118
import urllib
117119

118-
class FakeSocket(StringIO.StringIO):
120+
class FakeSocket(StringIO):
119121
def makefile(self, *args, **kwarg):
120122
ignore = args
121123
ignore = kwarg
@@ -127,30 +129,30 @@ def makefile(self, *args, **kwarg):
127129
resp.code = httpresp.status
128130
resp.msg = httpresp.reason
129131

130-
req = urllib2.Request(uri)
132+
req = Request(uri)
131133
req.add_data(request_body)
132-
opener = urllib2.build_opener()
134+
opener = build_opener()
133135

134136
for handler in opener.handlers:
135137
if hasattr(handler, "http_response"):
136138
handler.http_response(req, resp)
137-
except urllib2.HTTPError:
139+
except HTTPError:
138140
raise
139141
except:
140142
pass
141143

142144

143-
class _CURLTransport(xmlrpclib.Transport):
145+
class _CURLTransport(Transport):
144146
def __init__(self, url, cookiejar,
145147
sslverify=True, sslcafile=None, debug=0):
146-
if hasattr(xmlrpclib.Transport, "__init__"):
147-
xmlrpclib.Transport.__init__(self, use_datetime=False)
148+
if hasattr(Transport, "__init__"):
149+
Transport.__init__(self, use_datetime=False)
148150

149151
self.verbose = debug
150152

151153
# transport constructor needs full url too, as xmlrpc does not pass
152154
# scheme to request
153-
self.scheme = urlparse.urlparse(url)[0]
155+
self.scheme = urlparse(url)[0]
154156
if self.scheme not in ["http", "https"]:
155157
raise Exception("Invalid URL scheme: %s (%s)" % (self.scheme, url))
156158

@@ -186,8 +188,8 @@ def _open_helper(self, url, request_body):
186188
self.c.setopt(pycurl.URL, url)
187189
self.c.setopt(pycurl.POSTFIELDS, request_body)
188190

189-
b = StringIO.StringIO()
190-
headers = StringIO.StringIO()
191+
b = StringIO()
192+
headers = StringIO()
191193
self.c.setopt(pycurl.WRITEFUNCTION, b.write)
192194
self.c.setopt(pycurl.HEADERFUNCTION, headers.write)
193195

@@ -216,7 +218,7 @@ def _open_helper(self, url, request_body):
216218
raise KeyboardInterrupt
217219
except pycurl.error:
218220
e = sys.exc_info()[1]
219-
raise xmlrpclib.ProtocolError(url, e[0], e[1], None)
221+
raise ProtocolError(url, e[0], e[1], None)
220222

221223
b.seek(0)
222224
headers.seek(0)
@@ -291,12 +293,12 @@ def url_to_query(url):
291293
'''
292294
q = {}
293295
(ignore, ignore, path,
294-
ignore, query, ignore) = urlparse.urlparse(url)
296+
ignore, query, ignore) = urlparse(url)
295297

296298
if os.path.basename(path) not in ('buglist.cgi', 'query.cgi'):
297299
return {}
298300

299-
for (k, v) in urlparse.parse_qsl(query):
301+
for (k, v) in parse_qsl(query):
300302
if k not in q:
301303
q[k] = v
302304
elif isinstance(q[k], list):
@@ -357,9 +359,8 @@ def _init_private_data(self):
357359
self._components_details = {}
358360

359361
def _get_user_agent(self):
360-
ret = ('Python-urllib2/%s bugzilla.py/%s %s/%s' %
361-
(urllib2.__version__, __version__,
362-
str(self.__class__.__name__), self.version))
362+
ret = ('Python-urllib bugzilla.py/%s %s/%s' %
363+
(__version__, str(self.__class__.__name__), self.version))
363364
return ret
364365
user_agent = property(_get_user_agent)
365366

@@ -437,11 +438,10 @@ def readconfig(self, configpath=None):
437438
'''
438439
Read bugzillarc file(s) into memory.
439440
'''
440-
import ConfigParser
441441
if not configpath:
442442
configpath = self.configpath
443443
configpath = [os.path.expanduser(p) for p in configpath]
444-
c = ConfigParser.SafeConfigParser()
444+
c = SafeConfigParser()
445445
r = c.read(configpath)
446446
if not r:
447447
return
@@ -477,7 +477,7 @@ def connect(self, url=None):
477477
self._transport = _CURLTransport(url, self._cookiejar,
478478
sslverify=self._sslverify)
479479
self._transport.user_agent = self.user_agent
480-
self._proxy = xmlrpclib.ServerProxy(url, self._transport)
480+
self._proxy = ServerProxy(url, self._transport)
481481

482482

483483
self.url = url
@@ -533,7 +533,7 @@ def login(self, user=None, password=None):
533533
self.logged_in = True
534534
log.info("login successful - dropping password from memory")
535535
self.password = ''
536-
except xmlrpclib.Fault:
536+
except Fault:
537537
r = False
538538

539539
return r
@@ -1218,7 +1218,7 @@ def _attachment_uri(self, attachid):
12181218
def attachfile(self, idlist, attachfile, description, **kwargs):
12191219
'''
12201220
Attach a file to the given bug IDs. Returns the ID of the attachment
1221-
or raises xmlrpclib.Fault if something goes wrong.
1221+
or raises XMLRPC Fault if something goes wrong.
12221222
12231223
attachfile may be a filename (which will be opened) or a file-like
12241224
object, which must provide a 'read' method. If it's not one of these,
@@ -1259,7 +1259,7 @@ def attachfile(self, idlist, attachfile, description, **kwargs):
12591259
kwargs["file_name"] = kwargs.pop("filename")
12601260

12611261
kwargs['summary'] = description
1262-
kwargs['data'] = xmlrpclib.Binary(f.read())
1262+
kwargs['data'] = Binary(f.read())
12631263
kwargs['ids'] = self._listify(idlist)
12641264

12651265
if 'file_name' not in kwargs and hasattr(f, "name"):
@@ -1290,7 +1290,7 @@ def openattachment(self, attachid):
12901290
att_uri = self._attachment_uri(attachid)
12911291

12921292
headers = {}
1293-
ret = StringIO.StringIO()
1293+
ret = StringIO()
12941294

12951295
def headers_cb(buf):
12961296
if not ":" in buf:
@@ -1440,7 +1440,7 @@ def _getusers(self, ids=None, names=None, match=None):
14401440
:kwarg names: list of user names to return data on
14411441
:kwarg match: list of patterns. Returns users whose real name or
14421442
login name match the pattern.
1443-
:raises xmlrpclib.Fault: Code 51: if a Bad Login Name was sent to the
1443+
:raises XMLRPC Fault: Code 51: if a Bad Login Name was sent to the
14441444
names array.
14451445
Code 304: if the user was not authorized to see user they
14461446
requested.
@@ -1467,7 +1467,7 @@ def getuser(self, username):
14671467
'''Return a bugzilla User for the given username
14681468
14691469
:arg username: The username used in bugzilla.
1470-
:raises xmlrpclib.Fault: Code 51 if the username does not exist
1470+
:raises XMLRPC Fault: Code 51 if the username does not exist
14711471
:returns: User record for the username
14721472
'''
14731473
ret = self.getusers(username)
@@ -1509,7 +1509,7 @@ def createuser(self, email, name='', password=''):
15091509
:arg email: The email address to use in bugzilla
15101510
:kwarg name: Real name to associate with the account
15111511
:kwarg password: Password to set for the bugzilla account
1512-
:raises xmlrpclib.Fault: Code 501 if the username already exists
1512+
:raises XMLRPC Fault: Code 501 if the username already exists
15131513
Code 500 if the email address isn't valid
15141514
Code 502 if the password is too short
15151515
Code 503 if the password is too long

tests/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
import os
99
import shlex
1010
import sys
11-
import StringIO
11+
12+
from StringIO import StringIO
1213

1314

1415
_cleanup = []
@@ -63,7 +64,7 @@ def clicomm(argv, bzinstance, returnmain=False, printcliout=False,
6364
oldargv = sys.argv
6465
try:
6566
if not printcliout:
66-
out = StringIO.StringIO()
67+
out = StringIO()
6768
sys.stdout = out
6869
sys.stderr = out
6970
if stdin:

tests/rw_functional.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
import random
1717
import sys
1818
import unittest
19-
import urllib2
19+
20+
from urlparse import urlparse
2021

2122
import bugzilla
2223
from bugzilla import Bugzilla
@@ -40,7 +41,7 @@ def _testBZClass(self):
4041

4142
def _testCookie(self):
4243
cookiefile = cf
43-
domain = urllib2.urlparse.urlparse(self.url)[1]
44+
domain = urlparse(self.url)[1]
4445
if os.path.exists(cookiefile):
4546
out = open(cookiefile).read(1024)
4647
if domain in out:

0 commit comments

Comments
 (0)