Index: run_tests.py |
=================================================================== |
--- a/run_tests.py |
+++ b/run_tests.py |
@@ -11,51 +11,80 @@ |
# Adblock Plus is distributed in the hope that it will be useful, |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
# 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 io |
+import json |
+import os |
+import posixpath |
+import re |
+import urlparse |
+ |
import flask |
-import os |
-import re |
-from urlparse import urlparse |
app = flask.Flask(__name__) |
+def get_scripts(dir, reldir=""): |
+ for filename in os.listdir(dir): |
+ path = os.path.join(dir, filename) |
+ # "Relative path" will be the parameter passed in to require(), meaning |
+ # that it always has to use forward slashes. |
+ relpath = posixpath.join(reldir, filename) |
+ 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 json.dumps(str) |
+ |
+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 = "" |
Sebastian Noack
2015/02/05 11:32:21
Instead concatenating the whole string here, you m
Wladimir Palant
2015/02/05 14:51:26
I think we are getting extremely nit-picky now, co
|
+ for path, relpath in get_scripts(os.path.join(rootdir, "lib")): |
+ url = urlparse.urljoin(flask.request.url, "/lib/" + relpath) |
+ data += "require.sources[%s] = %s;\n" % ( |
+ js_encode(posixpath.splitext(relpath)[0]), |
+ 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) |