blob: 7598caffdc361c307e1898871dc654becf6fb88e [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
stephana38c27052017-01-13 13:16:40 -080070 # make sure the output directory exists.
71 if not os.path.exists(outputDir):
72 os.makedirs(outputDir)
73
stephanafa05e972017-01-02 06:19:41 -080074 def AddTestResult(self, testName, md5Hash, outputImagePath):
75 # Copy the image to <output_dir>/<md5Hash>.<image_extension>
76 imgExt = os.path.splitext(outputImagePath)[1].lstrip(".")
77 if not imgExt:
78 raise ValueError("File %s does not have an extension" % outputImagePath)
79 newFilePath = os.path.join(self._outputDir, md5Hash + '.' + imgExt)
80 shutil.copy2(outputImagePath, newFilePath)
81
82 # Add an entry to the list of test results
83 self._results.append({
84 "key": {
85 "name": testName,
86 "source_type": self._source_type,
87 },
88 "md5": md5Hash,
89 "options": {
90 "ext": imgExt,
91 "gamma_correct": "no"
92 }
93 })
94
95 def _parseKeyValuePairs(self, kvStr):
96 kvPairs = shlex.split(kvStr)
97 if len(kvPairs) % 2:
98 raise ValueError("Uneven number of key/value pairs. Got %s" % kvStr)
99 return { kvPairs[i]:kvPairs[i+1] for i in range(0, len(kvPairs), 2) }
100
101 def WriteResults(self):
102 self._properties.update({
103 "results": self._results
104 })
105
106 outputFileName = os.path.join(self._outputDir, "dm.json")
107 with open(outputFileName, 'wb') as outfile:
108 json.dump(self._properties, outfile, indent=1)
109 outfile.write("\n")
110
111# Produce example output for manual testing.
112if __name__ == "__main__":
113 # Create a test directory with three empty 'image' files.
114 testDir = "./testdirectory"
115 if not os.path.exists(testDir):
116 os.makedirs(testDir)
117 open(os.path.join(testDir, "image1.png"), 'wb').close()
118 open(os.path.join(testDir, "image2.png"), 'wb').close()
119 open(os.path.join(testDir, "image3.png"), 'wb').close()
120
121 # Create an instance and add results.
122 propStr = """build_number 2 "builder name" Builder-Name gitHash a4a338179013b029d6dd55e737b5bd648a9fb68c"""
123
124 keyStr = "arch arm64 compiler Clang configuration Debug"
125
126 gr = GoldResults("pdfium", testDir, propStr, keyStr)
127 gr.AddTestResult("test-1", "hash-1", os.path.join(testDir, "image1.png"))
128 gr.AddTestResult("test-2", "hash-2", os.path.join(testDir, "image2.png"))
129 gr.AddTestResult("test-3", "hash-3", os.path.join(testDir, "image3.png"))
130 gr.WriteResults()