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

Side by Side Diff: sitescripts/content_blocker_lists/bin/generate_lists.py

Issue 29329537: Issue 3168 - Add a script for generating content blocker lists (Closed)
Patch Set: Created Nov. 13, 2015, 7:43 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sitescripts/content_blocker_lists/bin/__init__.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 from sitescripts.utils import get_config
26
27 def _update_abp2blocklist():
28 abp2blocklist_path = get_config().get("content_blocker_lists",
29 "abp2blocklist_path")
30 if os.path.isdir(abp2blocklist_path):
31 subprocess.check_call(["hg", "pull", "-u", "-R", abp2blocklist_path])
32 else:
33 abp2blocklist_url = get_config().get("content_blocker_lists",
34 "abp2blocklist_url")
35 subprocess.check_call(["hg", "clone", abp2blocklist_url,
36 abp2blocklist_path])
37 subprocess.check_call(["npm", "install"], cwd=abp2blocklist_path)
38
39 def _download_filter_lists():
40 try:
41 easylist_url = get_config().get("content_blocker_lists", "easylist_url")
42 easylist_response = urllib2.urlopen(easylist_url)
43 easylist_file = tempfile.NamedTemporaryFile(mode="wb+")
44 shutil.copyfileobj(easylist_response, easylist_file)
45 finally:
46 easylist_response.close()
47
48 try:
49 exceptionrules_url = get_config().get("content_blocker_lists",
50 "exceptionrules_url")
51 exceptionrules_response = urllib2.urlopen(exceptionrules_url)
52 exceptionrules_file = tempfile.NamedTemporaryFile(mode="wb+")
53 shutil.copyfileobj(exceptionrules_response, exceptionrules_file)
54 finally:
55 exceptionrules_response.close()
56
57 return (easylist_file, exceptionrules_file)
58
59 def _convert_filter_list(source_file, destination_path):
60 source_file.seek(0)
61 with open(destination_path, "wb") as destination_file:
62 abp2blocklist_path = get_config().get("content_blocker_lists",
63 "abp2blocklist_path")
64 subprocess.check_call(["node", "abp2blocklist.js"],
65 cwd=abp2blocklist_path, stdin=source_file,
66 stdout=destination_file)
67
68 def _concatenate_files(*source_files):
69 destination_file = tempfile.NamedTemporaryFile(mode="wb+")
70 for source_file in source_files:
71 source_file.seek(0)
72 shutil.copyfileobj(source_file, destination_file)
73 return destination_file
74
75 if __name__ == "__main__":
76 print "Fetching/updating abp2blocklist ..."
77 _update_abp2blocklist()
78
79 print "Downloading filter lists ..."
80 easylist_file, exceptionrules_file = _download_filter_lists()
81
82 try:
83 easylist_content_blocker_path = get_config().get("content_blocker_lists",
84 "easylist_content_blocker_p ath")
85 print "Generating %s ..." % os.path.basename(easylist_content_blocker_path)
86 _convert_filter_list(easylist_file, easylist_content_blocker_path)
87
88 combined_content_blocker_path = get_config().get("content_blocker_lists",
89 "combined_content_blocker_p ath")
90 print "Generating %s ..." % os.path.basename(combined_content_blocker_path)
91 with _concatenate_files(easylist_file, exceptionrules_file) as combined_file :
92 _convert_filter_list(combined_file, combined_content_blocker_path)
93 finally:
94 easylist_file.close()
95 exceptionrules_file.close()
OLDNEW
« no previous file with comments | « sitescripts/content_blocker_lists/bin/__init__.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld