You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
rust-packaging/0005-Remove-half-downloaded...

45 lines
1.6 KiB

From 2f12c83d14afe71e9efed2d1be62e1e610e602e9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
Date: Fri, 17 Aug 2018 10:03:48 +0200
Subject: [PATCH 05/18] 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.
---
rust2rpm/__main__.py | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/rust2rpm/__main__.py b/rust2rpm/__main__.py
index e993e7b..8e6f6eb 100644
--- a/rust2rpm/__main__.py
+++ b/rust2rpm/__main__.py
@@ -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)
--
2.21.0