Home | History | Annotate | Download | only in gm
      1 #!/usr/bin/env python
      2 # Copyright (c) 2013 The Chromium Authors. All rights reserved.
      3 # Use of this source code is governed by a BSD-style license that can be
      4 # found in the LICENSE file.
      5 
      6 """Schema of the JSON summary file written out by the GM tool.
      7 
      8 This must be kept in sync with the kJsonKey_ constants in gm_expectations.cpp !
      9 """
     10 
     11 __author__ = 'Elliot Poger'
     12 
     13 
     14 # system-level imports
     15 import json
     16 import os
     17 
     18 
     19 # Key strings used in GM results JSON files (both expected-results.json and
     20 # actual-results.json).
     21 #
     22 # These constants must be kept in sync with the kJsonKey_ constants in
     23 # gm_expectations.cpp !
     24 
     25 
     26 JSONKEY_ACTUALRESULTS = 'actual-results'
     27 
     28 # Tests whose results failed to match expectations.
     29 JSONKEY_ACTUALRESULTS_FAILED = 'failed'
     30 
     31 # Tests whose results failed to match expectations, but IGNOREFAILURE causes
     32 # us to take them less seriously.
     33 JSONKEY_ACTUALRESULTS_FAILUREIGNORED = 'failure-ignored'
     34 
     35 # Tests for which we do not have any expectations.  They may be new tests that
     36 # we haven't had a chance to check in expectations for yet, or we may have
     37 # consciously decided to leave them without expectations because we are unhappy
     38 # with the results (although we should try to move away from that, and instead
     39 # check in expectations with the IGNOREFAILURE flag set).
     40 JSONKEY_ACTUALRESULTS_NOCOMPARISON = 'no-comparison'
     41 
     42 # Tests whose results matched their expectations.
     43 JSONKEY_ACTUALRESULTS_SUCCEEDED = 'succeeded'
     44 
     45 
     46 JSONKEY_EXPECTEDRESULTS = 'expected-results'
     47 
     48 # One or more [HashType/DigestValue] pairs representing valid results for this
     49 # test.  Typically, there will just be one pair, but we allow for multiple
     50 # expectations, and the test will pass if any one of them is matched.
     51 JSONKEY_EXPECTEDRESULTS_ALLOWEDDIGESTS = 'allowed-digests'
     52 
     53 # Optional: one or more integers listing Skia bugs (under
     54 # https://code.google.com/p/skia/issues/list ) that pertain to this expectation.
     55 JSONKEY_EXPECTEDRESULTS_BUGS = 'bugs'
     56 
     57 # If IGNOREFAILURE is set to True, a failure of this test will be reported
     58 # within the FAILUREIGNORED section (thus NOT causing the buildbots to go red)
     59 # rather than the FAILED section (which WOULD cause the buildbots to go red).
     60 JSONKEY_EXPECTEDRESULTS_IGNOREFAILURE = 'ignore-failure'
     61 
     62 # Optional: a free-form text string with human-readable information about
     63 # this expectation.
     64 JSONKEY_EXPECTEDRESULTS_NOTES = 'notes'
     65 
     66 # Optional: boolean indicating whether this expectation was reviewed/approved
     67 # by a human being.
     68 # If True: a human looked at this image and approved it.
     69 # If False: this expectation was committed blind.  (In such a case, please
     70 #   add notes indicating why!)
     71 # If absent: this expectation was committed by a tool that didn't enforce human
     72 #   review of expectations.
     73 JSONKEY_EXPECTEDRESULTS_REVIEWED = 'reviewed-by-human'
     74 
     75 
     76 # Allowed hash types for test expectations.
     77 JSONKEY_HASHTYPE_BITMAP_64BITMD5 = 'bitmap-64bitMD5'
     78 
     79 # Root directory where the buildbots store their actually-generated images...
     80 #  as a publicly readable HTTP URL:
     81 GM_ACTUALS_ROOT_HTTP_URL = (
     82     'http://chromium-skia-gm.commondatastorage.googleapis.com/gm')
     83 #  as a GS URL that allows credential-protected write access:
     84 GM_ACTUALS_ROOT_GS_URL = 'gs://chromium-skia-gm/gm'
     85 
     86 # Root directory where buildbots store skimage actual results json files.
     87 SKIMAGE_ACTUALS_BASE_URL = (
     88     'http://chromium-skia-gm.commondatastorage.googleapis.com/skimage/actuals')
     89 # Root directory inside trunk where skimage expectations are stored.
     90 SKIMAGE_EXPECTATIONS_ROOT = os.path.join('expectations', 'skimage')
     91 
     92 # Pattern used to assemble each image's filename
     93 IMAGE_FILENAME_PATTERN = '(\S+)_(\S+)\.png'  # matches (testname, config)
     94 
     95 def CreateGmActualUrl(test_name, hash_type, hash_digest,
     96                       gm_actuals_root_url=GM_ACTUALS_ROOT_HTTP_URL):
     97   """Return the URL we can use to download a particular version of
     98   the actually-generated image for this particular GM test.
     99 
    100   test_name: name of the test, e.g. 'perlinnoise'
    101   hash_type: string indicating the hash type used to generate hash_digest,
    102              e.g. JSONKEY_HASHTYPE_BITMAP_64BITMD5
    103   hash_digest: the hash digest of the image to retrieve
    104   gm_actuals_root_url: root url where actual images are stored
    105   """
    106   return '%s/%s/%s/%s.png' % (gm_actuals_root_url, hash_type, test_name,
    107                               hash_digest)
    108 
    109 def LoadFromString(file_contents):
    110   """Loads the JSON summary written out by the GM tool.
    111      Returns a dictionary keyed by the values listed as JSONKEY_ constants
    112      above."""
    113   # TODO(epoger): we should add a version number to the JSON file to ensure
    114   # that the writer and reader agree on the schema (raising an exception
    115   # otherwise).
    116   json_dict = json.loads(file_contents)
    117   return json_dict
    118 
    119 def LoadFromFile(file_path):
    120   """Loads the JSON summary written out by the GM tool.
    121      Returns a dictionary keyed by the values listed as JSONKEY_ constants
    122      above."""
    123   file_contents = open(file_path, 'r').read()
    124   return LoadFromString(file_contents)
    125 
    126 def WriteToFile(json_dict, file_path):
    127   """Writes the JSON summary in json_dict out to file_path."""
    128   with open(file_path, 'w') as outfile:
    129     json.dump(json_dict, outfile, sort_keys=True, indent=2)
    130