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 io
     16 import json
     17 import os
     18 import posixpath
     19 import re
     20 
     21 
     22 # Key strings used in GM results JSON files (both expected-results.json and
     23 # actual-results.json).
     24 #
     25 # NOTE: These constants must be kept in sync with the kJsonKey_ constants in
     26 # gm_expectations.cpp and tools/PictureRenderer.cpp !
     27 # Eric suggests: create gm/gm_expectations_constants.h containing ONLY variable
     28 # declarations so as to be readable by both gm/gm_expectations.cpp and Python.
     29 
     30 
     31 JSONKEY_ACTUALRESULTS = 'actual-results'
     32 
     33 # Tests whose results failed to match expectations.
     34 JSONKEY_ACTUALRESULTS_FAILED = 'failed'
     35 
     36 # Tests whose results failed to match expectations, but IGNOREFAILURE causes
     37 # us to take them less seriously.
     38 JSONKEY_ACTUALRESULTS_FAILUREIGNORED = 'failure-ignored'
     39 
     40 # Tests for which we do not have any expectations.  They may be new tests that
     41 # we haven't had a chance to check in expectations for yet, or we may have
     42 # consciously decided to leave them without expectations because we are unhappy
     43 # with the results (although we should try to move away from that, and instead
     44 # check in expectations with the IGNOREFAILURE flag set).
     45 JSONKEY_ACTUALRESULTS_NOCOMPARISON = 'no-comparison'
     46 
     47 # Tests whose results matched their expectations.
     48 JSONKEY_ACTUALRESULTS_SUCCEEDED = 'succeeded'
     49 
     50 
     51 JSONKEY_EXPECTEDRESULTS = 'expected-results'
     52 
     53 # One or more [HashType/DigestValue] pairs representing valid results for this
     54 # test.  Typically, there will just be one pair, but we allow for multiple
     55 # expectations, and the test will pass if any one of them is matched.
     56 JSONKEY_EXPECTEDRESULTS_ALLOWEDDIGESTS = 'allowed-digests'
     57 
     58 # Optional: one or more integers listing Skia bugs (under
     59 # https://code.google.com/p/skia/issues/list ) that pertain to this expectation.
     60 JSONKEY_EXPECTEDRESULTS_BUGS = 'bugs'
     61 
     62 # If IGNOREFAILURE is set to True, a failure of this test will be reported
     63 # within the FAILUREIGNORED section (thus NOT causing the buildbots to go red)
     64 # rather than the FAILED section (which WOULD cause the buildbots to go red).
     65 JSONKEY_EXPECTEDRESULTS_IGNOREFAILURE = 'ignore-failure'
     66 
     67 # Optional: a free-form text string with human-readable information about
     68 # this expectation.
     69 JSONKEY_EXPECTEDRESULTS_NOTES = 'notes'
     70 
     71 # Optional: boolean indicating whether this expectation was reviewed/approved
     72 # by a human being.
     73 # If True: a human looked at this image and approved it.
     74 # If False: this expectation was committed blind.  (In such a case, please
     75 #   add notes indicating why!)
     76 # If absent: this expectation was committed by a tool that didn't enforce human
     77 #   review of expectations.
     78 JSONKEY_EXPECTEDRESULTS_REVIEWED = 'reviewed-by-human'
     79 
     80 # Allowed hash types for test expectations.
     81 JSONKEY_HASHTYPE_BITMAP_64BITMD5 = 'bitmap-64bitMD5'
     82 
     83 JSONKEY_HEADER = 'header'
     84 JSONKEY_HEADER_TYPE = 'type'
     85 JSONKEY_HEADER_REVISION = 'revision'
     86 JSONKEY_IMAGE_CHECKSUMALGORITHM = 'checksumAlgorithm'
     87 JSONKEY_IMAGE_CHECKSUMVALUE = 'checksumValue'
     88 JSONKEY_IMAGE_COMPARISONRESULT = 'comparisonResult'
     89 JSONKEY_IMAGE_FILEPATH = 'filepath'
     90 JSONKEY_SOURCE_TILEDIMAGES = 'tiled-images'
     91 JSONKEY_SOURCE_WHOLEIMAGE = 'whole-image'
     92 
     93 
     94 # Root directory where the buildbots store their actually-generated images...
     95 #  as a publicly readable HTTP URL:
     96 GM_ACTUALS_ROOT_HTTP_URL = (
     97     'http://chromium-skia-gm.commondatastorage.googleapis.com/gm')
     98 #  as a GS URL that allows credential-protected write access:
     99 GM_ACTUALS_ROOT_GS_URL = 'gs://chromium-skia-gm/gm'
    100 
    101 # Root directory where buildbots store skimage actual results json files.
    102 SKIMAGE_ACTUALS_BASE_URL = (
    103     'http://chromium-skia-gm.commondatastorage.googleapis.com/skimage/actuals')
    104 # Root directory inside trunk where skimage expectations are stored.
    105 SKIMAGE_EXPECTATIONS_ROOT = os.path.join('expectations', 'skimage')
    106 
    107 # Pattern used to assemble each image's filename
    108 IMAGE_FILENAME_PATTERN = '(.+)_(.+)\.png'  # matches (testname, config)
    109 
    110 # Pattern used to create image URLs, relative to some base URL.
    111 GM_RELATIVE_URL_FORMATTER = '%s/%s/%s.png' # pass in (hash_type, test_name,
    112                                            #          hash_digest)
    113 GM_RELATIVE_URL_PATTERN = '(.+)/(.+)/(.+).png' # matches (hash_type, test_name,
    114                                                #          hash_digest)
    115 GM_RELATIVE_URL_RE = re.compile(GM_RELATIVE_URL_PATTERN)
    116 
    117 
    118 def CreateGmActualUrl(test_name, hash_type, hash_digest,
    119                       gm_actuals_root_url=GM_ACTUALS_ROOT_HTTP_URL):
    120   """Return the URL we can use to download a particular version of
    121   the actually-generated image for this particular GM test.
    122 
    123   test_name: name of the test, e.g. 'perlinnoise'
    124   hash_type: string indicating the hash type used to generate hash_digest,
    125              e.g. JSONKEY_HASHTYPE_BITMAP_64BITMD5
    126   hash_digest: the hash digest of the image to retrieve
    127   gm_actuals_root_url: root url where actual images are stored
    128   """
    129   return posixpath.join(
    130       gm_actuals_root_url, CreateGmRelativeUrl(
    131           test_name=test_name, hash_type=hash_type, hash_digest=hash_digest))
    132 
    133 
    134 def CreateGmRelativeUrl(test_name, hash_type, hash_digest):
    135   """Returns a relative URL pointing at a test result's image.
    136 
    137   Returns the URL we can use to download a particular version of
    138   the actually-generated image for this particular GM test,
    139   relative to the URL root.
    140 
    141   Args:
    142     test_name: name of the test, e.g. 'perlinnoise'
    143     hash_type: string indicating the hash type used to generate hash_digest,
    144                e.g. JSONKEY_HASHTYPE_BITMAP_64BITMD5
    145     hash_digest: the hash digest of the image to retrieve
    146   """
    147   return GM_RELATIVE_URL_FORMATTER % (hash_type, test_name, hash_digest)
    148 
    149 
    150 def SplitGmRelativeUrl(url):
    151   """Splits the relative URL into (test_name, hash_type, hash_digest) tuple.
    152 
    153   This is the inverse of CreateGmRelativeUrl().
    154 
    155   Args:
    156     url: a URL generated with CreateGmRelativeUrl().
    157 
    158   Returns: (test_name, hash_type, hash_digest) tuple.
    159   """
    160   hash_type, test_name, hash_digest = GM_RELATIVE_URL_RE.match(url).groups()
    161   return (test_name, hash_type, hash_digest)
    162 
    163 
    164 def LoadFromString(file_contents):
    165   """Loads the JSON summary written out by the GM tool.
    166      Returns a dictionary keyed by the values listed as JSONKEY_ constants
    167      above."""
    168   # TODO(epoger): we should add a version number to the JSON file to ensure
    169   # that the writer and reader agree on the schema (raising an exception
    170   # otherwise).
    171   json_dict = json.loads(file_contents)
    172   return json_dict
    173 
    174 
    175 def LoadFromFile(file_path):
    176   """Loads the JSON summary written out by the GM tool.
    177      Returns a dictionary keyed by the values listed as JSONKEY_ constants
    178      above."""
    179   file_contents = open(file_path, 'r').read()
    180   return LoadFromString(file_contents)
    181 
    182 
    183 def WriteToFile(json_dict, file_path):
    184   """Writes the JSON summary in json_dict out to file_path.
    185 
    186   The file is written Unix-style (each line ends with just LF, not CRLF);
    187   see https://code.google.com/p/skia/issues/detail?id=1815 for reasons."""
    188   with io.open(file_path, mode='w', newline='', encoding='utf-8') as outfile:
    189     outfile.write(unicode(json.dumps(json_dict, outfile, sort_keys=True,
    190                                      indent=2)))
    191