| Index: sitescripts/content_blocker_lists/bin/generate_lists.py |
| diff --git a/sitescripts/content_blocker_lists/bin/generate_lists.py b/sitescripts/content_blocker_lists/bin/generate_lists.py |
| index d86a52a95bc4edf0a14a017573c893f1f2c693bf..2e93dc6345c936ea7f0fa0cbb8e9705859b51551 100644 |
| --- a/sitescripts/content_blocker_lists/bin/generate_lists.py |
| +++ b/sitescripts/content_blocker_lists/bin/generate_lists.py |
| @@ -16,61 +16,88 @@ |
| # You should have received a copy of the GNU General Public License |
| # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| +from collections import OrderedDict |
| +from contextlib import closing |
| +from datetime import datetime |
| +import json |
| import os |
| import subprocess |
| +import thread |
| +import re |
| import urllib2 |
| from sitescripts.utils import get_config |
| -def _update_abp2blocklist(): |
| +config = dict(get_config().items("content_blocker_lists")) |
| + |
| +def update_abp2blocklist(): |
|
Felix Dahlke
2015/12/01 08:43:09
These functions were prefixed with an underscore f
Sebastian Noack
2015/12/01 10:18:39
Well, one could argue that this is less a module (
kzar
2015/12/01 12:13:38
I would prefer to leave them off, they don't reall
Felix Dahlke
2015/12/01 14:04:19
I would argue that this is still a module - this s
Sebastian Noack
2015/12/07 12:38:24
Well, technically every piece of code is part of a
Felix Dahlke
2015/12/08 06:51:24
Wladimir's newer code in Sitescripts also uses tho
|
| with open(os.devnull, "w") as devnull: |
| - config = get_config() |
| - abp2blocklist_path = config.get("content_blocker_lists", |
| - "abp2blocklist_path") |
| + abp2blocklist_path = config["abp2blocklist_path"] |
| if os.path.isdir(abp2blocklist_path): |
| subprocess.check_call(("hg", "pull", "-u", "-R", abp2blocklist_path), |
| stdout=devnull) |
| else: |
| - abp2blocklist_url = config.get("content_blocker_lists", |
| - "abp2blocklist_url") |
| - subprocess.check_call(("hg", "clone", abp2blocklist_url, |
| + subprocess.check_call(("hg", "clone", config["abp2blocklist_url"], |
| abp2blocklist_path), stdout=devnull) |
| subprocess.check_call(("npm", "install"), cwd=abp2blocklist_path, |
| stdout=devnull) |
| -def _download(url_key): |
| - url = get_config().get("content_blocker_lists", url_key) |
| - response = urllib2.urlopen(url) |
| - try: |
| - return response.read() |
| - finally: |
| - response.close() |
| +def download_filter_list(url): |
| + filter_list = {} |
| + with closing(urllib2.urlopen(url)) as response: |
| + filter_list["body"] = response.read() |
| + filter_list["header"] = parse_filter_list_header(filter_list["body"]) |
| + filter_list["header"]["url"] = url |
| + return filter_list |
|
Sebastian Noack
2015/12/01 11:01:48
I think it would be simpler if you just return a t
kzar
2015/12/01 12:13:38
Done.
|
| + |
| +def parse_filter_list_header(filter_list): |
| + body_start = re.search(r"^[^![]", filter_list, re.MULTILINE).start() |
| + field_re = re.compile(r"^!\s*([^:\s]+):\s*(.+)$", re.MULTILINE) |
| + return { match.group(1): match.group(2) |
| + for match in field_re.finditer(filter_list, 0, body_start) } |
| + |
| +def generate_metadata(filter_lists, expires): |
| + metadata = OrderedDict(( |
| + ("version", datetime.utcnow().strftime("%Y%m%d%H%M")), |
|
Felix Dahlke
2015/12/01 08:43:08
FWIW, we're using `time.strftime("%Y%m%d%H%M", tim
Sebastian Noack
2015/12/01 10:18:39
I tend to agree, creating a datetime object is unn
kzar
2015/12/01 12:13:39
Done.
|
| + ("expires", expires), |
| + ("sources", []) |
| + )) |
| + for filter_list in filter_lists: |
| + metadata["sources"].append({ k.lower(): filter_list["header"][k] |
| + for k in ["url", "Version"]}) |
|
Felix Dahlke
2015/12/01 08:43:08
Nit: Sebastian convinced me a while ago that tuple
Sebastian Noack
2015/12/01 10:18:39
Well, frankly, I don't think that it matters in th
Felix Dahlke
2015/12/01 10:26:29
Wouldn't insist either.
Sebastian Noack
2015/12/01 10:38:09
For reference, I found a quite interesting answer
kzar
2015/12/01 12:13:39
Acknowledged.
|
| + return metadata |
| -def _convert_filter_list(sources, destination_path_key): |
| - config = get_config() |
| - destination_path = config.get("content_blocker_lists", destination_path_key) |
| - with open(destination_path, "wb") as destination_file: |
| - abp2blocklist_path = config.get("content_blocker_lists", |
| - "abp2blocklist_path") |
| - process = subprocess.Popen(("node", "abp2blocklist.js"), |
| - cwd=abp2blocklist_path, stdin=subprocess.PIPE, |
| - stdout=destination_file) |
| +def write_block_list(filter_lists, path, expires): |
| + block_list = generate_metadata(filter_lists, expires) |
| + process = subprocess.Popen(("node", "abp2blocklist.js"), |
| + cwd=config["abp2blocklist_path"], |
| + stdin=subprocess.PIPE, stdout=subprocess.PIPE) |
| + def pipe_in(process): |
|
Sebastian Noack
2015/12/01 10:47:14
Nit: This is inconsistent. You pass in the process
kzar
2015/12/01 12:13:38
Done.
|
| try: |
| - for source in sources: |
| - print >>process.stdin, source |
| + for filter_list in filter_lists: |
| + print >>process.stdin, filter_list["body"] |
| finally: |
| process.stdin.close() |
| process.wait() |
| + thread.start_new_thread(pipe_in, (process,)) |
|
Sebastian Noack
2015/12/01 10:47:14
Please use the high-level threading module instead
kzar
2015/12/01 12:13:38
Done.
|
| + block_list["rules"] = json.load(process.stdout) |
| + |
| if process.returncode: |
| raise Exception("abp2blocklist returned %s" % process.returncode) |
| + with open(path, "wb") as destination_file: |
| + json.dump(block_list, destination_file, indent=2, separators=(",", ": ")) |
| + |
| if __name__ == "__main__": |
| - _update_abp2blocklist() |
| + update_abp2blocklist() |
| - easylist = _download("easylist_url") |
| - exceptionrules = _download("exceptionrules_url") |
| + easylist = download_filter_list(config["easylist_url"]) |
| + exceptionrules = download_filter_list(config["exceptionrules_url"]) |
| - _convert_filter_list([easylist], "easylist_content_blocker_path") |
| - _convert_filter_list([easylist, exceptionrules], |
| - "combined_content_blocker_path") |
| + write_block_list([easylist], |
|
Felix Dahlke
2015/12/01 08:43:08
Nit: "block list" is highly ambiguous, we often us
Sebastian Noack
2015/12/01 10:18:39
Well, after all, the program called here is also c
Felix Dahlke
2015/12/01 10:26:29
Good point, let's really leave it alone.
|
| + config["easylist_content_blocker_path"], |
| + config["easylist_content_blocker_expires"]) |
| + write_block_list([easylist, exceptionrules], |
| + config["combined_content_blocker_path"], |
| + config["combined_content_blocker_expires"]) |