Skip to content

Commit

Permalink
Move md5 check outside of with statement
Browse files Browse the repository at this point in the history
  • Loading branch information
jake authored and jake committed Apr 12, 2024
1 parent 3c6692f commit 7a895b1
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions internetarchive/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,6 @@ def download(# noqa: max-complexity=38
elif return_responses:
return response



if verbose:
total = int(response.headers.get('content-length', 0)) or None
progress_bar = tqdm(desc=f' downloading {self.name}',
Expand All @@ -317,26 +315,24 @@ def download(# noqa: max-complexity=38
for chunk in response.iter_content(chunk_size=chunk_size):
if chunk:
size = fileobj.write(chunk)
fileobj.flush()
if bar is not None:
bar.update(size)
if ors:
fileobj.write(os.environ.get("ORS", "\n").encode("utf-8"))
fileobj.flush()

if 'Range' in headers:
with open(file_path, 'rb') as fh:
local_checksum = utils.get_md5(fh)
try:
assert local_checksum == self.md5
except AssertionError:
msg = (f"\"{file_path}\" corrupt, "
"checksums do not match. "
"Remote file may have been modified, "
"retry download.")
os.remove(file_path.encode('utf-8'))
raise exceptions.InvalidChecksumError(msg)
break
if 'Range' in headers:
with open(file_path, 'rb') as fh:
local_checksum = utils.get_md5(fh)
try:
assert local_checksum == self.md5
except AssertionError:
msg = (f"\"{file_path}\" corrupt, "
"checksums do not match. "
"Remote file may have been modified, "
"retry download.")
os.remove(file_path.encode('utf-8'))
raise exceptions.InvalidChecksumError(msg)
break
except (RetryError, HTTPError, ConnectTimeout, OSError, ReadTimeout,
exceptions.InvalidChecksumError) as exc:
if retries > 0:
Expand Down

0 comments on commit 7a895b1

Please sign in to comment.