Print error when download is interrupted by server

Close #147
This commit is contained in:
Jakub Roztocil 2013-07-07 17:00:03 +02:00
parent 60f09776a5
commit 383dba524a
3 changed files with 15 additions and 12 deletions

View File

@ -132,6 +132,10 @@ def main(args=sys.argv[1:], env=Environment()):
download.finish() download.finish()
if download.interrupted: if download.interrupted:
exit_status = ExitStatus.ERROR exit_status = ExitStatus.ERROR
error('Incomplete download: size=%d; downloaded=%d' % (
download.status.total_size,
download.status.downloaded
))
except IOError as e: except IOError as e:
if not traceback and e.errno == errno.EPIPE: if not traceback and e.errno == errno.EPIPE:

View File

@ -162,9 +162,9 @@ class Download(object):
self._resumed_from = 0 self._resumed_from = 0
self.finished = False self.finished = False
self._status = Status() self.status = Status()
self._progress_reporter = ProgressReporterThread( self._progress_reporter = ProgressReporterThread(
status=self._status, status=self.status,
output=progress_file output=progress_file
) )
@ -197,7 +197,7 @@ class Download(object):
:return: RawStream, output_file :return: RawStream, output_file
""" """
assert not self._status.time_started assert not self.status.time_started
try: try:
total_size = int(response.headers['Content-Length']) total_size = int(response.headers['Content-Length'])
@ -232,7 +232,7 @@ class Download(object):
) )
self._output_file = open(get_unique_filename(fn), mode='a+b') self._output_file = open(get_unique_filename(fn), mode='a+b')
self._status.started( self.status.started(
resumed_from=self._resumed_from, resumed_from=self._resumed_from,
total_size=total_size total_size=total_size
) )
@ -260,7 +260,7 @@ class Download(object):
def finish(self): def finish(self):
assert not self.finished assert not self.finished
self.finished = True self.finished = True
self._status.finished() self.status.finished()
def failed(self): def failed(self):
self._progress_reporter.stop() self._progress_reporter.stop()
@ -269,8 +269,8 @@ class Download(object):
def interrupted(self): def interrupted(self):
return ( return (
self.finished self.finished
and self._status.total_size and self.status.total_size
and self._status.total_size != self._status.downloaded and self.status.total_size != self.status.downloaded
) )
def _chunk_downloaded(self, chunk): def _chunk_downloaded(self, chunk):
@ -282,7 +282,7 @@ class Download(object):
:type chunk: bytes :type chunk: bytes
""" """
self._status.chunk_downloaded(len(chunk)) self.status.chunk_downloaded(len(chunk))
class Status(object): class Status(object):

View File

@ -110,7 +110,8 @@ with open(BIN_FILE_PATH, 'rb') as f:
def httpbin(path): def httpbin(path):
return HTTPBIN_URL + path url = HTTPBIN_URL + path
return url
def mk_config_dir(): def mk_config_dir():
@ -1627,9 +1628,7 @@ class DownloadTest(BaseTestCase):
self.assertFalse(download.interrupted) self.assertFalse(download.interrupted)
def test_download_interrupted(self): def test_download_interrupted(self):
download = Download( download = Download(output_file=open(os.devnull, 'w'))
output_file=open(os.devnull, 'w')
)
download.start(Response( download.start(Response(
url=httpbin('/'), url=httpbin('/'),
headers={'Content-Length': 5} headers={'Content-Length': 5}