|
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 sys | |
23 | |
24 # Imports from within Skia | |
25 # | |
26 # We need to add the 'tools' directory, so that we can import svn.py within | |
27 # that directory. | |
28 # Make sure that the 'tools' dir is in the PYTHONPATH, but add it at the *end* | |
29 # so any dirs that are already in the PYTHONPATH will be preferred. | |
30 TRUNK_DIRECTORY = os.path.dirname(os.path.dirname(os.path.dirname( | |
31 os.path.realpath(__file__)))) | |
32 TOOLS_DIRECTORY = os.path.join(TRUNK_DIRECTORY, 'tools') | |
33 if TOOLS_DIRECTORY not in sys.path: | |
34 sys.path.append(TOOLS_DIRECTORY) | |
35 import svn | |
36 | |
37 # Imports from local dir | |
38 import results | |
39 | |
40 ACTUALS_SVN_REPO = 'http://skia-autogen.googlecode.com/svn/gm-actual' | |
41 PATHSPLIT_RE = re.compile('/([^/]+)/(.+)') | |
42 TRUNK_DIRECTORY = os.path.dirname(os.path.dirname(os.path.dirname( | |
43 os.path.realpath(__file__)))) | |
44 | |
45 # A simple dictionary of file name extensions to MIME types. The empty string | |
46 # entry is used as the default when no extension was given or if the extension | |
47 # has no entry in this dictionary. | |
48 MIME_TYPE_MAP = {'': 'application/octet-stream', | |
49 'html': 'text/html', | |
50 'css': 'text/css', | |
51 'png': 'image/png', | |
52 'js': 'application/javascript', | |
53 'json': 'application/json' | |
54 } | |
55 | |
56 DEFAULT_ACTUALS_DIR = '.gm-actuals' | |
57 DEFAULT_EXPECTATIONS_DIR = os.path.join(TRUNK_DIRECTORY, 'expectations', 'gm') | |
58 DEFAULT_PORT = 8888 | |
59 | |
60 _SERVER = None # This gets filled in by main() | |
61 | |
62 class Server(object): | |
63 """ HTTP server for our HTML rebaseline viewer. | |
64 | |
65 params: | |
66 actuals_dir: directory under which we will check out the latest actual | |
67 GM results | |
68 expectations_dir: directory under which to find GM expectations (they | |
69 must already be in that directory) | |
70 port: which TCP port to listen on for HTTP requests | |
71 export: whether to allow HTTP clients on other hosts to access this server | |
72 """ | |
73 def __init__(self, | |
74 actuals_dir=DEFAULT_ACTUALS_DIR, | |
75 expectations_dir=DEFAULT_EXPECTATIONS_DIR, | |
76 port=DEFAULT_PORT, export=False): | |
77 self._actuals_dir = actuals_dir | |
78 self._expectations_dir = expectations_dir | |
79 self._port = port | |
80 self._export = export | |
81 | |
82 def fetch_results(self): | |
83 """ Create self.results, based on the expectations in | |
84 self._expectations_dir and the latest actuals from skia-autogen. | |
85 | |
86 TODO(epoger): Add a new --browseonly mode setting. In that mode, | |
87 the gm-actuals and expectations will automatically be updated every few | |
88 minutes. See discussion in https://codereview.chromium.org/24274003/ . | |
89 """ | |
90 print 'Checking out latest actual GM results from %s into %s ...' % ( | |
91 ACTUALS_SVN_REPO, self._actuals_dir) | |
92 actuals_repo = svn.Svn(self._actuals_dir) | |
93 if not os.path.isdir(self._actuals_dir): | |
94 os.makedirs(self._actuals_dir) | |
95 actuals_repo.Checkout(ACTUALS_SVN_REPO, '.') | |
borenet
2013/09/26 19:02:01
Does this just check out into '.'? We haven't chd
epoger
2013/09/26 21:22:21
The svn.Svn() constructor takes care of this for u
| |
96 else: | |
97 actuals_repo.Update('.') | |
epoger
2013/09/26 17:26:51
Now updates the gm-actuals we checked out last tim
borenet
2013/09/26 19:02:01
Same question here as above: do we need to os.chdi
epoger
2013/09/26 21:22:21
See above. It works as-is.
| |
98 print 'Parsing results from actuals in %s and expectations in %s ...' % ( | |
99 self._actuals_dir, self._expectations_dir) | |
100 self.results = results.Results( | |
101 actuals_root=self._actuals_dir, | |
102 expected_root=self._expectations_dir) | |
103 | |
104 def run(self): | |
105 self.fetch_results() | |
106 if self._export: | |
107 server_address = ('', self._port) | |
108 print ('WARNING: Running in "export" mode. Users on other machines will ' | |
109 'be able to modify your GM expectations!') | |
110 else: | |
111 server_address = ('127.0.0.1', self._port) | |
112 http_server = BaseHTTPServer.HTTPServer(server_address, HTTPRequestHandler) | |
113 print 'Ready for requests on http://%s:%d' % ( | |
114 http_server.server_name, http_server.server_port) | |
115 http_server.serve_forever() | |
116 | |
117 | |
118 class HTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): | |
119 """ HTTP request handlers for various types of queries this server knows | |
120 how to handle (static HTML and Javascript, expected/actual results, etc.) | |
121 """ | |
122 def do_GET(self): | |
123 """ Handles all GET requests, forwarding them to the appropriate | |
124 do_GET_* dispatcher. """ | |
125 if self.path == '' or self.path == '/' or self.path == '/index.html' : | |
126 self.redirect_to('/static/view.html') | |
127 return | |
128 if self.path == '/favicon.ico' : | |
129 self.redirect_to('/static/favicon.ico') | |
130 return | |
131 | |
132 # All requests must be of this form: | |
133 # /dispatcher/remainder | |
134 # where "dispatcher" indicates which do_GET_* dispatcher to run | |
135 # and "remainder" is the remaining path sent to that dispatcher. | |
136 normpath = posixpath.normpath(self.path) | |
137 (dispatcher_name, remainder) = PATHSPLIT_RE.match(normpath).groups() | |
138 dispatchers = { | |
139 'results': self.do_GET_results, | |
140 'static': self.do_GET_static, | |
141 } | |
142 dispatcher = dispatchers[dispatcher_name] | |
143 dispatcher(remainder) | |
144 | |
145 def do_GET_results(self, result_type): | |
146 """ Handle a GET request for GM results. | |
147 For now, we ignore the remaining path info, because we only know how to | |
epoger
2013/09/26 17:26:51
This is a little funny... the client now requests
borenet
2013/09/26 19:02:01
Looks like I could request /results/pleasenoresult
epoger
2013/09/26 21:22:21
Again, I file this under "we don't know yet, let's
| |
148 return all results. """ | |
149 print 'do_GET_results: sending results of type "%s"' % result_type | |
150 response_dict = _SERVER.results.GetAll() | |
151 if response_dict: | |
152 self.send_json_dict(response_dict) | |
153 else: | |
154 self.send_error(404) | |
155 | |
156 def do_GET_static(self, path): | |
157 """ Handle a GET request for a file under the 'static' directory. """ | |
158 print 'do_GET_static: sending file "%s"' % path | |
159 self.send_file(posixpath.join('static', path)) | |
160 | |
161 def redirect_to(self, url): | |
162 """ Redirect the HTTP client to a different url. """ | |
163 self.send_response(301) | |
164 self.send_header('Location', url) | |
165 self.end_headers() | |
166 | |
167 def send_file(self, path): | |
168 """ Send the contents of the file at this path, with a mimetype based | |
169 on the filename extension. """ | |
170 # Grab the extension if there is one | |
171 extension = os.path.splitext(path)[1] | |
172 if len(extension) >= 1: | |
173 extension = extension[1:] | |
174 | |
175 # Determine the MIME type of the file from its extension | |
176 mime_type = MIME_TYPE_MAP.get(extension, MIME_TYPE_MAP['']) | |
177 | |
178 # Open the file and send it over HTTP | |
179 if os.path.isfile(path): | |
180 with open(path, 'rb') as sending_file: | |
181 self.send_response(200) | |
182 self.send_header('Content-type', mime_type) | |
183 self.end_headers() | |
184 self.wfile.write(sending_file.read()) | |
185 else: | |
186 self.send_error(404) | |
187 | |
188 def send_json_dict(self, json_dict): | |
189 """ Send the contents of this dictionary in JSON format, with a JSON | |
190 mimetype. """ | |
191 self.send_response(200) | |
192 self.send_header('Content-type', 'application/json') | |
193 self.end_headers() | |
194 json.dump(json_dict, self.wfile) | |
195 | |
196 | |
197 def main(): | |
198 parser = argparse.ArgumentParser() | |
199 parser.add_argument('--actuals-dir', | |
200 help=('Directory into which we will check out the latest ' | |
201 'actual GM results. If this directory does not ' | |
202 'exist, it will be created. Defaults to %(default)s'), | |
203 default=DEFAULT_ACTUALS_DIR) | |
204 parser.add_argument('--expectations-dir', | |
205 help=('Directory under which to find GM expectations; ' | |
206 'defaults to %(default)s .'), | |
borenet
2013/09/26 19:02:01
I think adding the period here will be confusing..
epoger
2013/09/26 21:22:21
Removed all trailing periods from the command-line
| |
207 default=DEFAULT_EXPECTATIONS_DIR) | |
208 parser.add_argument('--export', action='store_true', | |
209 help=('Instead of only allowing access from HTTP clients ' | |
210 'on localhost, allow HTTP clients on other hosts ' | |
211 'to access this server. WARNING: doing so will ' | |
212 'allow users on other hosts to modify your ' | |
213 'GM expectations!')) | |
214 parser.add_argument('--port', | |
215 help=('Which TCP port to listen on for HTTP requests; ' | |
216 'defaults to %(default)s .'), | |
217 default=DEFAULT_PORT) | |
218 args = parser.parse_args() | |
219 global _SERVER | |
220 _SERVER = Server(expectations_dir=args.expectations_dir, | |
221 port=args.port, export=args.export) | |
222 _SERVER.run() | |
223 | |
224 if __name__ == '__main__': | |
225 main() | |
OLD | NEW |