Remove half-downloaded crate on ^C
Subsequent invocations would fail with an error about a corrupted file. We don't have support for resuming a failed download, so let's remove the partial download results.
This commit is contained in:
parent
e6e9cbbb71
commit
2f12c83d14
1 changed files with 10 additions and 1 deletions
|
@ -83,6 +83,14 @@ def file_mtime(path):
|
|||
t = datetime.fromtimestamp(os.stat(path).st_mtime, timezone.utc)
|
||||
return t.astimezone().isoformat()
|
||||
|
||||
@contextlib.contextmanager
|
||||
def remove_on_error(path):
|
||||
try:
|
||||
yield
|
||||
except: # this is supposed to include ^C
|
||||
os.unlink(path)
|
||||
raise
|
||||
|
||||
def local_toml(toml, version):
|
||||
if os.path.isdir(toml):
|
||||
toml = os.path.join(toml, "Cargo.toml")
|
||||
|
@ -110,7 +118,8 @@ def download(crate, version):
|
|||
req = requests.get(url, stream=True)
|
||||
req.raise_for_status()
|
||||
total = int(req.headers["Content-Length"])
|
||||
with open(cratef, "wb") as f:
|
||||
with remove_on_error(cratef), \
|
||||
open(cratef, "wb") as f:
|
||||
for chunk in tqdm.tqdm(req.iter_content(), "Downloading {}".format(cratef_base),
|
||||
total=total, unit="B", unit_scale=True):
|
||||
f.write(chunk)
|
||||
|
|
Loading…
Reference in a new issue