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

Side by Side Diff: generate_lists.py

Issue 29328863: Issue 3168 - Add a script for generating new content blocker lists (Closed)
Patch Set: Created Oct. 6, 2015, 2:38 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« dependencies ('K') | « ensure_dependencies.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/usr/bin/env python
2 # coding: utf-8
3
4 # This file is part of Adblock Plus <https://adblockplus.org/>,
5 # Copyright (C) 2006-2015 Eyeo GmbH
6 #
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
9 # published by the Free Software Foundation.
10 #
11 # Adblock Plus is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
18
19 import os
20 import shutil
21 import subprocess
22 import tempfile
23 import urllib2
24
25 BASE_PATH = os.path.dirname(os.path.abspath(__file__))
26 ENSURE_DEPENDENCIES_PATH = os.path.join(BASE_PATH, "ensure_dependencies.py")
27 EASYLIST_URL = "https://easylist-downloads.adblockplus.org/easylist_noadult.txt"
28 EXCEPTIONRULES_URL = "https://easylist-downloads.adblockplus.org/exceptionrules. txt"
29 ABP2BLOCKLIST_PATH = os.path.join(BASE_PATH, "abp2blocklist")
30 EASYLIST_CONTENT_BLOCKER_PATH = os.path.join(BASE_PATH, "easylist_content_blocke r.json")
31 COMBINED_CONTENT_BLOCKER_PATH = os.path.join(BASE_PATH, "easylist+exceptionrules _content_blocker.json")
32
33 def _download_filter_lists():
34 easylist_response = urllib2.urlopen(EASYLIST_URL)
35 with tempfile.NamedTemporaryFile(mode="w", delete=False) as easylist_file:
36 easylist_file.write(easylist_response.read())
37 exceptionrules_response = urllib2.urlopen(EXCEPTIONRULES_URL)
38 with tempfile.NamedTemporaryFile(mode="w", delete=False) as exceptionrules_fil e:
39 exceptionrules_file.write(exceptionrules_response.read())
40 return (easylist_file.name, exceptionrules_file.name)
41
42 def _concatenate_files(*source_paths):
43 with tempfile.NamedTemporaryFile(mode="w", delete=False) as destination_file:
44 for source_path in source_paths:
45 with open(source_path, "r") as source_file:
46 shutil.copyfileobj(source_file, destination_file)
47 return destination_file.name
48
49 def _convert_filter_list(source_path, destination_path):
50 with open(source_path, "r") as source_file, \
51 open(destination_path, "w") as destination_file:
52 subprocess.check_call(["node", "abp2blocklist.js"],
53 cwd=ABP2BLOCKLIST_PATH, stdin=source_file,
54 stdout=destination_file)
55
56 if __name__ == "__main__":
57 subprocess.check_call([ENSURE_DEPENDENCIES_PATH])
58
59 print "Downloading filter lists ..."
60 easylist_path, exceptionrules_path = _download_filter_lists()
61
62 try:
63 print "Generating %s ..." % os.path.basename(EASYLIST_CONTENT_BLOCKER_PATH)
64 _convert_filter_list(easylist_path, EASYLIST_CONTENT_BLOCKER_PATH)
65
66 print "Generating %s ..." % os.path.basename(COMBINED_CONTENT_BLOCKER_PATH)
67 combined_path = _concatenate_files(easylist_path, exceptionrules_path)
68 try:
69 _convert_filter_list(combined_path, COMBINED_CONTENT_BLOCKER_PATH)
70 finally:
71 os.remove(combined_path)
72 finally:
73 os.remove(easylist_path)
74 os.remove(exceptionrules_path)
OLDNEW
« dependencies ('K') | « ensure_dependencies.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld