blob: fda63b6debb998f31c47e81702d697bd787a8c2c [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):
54 def __init__(self, source_type, outputDir, propertiesStr, keyStr):
55 """
56 source_type is the source_type (=corpus) field used for all results.
57 output_dir is the directory where the resulting images are copied and
58 the dm.json file is written.
59 propertiesStr is a string with space separated key/value pairs that
60 is used to set the top level fields in the output JSON file.
61 keyStr is a string with space separated key/value pairs that
62 is used to set the 'key' field in the output JSON file.
63 """
64 self._source_type = source_type
65 self._properties = self._parseKeyValuePairs(propertiesStr)
66 self._properties["key"] = self._parseKeyValuePairs(keyStr)
67 self._results = []
68 self._outputDir = outputDir
69
70 def AddTestResult(self, testName, md5Hash, outputImagePath):
71 # Copy the image to <output_dir>/<md5Hash>.<image_extension>
72 imgExt = os.path.splitext(outputImagePath)[1].lstrip(".")
73 if not imgExt:
74 raise ValueError("File %s does not have an extension" % outputImagePath)
75 newFilePath = os.path.join(self._outputDir, md5Hash + '.' + imgExt)
76 shutil.copy2(outputImagePath, newFilePath)
77
78 # Add an entry to the list of test results
79 self._results.append({
80 "key": {
81 "name": testName,
82 "source_type": self._source_type,
83 },
84 "md5": md5Hash,
85 "options": {
86 "ext": imgExt,
87 "gamma_correct": "no"
88 }
89 })
90
91 def _parseKeyValuePairs(self, kvStr):
92 kvPairs = shlex.split(kvStr)
93 if len(kvPairs) % 2:
94 raise ValueError("Uneven number of key/value pairs. Got %s" % kvStr)
95 return { kvPairs[i]:kvPairs[i+1] for i in range(0, len(kvPairs), 2) }
96
97 def WriteResults(self):
98 self._properties.update({
99 "results": self._results
100 })
101
102 outputFileName = os.path.join(self._outputDir, "dm.json")
103 with open(outputFileName, 'wb') as outfile:
104 json.dump(self._properties, outfile, indent=1)
105 outfile.write("\n")
106
107# Produce example output for manual testing.
108if __name__ == "__main__":
109 # Create a test directory with three empty 'image' files.
110 testDir = "./testdirectory"
111 if not os.path.exists(testDir):
112 os.makedirs(testDir)
113 open(os.path.join(testDir, "image1.png"), 'wb').close()
114 open(os.path.join(testDir, "image2.png"), 'wb').close()
115 open(os.path.join(testDir, "image3.png"), 'wb').close()
116
117 # Create an instance and add results.
118 propStr = """build_number 2 "builder name" Builder-Name gitHash a4a338179013b029d6dd55e737b5bd648a9fb68c"""
119
120 keyStr = "arch arm64 compiler Clang configuration Debug"
121
122 gr = GoldResults("pdfium", testDir, propStr, keyStr)
123 gr.AddTestResult("test-1", "hash-1", os.path.join(testDir, "image1.png"))
124 gr.AddTestResult("test-2", "hash-2", os.path.join(testDir, "image2.png"))
125 gr.AddTestResult("test-3", "hash-3", os.path.join(testDir, "image3.png"))
126 gr.WriteResults()