Download a cached release file again when its hash no longer matches its pin, in the same run, instead of stopping.

This commit is contained in:
Wellington Dias committed 2026-10-31 12:40:55 -03:00
1 parent e8f56106de
commit 522c69e951
1 file changed
+22 -18
+22 -18
View File
@@ -125,26 +125,30 @@ def fetch(name):
path = DL / Path(url.split('?')[0]).name.replace('%7E', '~')
if name == 'libvpx-src':
path = DL / 'libvpx-1.17.0.tar.gz'
if not path.is_file():
# into <name>.part, renamed once complete: a cut connection leaves a .part that the next run resumes, never a
# short file under the final name
part = path.with_name(path.name + '.part')
have = part.stat().st_size if part.is_file() else 0
print(f'download {url}' + (f' (resuming at {have} bytes)' if have else ''))
req = urllib.request.Request(url, headers={'Range': f'bytes={have}-'} if have else {})
try:
with urllib.request.urlopen(req) as r:
if have and r.status != 206: # the server ignored the range: start over
have = 0
with open(part, 'ab' if have else 'wb') as f:
shutil.copyfileobj(r, f)
except urllib.error.HTTPError as e:
if e.code != 416: # 416: the .part already holds the whole file
raise
part.replace(path)
if path.is_file():
if sha256_file(path) == want:
return path
print(f'{path}: not the pinned file (a cut download or an older pin); downloading it again')
path.unlink()
# into <name>.part, renamed once complete: a cut connection leaves a .part that the next run resumes, never a
# short file under the final name
part = path.with_name(path.name + '.part')
have = part.stat().st_size if part.is_file() else 0
print(f'download {url}' + (f' (resuming at {have} bytes)' if have else ''))
req = urllib.request.Request(url, headers={'Range': f'bytes={have}-'} if have else {})
try:
with urllib.request.urlopen(req) as r:
if have and r.status != 206: # the server ignored the range: start over
have = 0
with open(part, 'ab' if have else 'wb') as f:
shutil.copyfileobj(r, f)
except urllib.error.HTTPError as e:
if e.code != 416: # 416: the .part already holds the whole file
raise
part.replace(path)
got = sha256_file(path)
if got != want:
path.unlink() # so the next run downloads it again instead of failing on the same file
path.unlink() # a .part resumed from another upload, or the URL now serves another file: start clean next run
sys.exit(f'{path}: sha256 {got}, pinned {want}; deleted, run again to download it anew')
return path