stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 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 | |
| 5 | |
| 6 | import json |
| 7 | import os |
| 8 | import shlex |
| 9 | import shutil |
| 10 | |
| 11 | # This module collects and writes output in a format expected by the |
| 12 | # Gold baseline tool. Based on meta data provided explicitly and by |
| 13 | # adding a series of test results it can be used to produce |
| 14 | # a JSON file that is uploaded to Google Storage and ingested by Gold. |
| 15 | # |
| 16 | # The output will look similar this: |
| 17 | # |
| 18 | # { |
| 19 | # "build_number" : "2", |
| 20 | # "gitHash" : "a4a338179013b029d6dd55e737b5bd648a9fb68c", |
| 21 | # "key" : { |
| 22 | # "arch" : "arm64", |
| 23 | # "compiler" : "Clang", |
| 24 | # }, |
| 25 | # "results" : [ |
| 26 | # { |
| 27 | # "key" : { |
| 28 | # "config" : "vk", |
| 29 | # "name" : "yuv_nv12_to_rgb_effect", |
| 30 | # "source_type" : "gm" |
| 31 | # }, |
| 32 | # "md5" : "7db34da246868d50ab9ddd776ce6d779", |
| 33 | # "options" : { |
| 34 | # "ext" : "png", |
| 35 | # "gamma_correct" : "no" |
| 36 | # } |
| 37 | # }, |
| 38 | # { |
| 39 | # "key" : { |
| 40 | # "config" : "vk", |
| 41 | # "name" : "yuv_to_rgb_effect", |
| 42 | # "source_type" : "gm" |
| 43 | # }, |
| 44 | # "md5" : "0b955f387740c66eb23bf0e253c80d64", |
| 45 | # "options" : { |
| 46 | # "ext" : "png", |
| 47 | # "gamma_correct" : "no" |
| 48 | # } |
| 49 | # } |
| 50 | # ], |
| 51 | # } |
| 52 | # |
| 53 | class GoldResults(object): |
stephana | d532036 | 2017-01-26 15:18:54 -0800 | [diff] [blame] | 54 | def __init__(self, source_type, outputDir, propertiesStr, keyStr, |
| 55 | ignore_hashes_file): |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 56 | """ |
| 57 | source_type is the source_type (=corpus) field used for all results. |
| 58 | output_dir is the directory where the resulting images are copied and |
Stephan Altmueller | 90d80f8 | 2017-03-24 13:19:04 -0400 | [diff] [blame^] | 59 | the dm.json file is written. If the directory exists it will |
| 60 | be removed and recreated. |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 61 | propertiesStr is a string with space separated key/value pairs that |
| 62 | is used to set the top level fields in the output JSON file. |
| 63 | keyStr is a string with space separated key/value pairs that |
| 64 | is used to set the 'key' field in the output JSON file. |
stephana | d532036 | 2017-01-26 15:18:54 -0800 | [diff] [blame] | 65 | ignore_hashes_file is a file that contains a list of image hashes |
| 66 | that should be ignored. |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 67 | """ |
| 68 | self._source_type = source_type |
| 69 | self._properties = self._parseKeyValuePairs(propertiesStr) |
| 70 | self._properties["key"] = self._parseKeyValuePairs(keyStr) |
| 71 | self._results = [] |
| 72 | self._outputDir = outputDir |
| 73 | |
Stephan Altmueller | 90d80f8 | 2017-03-24 13:19:04 -0400 | [diff] [blame^] | 74 | # make sure the output directory exists and is empty. |
| 75 | if os.path.exists(outputDir): |
| 76 | shutil.rmtree(outputDir, ignore_errors=True) |
| 77 | os.makedirs(outputDir) |
stephana | 38c2705 | 2017-01-13 13:16:40 -0800 | [diff] [blame] | 78 | |
stephana | d532036 | 2017-01-26 15:18:54 -0800 | [diff] [blame] | 79 | self._ignore_hashes = set() |
| 80 | if ignore_hashes_file: |
| 81 | with open(ignore_hashes_file, 'r') as ig_file: |
| 82 | hashes=[x.strip() for x in ig_file.readlines() if x.strip()] |
| 83 | self._ignore_hashes = set(hashes) |
| 84 | |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 85 | def AddTestResult(self, testName, md5Hash, outputImagePath): |
stephana | d532036 | 2017-01-26 15:18:54 -0800 | [diff] [blame] | 86 | # If the hash is in the list of hashes to ignore then we don'try |
| 87 | # make a copy, but add it to the result. |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 88 | imgExt = os.path.splitext(outputImagePath)[1].lstrip(".") |
stephana | d532036 | 2017-01-26 15:18:54 -0800 | [diff] [blame] | 89 | if md5Hash not in self._ignore_hashes: |
| 90 | # Copy the image to <output_dir>/<md5Hash>.<image_extension> |
| 91 | if not imgExt: |
| 92 | raise ValueError("File %s does not have an extension" % outputImagePath) |
| 93 | newFilePath = os.path.join(self._outputDir, md5Hash + '.' + imgExt) |
| 94 | shutil.copy2(outputImagePath, newFilePath) |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 95 | |
| 96 | # Add an entry to the list of test results |
| 97 | self._results.append({ |
| 98 | "key": { |
| 99 | "name": testName, |
| 100 | "source_type": self._source_type, |
| 101 | }, |
| 102 | "md5": md5Hash, |
| 103 | "options": { |
| 104 | "ext": imgExt, |
| 105 | "gamma_correct": "no" |
| 106 | } |
| 107 | }) |
| 108 | |
| 109 | def _parseKeyValuePairs(self, kvStr): |
| 110 | kvPairs = shlex.split(kvStr) |
| 111 | if len(kvPairs) % 2: |
| 112 | raise ValueError("Uneven number of key/value pairs. Got %s" % kvStr) |
| 113 | return { kvPairs[i]:kvPairs[i+1] for i in range(0, len(kvPairs), 2) } |
| 114 | |
| 115 | def WriteResults(self): |
| 116 | self._properties.update({ |
| 117 | "results": self._results |
| 118 | }) |
| 119 | |
| 120 | outputFileName = os.path.join(self._outputDir, "dm.json") |
| 121 | with open(outputFileName, 'wb') as outfile: |
| 122 | json.dump(self._properties, outfile, indent=1) |
| 123 | outfile.write("\n") |
| 124 | |
| 125 | # Produce example output for manual testing. |
| 126 | if __name__ == "__main__": |
| 127 | # Create a test directory with three empty 'image' files. |
| 128 | testDir = "./testdirectory" |
| 129 | if not os.path.exists(testDir): |
| 130 | os.makedirs(testDir) |
| 131 | open(os.path.join(testDir, "image1.png"), 'wb').close() |
| 132 | open(os.path.join(testDir, "image2.png"), 'wb').close() |
| 133 | open(os.path.join(testDir, "image3.png"), 'wb').close() |
| 134 | |
| 135 | # Create an instance and add results. |
| 136 | propStr = """build_number 2 "builder name" Builder-Name gitHash a4a338179013b029d6dd55e737b5bd648a9fb68c""" |
| 137 | |
| 138 | keyStr = "arch arm64 compiler Clang configuration Debug" |
| 139 | |
stephana | d532036 | 2017-01-26 15:18:54 -0800 | [diff] [blame] | 140 | hash_file = os.path.join(testDir, "ignore_hashes.txt") |
| 141 | with open(hash_file, 'wb') as f: |
| 142 | f.write("\n".join(["hash-1","hash-4"]) + "\n") |
| 143 | |
| 144 | gr = GoldResults("pdfium", testDir, propStr, keyStr, hash_file) |
stephana | fa05e97 | 2017-01-02 06:19:41 -0800 | [diff] [blame] | 145 | gr.AddTestResult("test-1", "hash-1", os.path.join(testDir, "image1.png")) |
| 146 | gr.AddTestResult("test-2", "hash-2", os.path.join(testDir, "image2.png")) |
| 147 | gr.AddTestResult("test-3", "hash-3", os.path.join(testDir, "image3.png")) |
| 148 | gr.WriteResults() |