| OLD | NEW |
| 1 # coding: utf-8 | 1 # coding: utf-8 |
| 2 | 2 |
| 3 # This file is part of the Adblock Plus web scripts, | 3 # This file is part of the Adblock Plus web scripts, |
| 4 # Copyright (C) 2006-2015 Eyeo GmbH | 4 # Copyright (C) 2006-2015 Eyeo GmbH |
| 5 # | 5 # |
| 6 # Adblock Plus is free software: you can redistribute it and/or modify | 6 # Adblock Plus is free software: you can redistribute it and/or modify |
| 7 # it under the terms of the GNU General Public License version 3 as | 7 # it under the terms of the GNU General Public License version 3 as |
| 8 # published by the Free Software Foundation. | 8 # published by the Free Software Foundation. |
| 9 # | 9 # |
| 10 # Adblock Plus is distributed in the hope that it will be useful, | 10 # Adblock Plus is distributed in the hope that it will be useful, |
| 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 # GNU General Public License for more details. | 13 # GNU General Public License for more details. |
| 14 # | 14 # |
| 15 # You should have received a copy of the GNU General Public License | 15 # You should have received a copy of the GNU General Public License |
| 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 17 | 17 |
| 18 import os, re, subprocess, tarfile, codecs, time, traceback, json | 18 import codecs |
| 19 import os |
| 20 import re |
| 21 import subprocess |
| 22 import tarfile |
| 23 import time |
| 24 import traceback |
| 19 from StringIO import StringIO | 25 from StringIO import StringIO |
| 20 from sitescripts.utils import get_config, setupStderr | |
| 21 | 26 |
| 22 def parse_targetspec(value, name): | 27 from sitescripts.utils import get_config |
| 28 |
| 29 def _parse_targetspec(value, name): |
| 23 target = {} | 30 target = {} |
| 24 for spec in value.split(): | 31 for spec in value.split(): |
| 25 known = False | 32 known = False |
| 26 for parameter in ("extension", "application", "platform"): | 33 for parameter in ("extension", "application", "platform"): |
| 27 if spec.startswith(parameter + "="): | 34 if spec.startswith(parameter + "="): |
| 28 target[parameter] = spec[len(parameter + "="):] | 35 target[parameter] = spec[len(parameter + "="):] |
| 29 known = True | 36 known = True |
| 30 elif spec.startswith(parameter + "Version>="): | 37 elif spec.startswith(parameter + "Version>="): |
| 31 target[parameter + "MinVersion"] = spec[len(parameter + "Version>="):] | 38 target[parameter + "MinVersion"] = spec[len(parameter + "Version>="):] |
| 32 known = True | 39 known = True |
| 33 elif spec.startswith(parameter + "Version<="): | 40 elif spec.startswith(parameter + "Version<="): |
| 34 target[parameter + "MaxVersion"] = spec[len(parameter + "Version<="):] | 41 target[parameter + "MaxVersion"] = spec[len(parameter + "Version<="):] |
| 35 known = True | 42 known = True |
| 36 elif spec.startswith(parameter + "Version="): | 43 elif spec.startswith(parameter + "Version="): |
| 37 target[parameter + "MinVersion"] = target[parameter + "MaxVersion"] = sp
ec[len(parameter + "Version="):] | 44 target[parameter + "MinVersion"] = target[parameter + "MaxVersion"] = sp
ec[len(parameter + "Version="):] |
| 38 known = True | 45 known = True |
| 39 if not known: | 46 if not known: |
| 40 raise Exception("Unknown target specifier '%s' in file '%s'" % (spec, name
)) | 47 raise Exception("Unknown target specifier '%s' in file '%s'" % (spec, name
)) |
| 41 return target | 48 return target |
| 42 | 49 |
| 43 def parse_notification(data, name): | 50 def _parse_notification(data, name): |
| 44 notification = {"id": name, "severity": "information", "message": {}, "title":
{}} | 51 notification = {"id": name, "severity": "information", "message": {}, "title":
{}} |
| 45 | 52 |
| 46 for line in data: | 53 for line in data: |
| 47 if not re.search(r"\S", line): | 54 if not re.search(r"\S", line): |
| 48 continue | 55 continue |
| 49 | 56 |
| 50 if line.find("=") < 0: | 57 if line.find("=") < 0: |
| 51 raise Exception("Could not process line '%s' in file '%s'" % (line.strip()
, name)) | 58 raise Exception("Could not process line '%s' in file '%s'" % (line.strip()
, name)) |
| 52 | 59 |
| 53 key, value = map(unicode.strip, line.split("=", 1)) | 60 key, value = map(unicode.strip, line.split("=", 1)) |
| 54 | 61 |
| 55 if key == "inactive": | 62 if key == "inactive": |
| 56 notification["inactive"] = True | 63 notification["inactive"] = True |
| 57 elif key == "severity": | 64 elif key == "severity": |
| 58 if value not in ("information", "critical"): | 65 if value not in ("information", "critical"): |
| 59 raise Exception("Unknown severity value '%s' in file '%s'" % (value, nam
e)) | 66 raise Exception("Unknown severity value '%s' in file '%s'" % (value, nam
e)) |
| 60 notification["severity"] = value | 67 notification["severity"] = value |
| 61 elif key == "links": | 68 elif key == "links": |
| 62 notification["links"] = value.split() | 69 notification["links"] = value.split() |
| 63 elif key.startswith("title."): | 70 elif key.startswith("title."): |
| 64 locale = key[len("title."):] | 71 locale = key[len("title."):] |
| 65 notification["title"][locale] = value | 72 notification["title"][locale] = value |
| 66 elif key.startswith("message."): | 73 elif key.startswith("message."): |
| 67 locale = key[len("message."):] | 74 locale = key[len("message."):] |
| 68 notification["message"][locale] = value | 75 notification["message"][locale] = value |
| 69 elif key == "target": | 76 elif key == "target": |
| 70 target = parse_targetspec(value, name) | 77 target = _parse_targetspec(value, name) |
| 71 if "targets" in notification: | 78 if "targets" in notification: |
| 72 notification["targets"].append(target) | 79 notification["targets"].append(target) |
| 73 else: | 80 else: |
| 74 notification["targets"] = [target] | 81 notification["targets"] = [target] |
| 75 else: | 82 else: |
| 76 raise Exception("Unknown parameter '%s' in file '%s'" % (key, name)) | 83 raise Exception("Unknown parameter '%s' in file '%s'" % (key, name)) |
| 77 | 84 |
| 78 if "en-US" not in notification["title"]: | 85 if "en-US" not in notification["title"]: |
| 79 raise Exception("No title for en-US (default language) in file '%s'" % name) | 86 raise Exception("No title for en-US (default language) in file '%s'" % name) |
| 80 if "en-US" not in notification["message"]: | 87 if "en-US" not in notification["message"]: |
| 81 raise Exception("No message for en-US (default language) in file '%s'" % nam
e) | 88 raise Exception("No message for en-US (default language) in file '%s'" % nam
e) |
| 82 return notification | 89 return notification |
| 83 | 90 |
| 84 def generate_notifications(repo, path): | 91 def load_notifications(): |
| 92 repo = get_config().get("notifications", "repository") |
| 93 subprocess.call(["hg", "-R", repo, "pull", "-q"]) |
| 85 command = ["hg", "-R", repo, "archive", "-r", "default", "-t", "tar", | 94 command = ["hg", "-R", repo, "archive", "-r", "default", "-t", "tar", |
| 86 "-p", ".", "-X", os.path.join(repo, ".hg_archival.txt"), "-"] | 95 "-p", ".", "-X", os.path.join(repo, ".hg_archival.txt"), "-"] |
| 87 data = subprocess.check_output(command) | 96 data = subprocess.check_output(command) |
| 88 | 97 |
| 89 result = {"version": time.strftime("%Y%m%d%H%M", time.gmtime()), "notification
s": []} | 98 result = {"version": time.strftime("%Y%m%d%H%M", time.gmtime()), "notification
s": []} |
| 90 with tarfile.open(mode="r:", fileobj=StringIO(data)) as archive: | 99 with tarfile.open(mode="r:", fileobj=StringIO(data)) as archive: |
| 91 for fileinfo in archive: | 100 for fileinfo in archive: |
| 92 name = fileinfo.name | 101 name = fileinfo.name |
| 93 if name.startswith("./"): | 102 if name.startswith("./"): |
| 94 name = name[2:] | 103 name = name[2:] |
| 95 | 104 |
| 96 if fileinfo.type == tarfile.REGTYPE: | 105 if fileinfo.type == tarfile.REGTYPE: |
| 97 data = codecs.getreader("utf8")(archive.extractfile(fileinfo)) | 106 data = codecs.getreader("utf8")(archive.extractfile(fileinfo)) |
| 98 try: | 107 try: |
| 99 notification = parse_notification(data, name) | 108 notification = _parse_notification(data, name) |
| 100 if "inactive" in notification: | 109 if "inactive" in notification: |
| 101 continue | 110 continue |
| 102 result["notifications"].append(notification) | 111 result["notifications"].append(notification) |
| 103 except: | 112 except: |
| 104 traceback.print_exc() | 113 traceback.print_exc() |
| 105 | 114 return result |
| 106 with codecs.open(path, "wb", encoding="utf-8") as file: | |
| 107 json.dump(result, file, ensure_ascii=False, indent=2, | |
| 108 separators=(',', ': '), sort_keys=True) | |
| 109 | |
| 110 if __name__ == "__main__": | |
| 111 setupStderr() | |
| 112 repo = get_config().get("notifications", "repository") | |
| 113 output = get_config().get("notifications", "output") | |
| 114 subprocess.call(["hg", "-R", repo, "pull", "-q"]) | |
| 115 generate_notifications(repo, output) | |
| OLD | NEW |