Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: sitescripts/subscriptions/bin/updateMalwareDomainsList.py

Issue 29370984: Fixes 4784 - Improve error reporting in updateMalwareDomainsList and add tests (Closed)
Patch Set: Created Jan. 10, 2017, 6:08 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | sitescripts/subscriptions/test/test_updateMalwareDomainsList.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sitescripts/subscriptions/bin/updateMalwareDomainsList.py
===================================================================
--- a/sitescripts/subscriptions/bin/updateMalwareDomainsList.py
+++ b/sitescripts/subscriptions/bin/updateMalwareDomainsList.py
@@ -32,52 +32,60 @@
! Expires: 1d
!'''
MALWAREDOMAINS_PATH = '/files/justdomains.zip'
def try_mirror(mirror):
try:
- response = urllib2.urlopen(mirror + MALWAREDOMAINS_PATH)
- return response.read()
- except urllib2.HTTPError:
- return None
+ url = mirror + MALWAREDOMAINS_PATH
Sebastian Noack 2017/01/19 09:01:29 This line doesn't seem to belong in to the try-blo
Vasily Kuznetsov 2017/01/19 16:34:08 Done.
+ response = urllib2.urlopen(url)
Sebastian Noack 2017/01/19 09:01:29 I think we should explicitly close the response.
Jon Sonesen 2017/01/19 09:31:24 Could this be resolved by using a 'with' statement
Sebastian Noack 2017/01/19 09:48:18 Yes, this should be implemented either using try-f
Jon Sonesen 2017/01/19 09:55:01 Thanks for elaborating on those points :)
Vasily Kuznetsov 2017/01/19 16:34:08 Done.
+ return None, response.read()
+ except urllib2.URLError as exc:
Sebastian Noack 2017/01/19 09:01:29 Note that HTTPError (a subclass of URLError) is al
Vasily Kuznetsov 2017/01/19 16:34:08 Done.
+ error_message = 'Failed to fetch {}: {}'.format(url, exc)
Jon Sonesen 2017/01/18 17:04:48 Perhaps it is more useful to you the reason attrib
Vasily Kuznetsov 2017/01/18 18:46:35 We might also catch subclasses of URLError here. B
+ return error_message, None
-if __name__ == '__main__':
+def main():
config = get_config()
section = 'subscriptionDownloads'
repository = config.get(section, 'malwaredomains_repository')
mirrors = config.get(section, 'malwaredomains_mirrors').split()
tempdir = tempfile.mkdtemp(prefix='malwaredomains')
try:
subprocess.check_call(['hg', '-q', 'clone', '-U', repository, tempdir])
subprocess.check_call(['hg', '-q', 'up', '-R', tempdir, '-r', 'default'])
path = os.path.join(tempdir, 'malwaredomains_full.txt')
file = codecs.open(path, 'wb', encoding='utf-8')
print >>file, FILTERLIST_HEADER
+ error_report = ['Unable to fetch malware domains list', 'Errors:']
for mirror in mirrors:
- data = try_mirror(mirror)
+ error_message, data = try_mirror(mirror)
if data is not None:
break
+ error_report.append(error_message)
else:
- sys.exit('Unable to fetch malware domains list.')
+ sys.exit('\n'.join(error_report))
- zip = zipfile.ZipFile(StringIO(data), 'r')
Vasily Kuznetsov 2017/01/10 18:18:29 These three lines are an unrelated change. Flake8
- info = zip.infolist()[0]
- for line in str(zip.read(info.filename)).splitlines():
+ zf = zipfile.ZipFile(StringIO(data), 'r')
+ info = zf.infolist()[0]
+ for line in str(zf.read(info.filename)).splitlines():
domain = line.strip()
if not domain:
continue
print >>file, '||%s^' % domain.decode('idna')
file.close()
if subprocess.check_output(['hg', 'stat', '-R', tempdir]) != '':
subprocess.check_call(['hg', '-q', 'commit', '-R', tempdir, '-A', '-u', 'hgbot', '-m', 'Updated malwaredomains.com data'])
subprocess.check_call(['hg', '-q', 'push', '-R', tempdir])
finally:
shutil.rmtree(tempdir, ignore_errors=True)
+
+
+if __name__ == '__main__':
+ main()
« no previous file with comments | « no previous file | sitescripts/subscriptions/test/test_updateMalwareDomainsList.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld