2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
12 from build import parse_deps
13 from build import generate
16 import SimpleHTTPServer
22 toplevel_dir = os.path.abspath(os.path.dirname(__file__))
23 src_dir = os.path.join(toplevel_dir, 'src')
24 test_data_dir = os.path.join(toplevel_dir, 'test_data')
25 skp_data_dir = os.path.join(toplevel_dir, 'skp_data')
27 def find_all_js_module_filenames(search_paths):
31 if os.path.basename(x).startswith('.'):
33 if os.path.splitext(x)[1] != ".js":
37 for search_path in search_paths:
38 for dirpath, dirnames, filenames in os.walk(search_path):
40 x = os.path.join(dirpath, f)
43 all_filenames.append(os.path.relpath(x, search_path))
47 class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler):
48 def __init__(self, *args, **kwargs):
49 SimpleHTTPServer.SimpleHTTPRequestHandler.__init__(self, *args, **kwargs)
51 def send_response(self, code, message=None):
52 SimpleHTTPServer.SimpleHTTPRequestHandler.send_response(self, code, message)
54 self.send_header('Cache-Control', 'no-cache')
56 def do_GET_json_tests(self):
58 basename = os.path.basename(x)
59 if basename.startswith('.'):
62 if basename.endswith('_test.js'):
67 for dirpath, dirnames, filenames in os.walk(src_dir):
69 x = os.path.join(dirpath, f)
70 y = '/' + os.path.relpath(x, toplevel_dir)
72 test_filenames.append(y)
76 tests_as_json = json.dumps(test_filenames)
78 self.send_response(200)
79 self.send_header('Content-Type', 'application/json')
80 self.send_header('Content-Length', len(tests_as_json))
82 self.wfile.write(tests_as_json)
84 def do_GET_example_files(self):
86 for dirpath, dirnames, filenames in os.walk(test_data_dir):
91 files_as_json = json.dumps(data_files)
93 self.send_response(200)
94 self.send_header('Content-Type', 'application/json')
95 self.send_header('Content-Length', len(files_as_json))
97 self.wfile.write(files_as_json)
99 def do_GET_example_skp_files(self):
101 for dirpath, dirnames, filenames in os.walk(skp_data_dir):
106 files_as_json = json.dumps(data_files)
108 self.send_response(200)
109 self.send_header('Content-Type', 'application/json')
110 self.send_header('Content-Length', len(files_as_json))
112 self.wfile.write(files_as_json)
114 def do_GET_skp_file(self):
115 with open(toplevel_dir + self.path, 'r') as content_file:
116 content = content_file.read()
118 b64_file = base64.b64encode(content)
120 self.send_response(200)
121 self.send_header('Content-Type', 'text/plain')
122 self.send_header('Content-Length', len(b64_file))
124 self.wfile.write(b64_file)
126 def do_GET_deps(self):
128 self.server.update_deps_and_templates()
129 except Exception, ex:
130 msg = json.dumps({"details": traceback.format_exc(),
131 "message": ex.message});
132 self.log_error('While parsing deps: %s', ex.message)
133 self.send_response(500)
134 self.send_header('Content-Type', 'application/json')
135 self.send_header('Cache-Control', 'no-cache')
136 self.send_header('Content-Length', len(msg))
138 self.wfile.write(msg)
140 self.send_response(200)
141 self.send_header('Content-Type', 'application/javascript')
142 self.send_header('Content-Length', len(self.server.deps))
144 self.wfile.write(self.server.deps)
146 def do_GET_templates(self):
147 self.server.update_deps_and_templates()
148 self.send_response(200)
149 self.send_header('Content-Type', 'text/html')
150 self.send_header('Content-Length', len(self.server.templates))
152 self.wfile.write(self.server.templates)
155 if self.path == '/json/examples':
156 self.do_GET_example_files()
159 if self.path == '/json/examples/skp':
160 self.do_GET_example_skp_files()
163 if self.path == '/json/tests':
164 self.do_GET_json_tests()
167 if self.path.startswith('/skp_data'):
168 self.do_GET_skp_file()
171 if self.path == '/templates':
172 self.do_GET_templates()
175 if self.path == '/deps.js':
179 return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
181 def log_error(self, format, *args):
182 if self.path == '/favicon.ico':
184 self.log_message("While processing %s: ", self.path)
185 SimpleHTTPServer.SimpleHTTPRequestHandler.log_error(self, format, *args)
187 def log_request(self, code='-', size='-'):
188 # Dont spam the console unless it is important.
192 class Server(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer):
193 def __init__(self, *args, **kwargs):
194 BaseHTTPServer.HTTPServer.__init__(self, *args, **kwargs)
195 self.next_deps_check = -1
198 def update_deps_and_templates(self):
199 current_time = time.time()
200 if self.next_deps_check >= current_time:
202 print 'Regenerating deps and templates'
203 all_js_module_filenames = find_all_js_module_filenames([src_dir])
204 load_sequence = parse_deps.calc_load_sequence(
205 all_js_module_filenames, [src_dir])
206 self.deps = generate.generate_deps_js(load_sequence)
207 self.templates = generate.generate_html_for_combined_templates(
209 self.next_deps_check = current_time + DEPS_CHECK_DELAY
212 parser = optparse.OptionParser()
213 parser.add_option('--port',
216 default=DEFAULT_PORT,
217 help='Port to serve from')
218 options, args = parser.parse_args()
219 server = Server(('', options.port), Handler)
220 sys.stderr.write("Now running on http://localhost:%i\n" % options.port)
221 server.serve_forever()
223 if __name__ == '__main__':
224 os.chdir(toplevel_dir)
225 sys.exit(Main(sys.argv[1:]))