Index: sitescripts/management/bin/generateNotifications.py |
=================================================================== |
--- a/sitescripts/management/bin/generateNotifications.py |
+++ b/sitescripts/management/bin/generateNotifications.py |
@@ -14,17 +14,17 @@ |
# |
# 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): |
+def parse_targetspec(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>="): |
@@ -35,17 +35,17 @@ def parseTargetSpec(value, name): |
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): |
+def parse_notification(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)) |
@@ -62,54 +62,54 @@ def parseNotification(data, name): |
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) |
+ target = parse_targetspec(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"), "-"] |
+def generate_notifications(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)) |
- for fileInfo in tarFile: |
- name = fileInfo.name |
- if name.startswith("./"): |
- name = name[2:] |
+ with tarfile.open(mode="r:", fileobj=StringIO(data)) as archive: |
+ for fileinfo in archive: |
+ 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() |
- tarFile.close() |
+ if fileinfo.type == tarfile.REGTYPE: |
+ data = codecs.getreader("utf8")(archive.extractfile(fileinfo)) |
+ try: |
+ notification = parse_notification(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() |
+ with codecs.open(path, "wb", encoding="utf-8") as file: |
+ json.dump(result, file, ensure_ascii=False, indent=2, |
+ separators=(',', ': '), sort_keys=True) |
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) |
+ generate_notifications(repo, output) |