Index: run_tests.py |
=================================================================== |
--- a/run_tests.py |
+++ b/run_tests.py |
@@ -11,51 +11,75 @@ |
# 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 |
Felix Dahlke
2015/02/04 04:02:35
While we're nit picking :)
PEP-8: "You should put
|
-import os |
-import re |
-from urlparse import urlparse |
app = flask.Flask(__name__) |
+def get_scripts(root): |
+ for dir, _, files in os.walk(root): |
+ for file in files: |
+ if os.path.splitext(file)[1] == ".js": |
+ 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
|
+ relpath = posixpath.relpath(path, root) |
+ 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 = "" |
+ 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(os.path.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) |