PR#1854: do not use with statement with requests.get

Merges #1854
https://pagure.io/koji/pull-request/1854

Fixes: #1530
https://pagure.io/koji/issue/1530
Replace urllib.request with requests library
This commit is contained in:
Tomas Kopecek 2019-12-23 10:00:28 +01:00
commit 0c56cad2b5
2 changed files with 8 additions and 2 deletions

View file

@ -1676,9 +1676,12 @@ def openRemoteFile(relpath, topurl=None, topdir=None, tempdir=None):
if topurl:
url = "%s/%s" % (topurl, relpath)
fo = tempfile.TemporaryFile(dir=tempdir)
with requests.get(url) as resp:
try:
resp = requests.get(url)
for chunk in resp.iter_content(chunk_size=8192):
fo.write(chunk)
finally:
resp.close()
fo.seek(0)
elif topdir:
fn = "%s/%s" % (topdir, relpath)

View file

@ -478,12 +478,15 @@ class BaseTaskHandler(object):
return fn
self.logger.debug("Downloading %s", relpath)
url = "%s/%s" % (self.options.topurl, relpath)
with requests.get(url) as resp:
try:
resp = requests.get(url)
if not os.path.exists(os.path.dirname(fn)):
os.makedirs(os.path.dirname(fn))
with open(fn, 'wb') as fdst:
for chunk in resp.iter_content(chunk_size=8192):
fdst.write(chunk)
finally:
resp.close()
else:
fn = "%s/%s" % (self.options.topdir, relpath)
return fn