always delete incompletely downloaded files

This commit is contained in:
Mike Fährmann
2017-03-21 15:53:43 +01:00
parent 4e7661ab01
commit 0b5076815d
2 changed files with 15 additions and 17 deletions

View File

@@ -19,16 +19,14 @@ class BasicDownloader():
def download(self, url, pathfmt): def download(self, url, pathfmt):
"""Download the resource at 'url' and write it to a file-like object""" """Download the resource at 'url' and write it to a file-like object"""
try: try:
return self.download_impl(url, pathfmt) self.download_impl(url, pathfmt)
except: finally:
# remove file if download failed # remove file from incomplete downloads
try: if self.downloading:
if self.downloading: try:
os.unlink(pathfmt.realpath) os.remove(pathfmt.realpath)
except (AttributeError, FileNotFoundError): except (OSError, AttributeError):
pass pass
raise
def download_impl(self, url, file_handle): def download_impl(self, url, file_handle):
"""Actual implementaion of the download process""" """Actual implementaion of the download process"""
pass

View File

@@ -72,14 +72,14 @@ class Downloader(BasicDownloader):
# everything ok -- proceed to download # everything ok -- proceed to download
self.out.start(pathfmt.path) self.out.start(pathfmt.path)
self.downloading = True self.downloading = True
with pathfmt.open() as file: try:
try: with pathfmt.open() as file:
for data in response.iter_content(None): for data in response.iter_content(16384):
file.write(data) file.write(data)
except rexcepts.RequestException as exception: except rexcepts.RequestException as exception:
msg = exception msg = exception
response.close() response.close()
continue continue
self.downloading = False self.downloading = False
self.out.success(pathfmt.path, tries) self.out.success(pathfmt.path, tries)
return return