| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # coding: utf-8 | 2 # coding: utf-8 |
| 3 | 3 |
| 4 # This file is part of Adblock Plus <https://adblockplus.org/>, | 4 # This file is part of Adblock Plus <https://adblockplus.org/>, |
| 5 # Copyright (C) 2006-2015 Eyeo GmbH | 5 # Copyright (C) 2006-2015 Eyeo GmbH |
| 6 # | 6 # |
| 7 # Adblock Plus is free software: you can redistribute it and/or modify | 7 # Adblock Plus is free software: you can redistribute it and/or modify |
| 8 # it under the terms of the GNU General Public License version 3 as | 8 # it under the terms of the GNU General Public License version 3 as |
| 9 # published by the Free Software Foundation. | 9 # published by the Free Software Foundation. |
| 10 # | 10 # |
| 11 # Adblock Plus is distributed in the hope that it will be useful, | 11 # Adblock Plus is distributed in the hope that it will be useful, |
| 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of | 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 # GNU General Public License for more details. | 14 # GNU General Public License for more details. |
| 15 # | 15 # |
| 16 # You should have received a copy of the GNU General Public License | 16 # You should have received a copy of the GNU General Public License |
| 17 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 17 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 18 | 18 |
| 19 import io | |
| 20 import json | |
| 21 import os | |
| 22 import posixpath | |
| 23 import re | |
| 24 import urlparse | |
| 19 import flask | 25 import flask |
|
Felix Dahlke
2015/02/04 04:02:35
While we're nit picking :)
PEP-8: "You should put
| |
| 20 import os | |
| 21 import re | |
| 22 from urlparse import urlparse | |
| 23 | 26 |
| 24 app = flask.Flask(__name__) | 27 app = flask.Flask(__name__) |
| 25 | 28 |
| 29 def get_scripts(root): | |
| 30 for dir, _, files in os.walk(root): | |
| 31 for file in files: | |
| 32 if os.path.splitext(file)[1] == ".js": | |
| 33 path = os.path.join(dir, file) | |
|
Felix Dahlke
2015/02/04 03:58:51
posixpath.join?
Wladimir Palant
2015/02/04 15:37:14
No, we need a proper OS-specific path here, only t
Felix Dahlke
2015/02/04 18:20:25
OK, fair enough. Considering that I didn't fully g
Wladimir Palant
2015/02/04 20:46:14
I don't think variable naming will make it any cle
| |
| 34 relpath = posixpath.relpath(path, root) | |
| 35 yield (path, relpath) | |
| 36 | |
| 26 def js_encode(str): | 37 def js_encode(str): |
| 27 return re.sub(r"(['\\])", r"\\\1", str) | 38 return json.dumps(str) |
| 39 | |
| 40 def script_as_string(path, sourceURL, backcompat): | |
| 41 if backcompat: | |
| 42 from jshydra.abp_rewrite import doRewrite | |
| 43 data = doRewrite([os.path.abspath(path)], []).decode("utf-8") | |
| 44 else: | |
| 45 with io.open(path, "r", encoding="utf-8") as handle: | |
| 46 data = handle.read() | |
| 47 data += "\n//# sourceURL=%s" % sourceURL | |
| 48 return js_encode(data) | |
| 28 | 49 |
| 29 @app.route("/<path:path>", methods = ["GET"]) | 50 @app.route("/<path:path>", methods = ["GET"]) |
| 30 @app.route("/", methods = ["GET"]) | 51 @app.route("/", methods = ["GET"]) |
| 31 def multiplex(path=""): | 52 def multiplex(path=""): |
| 32 request_url = urlparse(flask.request.url) | 53 request_url = urlparse.urlparse(flask.request.url) |
| 33 request_path = request_url.path | 54 request_path = request_url.path |
| 34 islib = request_path.startswith("/lib/") | 55 islib = request_path.startswith("/lib/") |
| 35 backcompat = request_url.query == "backcompat" | 56 backcompat = request_url.query == "backcompat" |
| 36 | 57 |
| 37 rootdir = os.path.dirname(__file__) | 58 rootdir = os.path.dirname(__file__) |
| 38 if not islib: | 59 if request_path == "/lib.js": |
| 39 rootdir = os.path.join("test") | 60 data = "" |
| 40 | 61 for path, relpath in get_scripts(os.path.join(rootdir, "lib")): |
| 41 if islib or backcompat: | 62 url = urlparse.urljoin(flask.request.url, "/lib/" + relpath) |
| 42 path = flask.safe_join(rootdir, request_path.lstrip("/")) | 63 data += "require.sources[%s] = %s;\n" % ( |
| 43 if not os.path.isfile(path): | 64 js_encode(os.path.splitext(relpath)[0]), |
| 44 return flask.abort(404) | 65 script_as_string(path, url, backcompat) |
| 45 | 66 ) |
| 46 module = os.path.splitext(request_path[len("/lib/"):])[0] | 67 return (data, 200, {"Content-Type": "application/javascript; charset=utf-8"} ) |
| 47 if backcompat: | 68 elif request_path == "/tests.js": |
| 48 from jshydra.abp_rewrite import doRewrite | 69 data = "var tests = [" |
| 49 data = doRewrite([os.path.abspath(path)], ["module=true"] if islib else [] ) | 70 for path, relpath in get_scripts(os.path.join(rootdir, "test", "tests")): |
| 50 data = re.sub(r"require\.scopes\[.*?\]", "require.scopes['%s']" % module, data) | 71 url = urlparse.urljoin(flask.request.url, "/tests/" + relpath) |
| 51 else: | 72 data += " %s,\n" % script_as_string(path, url, backcompat) |
| 52 with open(path, "rb") as file: | 73 data += "];" |
| 53 data = "require.scopes['%s'] = function(){var exports={};%s\nreturn expo rts;}();" % (module, file.read()) | |
| 54 return (data, 200, {"Content-Type": "application/javascript; charset=utf-8"} ) | 74 return (data, 200, {"Content-Type": "application/javascript; charset=utf-8"} ) |
| 55 else: | 75 else: |
| 76 if request_path.startswith("/lib/"): | |
| 77 rootdir = os.path.join(rootdir, "lib") | |
| 78 else: | |
| 79 rootdir = os.path.join(rootdir, "test") | |
| 56 if request_path.endswith("/"): | 80 if request_path.endswith("/"): |
| 57 request_path += "index.html" | 81 request_path += "index.html" |
| 58 return flask.send_from_directory(rootdir, request_path.lstrip("/")) | 82 return flask.send_from_directory(rootdir, request_path.lstrip("/")) |
| 59 | 83 |
| 60 if __name__ == "__main__": | 84 if __name__ == "__main__": |
| 61 app.run(debug=True) | 85 app.run(debug=True) |
| OLD | NEW |