| OLD | NEW | 
| (Empty) |  | 
 |   1 # coding: utf-8 | 
 |   2  | 
 |   3 # This file is part of the Adblock Plus web scripts, | 
 |   4 # Copyright (C) 2006-2015 Eyeo GmbH | 
 |   5 # | 
 |   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 | 
 |   8 # published by the Free Software Foundation. | 
 |   9 # | 
 |  10 # Adblock Plus is distributed in the hope that it will be useful, | 
 |  11 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
 |  12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
 |  13 # GNU General Public License for more details. | 
 |  14 # | 
 |  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/>. | 
 |  17  | 
 |  18 import hashlib | 
 |  19 import os | 
 |  20 import re | 
 |  21 from urlparse import parse_qs | 
 |  22  | 
 |  23 from jinja2 import Template | 
 |  24  | 
 |  25 from sitescripts.utils import get_config | 
 |  26 from sitescripts.web import url_handler | 
 |  27  | 
 |  28 _MANIFEST_TEMPLATE = Template("""<?xml version="1.0"?> | 
 |  29 <updates> | 
 |  30 {% if build %} | 
 |  31     <update buildID="{{ build.build_id }}"> | 
 |  32         <patch | 
 |  33           URL="{{ build.url }}" | 
 |  34           hashFunction="{{ build.hash_function }}" | 
 |  35           hashValue="{{ build.hash_value }}" | 
 |  36           size="{{ build.size }}"/> | 
 |  37     </update> | 
 |  38 {% endif %} | 
 |  39 </updates> | 
 |  40  | 
 |  41 """, autoescape=True) | 
 |  42  | 
 |  43 def _get_latest_build(builds_dir): | 
 |  44   latest_build = {"id": 0} | 
 |  45   for file in os.listdir(builds_dir): | 
 |  46     match = re.search(r"^adblockbrowser-.*?(\d+)-\w+\.apk$", file) | 
 |  47     if match: | 
 |  48       build_id = int(match.group(1)) | 
 |  49       if build_id > latest_build["id"]: | 
 |  50         latest_build["id"] = build_id | 
 |  51         latest_build["path"] = os.path.join(builds_dir, file) | 
 |  52   if latest_build["id"] == 0: | 
 |  53     return {} | 
 |  54   return latest_build | 
 |  55  | 
 |  56 def _render_manifest(build=None): | 
 |  57   if not build: | 
 |  58     return _MANIFEST_TEMPLATE.render() | 
 |  59  | 
 |  60   nightlies_url = get_config().get("extensions", "nightliesURL") | 
 |  61   build_url = "%s/adblockbrowser/%s?update" % (nightlies_url.rstrip("/"), | 
 |  62                                                os.path.basename(build["path"])) | 
 |  63   with open(build["path"], "rb") as build_file: | 
 |  64     build_content = build_file.read() | 
 |  65   return _MANIFEST_TEMPLATE.render({ | 
 |  66     "build": { | 
 |  67       "build_id": build["id"], | 
 |  68       "url": build_url, | 
 |  69       "hash_function": "SHA512", | 
 |  70       "hash_value": hashlib.sha512(build_content).hexdigest(), | 
 |  71       "size": len(build_content) | 
 |  72     } | 
 |  73   }) | 
 |  74  | 
 |  75 def _get_update_manifest(current_build_id): | 
 |  76   nightlies_dir = get_config().get("extensions", "nightliesDirectory") | 
 |  77   builds_dir = os.path.join(nightlies_dir, "adblockbrowser") | 
 |  78   if not os.path.isdir(builds_dir): | 
 |  79     return _render_manifest() | 
 |  80  | 
 |  81   latest_build = _get_latest_build(builds_dir) | 
 |  82   if not latest_build or current_build_id >= latest_build["id"]: | 
 |  83     return _render_manifest() | 
 |  84   return _render_manifest(latest_build) | 
 |  85  | 
 |  86 @url_handler("/devbuilds/adblockbrowser/updates.xml") | 
 |  87 def adblockbrowser_updates(environ, start_response): | 
 |  88   params = parse_qs(environ.get("QUERY_STRING", "")) | 
 |  89   try: | 
 |  90     version = params.get("addonVersion", [""])[0] | 
 |  91     build_id = int(re.search(r"(\d+)$", version).group(1)) | 
 |  92   except: | 
 |  93     start_response("400 Processing Error", [("Content-Type", "text/plain")]) | 
 |  94     return ["Failed to parse addonVersion."] | 
 |  95   manifest = _get_update_manifest(build_id) | 
 |  96   response = manifest.encode("utf-8") | 
 |  97   start_response("200 OK", [("Content-Type", "application/xml; charset=utf-8")]) | 
 |  98   return [response] | 
| OLD | NEW |