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