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):
"""Download the resource at 'url' and write it to a file-like object"""
try:
return self.download_impl(url, pathfmt)
except:
# remove file if download failed
try:
if self.downloading:
os.unlink(pathfmt.realpath)
except (AttributeError, FileNotFoundError):
pass
raise
self.download_impl(url, pathfmt)
finally:
# remove file from incomplete downloads
if self.downloading:
try:
os.remove(pathfmt.realpath)
except (OSError, AttributeError):
pass
def download_impl(self, url, file_handle):
"""Actual implementaion of the download process"""
pass

View File

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