Skip to content

Commit

Permalink
Merge pull request #1326 from dmach/fix-missing-URLSchemeUnknown-in-o…
Browse files Browse the repository at this point in the history
…ld-urllib3

Fix grabber to work with old urllib3 versions that do not contain URLSchemeUnknown exception
  • Loading branch information
dmach committed May 16, 2023
2 parents d089e87 + 309f106 commit 04fb100
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
9 changes: 7 additions & 2 deletions osc/grabber.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
from urllib.parse import unquote
from urllib.error import URLError

import urllib3.exceptions
try:
from urllib3.exceptions import URLSchemeUnknown
except ImportError:
class URLSchemeUnknown(Exception):
pass

from .core import streamfile

Expand Down Expand Up @@ -39,7 +43,8 @@ def urlgrab(self, url, filename=None, text=None):
try:
self._grabber.urlgrab(mirror, filename, text)
return True
except (HTTPError, URLError, urllib3.exceptions.URLSchemeUnknown) as e:
except (HTTPError, URLError, URLSchemeUnknown, KeyError) as e:
# urllib3 1.25.10 throws a KeyError: pool_key_constructor = self.key_fn_by_scheme[scheme]
# try next mirror
pass

Expand Down
32 changes: 32 additions & 0 deletions tests/test_grabber.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import importlib
import os
import tempfile
import unittest

import osc.conf
import osc.grabber as osc_grabber


class TestMirrorGroup(unittest.TestCase):
def setUp(self):
self.tmpdir = tempfile.mkdtemp(prefix='osc_test')
# reset the global `config` in preparation for running the tests
importlib.reload(osc.conf)
osc.conf.get_config()

def tearDown(self):
# reset the global `config` to avoid impacting tests from other classes
importlib.reload(osc.conf)
try:
shutil.rmtree(self.tmpdir)
except:
pass

def test_invalid_scheme(self):
gr = osc_grabber.OscFileGrabber()
mg = osc_grabber.OscMirrorGroup(gr, ["container://example.com"])
mg.urlgrab(None, os.path.join(self.tmpdir, "file"))


if __name__ == "__main__":
unittest.main()

0 comments on commit 04fb100

Please sign in to comment.