Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright 2015 The PDFium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
|
borenet
2016/12/21 19:47:28
Style nit: 2 spaces between anything at the top le
stephana
2016/12/21 21:47:57
Done.
| |
| 5 import json | |
| 6 import os | |
| 7 import shutil | |
| 8 | |
| 9 # This module collects and writes output in a format expected by the | |
| 10 # Gold baseline tool. Based on meta data provided explicitly and by | |
| 11 # adding a series of test results it can be used to produce | |
| 12 # a JSON file that is uploaded to Google Storage and ingested by Gold. | |
| 13 # | |
| 14 # The output will look similar this: | |
| 15 # | |
| 16 # { | |
| 17 # "build_number" : "2", | |
| 18 # "gitHash" : "a4a338179013b029d6dd55e737b5bd648a9fb68c", | |
| 19 # "key" : { | |
| 20 # "arch" : "arm64", | |
| 21 # "compiler" : "Clang", | |
| 22 # }, | |
| 23 # "results" : [ | |
| 24 # { | |
| 25 # "key" : { | |
| 26 # "config" : "vk", | |
| 27 # "name" : "yuv_nv12_to_rgb_effect", | |
| 28 # "source_type" : "gm" | |
| 29 # }, | |
| 30 # "md5" : "7db34da246868d50ab9ddd776ce6d779", | |
| 31 # "options" : { | |
| 32 # "ext" : "png", | |
| 33 # "gamma_correct" : "no" | |
| 34 # } | |
| 35 # }, | |
| 36 # { | |
| 37 # "key" : { | |
| 38 # "config" : "vk", | |
| 39 # "name" : "yuv_to_rgb_effect", | |
| 40 # "source_type" : "gm" | |
| 41 # }, | |
| 42 # "md5" : "0b955f387740c66eb23bf0e253c80d64", | |
| 43 # "options" : { | |
| 44 # "ext" : "png", | |
| 45 # "gamma_correct" : "no" | |
| 46 # } | |
| 47 # } | |
| 48 # ], | |
| 49 # } | |
| 50 # | |
| 51 class GoldResults(object): | |
| 52 def __init__(self, source_type, outputDir, propertiesStr, keyStr): | |
| 53 """ | |
| 54 source_type is the source_type (=corpus) field used for all results. | |
| 55 output_dir is the directory where the resulting images are copied and | |
| 56 the dm.json file is written. | |
| 57 propertiesStr is a string with space separated key/value pairs that | |
| 58 is used to set the top level fields in the output JSON file. | |
| 59 keyStr is a string with space separated key/value pairs that | |
| 60 is used to set the 'key' field in the output JSON file. | |
| 61 """ | |
| 62 self._source_type = source_type | |
| 63 self._properties = self._parseKeyValuePairs(propertiesStr) | |
| 64 self._properties["key"] = self._parseKeyValuePairs(keyStr) | |
| 65 self._results = [] | |
| 66 self._outputDir = outputDir | |
| 67 | |
| 68 def AddTestResult(self, testName, md5Hash, outputImagePath): | |
| 69 # Copy the image to <output_dir>/<md5Hash>.<image_extension> | |
| 70 imgExt = os.path.splitext(outputImagePath)[1].lstrip(".") | |
| 71 if not imgExt: | |
| 72 raise ValueError("File %s does not have an extension" % outputImagePath) | |
| 73 newFilePath = os.path.join(self._outputDir, md5Hash + '.' + imgExt) | |
| 74 shutil.copy2(outputImagePath, newFilePath) | |
| 75 | |
| 76 # Add an entry to the list of test results | |
| 77 self._results.append({ | |
| 78 "key": { | |
| 79 "name": testName, | |
| 80 "source_type": self._source_type, | |
| 81 }, | |
| 82 "md5": md5Hash, | |
| 83 "options": { | |
| 84 "ext": imgExt, | |
| 85 "gamma_correct": "no" | |
| 86 } | |
| 87 }) | |
| 88 | |
| 89 def _parseKeyValuePairs(self, kvStr): | |
| 90 kvPairs = kvStr.split() | |
|
borenet
2016/12/21 19:47:28
I recommend shlex.split, if kvPairs might include
stephana
2016/12/21 21:47:57
Done.
| |
| 91 if len(kvPairs) % 2: | |
| 92 raise ValueError("Uneven number of key/value pairs. Got %s" % kvStr) | |
| 93 return { kvPairs[i]:kvPairs[i+1] for i in range(0, len(kvPairs), 2) } | |
| 94 | |
| 95 def WriteResults(self): | |
| 96 self._properties.update({ | |
| 97 "results": self._results | |
| 98 }) | |
| 99 | |
| 100 outputFileName = os.path.join(self._outputDir, "dm.json") | |
| 101 with open(outputFileName, 'wb') as outfile: | |
| 102 json.dump(self._properties, outfile, indent=1) | |
| 103 outfile.write("\n") | |
| 104 | |
| 105 # Produce example output for manual testing. | |
| 106 if __name__ == "__main__": | |
|
borenet
2016/12/21 19:47:28
I prefer to put the below lines into a function si
stephana
2016/12/21 21:47:57
I don't think so. If I run this from another file:
| |
| 107 # Create a test directory with three empty 'image' files. | |
| 108 testDir = "./testdirectory" | |
| 109 if not os.path.exists(testDir): | |
| 110 os.makedirs(testDir) | |
| 111 open(os.path.join(testDir, "image1.png"), 'wb').close() | |
| 112 open(os.path.join(testDir, "image2.png"), 'wb').close() | |
| 113 open(os.path.join(testDir, "image3.png"), 'wb').close() | |
| 114 | |
| 115 # Create an instance and add results. | |
| 116 propStr = "build_number 2 builder Builder-Name gitHash a4a338179013b029d6dd55e 737b5bd648a9fb68c" | |
|
borenet
2016/12/21 19:47:28
Nit: line break
stephana
2016/12/21 21:47:57
Done.
| |
| 117 keyStr = "arch arm64 compiler Clang configuration Debug" | |
| 118 | |
| 119 gr = GoldResults("pdfium", testDir, propStr, keyStr) | |
| 120 gr.AddTestResult("test-1", "hash-1", os.path.join(testDir, "image1.png")) | |
| 121 gr.AddTestResult("test-2", "hash-2", os.path.join(testDir, "image2.png")) | |
| 122 gr.AddTestResult("test-3", "hash-3", os.path.join(testDir, "image3.png")) | |
| 123 gr.WriteResults() | |
| OLD | NEW |