mirror of
https://git.launchpad.net/ubuntu-dev-tools
synced 2025-03-12 23:51:08 +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)
|
||||
auth = (username, password) if username or password else None
|
||||
|
||||
try:
|
||||
with requests.get(src, stream=True, auth=auth) as fsrc, dst.open('wb') as fdst:
|
||||
fsrc.raise_for_status()
|
||||
_download(fsrc, fdst, size)
|
||||
except requests.RequestException as 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
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
tmpdst = Path(d) / 'dst'
|
||||
try:
|
||||
with requests.get(src, stream=True, auth=auth) as fsrc, tmpdst.open('wb') as fdst:
|
||||
fsrc.raise_for_status()
|
||||
_download(fsrc, fdst, size)
|
||||
except requests.RequestException as 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):
|
||||
|
Loading…
x
Reference in New Issue
Block a user