mirror of
https://git.launchpad.net/ubuntu-dev-tools
synced 2025-03-16 01:21:07 +00:00
misc: download to tmp file, to avoid leftover 0-size file on error
This commit is contained in:
parent
a1b56ac31f
commit
a3ff68be5a
@ -327,14 +327,17 @@ def download(src, dst, size=0):
|
|||||||
(src, username, password) = extract_authentication(src)
|
(src, username, password) = extract_authentication(src)
|
||||||
auth = (username, password) if username or password else None
|
auth = (username, password) if username or password else None
|
||||||
|
|
||||||
try:
|
with tempfile.TemporaryDirectory() as d:
|
||||||
with requests.get(src, stream=True, auth=auth) as fsrc, dst.open('wb') as fdst:
|
tmpdst = Path(d) / 'dst'
|
||||||
fsrc.raise_for_status()
|
try:
|
||||||
_download(fsrc, fdst, size)
|
with requests.get(src, stream=True, auth=auth) as fsrc, tmpdst.open('wb') as fdst:
|
||||||
except requests.RequestException as e:
|
fsrc.raise_for_status()
|
||||||
if e.response and e.response.status_code == 404:
|
_download(fsrc, fdst, size)
|
||||||
raise NotFoundError(f'URL {src} not found') from e
|
except requests.RequestException as e:
|
||||||
raise DownloadError(f'Could not download {src} to {dst}') from e
|
if e.response and e.response.status_code == 404:
|
||||||
|
raise NotFoundError(f'URL {src} not found') from e
|
||||||
|
raise DownloadError(f'Could not download {src} to {dst}') from e
|
||||||
|
shutil.move(tmpdst, dst)
|
||||||
|
|
||||||
|
|
||||||
def _download(fsrc, fdst, size):
|
def _download(fsrc, fdst, size):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user