epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
epoger@google.com | 9fb6c8a | 2013-10-09 18:05:58 +0000 | [diff] [blame] | 3 | """ |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 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. |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 8 | |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 9 | HTTP server for our HTML rebaseline viewer. |
epoger@google.com | 9fb6c8a | 2013-10-09 18:05:58 +0000 | [diff] [blame] | 10 | """ |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 11 | |
| 12 | # System-level imports |
| 13 | import argparse |
| 14 | import BaseHTTPServer |
| 15 | import json |
epoger@google.com | dcb4e65 | 2013-10-11 18:45:33 +0000 | [diff] [blame] | 16 | import logging |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 17 | import os |
| 18 | import posixpath |
| 19 | import re |
| 20 | import shutil |
epoger@google.com | b08c707 | 2013-10-30 14:09:04 +0000 | [diff] [blame] | 21 | import socket |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 22 | import subprocess |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 23 | import sys |
epoger@google.com | 542b65f | 2013-10-15 20:10:33 +0000 | [diff] [blame] | 24 | import thread |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 25 | import threading |
epoger@google.com | 542b65f | 2013-10-15 20:10:33 +0000 | [diff] [blame] | 26 | import time |
epoger@google.com | dcb4e65 | 2013-10-11 18:45:33 +0000 | [diff] [blame] | 27 | import urlparse |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 28 | |
| 29 | # Imports from within Skia |
| 30 | # |
| 31 | # We need to add the 'tools' directory, so that we can import svn.py within |
| 32 | # that directory. |
| 33 | # Make sure that the 'tools' dir is in the PYTHONPATH, but add it at the *end* |
| 34 | # so any dirs that are already in the PYTHONPATH will be preferred. |
epoger@google.com | cb55f11 | 2013-10-02 19:27:35 +0000 | [diff] [blame] | 35 | PARENT_DIRECTORY = os.path.dirname(os.path.realpath(__file__)) |
| 36 | TRUNK_DIRECTORY = os.path.dirname(os.path.dirname(PARENT_DIRECTORY)) |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 37 | TOOLS_DIRECTORY = os.path.join(TRUNK_DIRECTORY, 'tools') |
| 38 | if TOOLS_DIRECTORY not in sys.path: |
| 39 | sys.path.append(TOOLS_DIRECTORY) |
| 40 | import svn |
| 41 | |
| 42 | # Imports from local dir |
| 43 | import results |
| 44 | |
| 45 | ACTUALS_SVN_REPO = 'http://skia-autogen.googlecode.com/svn/gm-actual' |
| 46 | PATHSPLIT_RE = re.compile('/([^/]+)/(.+)') |
epoger@google.com | b063e13 | 2013-11-25 18:06:29 +0000 | [diff] [blame] | 47 | EXPECTATIONS_DIR = os.path.join(TRUNK_DIRECTORY, 'expectations', 'gm') |
epoger@google.com | 9dddf6f | 2013-11-08 16:25:25 +0000 | [diff] [blame] | 48 | GENERATED_IMAGES_ROOT = os.path.join(PARENT_DIRECTORY, 'static', |
| 49 | 'generated-images') |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 50 | |
| 51 | # A simple dictionary of file name extensions to MIME types. The empty string |
| 52 | # entry is used as the default when no extension was given or if the extension |
| 53 | # has no entry in this dictionary. |
| 54 | MIME_TYPE_MAP = {'': 'application/octet-stream', |
| 55 | 'html': 'text/html', |
| 56 | 'css': 'text/css', |
| 57 | 'png': 'image/png', |
| 58 | 'js': 'application/javascript', |
| 59 | 'json': 'application/json' |
| 60 | } |
| 61 | |
| 62 | DEFAULT_ACTUALS_DIR = '.gm-actuals' |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 63 | DEFAULT_PORT = 8888 |
| 64 | |
epoger@google.com | 2682c90 | 2013-12-05 16:05:16 +0000 | [diff] [blame] | 65 | # How often (in seconds) clients should reload while waiting for initial |
| 66 | # results to load. |
| 67 | RELOAD_INTERVAL_UNTIL_READY = 10 |
| 68 | |
epoger@google.com | eb83259 | 2013-10-23 15:07:26 +0000 | [diff] [blame] | 69 | _HTTP_HEADER_CONTENT_LENGTH = 'Content-Length' |
| 70 | _HTTP_HEADER_CONTENT_TYPE = 'Content-Type' |
| 71 | |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 72 | _SERVER = None # This gets filled in by main() |
| 73 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 74 | |
| 75 | def _run_command(args, directory): |
| 76 | """Runs a command and returns stdout as a single string. |
| 77 | |
| 78 | Args: |
| 79 | args: the command to run, as a list of arguments |
| 80 | directory: directory within which to run the command |
| 81 | |
| 82 | Returns: stdout, as a string |
| 83 | |
| 84 | Raises an Exception if the command failed (exited with nonzero return code). |
| 85 | """ |
| 86 | logging.debug('_run_command: %s in directory %s' % (args, directory)) |
| 87 | proc = subprocess.Popen(args, cwd=directory, |
| 88 | stdout=subprocess.PIPE, |
| 89 | stderr=subprocess.PIPE) |
| 90 | (stdout, stderr) = proc.communicate() |
| 91 | if proc.returncode is not 0: |
| 92 | raise Exception('command "%s" failed in dir "%s": %s' % |
| 93 | (args, directory, stderr)) |
| 94 | return stdout |
| 95 | |
| 96 | |
epoger@google.com | 591469b | 2013-11-20 19:58:06 +0000 | [diff] [blame] | 97 | def _get_routable_ip_address(): |
epoger@google.com | b08c707 | 2013-10-30 14:09:04 +0000 | [diff] [blame] | 98 | """Returns routable IP address of this host (the IP address of its network |
| 99 | interface that would be used for most traffic, not its localhost |
| 100 | interface). See http://stackoverflow.com/a/166589 """ |
| 101 | sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 102 | sock.connect(('8.8.8.8', 80)) |
| 103 | host = sock.getsockname()[0] |
| 104 | sock.close() |
| 105 | return host |
| 106 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 107 | |
epoger@google.com | 591469b | 2013-11-20 19:58:06 +0000 | [diff] [blame] | 108 | def _create_svn_checkout(dir_path, repo_url): |
| 109 | """Creates local checkout of an SVN repository at the specified directory |
| 110 | path, returning an svn.Svn object referring to the local checkout. |
| 111 | |
| 112 | Args: |
| 113 | dir_path: path to the local checkout; if this directory does not yet exist, |
| 114 | it will be created and the repo will be checked out into it |
| 115 | repo_url: URL of SVN repo to check out into dir_path (unless the local |
| 116 | checkout already exists) |
| 117 | Returns: an svn.Svn object referring to the local checkout. |
| 118 | """ |
| 119 | local_checkout = svn.Svn(dir_path) |
| 120 | if not os.path.isdir(dir_path): |
| 121 | os.makedirs(dir_path) |
| 122 | local_checkout.Checkout(repo_url, '.') |
| 123 | return local_checkout |
| 124 | |
epoger@google.com | b08c707 | 2013-10-30 14:09:04 +0000 | [diff] [blame] | 125 | |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 126 | class Server(object): |
epoger@google.com | 9fb6c8a | 2013-10-09 18:05:58 +0000 | [diff] [blame] | 127 | """ HTTP server for our HTML rebaseline viewer. """ |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 128 | |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 129 | def __init__(self, |
| 130 | actuals_dir=DEFAULT_ACTUALS_DIR, |
epoger@google.com | 542b65f | 2013-10-15 20:10:33 +0000 | [diff] [blame] | 131 | port=DEFAULT_PORT, export=False, editable=True, |
| 132 | reload_seconds=0): |
epoger@google.com | 9fb6c8a | 2013-10-09 18:05:58 +0000 | [diff] [blame] | 133 | """ |
| 134 | Args: |
| 135 | actuals_dir: directory under which we will check out the latest actual |
| 136 | GM results |
epoger@google.com | 9fb6c8a | 2013-10-09 18:05:58 +0000 | [diff] [blame] | 137 | port: which TCP port to listen on for HTTP requests |
| 138 | export: whether to allow HTTP clients on other hosts to access this server |
epoger@google.com | 542b65f | 2013-10-15 20:10:33 +0000 | [diff] [blame] | 139 | editable: whether HTTP clients are allowed to submit new baselines |
| 140 | reload_seconds: polling interval with which to check for new results; |
| 141 | if 0, don't check for new results at all |
epoger@google.com | 9fb6c8a | 2013-10-09 18:05:58 +0000 | [diff] [blame] | 142 | """ |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 143 | self._actuals_dir = actuals_dir |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 144 | self._port = port |
| 145 | self._export = export |
epoger@google.com | 542b65f | 2013-10-15 20:10:33 +0000 | [diff] [blame] | 146 | self._editable = editable |
| 147 | self._reload_seconds = reload_seconds |
epoger@google.com | 591469b | 2013-11-20 19:58:06 +0000 | [diff] [blame] | 148 | self._actuals_repo = _create_svn_checkout( |
| 149 | dir_path=actuals_dir, repo_url=ACTUALS_SVN_REPO) |
| 150 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 151 | # Reentrant lock that must be held whenever updating EITHER of: |
| 152 | # 1. self._results |
| 153 | # 2. the expected or actual results on local disk |
| 154 | self.results_rlock = threading.RLock() |
| 155 | # self._results will be filled in by calls to update_results() |
| 156 | self._results = None |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 157 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 158 | @property |
| 159 | def results(self): |
commit-bot@chromium.org | 50ad8e4 | 2013-12-17 18:06:13 +0000 | [diff] [blame] | 160 | """ Returns the most recently generated results, or None if we don't have |
| 161 | any valid results (update_results() has not completed yet). """ |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 162 | return self._results |
| 163 | |
| 164 | @property |
epoger@google.com | 9fb6c8a | 2013-10-09 18:05:58 +0000 | [diff] [blame] | 165 | def is_exported(self): |
| 166 | """ Returns true iff HTTP clients on other hosts are allowed to access |
| 167 | this server. """ |
| 168 | return self._export |
| 169 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 170 | @property |
epoger@google.com | 542b65f | 2013-10-15 20:10:33 +0000 | [diff] [blame] | 171 | def is_editable(self): |
| 172 | """ Returns true iff HTTP clients are allowed to submit new baselines. """ |
| 173 | return self._editable |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 174 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 175 | @property |
epoger@google.com | 542b65f | 2013-10-15 20:10:33 +0000 | [diff] [blame] | 176 | def reload_seconds(self): |
| 177 | """ Returns the result reload period in seconds, or 0 if we don't reload |
| 178 | results. """ |
| 179 | return self._reload_seconds |
| 180 | |
commit-bot@chromium.org | 50ad8e4 | 2013-12-17 18:06:13 +0000 | [diff] [blame] | 181 | def update_results(self, invalidate=False): |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 182 | """ Create or update self._results, based on the expectations in |
epoger@google.com | b063e13 | 2013-11-25 18:06:29 +0000 | [diff] [blame] | 183 | EXPECTATIONS_DIR and the latest actuals from skia-autogen. |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 184 | |
| 185 | We hold self.results_rlock while we do this, to guarantee that no other |
| 186 | thread attempts to update either self._results or the underlying files at |
| 187 | the same time. |
commit-bot@chromium.org | 50ad8e4 | 2013-12-17 18:06:13 +0000 | [diff] [blame] | 188 | |
| 189 | Args: |
| 190 | invalidate: if True, invalidate self._results immediately upon entry; |
| 191 | otherwise, we will let readers see those results until we |
| 192 | replace them |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 193 | """ |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 194 | with self.results_rlock: |
commit-bot@chromium.org | 50ad8e4 | 2013-12-17 18:06:13 +0000 | [diff] [blame] | 195 | if invalidate: |
| 196 | self._results = None |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 197 | logging.info('Updating actual GM results in %s from SVN repo %s ...' % ( |
| 198 | self._actuals_dir, ACTUALS_SVN_REPO)) |
| 199 | self._actuals_repo.Update('.') |
epoger@google.com | 591469b | 2013-11-20 19:58:06 +0000 | [diff] [blame] | 200 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 201 | # We only update the expectations dir if the server was run with a |
| 202 | # nonzero --reload argument; otherwise, we expect the user to maintain |
| 203 | # her own expectations as she sees fit. |
| 204 | # |
| 205 | # Because the Skia repo is moving from SVN to git, and git does not |
| 206 | # support updating a single directory tree, we have to update the entire |
| 207 | # repo checkout. |
| 208 | # |
| 209 | # Because Skia uses depot_tools, we have to update using "gclient sync" |
| 210 | # instead of raw git (or SVN) update. Happily, this will work whether |
| 211 | # the checkout was created using git or SVN. |
| 212 | if self._reload_seconds: |
| 213 | logging.info( |
| 214 | 'Updating expected GM results in %s by syncing Skia repo ...' % |
| 215 | EXPECTATIONS_DIR) |
| 216 | _run_command(['gclient', 'sync'], TRUNK_DIRECTORY) |
| 217 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 218 | self._results = results.Results( |
| 219 | actuals_root=self._actuals_dir, |
| 220 | expected_root=EXPECTATIONS_DIR, |
| 221 | generated_images_root=GENERATED_IMAGES_ROOT) |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 222 | |
epoger@google.com | 2682c90 | 2013-12-05 16:05:16 +0000 | [diff] [blame] | 223 | def _result_loader(self, reload_seconds=0): |
| 224 | """ Call self.update_results(), either once or periodically. |
| 225 | |
| 226 | Params: |
| 227 | reload_seconds: integer; if nonzero, reload results at this interval |
| 228 | (in which case, this method will never return!) |
epoger@google.com | 542b65f | 2013-10-15 20:10:33 +0000 | [diff] [blame] | 229 | """ |
epoger@google.com | 2682c90 | 2013-12-05 16:05:16 +0000 | [diff] [blame] | 230 | self.update_results() |
| 231 | logging.info('Initial results loaded. Ready for requests on %s' % self._url) |
| 232 | if reload_seconds: |
| 233 | while True: |
| 234 | time.sleep(reload_seconds) |
| 235 | self.update_results() |
epoger@google.com | 542b65f | 2013-10-15 20:10:33 +0000 | [diff] [blame] | 236 | |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 237 | def run(self): |
epoger@google.com | 2682c90 | 2013-12-05 16:05:16 +0000 | [diff] [blame] | 238 | arg_tuple = (self._reload_seconds,) # start_new_thread needs a tuple, |
| 239 | # even though it holds just one param |
| 240 | thread.start_new_thread(self._result_loader, arg_tuple) |
epoger@google.com | 542b65f | 2013-10-15 20:10:33 +0000 | [diff] [blame] | 241 | |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 242 | if self._export: |
| 243 | server_address = ('', self._port) |
epoger@google.com | 591469b | 2013-11-20 19:58:06 +0000 | [diff] [blame] | 244 | host = _get_routable_ip_address() |
epoger@google.com | 542b65f | 2013-10-15 20:10:33 +0000 | [diff] [blame] | 245 | if self._editable: |
| 246 | logging.warning('Running with combination of "export" and "editable" ' |
| 247 | 'flags. Users on other machines will ' |
| 248 | 'be able to modify your GM expectations!') |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 249 | else: |
epoger@google.com | b08c707 | 2013-10-30 14:09:04 +0000 | [diff] [blame] | 250 | host = '127.0.0.1' |
| 251 | server_address = (host, self._port) |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 252 | http_server = BaseHTTPServer.HTTPServer(server_address, HTTPRequestHandler) |
epoger@google.com | 2682c90 | 2013-12-05 16:05:16 +0000 | [diff] [blame] | 253 | self._url = 'http://%s:%d' % (host, http_server.server_port) |
| 254 | logging.info('Listening for requests on %s' % self._url) |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 255 | http_server.serve_forever() |
| 256 | |
| 257 | |
| 258 | class HTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): |
| 259 | """ HTTP request handlers for various types of queries this server knows |
| 260 | how to handle (static HTML and Javascript, expected/actual results, etc.) |
| 261 | """ |
| 262 | def do_GET(self): |
commit-bot@chromium.org | e6af4fb | 2014-02-07 18:21:59 +0000 | [diff] [blame^] | 263 | """ |
| 264 | Handles all GET requests, forwarding them to the appropriate |
| 265 | do_GET_* dispatcher. |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 266 | |
commit-bot@chromium.org | e6af4fb | 2014-02-07 18:21:59 +0000 | [diff] [blame^] | 267 | If we see any Exceptions, return a 404. This fixes http://skbug.com/2147 |
| 268 | """ |
| 269 | try: |
| 270 | logging.debug('do_GET: path="%s"' % self.path) |
| 271 | if self.path == '' or self.path == '/' or self.path == '/index.html' : |
| 272 | self.redirect_to('/static/index.html') |
| 273 | return |
| 274 | if self.path == '/favicon.ico' : |
| 275 | self.redirect_to('/static/favicon.ico') |
| 276 | return |
| 277 | |
| 278 | # All requests must be of this form: |
| 279 | # /dispatcher/remainder |
| 280 | # where 'dispatcher' indicates which do_GET_* dispatcher to run |
| 281 | # and 'remainder' is the remaining path sent to that dispatcher. |
| 282 | normpath = posixpath.normpath(self.path) |
| 283 | (dispatcher_name, remainder) = PATHSPLIT_RE.match(normpath).groups() |
| 284 | dispatchers = { |
| 285 | 'results': self.do_GET_results, |
| 286 | 'static': self.do_GET_static, |
| 287 | } |
| 288 | dispatcher = dispatchers[dispatcher_name] |
| 289 | dispatcher(remainder) |
| 290 | except: |
| 291 | self.send_error(404) |
| 292 | raise |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 293 | |
epoger@google.com | dcb4e65 | 2013-10-11 18:45:33 +0000 | [diff] [blame] | 294 | def do_GET_results(self, type): |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 295 | """ Handle a GET request for GM results. |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 296 | |
epoger@google.com | 9fb6c8a | 2013-10-09 18:05:58 +0000 | [diff] [blame] | 297 | Args: |
epoger@google.com | dcb4e65 | 2013-10-11 18:45:33 +0000 | [diff] [blame] | 298 | type: string indicating which set of results to return; |
| 299 | must be one of the results.RESULTS_* constants |
| 300 | """ |
| 301 | logging.debug('do_GET_results: sending results of type "%s"' % type) |
commit-bot@chromium.org | e6af4fb | 2014-02-07 18:21:59 +0000 | [diff] [blame^] | 302 | # Since we must make multiple calls to the Results object, grab a |
| 303 | # reference to it in case it is updated to point at a new Results |
| 304 | # object within another thread. |
| 305 | # |
| 306 | # TODO(epoger): Rather than using a global variable for the handler |
| 307 | # to refer to the Server object, make Server a subclass of |
| 308 | # HTTPServer, and then it could be available to the handler via |
| 309 | # the handler's .server instance variable. |
| 310 | results_obj = _SERVER.results |
| 311 | if results_obj: |
| 312 | response_dict = self.package_results(results_obj, type) |
| 313 | else: |
| 314 | now = int(time.time()) |
| 315 | response_dict = { |
| 316 | 'header': { |
| 317 | 'resultsStillLoading': True, |
| 318 | 'timeUpdated': now, |
| 319 | 'timeNextUpdateAvailable': now + RELOAD_INTERVAL_UNTIL_READY, |
| 320 | }, |
| 321 | } |
| 322 | self.send_json_dict(response_dict) |
epoger@google.com | 542b65f | 2013-10-15 20:10:33 +0000 | [diff] [blame] | 323 | |
epoger@google.com | 2682c90 | 2013-12-05 16:05:16 +0000 | [diff] [blame] | 324 | def package_results(self, results_obj, type): |
| 325 | """ Given a nonempty "results" object, package it as a response_dict |
| 326 | as needed within do_GET_results. |
| 327 | |
| 328 | Args: |
| 329 | results_obj: nonempty "results" object |
| 330 | type: string indicating which set of results to return; |
| 331 | must be one of the results.RESULTS_* constants |
| 332 | """ |
| 333 | response_dict = results_obj.get_results_of_type(type) |
| 334 | time_updated = results_obj.get_timestamp() |
| 335 | response_dict['header'] = { |
epoger@google.com | 542b65f | 2013-10-15 20:10:33 +0000 | [diff] [blame] | 336 | # Timestamps: |
| 337 | # 1. when this data was last updated |
| 338 | # 2. when the caller should check back for new data (if ever) |
| 339 | # |
| 340 | # We only return these timestamps if the --reload argument was passed; |
| 341 | # otherwise, we have no idea when the expectations were last updated |
| 342 | # (we allow the user to maintain her own expectations as she sees fit). |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 343 | 'timeUpdated': time_updated if _SERVER.reload_seconds else None, |
epoger@google.com | 542b65f | 2013-10-15 20:10:33 +0000 | [diff] [blame] | 344 | 'timeNextUpdateAvailable': ( |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 345 | (time_updated+_SERVER.reload_seconds) if _SERVER.reload_seconds |
epoger@google.com | 542b65f | 2013-10-15 20:10:33 +0000 | [diff] [blame] | 346 | else None), |
| 347 | |
epoger@google.com | eb83259 | 2013-10-23 15:07:26 +0000 | [diff] [blame] | 348 | # The type we passed to get_results_of_type() |
| 349 | 'type': type, |
| 350 | |
epoger@google.com | 9fb6c8a | 2013-10-09 18:05:58 +0000 | [diff] [blame] | 351 | # Hash of testData, which the client must return with any edits-- |
| 352 | # this ensures that the edits were made to a particular dataset. |
epoger@google.com | 542b65f | 2013-10-15 20:10:33 +0000 | [diff] [blame] | 353 | 'dataHash': str(hash(repr(response_dict['testData']))), |
epoger@google.com | 9fb6c8a | 2013-10-09 18:05:58 +0000 | [diff] [blame] | 354 | |
| 355 | # Whether the server will accept edits back. |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 356 | 'isEditable': _SERVER.is_editable, |
epoger@google.com | 9fb6c8a | 2013-10-09 18:05:58 +0000 | [diff] [blame] | 357 | |
| 358 | # Whether the service is accessible from other hosts. |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 359 | 'isExported': _SERVER.is_exported, |
epoger@google.com | 2682c90 | 2013-12-05 16:05:16 +0000 | [diff] [blame] | 360 | } |
| 361 | return response_dict |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 362 | |
| 363 | def do_GET_static(self, path): |
epoger@google.com | cb55f11 | 2013-10-02 19:27:35 +0000 | [diff] [blame] | 364 | """ Handle a GET request for a file under the 'static' directory. |
| 365 | Only allow serving of files within the 'static' directory that is a |
epoger@google.com | 9fb6c8a | 2013-10-09 18:05:58 +0000 | [diff] [blame] | 366 | filesystem sibling of this script. |
| 367 | |
| 368 | Args: |
| 369 | path: path to file (under static directory) to retrieve |
| 370 | """ |
epoger@google.com | dcb4e65 | 2013-10-11 18:45:33 +0000 | [diff] [blame] | 371 | # Strip arguments ('?resultsToLoad=all') from the path |
| 372 | path = urlparse.urlparse(path).path |
| 373 | |
| 374 | logging.debug('do_GET_static: sending file "%s"' % path) |
epoger@google.com | cb55f11 | 2013-10-02 19:27:35 +0000 | [diff] [blame] | 375 | static_dir = os.path.realpath(os.path.join(PARENT_DIRECTORY, 'static')) |
| 376 | full_path = os.path.realpath(os.path.join(static_dir, path)) |
| 377 | if full_path.startswith(static_dir): |
| 378 | self.send_file(full_path) |
| 379 | else: |
epoger@google.com | dcb4e65 | 2013-10-11 18:45:33 +0000 | [diff] [blame] | 380 | logging.error( |
| 381 | 'Attempted do_GET_static() of path [%s] outside of static dir [%s]' |
| 382 | % (full_path, static_dir)) |
epoger@google.com | cb55f11 | 2013-10-02 19:27:35 +0000 | [diff] [blame] | 383 | self.send_error(404) |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 384 | |
epoger@google.com | eb83259 | 2013-10-23 15:07:26 +0000 | [diff] [blame] | 385 | def do_POST(self): |
| 386 | """ Handles all POST requests, forwarding them to the appropriate |
| 387 | do_POST_* dispatcher. """ |
| 388 | # All requests must be of this form: |
| 389 | # /dispatcher |
| 390 | # where 'dispatcher' indicates which do_POST_* dispatcher to run. |
commit-bot@chromium.org | e6af4fb | 2014-02-07 18:21:59 +0000 | [diff] [blame^] | 391 | logging.debug('do_POST: path="%s"' % self.path) |
epoger@google.com | eb83259 | 2013-10-23 15:07:26 +0000 | [diff] [blame] | 392 | normpath = posixpath.normpath(self.path) |
| 393 | dispatchers = { |
| 394 | '/edits': self.do_POST_edits, |
| 395 | } |
| 396 | try: |
| 397 | dispatcher = dispatchers[normpath] |
| 398 | dispatcher() |
| 399 | self.send_response(200) |
| 400 | except: |
| 401 | self.send_error(404) |
| 402 | raise |
| 403 | |
| 404 | def do_POST_edits(self): |
| 405 | """ Handle a POST request with modifications to GM expectations, in this |
| 406 | format: |
| 407 | |
| 408 | { |
| 409 | 'oldResultsType': 'all', # type of results that the client loaded |
| 410 | # and then made modifications to |
| 411 | 'oldResultsHash': 39850913, # hash of results when the client loaded them |
| 412 | # (ensures that the client and server apply |
| 413 | # modifications to the same base) |
| 414 | 'modifications': [ |
| 415 | { |
| 416 | 'builder': 'Test-Android-Nexus10-MaliT604-Arm7-Debug', |
| 417 | 'test': 'strokerect', |
| 418 | 'config': 'gpu', |
| 419 | 'expectedHashType': 'bitmap-64bitMD5', |
| 420 | 'expectedHashDigest': '1707359671708613629', |
| 421 | }, |
| 422 | ... |
| 423 | ], |
| 424 | } |
| 425 | |
| 426 | Raises an Exception if there were any problems. |
| 427 | """ |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 428 | if not _SERVER.is_editable: |
epoger@google.com | eb83259 | 2013-10-23 15:07:26 +0000 | [diff] [blame] | 429 | raise Exception('this server is not running in --editable mode') |
| 430 | |
| 431 | content_type = self.headers[_HTTP_HEADER_CONTENT_TYPE] |
| 432 | if content_type != 'application/json;charset=UTF-8': |
| 433 | raise Exception('unsupported %s [%s]' % ( |
| 434 | _HTTP_HEADER_CONTENT_TYPE, content_type)) |
| 435 | |
| 436 | content_length = int(self.headers[_HTTP_HEADER_CONTENT_LENGTH]) |
| 437 | json_data = self.rfile.read(content_length) |
| 438 | data = json.loads(json_data) |
| 439 | logging.debug('do_POST_edits: received new GM expectations data [%s]' % |
| 440 | data) |
| 441 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 442 | # Update the results on disk with the information we received from the |
| 443 | # client. |
| 444 | # We must hold _SERVER.results_rlock while we do this, to guarantee that |
| 445 | # no other thread updates expectations (from the Skia repo) while we are |
| 446 | # updating them (using the info we received from the client). |
| 447 | with _SERVER.results_rlock: |
| 448 | oldResultsType = data['oldResultsType'] |
| 449 | oldResults = _SERVER.results.get_results_of_type(oldResultsType) |
| 450 | oldResultsHash = str(hash(repr(oldResults['testData']))) |
| 451 | if oldResultsHash != data['oldResultsHash']: |
| 452 | raise Exception('results of type "%s" changed while the client was ' |
| 453 | 'making modifications. The client should reload the ' |
| 454 | 'results and submit the modifications again.' % |
| 455 | oldResultsType) |
| 456 | _SERVER.results.edit_expectations(data['modifications']) |
commit-bot@chromium.org | 50ad8e4 | 2013-12-17 18:06:13 +0000 | [diff] [blame] | 457 | |
| 458 | # Read the updated results back from disk. |
| 459 | # We can do this in a separate thread; we should return our success message |
| 460 | # to the UI as soon as possible. |
| 461 | thread.start_new_thread(_SERVER.update_results, (True,)) |
epoger@google.com | eb83259 | 2013-10-23 15:07:26 +0000 | [diff] [blame] | 462 | |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 463 | def redirect_to(self, url): |
epoger@google.com | 9fb6c8a | 2013-10-09 18:05:58 +0000 | [diff] [blame] | 464 | """ Redirect the HTTP client to a different url. |
| 465 | |
| 466 | Args: |
| 467 | url: URL to redirect the HTTP client to |
| 468 | """ |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 469 | self.send_response(301) |
| 470 | self.send_header('Location', url) |
| 471 | self.end_headers() |
| 472 | |
| 473 | def send_file(self, path): |
| 474 | """ Send the contents of the file at this path, with a mimetype based |
epoger@google.com | 9fb6c8a | 2013-10-09 18:05:58 +0000 | [diff] [blame] | 475 | on the filename extension. |
| 476 | |
| 477 | Args: |
| 478 | path: path of file whose contents to send to the HTTP client |
| 479 | """ |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 480 | # Grab the extension if there is one |
| 481 | extension = os.path.splitext(path)[1] |
| 482 | if len(extension) >= 1: |
| 483 | extension = extension[1:] |
| 484 | |
| 485 | # Determine the MIME type of the file from its extension |
| 486 | mime_type = MIME_TYPE_MAP.get(extension, MIME_TYPE_MAP['']) |
| 487 | |
| 488 | # Open the file and send it over HTTP |
| 489 | if os.path.isfile(path): |
| 490 | with open(path, 'rb') as sending_file: |
| 491 | self.send_response(200) |
| 492 | self.send_header('Content-type', mime_type) |
| 493 | self.end_headers() |
| 494 | self.wfile.write(sending_file.read()) |
| 495 | else: |
| 496 | self.send_error(404) |
| 497 | |
| 498 | def send_json_dict(self, json_dict): |
| 499 | """ Send the contents of this dictionary in JSON format, with a JSON |
epoger@google.com | 9fb6c8a | 2013-10-09 18:05:58 +0000 | [diff] [blame] | 500 | mimetype. |
| 501 | |
| 502 | Args: |
| 503 | json_dict: dictionary to send |
| 504 | """ |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 505 | self.send_response(200) |
| 506 | self.send_header('Content-type', 'application/json') |
| 507 | self.end_headers() |
| 508 | json.dump(json_dict, self.wfile) |
| 509 | |
| 510 | |
| 511 | def main(): |
commit-bot@chromium.org | a6ecbb8 | 2013-12-19 19:08:31 +0000 | [diff] [blame] | 512 | logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', |
| 513 | datefmt='%m/%d/%Y %H:%M:%S', |
| 514 | level=logging.INFO) |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 515 | parser = argparse.ArgumentParser() |
| 516 | parser.add_argument('--actuals-dir', |
| 517 | help=('Directory into which we will check out the latest ' |
| 518 | 'actual GM results. If this directory does not ' |
| 519 | 'exist, it will be created. Defaults to %(default)s'), |
| 520 | default=DEFAULT_ACTUALS_DIR) |
epoger@google.com | 542b65f | 2013-10-15 20:10:33 +0000 | [diff] [blame] | 521 | parser.add_argument('--editable', action='store_true', |
epoger@google.com | eb83259 | 2013-10-23 15:07:26 +0000 | [diff] [blame] | 522 | help=('Allow HTTP clients to submit new baselines.')) |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 523 | parser.add_argument('--export', action='store_true', |
| 524 | help=('Instead of only allowing access from HTTP clients ' |
| 525 | 'on localhost, allow HTTP clients on other hosts ' |
| 526 | 'to access this server. WARNING: doing so will ' |
| 527 | 'allow users on other hosts to modify your ' |
epoger@google.com | 542b65f | 2013-10-15 20:10:33 +0000 | [diff] [blame] | 528 | 'GM expectations, if combined with --editable.')) |
epoger@google.com | afaad3d | 2013-09-30 15:06:25 +0000 | [diff] [blame] | 529 | parser.add_argument('--port', type=int, |
| 530 | help=('Which TCP port to listen on for HTTP requests; ' |
| 531 | 'defaults to %(default)s'), |
| 532 | default=DEFAULT_PORT) |
epoger@google.com | 542b65f | 2013-10-15 20:10:33 +0000 | [diff] [blame] | 533 | parser.add_argument('--reload', type=int, |
| 534 | help=('How often (a period in seconds) to update the ' |
epoger@google.com | b063e13 | 2013-11-25 18:06:29 +0000 | [diff] [blame] | 535 | 'results. If specified, both expected and actual ' |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 536 | 'results will be updated by running "gclient sync" ' |
| 537 | 'on your Skia checkout as a whole. ' |
epoger@google.com | 542b65f | 2013-10-15 20:10:33 +0000 | [diff] [blame] | 538 | 'By default, we do not reload at all, and you ' |
| 539 | 'must restart the server to pick up new data.'), |
| 540 | default=0) |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 541 | args = parser.parse_args() |
| 542 | global _SERVER |
epoger@google.com | 542b65f | 2013-10-15 20:10:33 +0000 | [diff] [blame] | 543 | _SERVER = Server(actuals_dir=args.actuals_dir, |
epoger@google.com | 542b65f | 2013-10-15 20:10:33 +0000 | [diff] [blame] | 544 | port=args.port, export=args.export, editable=args.editable, |
| 545 | reload_seconds=args.reload) |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 546 | _SERVER.run() |
| 547 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 548 | |
epoger@google.com | f9d134d | 2013-09-27 15:02:44 +0000 | [diff] [blame] | 549 | if __name__ == '__main__': |
| 550 | main() |