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

Unified Diff: sitescripts/management/bin/generateNotifications.py

Issue 11275006: Added script to generate notification.json for the emergencynotification mechanism (Closed)
Patch Set: Created July 26, 2013, 9:15 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « .sitescripts.example ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sitescripts/management/bin/generateNotifications.py
===================================================================
new file mode 100644
--- /dev/null
+++ b/sitescripts/management/bin/generateNotifications.py
@@ -0,0 +1,114 @@
+# coding: utf-8
Sebastian Noack 2013/07/27 14:06:05 There is no shebang and the file mode is 644 inste
Wladimir Palant 2013/07/29 14:09:44 No, and we don't. It's being run with the followin
+
+# This file is part of the Adblock Plus web scripts,
+# Copyright (C) 2006-2013 Eyeo GmbH
Sebastian Noack 2013/07/27 14:06:05 Since this is a new file, doesn't have the copyrig
Wladimir Palant 2013/07/29 14:09:44 I don't think that this approach would be anywhere
+#
+# Adblock Plus is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 3 as
+# published by the Free Software Foundation.
+#
+# Adblock Plus is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+
+import os, re, subprocess, tarfile, codecs, time, traceback, json
+from StringIO import StringIO
+from sitescripts.utils import get_config, setupStderr
+
+def parseTargetSpec(value, name):
+ target = {}
+ for spec in value.split():
+ known = False
+ for parameter in ("extension", "application", "platform"):
+ if spec.startswith(parameter + "="):
+ target[parameter] = spec[len(parameter + "="):]
+ known = True
+ elif spec.startswith(parameter + "Version>="):
+ target[parameter + "MinVersion"] = spec[len(parameter + "Version>="):]
+ known = True
+ elif spec.startswith(parameter + "Version<="):
+ target[parameter + "MaxVersion"] = spec[len(parameter + "Version<="):]
+ known = True
+ elif spec.startswith(parameter + "Version="):
+ target[parameter + "MinVersion"] = target[parameter + "MaxVersion"] = spec[len(parameter + "Version="):]
+ known = True
+ if not known:
+ raise Exception("Unknown target specifier '%s' in file '%s'" % (spec, name))
+ return target
+
+def parseNotification(data, name):
+ notification = {"id": name, "severity": "information", "message": {}, "title": {}}
+
+ for line in data:
+ if not re.search(r"\S", line):
+ continue
+
+ if line.find("=") < 0:
+ raise Exception("Could not process line '%s' in file '%s'" % (line.strip(), name))
+
+ key, value = map(unicode.strip, line.split("=", 1))
+
+ if key == "inactive":
+ notification["inactive"] = True
+ elif key == "severity":
+ if value not in ("information", "critical"):
+ raise Exception("Unknown severity value '%s' in file '%s'" % (value, name))
+ notification["severity"] = value
+ elif key == "links":
+ notification["links"] = value.split()
+ elif key.startswith("title."):
+ locale = key[len("title."):]
+ notification["title"][locale] = value
+ elif key.startswith("message."):
+ locale = key[len("message."):]
+ notification["message"][locale] = value
+ elif key == "target":
+ target = parseTargetSpec(value, name)
+ if "targets" in notification:
+ notification["targets"].append(target)
+ else:
+ notification["targets"] = [target]
+ else:
+ raise Exception("Unknown parameter '%s' in file '%s'" % (key, name))
+
+ if "en-US" not in notification["title"]:
+ raise Exception("No title for en-US (default language) in file '%s'" % name)
+ if "en-US" not in notification["message"]:
+ raise Exception("No message for en-US (default language) in file '%s'" % name)
+ return notification
+
+def generateNotifications(repo, path):
+ command = ["hg", "-R", repo, "archive", "-r", "default", "-t", "tar", "-p", ".", "-X", os.path.join(repo, ".hg_archival.txt"), "-"]
+ data = subprocess.check_output(command)
+
+ result = {"version": time.strftime("%Y%m%d%H%M", time.gmtime()), "notifications": []}
+ tarFile = tarfile.open(mode="r:", fileobj=StringIO(data))
Sebastian Noack 2013/07/27 14:06:05 You should close the tarfile object, also if you u
+ for fileInfo in tarFile:
+ name = fileInfo.name
+ if name.startswith("./"):
+ name = name[2:]
+
+ if fileInfo.type == tarfile.REGTYPE:
+ data = codecs.getreader("utf8")(tarFile.extractfile(fileInfo))
+ try:
+ notification = parseNotification(data, name)
+ if "inactive" in notification:
+ continue
+ result["notifications"].append(notification)
+ except:
+ traceback.print_exc()
+
+ file = codecs.open(path, "wb", encoding="utf-8")
+ json.dump(result, file, ensure_ascii=False, indent=2, separators=(',', ': '), sort_keys=True)
+ file.close()
Sebastian Noack 2013/07/27 14:06:05 Use try-finally block or even better the with stat
Wladimir Palant 2013/07/29 14:09:44 I guess a with block would do better here - but is
+
+if __name__ == "__main__":
+ setupStderr()
+ repo = get_config().get("notifications", "repository")
+ output = get_config().get("notifications", "output")
+ subprocess.call(["hg", "-R", repo, "pull", "-q"])
+ generateNotifications(repo, output)
« no previous file with comments | « .sitescripts.example ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld