forked from google/vsaq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vsaq_server.py
116 lines (97 loc) · 3.68 KB
/
vsaq_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# Copyright 2016 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Runs a test server for VSAQ."""
import BaseHTTPServer
import cgi
import fnmatch
import os
import os.path
import re
import SimpleHTTPServer
import sys
PORT = 9000
if len(sys.argv) > 1:
PORT = int(sys.argv[1])
server_address = ("127.0.0.1", PORT)
#./ do.sh testserver generates the file
DEPS_FILE = "build/deps-runfiles.js"
ALL_JSTESTS_FILE = "build/all_tests.js"
class TestServerRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
"""Request handler for VSAQ test server."""
DIRECTORY_MAP = {
"/": "build/",
"/vsaq/": "vsaq/",
"/vsaq/static/questionnaire/": "vsaq/static/questionnaire/",
"/javascript/closure/": "third_party/closure-library/closure/goog/",
"/javascript/vsaq/": "vsaq/",
"/third_party/closure/":
"third_party/closure-library/third_party/closure/",
"/third_party/closure-templates-compiler/":
"third_party/closure-templates-compiler/",
"/build/templates/vsaq/static/questionnaire/":
"build/templates/vsaq/static/questionnaire/",
}
def get_test_files(self):
test_files = []
for root, _, files in os.walk("vsaq/"):
for f in fnmatch.filter(files, "*test_dom.html"):
test_files.append(os.path.join(root, f))
return test_files
def generate_all_tests_file(self):
if os.path.exists(ALL_JSTESTS_FILE):
return
with open(ALL_JSTESTS_FILE, "wb") as f:
f.write("var _allTests=")
f.write(repr(self.get_test_files()))
f.write(";")
def show_tests(self):
"""Lists only vsaq/**/_test.html files."""
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
test_files = self.get_test_files()
self.wfile.write("<h2>VSAQ test server</h2>")
self.wfile.write("<h3>Test suite</h3>")
self.wfile.write("<a href=\"%s\">%s</a>\n" % ("/all_tests.html",
"all_tests.html"))
self.wfile.write("<h3>Individual tests</h3>")
self.wfile.write("<ul>")
for f in test_files:
self.wfile.write("<li><a href=\"/%s\">%s</a>\n" % (f, cgi.escape(f)))
self.wfile.write("</ul>")
return
def do_GET(self):
print self.path
if self.path == "/tests.html" or self.path == "/tests.html/":
self.show_tests()
else:
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
def translate_path(self, path):
"""Serves files from different directories."""
# Remove all parameters from filenames.
path = re.sub(r"\?.*$", "", path)
if path.endswith("deps-runfiles.js"):
return DEPS_FILE
if path == "/" + ALL_JSTESTS_FILE:
self.generate_all_tests_file()
return ALL_JSTESTS_FILE
for prefix, dest_dir in TestServerRequestHandler.DIRECTORY_MAP.items():
print "checking: " + dest_dir + path[len(prefix):]
if path.startswith(prefix) and os.path.isfile(
dest_dir + path[len(prefix):]):
return dest_dir + path[len(prefix):]
return "build/index.html"
httpd = BaseHTTPServer.HTTPServer(server_address, TestServerRequestHandler)
print "Starting the VSAQ server at http://%s:%d" % server_address
httpd.serve_forever()