aboutsummaryrefslogtreecommitdiffhomepage
path: root/gm/rebaseline_server/server.py
blob: 6d191ea4098b2721f09f40d5829550e957bd6944 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#!/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 logging
import os
import posixpath
import re
import shutil
import socket
import sys
import thread
import time
import urlparse

# Imports from within Skia
#
# We need to add the 'tools' directory, so that we can import svn.py within
# that directory.
# Make sure that the 'tools' dir is in the PYTHONPATH, but add it at the *end*
# so any dirs that are already in the PYTHONPATH will be preferred.
PARENT_DIRECTORY = os.path.dirname(os.path.realpath(__file__))
TRUNK_DIRECTORY = os.path.dirname(os.path.dirname(PARENT_DIRECTORY))
TOOLS_DIRECTORY = os.path.join(TRUNK_DIRECTORY, 'tools')
if TOOLS_DIRECTORY not in sys.path:
  sys.path.append(TOOLS_DIRECTORY)
import svn

# Imports from local dir
import results

ACTUALS_SVN_REPO = 'http://skia-autogen.googlecode.com/svn/gm-actual'
EXPECTATIONS_SVN_REPO = 'http://skia.googlecode.com/svn/trunk/expectations/gm'
PATHSPLIT_RE = re.compile('/([^/]+)/(.+)')
TRUNK_DIRECTORY = os.path.dirname(os.path.dirname(os.path.dirname(
    os.path.realpath(__file__))))
GENERATED_IMAGES_ROOT = os.path.join(PARENT_DIRECTORY, 'static',
                                     'generated-images')

# 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_ACTUALS_DIR = '.gm-actuals'
DEFAULT_EXPECTATIONS_DIR = os.path.join(TRUNK_DIRECTORY, 'expectations', 'gm')
DEFAULT_PORT = 8888

_HTTP_HEADER_CONTENT_LENGTH = 'Content-Length'
_HTTP_HEADER_CONTENT_TYPE = 'Content-Type'

_SERVER = None   # This gets filled in by main()

def _get_routable_ip_address():
  """Returns routable IP address of this host (the IP address of its network
     interface that would be used for most traffic, not its localhost
     interface).  See http://stackoverflow.com/a/166589 """
  sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  sock.connect(('8.8.8.8', 80))
  host = sock.getsockname()[0]
  sock.close()
  return host

def _create_svn_checkout(dir_path, repo_url):
  """Creates local checkout of an SVN repository at the specified directory
  path, returning an svn.Svn object referring to the local checkout.

  Args:
    dir_path: path to the local checkout; if this directory does not yet exist,
              it will be created and the repo will be checked out into it
    repo_url: URL of SVN repo to check out into dir_path (unless the local
              checkout already exists)
  Returns: an svn.Svn object referring to the local checkout.
  """
  local_checkout = svn.Svn(dir_path)
  if not os.path.isdir(dir_path):
    os.makedirs(dir_path)
    local_checkout.Checkout(repo_url, '.')
  return local_checkout


class Server(object):
  """ HTTP server for our HTML rebaseline viewer. """

  def __init__(self,
               actuals_dir=DEFAULT_ACTUALS_DIR,
               expectations_dir=DEFAULT_EXPECTATIONS_DIR,
               port=DEFAULT_PORT, export=False, editable=True,
               reload_seconds=0):
    """
    Args:
      actuals_dir: directory under which we will check out the latest actual
                   GM results
      expectations_dir: DEPRECATED: 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
      editable: whether HTTP clients are allowed to submit new baselines
      reload_seconds: polling interval with which to check for new results;
                      if 0, don't check for new results at all
    """
    self._actuals_dir = actuals_dir
    self._expectations_dir = expectations_dir
    self._port = port
    self._export = export
    self._editable = editable
    self._reload_seconds = reload_seconds
    self._actuals_repo = _create_svn_checkout(
        dir_path=actuals_dir, repo_url=ACTUALS_SVN_REPO)

    # We only update the expectations dir if the server was run with a
    # nonzero --reload argument; otherwise, we expect the user to maintain
    # her own expectations as she sees fit.
    #
    # TODO(epoger): Use git instead of svn to check out expectations, since
    # the Skia repo is moving to git.
    if reload_seconds:
      self._expectations_repo = _create_svn_checkout(
          dir_path=expectations_dir, repo_url=EXPECTATIONS_SVN_REPO)
    else:
      self._expectations_repo = None

  def is_exported(self):
    """ Returns true iff HTTP clients on other hosts are allowed to access
    this server. """
    return self._export

  def is_editable(self):
    """ Returns true iff HTTP clients are allowed to submit new baselines. """
    return self._editable

  def reload_seconds(self):
    """ Returns the result reload period in seconds, or 0 if we don't reload
    results. """
    return self._reload_seconds

  def update_results(self):
    """ Create or update self.results, based on the expectations in
    self._expectations_dir and the latest actuals from skia-autogen.
    """
    logging.info('Updating actual GM results in %s from SVN repo %s ...' % (
        self._actuals_dir, ACTUALS_SVN_REPO))
    self._actuals_repo.Update('.')

    if self._expectations_repo:
      logging.info(
          'Updating expected GM results in %s from SVN repo %s ...' % (
              self._expectations_dir, EXPECTATIONS_SVN_REPO))
      self._expectations_repo.Update('.')

    logging.info(
          ('Parsing results from actuals in %s and expectations in %s, '
          + 'and generating pixel diffs (may take a while) ...') % (
          self._actuals_dir, self._expectations_dir))
    self.results = results.Results(
        actuals_root=self._actuals_dir,
        expected_root=self._expectations_dir,
        generated_images_root=GENERATED_IMAGES_ROOT)

  def _result_reloader(self):
    """ If --reload argument was specified, reload results at the appropriate
    interval.
    """
    while self._reload_seconds:
      time.sleep(self._reload_seconds)
      self.update_results()

  def run(self):
    self.update_results()
    thread.start_new_thread(self._result_reloader, ())

    if self._export:
      server_address = ('', self._port)
      host = _get_routable_ip_address()
      if self._editable:
        logging.warning('Running with combination of "export" and "editable" '
                        'flags.  Users on other machines will '
                        'be able to modify your GM expectations!')
    else:
      host = '127.0.0.1'
      server_address = (host, self._port)
    http_server = BaseHTTPServer.HTTPServer(server_address, HTTPRequestHandler)
    logging.info('Ready for requests on http://%s:%d' % (
        host, 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/index.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, type):
    """ Handle a GET request for GM results.

    Args:
      type: string indicating which set of results to return;
            must be one of the results.RESULTS_* constants
    """
    logging.debug('do_GET_results: sending results of type "%s"' % type)
    try:
      # Since we must make multiple calls to the Results object, grab a
      # reference to it in case it is updated to point at a new Results
      # object within another thread.
      #
      # TODO(epoger): Rather than using a global variable for the handler
      # to refer to the Server object, make Server a subclass of
      # HTTPServer, and then it could be available to the handler via
      # the handler's .server instance variable.
      results_obj = _SERVER.results
      response_dict = results_obj.get_results_of_type(type)
      time_updated = results_obj.get_timestamp()

      response_dict['header'] = {
        # Timestamps:
        # 1. when this data was last updated
        # 2. when the caller should check back for new data (if ever)
        #
        # We only return these timestamps if the --reload argument was passed;
        # otherwise, we have no idea when the expectations were last updated
        # (we allow the user to maintain her own expectations as she sees fit).
        'timeUpdated': time_updated if _SERVER.reload_seconds() else None,
        'timeNextUpdateAvailable': (
            (time_updated+_SERVER.reload_seconds()) if _SERVER.reload_seconds()
            else None),

        # The type we passed to get_results_of_type()
        'type': type,

        # Hash of testData, which the client must return with any edits--
        # this ensures that the edits were made to a particular dataset.
        'dataHash': str(hash(repr(response_dict['testData']))),

        # Whether the server will accept edits back.
        'isEditable': _SERVER.is_editable(),

        # Whether the service is accessible from other hosts.
        'isExported': _SERVER.is_exported(),
      }
      self.send_json_dict(response_dict)
    except:
      self.send_error(404)
      raise

  def do_GET_static(self, path):
    """ Handle a GET request for a file under the 'static' directory.
    Only allow serving of files within the 'static' directory that is a
    filesystem sibling of this script.

    Args:
      path: path to file (under static directory) to retrieve
    """
    # Strip arguments ('?resultsToLoad=all') from the path
    path = urlparse.urlparse(path).path

    logging.debug('do_GET_static: sending file "%s"' % path)
    static_dir = os.path.realpath(os.path.join(PARENT_DIRECTORY, 'static'))
    full_path = os.path.realpath(os.path.join(static_dir, path))
    if full_path.startswith(static_dir):
      self.send_file(full_path)
    else:
      logging.error(
          'Attempted do_GET_static() of path [%s] outside of static dir [%s]'
          % (full_path, static_dir))
      self.send_error(404)

  def do_POST(self):
    """ Handles all POST requests, forwarding them to the appropriate
        do_POST_* dispatcher. """
    # All requests must be of this form:
    #   /dispatcher
    # where 'dispatcher' indicates which do_POST_* dispatcher to run.
    normpath = posixpath.normpath(self.path)
    dispatchers = {
      '/edits': self.do_POST_edits,
    }
    try:
      dispatcher = dispatchers[normpath]
      dispatcher()
      self.send_response(200)
    except:
      self.send_error(404)
      raise

  def do_POST_edits(self):
    """ Handle a POST request with modifications to GM expectations, in this
    format:

    {
      'oldResultsType': 'all',    # type of results that the client loaded
                                  # and then made modifications to
      'oldResultsHash': 39850913, # hash of results when the client loaded them
                                  # (ensures that the client and server apply
                                  # modifications to the same base)
      'modifications': [
        {
          'builder': 'Test-Android-Nexus10-MaliT604-Arm7-Debug',
          'test': 'strokerect',
          'config': 'gpu',
          'expectedHashType': 'bitmap-64bitMD5',
          'expectedHashDigest': '1707359671708613629',
        },
        ...
      ],
    }

    Raises an Exception if there were any problems.
    """
    if not _SERVER.is_editable():
      raise Exception('this server is not running in --editable mode')

    content_type = self.headers[_HTTP_HEADER_CONTENT_TYPE]
    if content_type != 'application/json;charset=UTF-8':
      raise Exception('unsupported %s [%s]' % (
          _HTTP_HEADER_CONTENT_TYPE, content_type))

    content_length = int(self.headers[_HTTP_HEADER_CONTENT_LENGTH])
    json_data = self.rfile.read(content_length)
    data = json.loads(json_data)
    logging.debug('do_POST_edits: received new GM expectations data [%s]' %
                  data)

    # Since we must make multiple calls to the Results object, grab a
    # reference to it in case it is updated to point at a new Results
    # object within another thread.
    results_obj = _SERVER.results
    oldResultsType = data['oldResultsType']
    oldResults = results_obj.get_results_of_type(oldResultsType)
    oldResultsHash = str(hash(repr(oldResults['testData'])))
    if oldResultsHash != data['oldResultsHash']:
      raise Exception('results of type "%s" changed while the client was '
                      'making modifications. The client should reload the '
                      'results and submit the modifications again.' %
                      oldResultsType)
    results_obj.edit_expectations(data['modifications'])

    # Now that the edits have been committed, update results to reflect them.
    _SERVER.update_results()

  def redirect_to(self, url):
    """ Redirect the HTTP client to a different url.

    Args:
      url: URL to redirect the HTTP client to
    """
    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.

    Args:
      path: path of file whose contents to send to the HTTP client
    """
    # 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.

    Args:
      json_dict: dictionary to send
    """
    self.send_response(200)
    self.send_header('Content-type', 'application/json')
    self.end_headers()
    json.dump(json_dict, self.wfile)


def main():
  logging.basicConfig(level=logging.INFO)
  parser = argparse.ArgumentParser()
  parser.add_argument('--actuals-dir',
                    help=('Directory into which we will check out the latest '
                          'actual GM results. If this directory does not '
                          'exist, it will be created. Defaults to %(default)s'),
                    default=DEFAULT_ACTUALS_DIR)
  parser.add_argument('--editable', action='store_true',
                      help=('Allow HTTP clients to submit new baselines.'))
  # Deprecated the --expectations-dir option, because once our GM expectations
  # are maintained within git we will no longer be able to check out and update
  # them in isolation (in SVN you can update a single directory subtree within
  # a checkout, but you cannot do that with git).
  #
  # In a git world, we will force the user to refer to expectations
  # within the same checkout as this tool (at the relative path
  # ../../expectations/gm ).  If they specify the --reload option, we will
  # periodically run "git pull" on the entire Skia checkout, which will update
  # the GM expectations along with everything else (such as this script).
  #
  # We can still allow --actuals-dir to be specified, though, because the
  # actual results will continue to be maintained in the skia-autogen
  # SVN repository.
  parser.add_argument('--deprecated-expectations-dir',
                    help=('DEPRECATED due to our transition from SVN to git '
                          '(formerly known as --expectations-dir).  '
                          'If you still need this option, contact '
                          'epoger@google.com as soon as possible.  WAS: '
                          '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.  WARNING: doing so will '
                            'allow users on other hosts to modify your '
                            'GM expectations, if combined with --editable.'))
  parser.add_argument('--port', type=int,
                      help=('Which TCP port to listen on for HTTP requests; '
                            'defaults to %(default)s'),
                      default=DEFAULT_PORT)
  parser.add_argument('--reload', type=int,
                      help=('How often (a period in seconds) to update the '
                            'results.  If specified, both '
                            'DEPRECATED_EXPECTATIONS_DIR and '
                            'ACTUAL_DIR will be updated.  '
                            'By default, we do not reload at all, and you '
                            'must restart the server to pick up new data.'),
                      default=0)
  args = parser.parse_args()
  global _SERVER
  _SERVER = Server(actuals_dir=args.actuals_dir,
                   expectations_dir=args.deprecated_expectations_dir,
                   port=args.port, export=args.export, editable=args.editable,
                   reload_seconds=args.reload)
  _SERVER.run()

if __name__ == '__main__':
  main()