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

Delta Between Two Patch Sets: sitescripts/management/bin/generateNotifications.py

Issue 11275006: Added script to generate notification.json for the emergencynotification mechanism (Closed)
Left Patch Set: Created July 26, 2013, 9:15 a.m.
Right Patch Set: Addressed review comments Created Nov. 4, 2013, 10:28 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « sitescripts/management/bin/generateHgAuth.py ('k') | sitescripts/subscriptions/subscriptionParser.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 # coding: utf-8 1 # 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
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-2013 Eyeo GmbH 4 # 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
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 os, re, subprocess, tarfile, codecs, time, traceback, json
19 from StringIO import StringIO 19 from StringIO import StringIO
20 from sitescripts.utils import get_config, setupStderr 20 from sitescripts.utils import get_config, setupStderr
21 21
22 def parseTargetSpec(value, name): 22 def parse_targetspec(value, name):
23 target = {} 23 target = {}
24 for spec in value.split(): 24 for spec in value.split():
25 known = False 25 known = False
26 for parameter in ("extension", "application", "platform"): 26 for parameter in ("extension", "application", "platform"):
27 if spec.startswith(parameter + "="): 27 if spec.startswith(parameter + "="):
28 target[parameter] = spec[len(parameter + "="):] 28 target[parameter] = spec[len(parameter + "="):]
29 known = True 29 known = True
30 elif spec.startswith(parameter + "Version>="): 30 elif spec.startswith(parameter + "Version>="):
31 target[parameter + "MinVersion"] = spec[len(parameter + "Version>="):] 31 target[parameter + "MinVersion"] = spec[len(parameter + "Version>="):]
32 known = True 32 known = True
33 elif spec.startswith(parameter + "Version<="): 33 elif spec.startswith(parameter + "Version<="):
34 target[parameter + "MaxVersion"] = spec[len(parameter + "Version<="):] 34 target[parameter + "MaxVersion"] = spec[len(parameter + "Version<="):]
35 known = True 35 known = True
36 elif spec.startswith(parameter + "Version="): 36 elif spec.startswith(parameter + "Version="):
37 target[parameter + "MinVersion"] = target[parameter + "MaxVersion"] = sp ec[len(parameter + "Version="):] 37 target[parameter + "MinVersion"] = target[parameter + "MaxVersion"] = sp ec[len(parameter + "Version="):]
38 known = True 38 known = True
39 if not known: 39 if not known:
40 raise Exception("Unknown target specifier '%s' in file '%s'" % (spec, name )) 40 raise Exception("Unknown target specifier '%s' in file '%s'" % (spec, name ))
41 return target 41 return target
42 42
43 def parseNotification(data, name): 43 def parse_notification(data, name):
44 notification = {"id": name, "severity": "information", "message": {}, "title": {}} 44 notification = {"id": name, "severity": "information", "message": {}, "title": {}}
45 45
46 for line in data: 46 for line in data:
47 if not re.search(r"\S", line): 47 if not re.search(r"\S", line):
48 continue 48 continue
49 49
50 if line.find("=") < 0: 50 if line.find("=") < 0:
51 raise Exception("Could not process line '%s' in file '%s'" % (line.strip() , name)) 51 raise Exception("Could not process line '%s' in file '%s'" % (line.strip() , name))
52 52
53 key, value = map(unicode.strip, line.split("=", 1)) 53 key, value = map(unicode.strip, line.split("=", 1))
54 54
55 if key == "inactive": 55 if key == "inactive":
56 notification["inactive"] = True 56 notification["inactive"] = True
57 elif key == "severity": 57 elif key == "severity":
58 if value not in ("information", "critical"): 58 if value not in ("information", "critical"):
59 raise Exception("Unknown severity value '%s' in file '%s'" % (value, nam e)) 59 raise Exception("Unknown severity value '%s' in file '%s'" % (value, nam e))
60 notification["severity"] = value 60 notification["severity"] = value
61 elif key == "links": 61 elif key == "links":
62 notification["links"] = value.split() 62 notification["links"] = value.split()
63 elif key.startswith("title."): 63 elif key.startswith("title."):
64 locale = key[len("title."):] 64 locale = key[len("title."):]
65 notification["title"][locale] = value 65 notification["title"][locale] = value
66 elif key.startswith("message."): 66 elif key.startswith("message."):
67 locale = key[len("message."):] 67 locale = key[len("message."):]
68 notification["message"][locale] = value 68 notification["message"][locale] = value
69 elif key == "target": 69 elif key == "target":
70 target = parseTargetSpec(value, name) 70 target = parse_targetspec(value, name)
71 if "targets" in notification: 71 if "targets" in notification:
72 notification["targets"].append(target) 72 notification["targets"].append(target)
73 else: 73 else:
74 notification["targets"] = [target] 74 notification["targets"] = [target]
75 else: 75 else:
76 raise Exception("Unknown parameter '%s' in file '%s'" % (key, name)) 76 raise Exception("Unknown parameter '%s' in file '%s'" % (key, name))
77 77
78 if "en-US" not in notification["title"]: 78 if "en-US" not in notification["title"]:
79 raise Exception("No title for en-US (default language) in file '%s'" % name) 79 raise Exception("No title for en-US (default language) in file '%s'" % name)
80 if "en-US" not in notification["message"]: 80 if "en-US" not in notification["message"]:
81 raise Exception("No message for en-US (default language) in file '%s'" % nam e) 81 raise Exception("No message for en-US (default language) in file '%s'" % nam e)
82 return notification 82 return notification
83 83
84 def generateNotifications(repo, path): 84 def generate_notifications(repo, path):
85 command = ["hg", "-R", repo, "archive", "-r", "default", "-t", "tar", "-p", ". ", "-X", os.path.join(repo, ".hg_archival.txt"), "-"] 85 command = ["hg", "-R", repo, "archive", "-r", "default", "-t", "tar",
86 "-p", ".", "-X", os.path.join(repo, ".hg_archival.txt"), "-"]
86 data = subprocess.check_output(command) 87 data = subprocess.check_output(command)
87 88
88 result = {"version": time.strftime("%Y%m%d%H%M", time.gmtime()), "notification s": []} 89 result = {"version": time.strftime("%Y%m%d%H%M", time.gmtime()), "notification s": []}
89 tarFile = tarfile.open(mode="r:", fileobj=StringIO(data)) 90 with tarfile.open(mode="r:", fileobj=StringIO(data)) as archive:
Sebastian Noack 2013/07/27 14:06:05 You should close the tarfile object, also if you u
90 for fileInfo in tarFile: 91 for fileinfo in archive:
91 name = fileInfo.name 92 name = fileinfo.name
92 if name.startswith("./"): 93 if name.startswith("./"):
93 name = name[2:] 94 name = name[2:]
94 95
95 if fileInfo.type == tarfile.REGTYPE: 96 if fileinfo.type == tarfile.REGTYPE:
96 data = codecs.getreader("utf8")(tarFile.extractfile(fileInfo)) 97 data = codecs.getreader("utf8")(archive.extractfile(fileinfo))
97 try: 98 try:
98 notification = parseNotification(data, name) 99 notification = parse_notification(data, name)
99 if "inactive" in notification: 100 if "inactive" in notification:
100 continue 101 continue
101 result["notifications"].append(notification) 102 result["notifications"].append(notification)
102 except: 103 except:
103 traceback.print_exc() 104 traceback.print_exc()
104 105
105 file = codecs.open(path, "wb", encoding="utf-8") 106 with codecs.open(path, "wb", encoding="utf-8") as file:
106 json.dump(result, file, ensure_ascii=False, indent=2, separators=(',', ': '), sort_keys=True) 107 json.dump(result, file, ensure_ascii=False, indent=2,
107 file.close() 108 separators=(',', ': '), sort_keys=True)
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
108 109
109 if __name__ == "__main__": 110 if __name__ == "__main__":
110 setupStderr() 111 setupStderr()
111 repo = get_config().get("notifications", "repository") 112 repo = get_config().get("notifications", "repository")
112 output = get_config().get("notifications", "output") 113 output = get_config().get("notifications", "output")
113 subprocess.call(["hg", "-R", repo, "pull", "-q"]) 114 subprocess.call(["hg", "-R", repo, "pull", "-q"])
114 generateNotifications(repo, output) 115 generate_notifications(repo, output)
LEFTRIGHT

Powered by Google App Engine
This is Rietveld