#58 Better handling for two error conditions
Merged 5 years ago by zbyszek. Opened 5 years ago by zbyszek.
fedora-rust/ zbyszek/rust2rpm two-cleanups  into  master

file modified
+13 -1
@@ -83,6 +83,14 @@ 

      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 @@ 

          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)
@@ -169,6 +178,9 @@ 

          if crate.endswith(".crate"):

              cratef, crate, version = local_crate(crate, version)

          else:

+             if store:

+                 raise ValueError('--store-crate can only be used for a crate')

+ 

              toml, crate, version = local_toml(crate, version)

              diff = make_patch(toml, enabled=patch, tmpfile=True)

              metadata = Metadata.from_file(toml)

no initial comment

why not open_with_removal_on_error or smth like that which would combine 2 functions?

Dunno, just to keep concerns separate and add a minimal implementation which does the job?

rebased onto d5d78d9

5 years ago

Please rebase and merge ;)

rebased onto 2f12c83

5 years ago

Pull-Request has been merged by zbyszek

5 years ago
Metadata