From 8bddb0996c44ce8ce989359a7b4c9ff102466ef2 Mon Sep 17 00:00:00 2001 From: Sam Thursfield Date: Fri, 9 Jan 2015 12:17:06 +0000 Subject: Delete downloaded tarballs on failure Previously if a tarball lorry failed, a 0-byte tar file would be kept around and subsequent calls to lorry would return success without actually importing anything. --- lorry | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lorry b/lorry index 127a1fb..77fc9c8 100755 --- a/lorry +++ b/lorry @@ -477,10 +477,15 @@ class Lorry(cliapp.Application): self.progress('.. checking if we need to fetch %s' % basename) if not os.path.exists(tardest): self.progress('.. attempting to fetch.') - with open(tardest, 'w') as tarfile: - urlfile = urllib2.urlopen(spec['url']) - tarfile.write(urlfile.read()) - urlfile.close() + try: + with open(tardest, 'w') as tarfile: + urlfile = urllib2.urlopen(spec['url']) + tarfile.write(urlfile.read()) + urlfile.close() + except Exception as e: + if os.path.exists(tardest): + os.unlink(tardest) + raise else: self.progress('.. no need to run, nothing to do') return -- cgit v1.2.1 From acf80a68ee41ec1d6bea531ea2fb00dc1227f2e3 Mon Sep 17 00:00:00 2001 From: Sam Thursfield Date: Fri, 9 Jan 2015 12:02:15 +0000 Subject: Redownload a tarball if it exists but is 0 bytes long This is a safety measure. Commit 8bddb0996c44ce8ce989359a7b4c9ff102466ef2 should mean that this can't happen, but 'lorry' runs unattended so we really don't want it to break. --- lorry | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lorry b/lorry index 77fc9c8..d28d064 100755 --- a/lorry +++ b/lorry @@ -30,6 +30,12 @@ __version__ = '0.0' lorry_path = os.path.realpath(__file__) + +def file_missing_or_empty(filename): + ''' A more comprehensive alternative to os.path.exists(). ''' + return (not os.path.isfile(filename)) or (os.path.getsize(filename) <= 0) + + def quote_url(url): ''' Convert URIs to strings that only contain digits, letters, % and _. @@ -475,7 +481,7 @@ class Lorry(cliapp.Application): basename = os.path.basename(url_path) tardest = os.path.join(dirname, basename) self.progress('.. checking if we need to fetch %s' % basename) - if not os.path.exists(tardest): + if file_missing_or_empty(tardest): self.progress('.. attempting to fetch.') try: with open(tardest, 'w') as tarfile: -- cgit v1.2.1