Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
42 | 42 |
43 """, autoescape=True) | 43 """, autoescape=True) |
44 | 44 |
45 def _get_latest_build(builds_dir): | 45 def _get_latest_build(builds_dir): |
46 latest_build = {"id": 0} | 46 latest_build = {"id": 0} |
47 for json_path in glob.glob(os.path.join(builds_dir, "adblockbrowser-*.json")): | 47 for json_path in glob.glob(os.path.join(builds_dir, "adblockbrowser-*.json")): |
48 with open(json_path) as json_file: | 48 with open(json_path) as json_file: |
49 build_id = int(json.loads(json_file.read())["buildid"]) | 49 build_id = int(json.loads(json_file.read())["buildid"]) |
50 if build_id > latest_build["id"]: | 50 if build_id > latest_build["id"]: |
51 latest_build["id"] = build_id | 51 latest_build["id"] = build_id |
52 apk_path = re.sub(r"\.json$", ".apk", json_path) | 52 apk_path = os.path.splitext(json_path)[0] + ".apk" |
Sebastian Noack
2015/11/13 22:07:39
Checking for the file extension here is redundant.
Felix Dahlke
2015/11/14 00:41:05
Done.
| |
53 latest_build["path"] = os.path.join(builds_dir, apk_path) | 53 latest_build["path"] = os.path.join(builds_dir, apk_path) |
54 if latest_build["id"] == 0: | 54 if latest_build["id"] == 0: |
55 return {} | 55 return {} |
56 return latest_build | 56 return latest_build |
57 | 57 |
58 def _render_manifest(build=None, devbuild=False): | 58 def _render_manifest(build=None, builds_url=None): |
59 if not build: | 59 if not build: |
60 return _MANIFEST_TEMPLATE.render() | 60 return _MANIFEST_TEMPLATE.render() |
61 | 61 |
62 if devbuild: | |
63 nightlies_url = get_config().get("extensions", "nightliesURL") | |
64 builds_url = "%s/adblockbrowser" % nightlies_url.rstrip("/") | |
Sebastian Noack
2015/11/13 22:07:39
rstrip("/") is duplicated. How about moving it bel
Felix Dahlke
2015/11/14 00:41:05
How would that look? Note that I need to call rstr
| |
65 else: | |
66 builds_url = get_config().get("extensions", "downloadsURL").rstrip("/") | |
67 build_url = "%s/%s?update" % (builds_url, os.path.basename(build["path"])) | 62 build_url = "%s/%s?update" % (builds_url, os.path.basename(build["path"])) |
68 with open(build["path"], "rb") as build_file: | 63 with open(build["path"], "rb") as build_file: |
69 build_content = build_file.read() | 64 build_content = build_file.read() |
70 return _MANIFEST_TEMPLATE.render({ | 65 return _MANIFEST_TEMPLATE.render({ |
71 "build": { | 66 "build": { |
72 "build_id": build["id"], | 67 "build_id": build["id"], |
73 "url": build_url, | 68 "url": build_url, |
74 "hash_function": "SHA512", | 69 "hash_function": "SHA512", |
75 "hash_value": hashlib.sha512(build_content).hexdigest(), | 70 "hash_value": hashlib.sha512(build_content).hexdigest(), |
76 "size": len(build_content) | 71 "size": len(build_content) |
77 } | 72 } |
78 }) | 73 }) |
79 | 74 |
80 def _get_update_manifest(current_build_id, devbuild): | 75 def _get_update_manifest(current_build_id, builds_dir, builds_url): |
81 if devbuild: | |
82 nightlies_dir = get_config().get("extensions", "nightliesDirectory") | |
83 builds_dir = os.path.join(nightlies_dir, "adblockbrowser") | |
84 else: | |
85 builds_dir = get_config().get("extensions", "downloadsDirectory") | |
86 | |
87 if not os.path.isdir(builds_dir): | 76 if not os.path.isdir(builds_dir): |
88 return _render_manifest() | 77 return _render_manifest() |
89 | 78 |
90 latest_build = _get_latest_build(builds_dir) | 79 latest_build = _get_latest_build(builds_dir) |
91 if not latest_build or current_build_id >= latest_build["id"]: | 80 if not latest_build or current_build_id >= latest_build["id"]: |
92 return _render_manifest() | 81 return _render_manifest() |
93 return _render_manifest(latest_build, devbuild) | 82 return _render_manifest(latest_build, builds_url) |
94 | 83 |
95 def _handle_request(environ, start_response, devbuild): | 84 def _handle_request(environ, start_response, builds_dir, builds_url): |
96 params = parse_qs(environ.get("QUERY_STRING", "")) | 85 params = parse_qs(environ.get("QUERY_STRING", "")) |
97 try: | 86 try: |
98 version = params.get("addonVersion", [""])[0] | 87 version = params.get("addonVersion", [""])[0] |
99 build_id = int(re.search(r"(\d+)$", version).group(1)) | 88 build_id = int(re.search(r"(\d+)$", version).group(1)) |
100 except: | 89 except: |
101 start_response("400 Processing Error", [("Content-Type", "text/plain")]) | 90 start_response("400 Processing Error", [("Content-Type", "text/plain")]) |
102 return ["Failed to parse addonVersion."] | 91 return ["Failed to parse addonVersion."] |
103 manifest = _get_update_manifest(build_id, devbuild) | 92 manifest = _get_update_manifest(build_id, builds_dir, builds_url) |
104 response = manifest.encode("utf-8") | 93 response = manifest.encode("utf-8") |
105 start_response("200 OK", [("Content-Type", "application/xml; charset=utf-8")]) | 94 start_response("200 OK", [("Content-Type", "application/xml; charset=utf-8")]) |
106 return [response] | 95 return [response] |
107 | 96 |
108 @url_handler("/adblockbrowser/updates.xml") | 97 @url_handler("/adblockbrowser/updates.xml") |
109 def adblockbrowser_updates(environ, start_response): | 98 def adblockbrowser_updates(environ, start_response): |
110 return _handle_request(environ, start_response, False) | 99 config = get_config() |
Sebastian Noack
2015/11/13 22:07:39
I think the code flow would be simpler when you de
Felix Dahlke
2015/11/14 00:41:05
Done.
| |
100 | |
101 builds_dir = config.get("extensions", "downloadsDirectory") | |
102 builds_url = config.get("extensions", "downloadsURL").rstrip("/") | |
103 | |
104 return _handle_request(environ, start_response, builds_dir, builds_url) | |
111 | 105 |
112 @url_handler("/devbuilds/adblockbrowser/updates.xml") | 106 @url_handler("/devbuilds/adblockbrowser/updates.xml") |
113 def adblockbrowser_devbuild_updates(environ, start_response): | 107 def adblockbrowser_devbuild_updates(environ, start_response): |
114 return _handle_request(environ, start_response, True) | 108 config = get_config() |
109 | |
110 nightlies_dir = config.get("extensions", "nightliesDirectory") | |
111 builds_dir = os.path.join(nightlies_dir, "adblockbrowser") | |
112 | |
113 nightlies_url = config.get("extensions", "nightliesURL").rstrip("/") | |
114 builds_url = "%s/adblockbrowser" % nightlies_url | |
115 | |
116 return _handle_request(environ, start_response, builds_dir, builds_url) | |
LEFT | RIGHT |