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

Delta Between Two Patch Sets: sitescripts/subscriptions/bin/updateMalwareDomainsList.py

Issue 29370984: Fixes 4784 - Improve error reporting in updateMalwareDomainsList and add tests (Closed)
Left Patch Set: Created Jan. 10, 2017, 6:08 p.m.
Right Patch Set: Improve the error handling flow in try_mirror Created Jan. 20, 2017, 1:43 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | sitescripts/subscriptions/test/test_updateMalwareDomainsList.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 # This file is part of the Adblock Plus web scripts, 1 # This file is part of the Adblock Plus web scripts,
2 # Copyright (C) 2006-2016 Eyeo GmbH 2 # Copyright (C) 2006-2016 Eyeo GmbH
3 # 3 #
4 # Adblock Plus is free software: you can redistribute it and/or modify 4 # Adblock Plus is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License version 3 as 5 # it under the terms of the GNU General Public License version 3 as
6 # published by the Free Software Foundation. 6 # published by the Free Software Foundation.
7 # 7 #
8 # Adblock Plus is distributed in the hope that it will be useful, 8 # Adblock Plus is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details. 11 # GNU General Public License for more details.
12 # 12 #
13 # You should have received a copy of the GNU General Public License 13 # You should have received a copy of the GNU General Public License
14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
15 15
16 import os 16 import os
17 import subprocess 17 import subprocess
18 import codecs 18 import codecs
19 import contextlib
19 import urllib2 20 import urllib2
20 import zipfile 21 import zipfile
21 import tempfile 22 import tempfile
22 import shutil 23 import shutil
23 import sys 24 import sys
24 from StringIO import StringIO 25 from StringIO import StringIO
25 from sitescripts.utils import get_config 26 from sitescripts.utils import get_config
26 27
27 28
28 FILTERLIST_HEADER = '''[Adblock Plus 1.1] 29 FILTERLIST_HEADER = '''[Adblock Plus 1.1]
29 ! This is a list of malware domains generated from malwaredomains.com data. 30 ! This is a list of malware domains generated from malwaredomains.com data.
30 ! Homepage: http://malwaredomains.com/?page_id=2 31 ! Homepage: http://malwaredomains.com/?page_id=2
31 ! Last modified: %timestamp% 32 ! Last modified: %timestamp%
32 ! Expires: 1d 33 ! Expires: 1d
33 !''' 34 !'''
34 35
35 MALWAREDOMAINS_PATH = '/files/justdomains.zip' 36 MALWAREDOMAINS_PATH = '/files/justdomains.zip'
36 37
37 38
38 def try_mirror(mirror): 39 def try_mirror(mirror):
40 url = mirror + MALWAREDOMAINS_PATH
39 try: 41 try:
40 url = mirror + MALWAREDOMAINS_PATH 42 with contextlib.closing(urllib2.urlopen(url)) as response:
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.
41 response = urllib2.urlopen(url) 43 return None, response.read()
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.
42 return None, response.read() 44 except urllib2.HTTPError as exc:
45 exc.close()
43 except urllib2.URLError as exc: 46 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.
44 error_message = 'Failed to fetch {}: {}'.format(url, exc) 47 pass
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
45 return error_message, None 48 return 'Failed to fetch {}: {}'.format(url, exc), None
46 49
47 50
48 def main(): 51 def main():
49 config = get_config() 52 config = get_config()
50 section = 'subscriptionDownloads' 53 section = 'subscriptionDownloads'
51 repository = config.get(section, 'malwaredomains_repository') 54 repository = config.get(section, 'malwaredomains_repository')
52 mirrors = config.get(section, 'malwaredomains_mirrors').split() 55 mirrors = config.get(section, 'malwaredomains_mirrors').split()
53 56
54 tempdir = tempfile.mkdtemp(prefix='malwaredomains') 57 tempdir = tempfile.mkdtemp(prefix='malwaredomains')
55 try: 58 try:
(...skipping 26 matching lines...) Expand all
82 85
83 if subprocess.check_output(['hg', 'stat', '-R', tempdir]) != '': 86 if subprocess.check_output(['hg', 'stat', '-R', tempdir]) != '':
84 subprocess.check_call(['hg', '-q', 'commit', '-R', tempdir, '-A', '- u', 'hgbot', '-m', 'Updated malwaredomains.com data']) 87 subprocess.check_call(['hg', '-q', 'commit', '-R', tempdir, '-A', '- u', 'hgbot', '-m', 'Updated malwaredomains.com data'])
85 subprocess.check_call(['hg', '-q', 'push', '-R', tempdir]) 88 subprocess.check_call(['hg', '-q', 'push', '-R', tempdir])
86 finally: 89 finally:
87 shutil.rmtree(tempdir, ignore_errors=True) 90 shutil.rmtree(tempdir, ignore_errors=True)
88 91
89 92
90 if __name__ == '__main__': 93 if __name__ == '__main__':
91 main() 94 main()
LEFTRIGHT

Powered by Google App Engine
This is Rietveld