blob: bdae9f4195211878ca7f0a8f02756aa159a139b8 [file] [log] [blame]
stephanafa05e972017-01-02 06:19:41 -08001# 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
6import json
7import os
8import shlex
9import 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#
53class GoldResults(object):
stephanad5320362017-01-26 15:18:54 -080054 def __init__(self, source_type, outputDir, propertiesStr, keyStr,
55 ignore_hashes_file):
stephanafa05e972017-01-02 06:19:41 -080056 """
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 Altmueller90d80f82017-03-24 13:19:04 -040059 the dm.json file is written. If the directory exists it will
60 be removed and recreated.
stephanafa05e972017-01-02 06:19:41 -080061 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.
stephanad5320362017-01-26 15:18:54 -080065 ignore_hashes_file is a file that contains a list of image hashes
66 that should be ignored.
stephanafa05e972017-01-02 06:19:41 -080067 """
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 Altmueller90d80f82017-03-24 13:19:04 -040074 # 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)
stephana38c27052017-01-13 13:16:40 -080078
stephanad5320362017-01-26 15:18:54 -080079 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
stephanafa05e972017-01-02 06:19:41 -080085 def AddTestResult(self, testName, md5Hash, outputImagePath):
stephanad5320362017-01-26 15:18:54 -080086 # 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.
stephanafa05e972017-01-02 06:19:41 -080088 imgExt = os.path.splitext(outputImagePath)[1].lstrip(".")
stephanad5320362017-01-26 15:18:54 -080089 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)
stephanafa05e972017-01-02 06:19:41 -080095
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.
126if __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
stephanad5320362017-01-26 15:18:54 -0800140 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)
stephanafa05e972017-01-02 06:19:41 -0800145 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()