mirror of
https://github.com/valentderah/totk-pc-recomp.git
synced 2026-11-19 04:15:57 +00:00
Download the release's pinned files into a .part file that a rerun resumes, and delete a file whose hash is wrong, so a cut download no longer fails every later run.
This commit is contained in:
1 parent
ec7bb5db0a
commit
8f6664d758
1 file changed
+19
-4
+19
-4
@@ -38,6 +38,7 @@ import subprocess
|
||||
import sys
|
||||
import tarfile
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
import zipfile
|
||||
from pathlib import Path
|
||||
@@ -125,12 +126,26 @@ def fetch(name):
|
||||
if name == 'libvpx-src':
|
||||
path = DL / 'libvpx-1.17.0.tar.gz'
|
||||
if not path.is_file():
|
||||
print(f'download {url}')
|
||||
with urllib.request.urlopen(url) as r, open(path, 'wb') as f:
|
||||
shutil.copyfileobj(r, f)
|
||||
# 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:
|
||||
sys.exit(f'{path}: sha256 {got}, pinned {want}')
|
||||
path.unlink() # so the next run downloads it again instead of failing on the same file
|
||||
sys.exit(f'{path}: sha256 {got}, pinned {want}; deleted, run again to download it anew')
|
||||
return path
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user