Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions testdata/dnn/download_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ def verify(self):
break
sha.update(buf)
print(' actual {}'.format(sha.hexdigest()))
return self.sha == sha.hexdigest()
self.sha_actual = sha.hexdigest()
return self.sha == self.sha_actual
except Exception as e:
print(' catch {}'.format(e))

Expand Down Expand Up @@ -82,7 +83,10 @@ def get(self):

print(' done')
print(' file {}'.format(self.filename))
return self.verify()
candidate_verify = self.verify()
if not candidate_verify:
self.handle_bad_download()
return candidate_verify

def download(self):
try:
Expand Down Expand Up @@ -112,6 +116,27 @@ def save(self, r):
print('>', end='')
sys.stdout.flush()

def handle_bad_download(self):
if os.path.exists(self.filename):
# rename file for further investigation
try:
# NB: using `self.sha_actual` may create unbounded number of files
rename_target = self.filename + '.invalid'
# TODO: use os.replace (Python 3.3+)
try:
if os.path.exists(rename_target): # avoid FileExistsError on Windows from os.rename()
os.remove(rename_target)
finally:
os.rename(self.filename, rename_target)
print(' renaming invalid file to ' + rename_target)
except:
import traceback
traceback.print_exc()
finally:
if os.path.exists(self.filename):
print(' deleting invalid file')
os.remove(self.filename)


def GDrive(gid):
def download_gdrive(dst):
Expand Down