| OLD | NEW | 
|    1 # This file is part of the Adblock Plus web scripts, |    1 # This file is part of the Adblock Plus web scripts, | 
|    2 # Copyright (C) 2006-2016 Eyeo GmbH |    2 # Copyright (C) 2006-2016 Eyeo GmbH | 
|    3 # |    3 # | 
|    4 # Adblock Plus is free software: you can redistribute it and/or modify |    4 # Adblock Plus is free software: you can redistribute it and/or modify | 
|    5 # it under the terms of the GNU General Public License version 3 as |    5 # it under the terms of the GNU General Public License version 3 as | 
|    6 # published by the Free Software Foundation. |    6 # published by the Free Software Foundation. | 
|    7 # |    7 # | 
|    8 # Adblock Plus is distributed in the hope that it will be useful, |    8 # Adblock Plus is distributed in the hope that it will be useful, | 
|    9 # but WITHOUT ANY WARRANTY; without even the implied warranty of |    9 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|   10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the |   10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|   11 # GNU General Public License for more details. |   11 # GNU General Public License for more details. | 
|   12 # |   12 # | 
|   13 # You should have received a copy of the GNU General Public License |   13 # You should have received a copy of the GNU General Public License | 
|   14 # along with Adblock Plus.  If not, see <http://www.gnu.org/licenses/>. |   14 # along with Adblock Plus.  If not, see <http://www.gnu.org/licenses/>. | 
|   15  |   15  | 
|   16 import glob |   16 import glob | 
|   17 import hashlib |   17 import hashlib | 
|   18 import json |   18 import json | 
|   19 import os |   19 import os | 
|   20 import re |   20 import re | 
|   21 from urlparse import parse_qs |   21 from urlparse import parse_qs | 
|   22  |   22  | 
|   23 from jinja2 import Template |   23 from jinja2 import Template | 
|   24  |   24  | 
|   25 from sitescripts.utils import get_config |   25 from sitescripts.utils import get_config | 
|   26 from sitescripts.web import url_handler |   26 from sitescripts.web import url_handler | 
|   27  |   27  | 
|   28 _MANIFEST_TEMPLATE = Template("""<?xml version="1.0"?> |   28 _MANIFEST_TEMPLATE = Template('''<?xml version="1.0"?> | 
|   29 <updates> |   29 <updates> | 
|   30 {% if build %} |   30 {% if build %} | 
|   31     <update buildID="{{ build.build_id }}"> |   31     <update buildID="{{ build.build_id }}"> | 
|   32         <patch |   32         <patch | 
|   33           URL="{{ build.url }}" |   33           URL="{{ build.url }}" | 
|   34           hashFunction="{{ build.hash_function }}" |   34           hashFunction="{{ build.hash_function }}" | 
|   35           hashValue="{{ build.hash_value }}" |   35           hashValue="{{ build.hash_value }}" | 
|   36           size="{{ build.size }}"/> |   36           size="{{ build.size }}"/> | 
|   37     </update> |   37     </update> | 
|   38 {% endif %} |   38 {% endif %} | 
|   39 </updates> |   39 </updates> | 
|   40  |   40  | 
|   41 """, autoescape=True) |   41 ''', autoescape=True) | 
|   42  |   42  | 
|   43  |   43  | 
|   44 def _get_latest_build(builds_dir): |   44 def _get_latest_build(builds_dir): | 
|   45     latest_build = {"id": 0} |   45     latest_build = {'id': 0} | 
|   46     for json_path in glob.glob(os.path.join(builds_dir, "adblockbrowser-*.json")
     ): |   46     for json_path in glob.glob(os.path.join(builds_dir, 'adblockbrowser-*.json')
     ): | 
|   47         with open(json_path) as json_file: |   47         with open(json_path) as json_file: | 
|   48             build_id = int(json.loads(json_file.read())["buildid"]) |   48             build_id = int(json.loads(json_file.read())['buildid']) | 
|   49         if build_id > latest_build["id"]: |   49         if build_id > latest_build['id']: | 
|   50             latest_build["id"] = build_id |   50             latest_build['id'] = build_id | 
|   51             apk_path = os.path.splitext(json_path)[0] + ".apk" |   51             apk_path = os.path.splitext(json_path)[0] + '.apk' | 
|   52             latest_build["path"] = os.path.join(builds_dir, apk_path) |   52             latest_build['path'] = os.path.join(builds_dir, apk_path) | 
|   53     if latest_build["id"] == 0: |   53     if latest_build['id'] == 0: | 
|   54         return {} |   54         return {} | 
|   55     return latest_build |   55     return latest_build | 
|   56  |   56  | 
|   57  |   57  | 
|   58 def _render_manifest(build=None, builds_url=None): |   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     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'])) | 
|   63     with open(build["path"], "rb") as build_file: |   63     with open(build['path'], 'rb') as build_file: | 
|   64         build_content = build_file.read() |   64         build_content = build_file.read() | 
|   65     return _MANIFEST_TEMPLATE.render({ |   65     return _MANIFEST_TEMPLATE.render({ | 
|   66         "build": { |   66         'build': { | 
|   67             "build_id": build["id"], |   67             'build_id': build['id'], | 
|   68             "url": build_url, |   68             'url': build_url, | 
|   69             "hash_function": "SHA512", |   69             'hash_function': 'SHA512', | 
|   70             "hash_value": hashlib.sha512(build_content).hexdigest(), |   70             'hash_value': hashlib.sha512(build_content).hexdigest(), | 
|   71             "size": len(build_content) |   71             'size': len(build_content) | 
|   72         } |   72         } | 
|   73     }) |   73     }) | 
|   74  |   74  | 
|   75  |   75  | 
|   76 def _get_update_manifest(current_build_id, builds_dir, builds_url): |   76 def _get_update_manifest(current_build_id, builds_dir, builds_url): | 
|   77     if not os.path.isdir(builds_dir): |   77     if not os.path.isdir(builds_dir): | 
|   78         return _render_manifest() |   78         return _render_manifest() | 
|   79  |   79  | 
|   80     latest_build = _get_latest_build(builds_dir) |   80     latest_build = _get_latest_build(builds_dir) | 
|   81     if not latest_build or current_build_id >= latest_build["id"]: |   81     if not latest_build or current_build_id >= latest_build['id']: | 
|   82         return _render_manifest() |   82         return _render_manifest() | 
|   83     return _render_manifest(latest_build, builds_url) |   83     return _render_manifest(latest_build, builds_url) | 
|   84  |   84  | 
|   85  |   85  | 
|   86 def _handle_request(environ, start_response, builds_dir, builds_url): |   86 def _handle_request(environ, start_response, builds_dir, builds_url): | 
|   87     params = parse_qs(environ.get("QUERY_STRING", "")) |   87     params = parse_qs(environ.get('QUERY_STRING', '')) | 
|   88     try: |   88     try: | 
|   89         version = params.get("addonVersion", [""])[0] |   89         version = params.get('addonVersion', [''])[0] | 
|   90         build_id = int(re.search(r"(\d+)$", version).group(1)) |   90         build_id = int(re.search(r'(\d+)$', version).group(1)) | 
|   91     except: |   91     except: | 
|   92         start_response("400 Processing Error", [("Content-Type", "text/plain")]) |   92         start_response('400 Processing Error', [('Content-Type', 'text/plain')]) | 
|   93         return ["Failed to parse addonVersion."] |   93         return ['Failed to parse addonVersion.'] | 
|   94     manifest = _get_update_manifest(build_id, builds_dir, builds_url) |   94     manifest = _get_update_manifest(build_id, builds_dir, builds_url) | 
|   95     response = manifest.encode("utf-8") |   95     response = manifest.encode('utf-8') | 
|   96     start_response("200 OK", [("Content-Type", "application/xml; charset=utf-8")
     ]) |   96     start_response('200 OK', [('Content-Type', 'application/xml; charset=utf-8')
     ]) | 
|   97     return [response] |   97     return [response] | 
|   98  |   98  | 
|   99  |   99  | 
|  100 @url_handler("/adblockbrowser/updates.xml") |  100 @url_handler('/adblockbrowser/updates.xml') | 
|  101 def adblockbrowser_updates(environ, start_response): |  101 def adblockbrowser_updates(environ, start_response): | 
|  102     config = get_config() |  102     config = get_config() | 
|  103  |  103  | 
|  104     builds_dir = config.get("extensions", "downloadsDirectory") |  104     builds_dir = config.get('extensions', 'downloadsDirectory') | 
|  105     builds_url = config.get("extensions", "downloadsURL").rstrip("/") |  105     builds_url = config.get('extensions', 'downloadsURL').rstrip('/') | 
|  106  |  106  | 
|  107     return _handle_request(environ, start_response, builds_dir, builds_url) |  107     return _handle_request(environ, start_response, builds_dir, builds_url) | 
|  108  |  108  | 
|  109  |  109  | 
|  110 @url_handler("/devbuilds/adblockbrowser/updates.xml") |  110 @url_handler('/devbuilds/adblockbrowser/updates.xml') | 
|  111 def adblockbrowser_devbuild_updates(environ, start_response): |  111 def adblockbrowser_devbuild_updates(environ, start_response): | 
|  112     config = get_config() |  112     config = get_config() | 
|  113  |  113  | 
|  114     nightlies_dir = config.get("extensions", "nightliesDirectory") |  114     nightlies_dir = config.get('extensions', 'nightliesDirectory') | 
|  115     builds_dir = os.path.join(nightlies_dir, "adblockbrowser") |  115     builds_dir = os.path.join(nightlies_dir, 'adblockbrowser') | 
|  116  |  116  | 
|  117     nightlies_url = config.get("extensions", "nightliesURL").rstrip("/") |  117     nightlies_url = config.get('extensions', 'nightliesURL').rstrip('/') | 
|  118     builds_url = "%s/adblockbrowser" % nightlies_url |  118     builds_url = '%s/adblockbrowser' % nightlies_url | 
|  119  |  119  | 
|  120     return _handle_request(environ, start_response, builds_dir, builds_url) |  120     return _handle_request(environ, start_response, builds_dir, builds_url) | 
| OLD | NEW |