Index: run_tests.py |
=================================================================== |
--- a/run_tests.py |
+++ b/run_tests.py |
@@ -14,48 +14,73 @@ |
# GNU General Public License for more details. |
# |
# 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 flask |
import os |
import re |
-from urlparse import urlparse |
+import io |
Felix Dahlke
2015/02/03 06:21:16
Nit: The import statements seem to be ordered alph
Sebastian Noack
2015/02/03 09:21:10
Note that according to PEP-8, third-party module i
Wladimir Palant
2015/02/03 16:26:04
Done.
|
+import urlparse |
app = flask.Flask(__name__) |
+def get_scripts(dir, reldir = ""): |
Felix Dahlke
2015/02/03 06:21:16
This would be a bit shorter (and IMHO easier to re
Sebastian Noack
2015/02/03 09:21:10
Even more compact:
import glob
...
def get_scri
Wladimir Palant
2015/02/03 16:26:04
This won't find anything in subdirectories.
Perso
|
+ for name in os.listdir(dir): |
+ path = os.path.join(dir, name) |
+ relpath = reldir + "/" + name |
+ if os.path.isdir(path): |
+ for path, relpath in get_scripts(path, relpath): |
+ yield (path, relpath) |
+ elif os.path.splitext(path)[1] == ".js": |
+ yield (path, relpath) |
+ |
def js_encode(str): |
- return re.sub(r"(['\\])", r"\\\1", str) |
+ return (str.replace("\\", "\\\\").replace("'", "\\'") |
+ .replace("\r", "\\r").replace("\n", "\\n")) |
Felix Dahlke
2015/02/03 06:21:16
Makes me wonder if we'll run into trouble when usi
Wladimir Palant
2015/02/03 16:26:04
No, these should be fine within a string. See http
|
+ |
+def script_as_string(path, sourceURL, backcompat): |
+ if backcompat: |
+ from jshydra.abp_rewrite import doRewrite |
+ data = doRewrite([os.path.abspath(path)], []).decode("utf-8") |
+ else: |
+ with io.open(path, "r", encoding="utf-8") as handle: |
+ data = handle.read() |
+ data += "\n//# sourceURL=%s" % sourceURL |
+ return js_encode(data) |
@app.route("/<path:path>", methods = ["GET"]) |
@app.route("/", methods = ["GET"]) |
def multiplex(path=""): |
- request_url = urlparse(flask.request.url) |
+ request_url = urlparse.urlparse(flask.request.url) |
request_path = request_url.path |
islib = request_path.startswith("/lib/") |
backcompat = request_url.query == "backcompat" |
rootdir = os.path.dirname(__file__) |
- if not islib: |
- rootdir = os.path.join("test") |
- |
- if islib or backcompat: |
- path = flask.safe_join(rootdir, request_path.lstrip("/")) |
- if not os.path.isfile(path): |
- return flask.abort(404) |
- |
- module = os.path.splitext(request_path[len("/lib/"):])[0] |
- if backcompat: |
- from jshydra.abp_rewrite import doRewrite |
- data = doRewrite([os.path.abspath(path)], ["module=true"] if islib else []) |
- data = re.sub(r"require\.scopes\[.*?\]", "require.scopes['%s']" % module, data) |
- else: |
- with open(path, "rb") as file: |
- data = "require.scopes['%s'] = function(){var exports={};%s\nreturn exports;}();" % (module, file.read()) |
+ if request_path == "/lib.js": |
+ data = "" |
+ for path, relpath in get_scripts(os.path.join(rootdir, "lib")): |
+ url = urlparse.urljoin(flask.request.url, "/lib" + relpath) |
+ data += "require.modules['%s'] = '%s';\n" % ( |
+ os.path.splitext(relpath)[0][1:], |
+ script_as_string(path, url, backcompat) |
+ ) |
+ return (data, 200, {"Content-Type": "application/javascript; charset=utf-8"}) |
+ elif request_path == "/tests.js": |
+ data = "var tests = [" |
+ for path, relpath in get_scripts(os.path.join(rootdir, "test", "tests")): |
+ url = urlparse.urljoin(flask.request.url, "/tests" + relpath) |
+ data += " '%s',\n" % script_as_string(path, url, backcompat) |
+ data += "];" |
return (data, 200, {"Content-Type": "application/javascript; charset=utf-8"}) |
else: |
+ if request_path.startswith("/lib/"): |
+ rootdir = os.path.join(rootdir, "lib") |
+ else: |
+ rootdir = os.path.join(rootdir, "test") |
if request_path.endswith("/"): |
request_path += "index.html" |
return flask.send_from_directory(rootdir, request_path.lstrip("/")) |
if __name__ == "__main__": |
app.run(debug=True) |