+ try:
+ r = requests.head(srcurl, allow_redirects=True, timeout=60)
+ if r.status_code == 200:
+ content_length = int(r.headers.get('content-length'))
+ else:
+ content_length = local_length # skip the download
+ except requests.exceptions.RequestException as e:
+ content_length = local_length # skip the download
+ print(e)
+
+ if local_length == content_length:
+ download = False
+ elif local_length > content_length:
+ print('deleting corrupt file from cache: ' + local_filename)
+ os.remove(local_filename)
+ print("Downloading " + filename + " to cache")
+ elif local_length > -1 and local_length < content_length:
+ print("Resuming download of " + local_filename)
+ resume_header = {'Range': 'bytes=%d-%d' % (local_length, content_length)}
+ else:
+ print("Downloading " + filename + " to cache")
+
+ if download:
+ r = requests.get(srcurl, headers=resume_header,
+ stream=True, verify=False, allow_redirects=True)
+ content_length = int(r.headers.get('content-length'))
+ with open(local_filename, 'ab') as f:
+ for chunk in progress.bar(r.iter_content(chunk_size=1024),
+ expected_size=(content_length / 1024) + 1):
+ if chunk: # filter out keep-alive new chunks
+ f.write(chunk)
+
+ v = sha256_for_file(local_filename)
+ if v == shasum:
+ print("\t...shasum verified for " + local_filename)
+ else:
+ print("Invalid shasum of '" + v + "' detected for " + local_filename)
+ sys.exit(1)