Skip to content

Commit

Permalink
Merge pull request #1846 from nanonyme/nanonyme/download
Browse files Browse the repository at this point in the history
Raise ContentTooShortError if we don't get enough bytes
  • Loading branch information
abderrahim authored Jul 14, 2023
2 parents 899dbba + 1993f0b commit 52d0899
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/buildstream/downloadablefilesource.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,18 @@ def _download_file(opener_creator, url, etag, directory):
return None, None, None

etag = info["ETag"]
length = info.get("Content-Length")

filename = info.get_filename(default_name)
filename = os.path.basename(filename)
local_file = os.path.join(directory, filename)
with open(local_file, "wb") as dest:
shutil.copyfileobj(response, dest)

actual_length = dest.tell()
if length and actual_length < int(length):
raise ValueError(f"Partial file {actual_length}/{length}")

except urllib.error.HTTPError as e:
if e.code == 304:
# 304 Not Modified.
Expand All @@ -122,7 +127,7 @@ def _download_file(opener_creator, url, etag, directory):
return None, None, None

return None, None, str(e)
except (urllib.error.URLError, urllib.error.ContentTooShortError, OSError, ValueError) as e:
except (urllib.error.URLError, OSError, ValueError) as e:
# Note that urllib.request.Request in the try block may throw a
# ValueError for unknown url types, so we handle it here.
return None, None, str(e)
Expand Down

0 comments on commit 52d0899

Please sign in to comment.