diff --git a/osc/commandline.py b/osc/commandline.py index 3393275a..a99948f4 100644 --- a/osc/commandline.py +++ b/osc/commandline.py @@ -7412,7 +7412,7 @@ Please submit there instead, or use --nodevelproject to force direct submission. entries.update(entries_new) for name in sorted(entries.keys()): if name.startswith('_service:') or name.startswith('_service_'): - continue + continue md5_old = entries_old.get(name, '') md5_new = entries_new.get(name, '') md5_oldpatched = entries_oldpatched.get(name, '') diff --git a/tests/common.py b/tests/common.py index fd1d7dce..38ce9357 100644 --- a/tests/common.py +++ b/tests/common.py @@ -1,7 +1,5 @@ import unittest -import urllib2 import osc.core -import StringIO import shutil import tempfile import os @@ -9,6 +7,13 @@ import sys from xml.etree import cElementTree as ET EXPECTED_REQUESTS = [] +try: + from cStringIO import StringIO + from urllib2 import HTTPHandler, addinfourl, build_opener +except ImportError: + from io import StringIO + from urllib.request import HTTPHandler, addinfourl, build_opener + class RequestWrongOrder(Exception): """raised if an unexpected request is issued to urllib2""" def __init__(self, url, exp_url, method, exp_method): @@ -31,9 +36,9 @@ class RequestDataMismatch(Exception): def __str__(self): return '%s, %s, %s' % (self.url, self.got, self.exp) -class MyHTTPHandler(urllib2.HTTPHandler): +class MyHTTPHandler(HTTPHandler): def __init__(self, exp_requests, fixtures_dir): - urllib2.HTTPHandler.__init__(self) + HTTPHandler.__init__(self) self.__exp_requests = exp_requests self.__fixtures_dir = fixtures_dir @@ -51,9 +56,9 @@ class MyHTTPHandler(urllib2.HTTPHandler): def __mock_PUT(self, req, **kwargs): exp = kwargs.get('exp', None) - if exp is not None and kwargs.has_key('expfile'): + if exp is not None and 'expfile' in kwargs: raise RuntimeError('either specify exp or expfile') - elif kwargs.has_key('expfile'): + elif 'expfile' in kwargs: exp = open(os.path.join(self.__fixtures_dir, kwargs['expfile']), 'r').read() elif exp is None: raise RuntimeError('exp or expfile required') @@ -64,15 +69,15 @@ class MyHTTPHandler(urllib2.HTTPHandler): def __get_response(self, url, **kwargs): f = None - if kwargs.has_key('exception'): + if 'exception' in kwargs: raise kwargs['exception'] - if not kwargs.has_key('text') and kwargs.has_key('file'): - f = StringIO.StringIO(open(os.path.join(self.__fixtures_dir, kwargs['file']), 'r').read()) - elif kwargs.has_key('text') and not kwargs.has_key('file'): - f = StringIO.StringIO(kwargs['text']) + if 'text' not in kwargs and 'file' in kwargs: + f = StringIO(open(os.path.join(self.__fixtures_dir, kwargs['file']), 'r').read()) + elif 'text' in kwargs and 'file' not in kwargs: + f = StringIO(kwargs['text']) else: raise RuntimeError('either specify text or file') - resp = urllib2.addinfourl(f, {}, url) + resp = addinfourl(f, {}, url) resp.code = kwargs.get('code', 200) resp.msg = '' return resp @@ -116,9 +121,9 @@ class OscTestCase(unittest.TestCase): shutil.copytree(os.path.join(self._get_fixtures_dir(), 'osctest'), os.path.join(self.tmpdir, 'osctest')) global EXPECTED_REQUESTS EXPECTED_REQUESTS = [] - osc.core.conf._build_opener = lambda u: urllib2.build_opener(MyHTTPHandler(EXPECTED_REQUESTS, self._get_fixtures_dir())) + osc.core.conf._build_opener = lambda u: build_opener(MyHTTPHandler(EXPECTED_REQUESTS, self._get_fixtures_dir())) self.stdout = sys.stdout - sys.stdout = StringIO.StringIO() + sys.stdout = StringIO() def tearDown(self): self.assertTrue(len(EXPECTED_REQUESTS) == 0) diff --git a/tests/test_prdiff.py b/tests/test_prdiff.py index 22e4a7a8..595b80c7 100644 --- a/tests/test_prdiff.py +++ b/tests/test_prdiff.py @@ -4,7 +4,6 @@ import osc.oscerr import os import re import sys -import urllib2 from common import GET, POST, OscTestCase, addExpectedRequest, EXPECTED_REQUESTS diff --git a/tests/test_repairwc.py b/tests/test_repairwc.py index c239da7f..f2b090b8 100644 --- a/tests/test_repairwc.py +++ b/tests/test_repairwc.py @@ -202,11 +202,14 @@ class TestRepairWC(OscTestCase): def test_invalidapiurl_param(self): """pass an invalid apiurl to wc_repair""" - import urllib2 + try: + from urllib.error import URLError + except ImportError: + from urllib2 import URLError self._change_to_pkg('invalid_apiurl') p = osc.core.Package('.', wc_check=False) - self.assertRaises(urllib2.URLError, p.wc_repair, 'http:/localhost') - self.assertRaises(urllib2.URLError, p.wc_repair, 'invalid') + self.assertRaises(URLError, p.wc_repair, 'http:/localhost') + self.assertRaises(URLError, p.wc_repair, 'invalid') def test_noapiurlNotExistingApiurl(self): """the package wc has no _apiurl file and no apiurl is passed to repairwc""" @@ -245,14 +248,17 @@ class TestRepairWC(OscTestCase): def test_project_invalidapiurl_param(self): """pass an invalid apiurl to wc_repair""" import shutil - import urllib2 + try: + from urllib.error import URLError + except ImportError: + from urllib2 import URLError prj_dir = os.path.join(self.tmpdir, 'prj_invalidapiurl') shutil.copytree(os.path.join(self._get_fixtures_dir(), 'prj_invalidapiurl'), prj_dir) storedir = os.path.join(prj_dir, osc.core.store) self.assertRaises(osc.oscerr.WorkingCopyInconsistent, osc.core.Project, prj_dir, getPackageList=False) prj = osc.core.Project(prj_dir, wc_check=False, getPackageList=False) - self.assertRaises(urllib2.URLError, prj.wc_repair, 'http:/localhost') - self.assertRaises(urllib2.URLError, prj.wc_repair, 'invalid') + self.assertRaises(URLError, prj.wc_repair, 'http:/localhost') + self.assertRaises(URLError, prj.wc_repair, 'invalid') if __name__ == '__main__': import unittest diff --git a/tests/test_setlinkrev.py b/tests/test_setlinkrev.py index 966177e6..828c6c29 100644 --- a/tests/test_setlinkrev.py +++ b/tests/test_setlinkrev.py @@ -50,9 +50,12 @@ class TestSetLinkRev(OscTestCase): @GET('http://localhost/source/srcprj/srcpkg?rev=latest&expand=1', text='conflict in file merge', code=404) def test_linkerror(self): """link is broken""" - import urllib2 + try: + from urllib.error import HTTPError + except ImportError: + from urllib2 import HTTPError # the backend returns status 404 if we try to expand a broken _link - self.assertRaises(urllib2.HTTPError, osc.core.set_link_rev, 'http://localhost', 'osctest', 'simple', expand=True) + self.assertRaises(HTTPError, osc.core.set_link_rev, 'http://localhost', 'osctest', 'simple', expand=True) @GET('http://localhost/source/osctest/simple/_link', file='rev_link') @PUT('http://localhost/source/osctest/simple/_link',