Chromium Code Reviews| Index: gm/rebaseline_server/server.py |
| =================================================================== |
| --- gm/rebaseline_server/server.py (revision 0) |
| +++ gm/rebaseline_server/server.py (revision 0) |
| @@ -0,0 +1,196 @@ |
| +#!/usr/bin/python |
| + |
| +''' |
| +Copyright 2013 Google Inc. |
| + |
| +Use of this source code is governed by a BSD-style license that can be |
| +found in the LICENSE file. |
| +''' |
| + |
| +''' |
| +HTTP server for our HTML rebaseline viewer. |
| +''' |
| + |
| +# System-level imports |
| +import argparse |
| +import BaseHTTPServer |
| +import json |
| +import os |
| +import posixpath |
| +import re |
| +import shutil |
| +import tempfile |
| + |
| +# Imports from local dir |
| +import results |
| +import svn |
| + |
| +ACTUALS_SVN_REPO = 'http://skia-autogen.googlecode.com/svn/gm-actual' |
| +PATHSPLIT_RE = re.compile('/([^/]+)/(.+)') |
| +TRUNK_DIRECTORY = os.path.dirname(os.path.dirname(os.path.dirname( |
| + os.path.realpath(__file__)))) |
| + |
| +# A simple dictionary of file name extensions to MIME types. The empty string |
| +# entry is used as the default when no extension was given or if the extension |
| +# has no entry in this dictionary. |
| +MIME_TYPE_MAP = {'': 'application/octet-stream', |
| + 'html': 'text/html', |
| + 'css': 'text/css', |
| + 'png': 'image/png', |
| + 'js': 'application/javascript', |
| + 'json': 'application/json' |
| + } |
| + |
| +DEFAULT_EXPECTATIONS_DIR = os.path.join(TRUNK_DIRECTORY, 'expectations', 'gm') |
| +DEFAULT_PORT = 8888 |
| + |
| +_SERVER = None # This gets filled in by main() |
| + |
| +class Server(object): |
| + """ HTTP server for our HTML rebaseline viewer. |
| + |
| + params: |
| + expectations_dir: directory under which to find GM expectations (they |
| + must already be in that directory) |
| + port: which TCP port to listen on for HTTP requests |
| + export: whether to allow HTTP clients on other hosts to access this server |
| + """ |
| + def __init__(self, |
| + expectations_dir=DEFAULT_EXPECTATIONS_DIR, |
| + port=DEFAULT_PORT, export=False): |
| + self._expectations_dir = expectations_dir |
| + self._port = port |
| + self._export = export |
| + |
| + def fetch_results(self): |
| + """ Create self.results, based on the expectations in |
| + self._expectations_dir and the latest actuals from skia-autogen. |
| + |
| + TODO(epoger): periodically (or upon demand?), generate a NEW |
| + self.results object based on UPDATED checkouts of gm-actual and |
| + expectations... OR, just let HTTPRequestHandler fire up its own |
| + 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
|
| + """ |
| + actuals_root = tempfile.mkdtemp() |
| + print 'Checking out latest actual GM results from %s into %s ...' % ( |
| + ACTUALS_SVN_REPO, actuals_root) |
| + actuals_repo = svn.Svn(actuals_root) |
| + actuals_repo.Checkout(ACTUALS_SVN_REPO, '.') |
| + print 'Parsing results from actuals in %s and expectations in %s ...' % ( |
| + actuals_root, self._expectations_dir) |
| + self.results = results.Results( |
| + actuals_root=actuals_root, |
| + expected_root=self._expectations_dir) |
| + 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
|
| + |
| + def run(self): |
| + self.fetch_results() |
| + 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
|
| + server_address = ('', self._port) |
| + else: |
| + server_address = ('127.0.0.1', self._port) |
| + http_server = BaseHTTPServer.HTTPServer(server_address, HTTPRequestHandler) |
| + print 'Ready for requests on http://%s:%d' % ( |
| + http_server.server_name, http_server.server_port) |
| + http_server.serve_forever() |
| + |
| + |
| +class HTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): |
| + """ HTTP request handlers for various types of queries this server knows |
| + how to handle (static HTML and Javascript, expected/actual results, etc.) |
| + """ |
| + def do_GET(self): |
| + """ Handles all GET requests, forwarding them to the appropriate |
| + do_GET_* dispatcher. """ |
| + if self.path == '' or self.path == '/' or self.path == '/index.html' : |
| + self.redirect_to('/static/view.html') |
| + return |
| + if self.path == '/favicon.ico' : |
| + self.redirect_to('/static/favicon.ico') |
| + return |
| + |
| + # All requests must be of this form: |
| + # /dispatcher/remainder |
| + # where "dispatcher" indicates which do_GET_* dispatcher to run |
| + # and "remainder" is the remaining path sent to that dispatcher. |
| + normpath = posixpath.normpath(self.path) |
| + (dispatcher_name, remainder) = PATHSPLIT_RE.match(normpath).groups() |
| + dispatchers = { |
| + 'results': self.do_GET_results, |
| + 'static': self.do_GET_static, |
| + } |
| + dispatcher = dispatchers[dispatcher_name] |
| + dispatcher(remainder) |
| + |
| + def do_GET_results(self, result_type): |
| + """ Handle a GET request for GM results of this result_type. """ |
| + print 'do_GET_results: sending results of type "%s"' % result_type |
| + response_dict = _SERVER.results.OfType(result_type) |
| + if response_dict: |
| + self.send_json_dict(response_dict) |
| + else: |
| + self.send_error(404) |
| + |
| + def do_GET_static(self, path): |
| + """ Handle a GET request for a file under the 'static' directory. """ |
| + print 'do_GET_static: sending file "%s"' % path |
| + self.send_file(posixpath.join('static', path)) |
| + |
| + def redirect_to(self, url): |
| + """ Redirect the HTTP client to a different url. """ |
| + self.send_response(301) |
| + self.send_header('Location', url) |
| + self.end_headers() |
| + |
| + def send_file(self, path): |
| + """ Send the contents of the file at this path, with a mimetype based |
| + on the filename extension. """ |
| + # Grab the extension if there is one |
| + extension = os.path.splitext(path)[1] |
| + if len(extension) >= 1: |
| + extension = extension[1:] |
| + |
| + # Determine the MIME type of the file from its extension |
| + mime_type = MIME_TYPE_MAP.get(extension, MIME_TYPE_MAP['']) |
| + |
| + # Open the file and send it over HTTP |
| + if os.path.isfile(path): |
| + with open(path, 'rb') as sending_file: |
| + self.send_response(200) |
| + self.send_header('Content-type', mime_type) |
| + self.end_headers() |
| + self.wfile.write(sending_file.read()) |
| + else: |
| + self.send_error(404) |
| + |
| + def send_json_dict(self, json_dict): |
| + """ Send the contents of this dictionary in JSON format, with a JSON |
| + mimetype. """ |
| + self.send_response(200) |
| + self.send_header('Content-type', 'application/json') |
| + self.end_headers() |
| + json.dump(json_dict, self.wfile) |
| + |
| + |
| +def main(): |
| + parser = argparse.ArgumentParser() |
| + parser.add_argument('--expectations-dir', |
| + help=('directory under which to find GM expectations; ' |
| + 'defaults to %(default)s'), |
| + default=DEFAULT_EXPECTATIONS_DIR) |
| + parser.add_argument('--export', action='store_true', |
| + help=('instead of only allowing access from HTTP clients ' |
| + 'on localhost, allow HTTP clients on other hosts ' |
| + 'to access this server')) |
| + parser.add_argument('--port', |
| + help=('which TCP port to listen on for HTTP requests; ' |
| + 'defaults to %(default)s'), |
| + default=DEFAULT_PORT) |
| + args = parser.parse_args() |
| + global _SERVER |
| + _SERVER = Server(expectations_dir=args.expectations_dir, |
| + port=args.port, export=args.export) |
| + _SERVER.run() |
| + |
| +if __name__ == '__main__': |
| + main() |
| Property changes on: gm/rebaseline_server/server.py |
| ___________________________________________________________________ |
| Added: svn:executable |
| + * |