LEFT | RIGHT |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # coding: utf-8 | 2 # coding: utf-8 |
3 | 3 |
4 # This file is part of Adblock Plus <https://adblockplus.org/>, | 4 # This file is part of Adblock Plus <https://adblockplus.org/>, |
5 # Copyright (C) 2006-2015 Eyeo GmbH | 5 # Copyright (C) 2006-2015 Eyeo GmbH |
6 # | 6 # |
7 # Adblock Plus is free software: you can redistribute it and/or modify | 7 # Adblock Plus is free software: you can redistribute it and/or modify |
8 # it under the terms of the GNU General Public License version 3 as | 8 # it under the terms of the GNU General Public License version 3 as |
9 # published by the Free Software Foundation. | 9 # published by the Free Software Foundation. |
10 # | 10 # |
(...skipping 23 matching lines...) Expand all Loading... |
34 if os.path.isdir(ABP2BLOCKLIST_PATH): | 34 if os.path.isdir(ABP2BLOCKLIST_PATH): |
35 subprocess.check_call(["hg", "pull", "-u", "-R", ABP2BLOCKLIST_PATH]) | 35 subprocess.check_call(["hg", "pull", "-u", "-R", ABP2BLOCKLIST_PATH]) |
36 else: | 36 else: |
37 subprocess.check_call(["hg", "clone", ABP2BLOCKLIST_URL, | 37 subprocess.check_call(["hg", "clone", ABP2BLOCKLIST_URL, |
38 ABP2BLOCKLIST_PATH]) | 38 ABP2BLOCKLIST_PATH]) |
39 subprocess.check_call(["npm", "install"], cwd=ABP2BLOCKLIST_PATH) | 39 subprocess.check_call(["npm", "install"], cwd=ABP2BLOCKLIST_PATH) |
40 | 40 |
41 def _download_filter_lists(): | 41 def _download_filter_lists(): |
42 try: | 42 try: |
43 easylist_response = urllib2.urlopen(EASYLIST_URL) | 43 easylist_response = urllib2.urlopen(EASYLIST_URL) |
44 easylist_file = tempfile.NamedTemporaryFile(mode="w+") | 44 easylist_file = tempfile.NamedTemporaryFile(mode="wb+") |
45 shutil.copyfileobj(easylist_response, easylist_file) | 45 shutil.copyfileobj(easylist_response, easylist_file) |
46 finally: | 46 finally: |
47 easylist_response.close() | 47 easylist_response.close() |
| 48 |
48 try: | 49 try: |
49 exceptionrules_response = urllib2.urlopen(EXCEPTIONRULES_URL) | 50 exceptionrules_response = urllib2.urlopen(EXCEPTIONRULES_URL) |
50 exceptionrules_file = tempfile.NamedTemporaryFile(mode="w+") | 51 exceptionrules_file = tempfile.NamedTemporaryFile(mode="wb+") |
51 shutil.copyfileobj(exceptionrules_response, exceptionrules_file) | 52 shutil.copyfileobj(exceptionrules_response, exceptionrules_file) |
52 finally: | 53 finally: |
53 exceptionrules_response.close() | 54 exceptionrules_response.close() |
| 55 |
54 return (easylist_file, exceptionrules_file) | 56 return (easylist_file, exceptionrules_file) |
55 | 57 |
| 58 def _convert_filter_list(source_file, destination_path): |
| 59 source_file.seek(0) |
| 60 with open(destination_path, "wb") as destination_file: |
| 61 subprocess.check_call(["node", "abp2blocklist.js"], |
| 62 cwd=ABP2BLOCKLIST_PATH, stdin=source_file, |
| 63 stdout=destination_file) |
| 64 |
56 def _concatenate_files(*source_files): | 65 def _concatenate_files(*source_files): |
57 destination_file = tempfile.NamedTemporaryFile(mode="w+") | 66 destination_file = tempfile.NamedTemporaryFile(mode="wb+") |
58 for source_file in source_files: | 67 for source_file in source_files: |
59 source_file.seek(0) | 68 source_file.seek(0) |
60 shutil.copyfileobj(source_file, destination_file) | 69 shutil.copyfileobj(source_file, destination_file) |
61 return destination_file | 70 return destination_file |
62 | |
63 def _convert_filter_list(source_file, destination_path): | |
64 source_file.seek(0) | |
65 with open(destination_path, "w") as destination_file: | |
66 subprocess.check_call(["node", "abp2blocklist.js"], | |
67 cwd=ABP2BLOCKLIST_PATH, stdin=source_file, | |
68 stdout=destination_file) | |
69 | 71 |
70 if __name__ == "__main__": | 72 if __name__ == "__main__": |
71 print "Fetching/updating abp2blocklist ..." | 73 print "Fetching/updating abp2blocklist ..." |
72 _update_abp2blocklist() | 74 _update_abp2blocklist() |
73 | 75 |
74 print "Downloading filter lists ..." | 76 print "Downloading filter lists ..." |
75 easylist_file, exceptionrules_file = _download_filter_lists() | 77 easylist_file, exceptionrules_file = _download_filter_lists() |
76 | 78 |
77 print "Generating %s ..." % os.path.basename(EASYLIST_CONTENT_BLOCKER_PATH) | 79 try: |
78 _convert_filter_list(easylist_file, EASYLIST_CONTENT_BLOCKER_PATH) | 80 print "Generating %s ..." % os.path.basename(EASYLIST_CONTENT_BLOCKER_PATH) |
| 81 _convert_filter_list(easylist_file, EASYLIST_CONTENT_BLOCKER_PATH) |
79 | 82 |
80 print "Generating %s ..." % os.path.basename(COMBINED_CONTENT_BLOCKER_PATH) | 83 print "Generating %s ..." % os.path.basename(COMBINED_CONTENT_BLOCKER_PATH) |
81 combined_file = _concatenate_files(easylist_file, exceptionrules_file) | 84 with _concatenate_files(easylist_file, exceptionrules_file) as combined_file
: |
82 _convert_filter_list(combined_file, COMBINED_CONTENT_BLOCKER_PATH) | 85 _convert_filter_list(combined_file, COMBINED_CONTENT_BLOCKER_PATH) |
| 86 finally: |
| 87 easylist_file.close() |
| 88 exceptionrules_file.close() |
LEFT | RIGHT |