|
OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/python | |
2 | |
3 ''' | |
4 Copyright 2013 Google Inc. | |
5 | |
6 Use of this source code is governed by a BSD-style license that can be | |
7 found in the LICENSE file. | |
8 ''' | |
9 | |
10 ''' | |
11 HTTP server for our HTML rebaseline viewer. | |
12 ''' | |
13 | |
14 # System-level imports | |
15 import argparse | |
16 import BaseHTTPServer | |
17 import json | |
18 import os | |
19 import posixpath | |
20 import re | |
21 import shutil | |
22 import tempfile | |
23 | |
24 # Imports from local dir | |
25 import results | |
26 import svn | |
27 | |
28 ACTUALS_SVN_REPO = 'http://skia-autogen.googlecode.com/svn/gm-actual' | |
29 PATHSPLIT_RE = re.compile('/([^/]+)/(.+)') | |
30 TRUNK_DIRECTORY = os.path.dirname(os.path.dirname(os.path.dirname( | |
31 os.path.realpath(__file__)))) | |
32 | |
33 # A simple dictionary of file name extensions to MIME types. The empty string | |
34 # entry is used as the default when no extension was given or if the extension | |
35 # has no entry in this dictionary. | |
36 MIME_TYPE_MAP = {'': 'application/octet-stream', | |
37 'html': 'text/html', | |
38 'css': 'text/css', | |
39 'png': 'image/png', | |
40 'js': 'application/javascript', | |
41 'json': 'application/json' | |
42 } | |
43 | |
44 DEFAULT_EXPECTATIONS_DIR = os.path.join(TRUNK_DIRECTORY, 'expectations', 'gm') | |
45 DEFAULT_PORT = 8888 | |
46 | |
47 _SERVER = None # This gets filled in by main() | |
48 | |
49 class Server(object): | |
50 """ HTTP server for our HTML rebaseline viewer. | |
51 | |
52 params: | |
53 expectations_dir: directory under which to find GM expectations (they | |
54 must already be in that directory) | |
55 port: which TCP port to listen on for HTTP requests | |
56 export: whether to allow HTTP clients on other hosts to access this server | |
57 """ | |
58 def __init__(self, | |
59 expectations_dir=DEFAULT_EXPECTATIONS_DIR, | |
60 port=DEFAULT_PORT, export=False): | |
61 self._expectations_dir = expectations_dir | |
62 self._port = port | |
63 self._export = export | |
64 | |
65 def fetch_results(self): | |
66 """ Create self.results, based on the expectations in | |
67 self._expectations_dir and the latest actuals from skia-autogen. | |
68 | |
69 TODO(epoger): periodically (or upon demand?), generate a NEW | |
70 self.results object based on UPDATED checkouts of gm-actual and | |
71 expectations... OR, just let HTTPRequestHandler fire up its own | |
72 Results instances, a new one for each request? | |
borenet
2013/09/25 15:08:08
Per-request sounds good, but I think it might be t
epoger
2013/09/25 16:21:18
One thing that makes this a bit tricky is that I i
epoger
2013/09/25 18:26:33
Brian and I came up with an idea for a mode switch
borenet
2013/09/25 19:28:17
Personally, I think a long-running server which is
| |
73 """ | |
74 actuals_root = tempfile.mkdtemp() | |
75 print 'Checking out latest actual GM results from %s into %s ...' % ( | |
76 ACTUALS_SVN_REPO, actuals_root) | |
77 actuals_repo = svn.Svn(actuals_root) | |
78 actuals_repo.Checkout(ACTUALS_SVN_REPO, '.') | |
79 print 'Parsing results from actuals in %s and expectations in %s ...' % ( | |
80 actuals_root, self._expectations_dir) | |
81 self.results = results.Results( | |
82 actuals_root=actuals_root, | |
83 expected_root=self._expectations_dir) | |
84 shutil.rmtree(actuals_root) | |
borenet
2013/09/25 15:08:08
Why put this in a temp dir? Won't it be faster if
epoger
2013/09/25 16:21:18
See my discussion above, of the different uses for
borenet
2013/09/25 19:28:17
I think that's fine. An alternative would be to a
| |
85 | |
86 def run(self): | |
87 self.fetch_results() | |
88 if self._export: | |
jcgregorio
2013/09/24 20:19:35
Given that this tool will allow non-readonly acces
epoger
2013/09/25 16:21:18
I added the warning to stdout (and within the --he
| |
89 server_address = ('', self._port) | |
90 else: | |
91 server_address = ('127.0.0.1', self._port) | |
92 http_server = BaseHTTPServer.HTTPServer(server_address, HTTPRequestHandler) | |
93 print 'Ready for requests on http://%s:%d' % ( | |
94 http_server.server_name, http_server.server_port) | |
95 http_server.serve_forever() | |
96 | |
97 | |
98 class HTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): | |
99 """ HTTP request handlers for various types of queries this server knows | |
100 how to handle (static HTML and Javascript, expected/actual results, etc.) | |
101 """ | |
102 def do_GET(self): | |
103 """ Handles all GET requests, forwarding them to the appropriate | |
104 do_GET_* dispatcher. """ | |
105 if self.path == '' or self.path == '/' or self.path == '/index.html' : | |
106 self.redirect_to('/static/view.html') | |
107 return | |
108 if self.path == '/favicon.ico' : | |
109 self.redirect_to('/static/favicon.ico') | |
110 return | |
111 | |
112 # All requests must be of this form: | |
113 # /dispatcher/remainder | |
114 # where "dispatcher" indicates which do_GET_* dispatcher to run | |
115 # and "remainder" is the remaining path sent to that dispatcher. | |
116 normpath = posixpath.normpath(self.path) | |
117 (dispatcher_name, remainder) = PATHSPLIT_RE.match(normpath).groups() | |
118 dispatchers = { | |
119 'results': self.do_GET_results, | |
120 'static': self.do_GET_static, | |
121 } | |
122 dispatcher = dispatchers[dispatcher_name] | |
123 dispatcher(remainder) | |
124 | |
125 def do_GET_results(self, result_type): | |
126 """ Handle a GET request for GM results of this result_type. """ | |
127 print 'do_GET_results: sending results of type "%s"' % result_type | |
128 response_dict = _SERVER.results.OfType(result_type) | |
129 if response_dict: | |
130 self.send_json_dict(response_dict) | |
131 else: | |
132 self.send_error(404) | |
133 | |
134 def do_GET_static(self, path): | |
135 """ Handle a GET request for a file under the 'static' directory. """ | |
136 print 'do_GET_static: sending file "%s"' % path | |
137 self.send_file(posixpath.join('static', path)) | |
138 | |
139 def redirect_to(self, url): | |
140 """ Redirect the HTTP client to a different url. """ | |
141 self.send_response(301) | |
142 self.send_header('Location', url) | |
143 self.end_headers() | |
144 | |
145 def send_file(self, path): | |
146 """ Send the contents of the file at this path, with a mimetype based | |
147 on the filename extension. """ | |
148 # Grab the extension if there is one | |
149 extension = os.path.splitext(path)[1] | |
150 if len(extension) >= 1: | |
151 extension = extension[1:] | |
152 | |
153 # Determine the MIME type of the file from its extension | |
154 mime_type = MIME_TYPE_MAP.get(extension, MIME_TYPE_MAP['']) | |
155 | |
156 # Open the file and send it over HTTP | |
157 if os.path.isfile(path): | |
158 with open(path, 'rb') as sending_file: | |
159 self.send_response(200) | |
160 self.send_header('Content-type', mime_type) | |
161 self.end_headers() | |
162 self.wfile.write(sending_file.read()) | |
163 else: | |
164 self.send_error(404) | |
165 | |
166 def send_json_dict(self, json_dict): | |
167 """ Send the contents of this dictionary in JSON format, with a JSON | |
168 mimetype. """ | |
169 self.send_response(200) | |
170 self.send_header('Content-type', 'application/json') | |
171 self.end_headers() | |
172 json.dump(json_dict, self.wfile) | |
173 | |
174 | |
175 def main(): | |
176 parser = argparse.ArgumentParser() | |
177 parser.add_argument('--expectations-dir', | |
178 help=('directory under which to find GM expectations; ' | |
179 'defaults to %(default)s'), | |
180 default=DEFAULT_EXPECTATIONS_DIR) | |
181 parser.add_argument('--export', action='store_true', | |
182 help=('instead of only allowing access from HTTP clients ' | |
183 'on localhost, allow HTTP clients on other hosts ' | |
184 'to access this server')) | |
185 parser.add_argument('--port', | |
186 help=('which TCP port to listen on for HTTP requests; ' | |
187 'defaults to %(default)s'), | |
188 default=DEFAULT_PORT) | |
189 args = parser.parse_args() | |
190 global _SERVER | |
191 _SERVER = Server(expectations_dir=args.expectations_dir, | |
192 port=args.port, export=args.export) | |
193 _SERVER.run() | |
194 | |
195 if __name__ == '__main__': | |
196 main() | |
OLD | NEW |