blob: 57743ae8953adb2c430d63ba2dcb7e3736892881 [file] [log] [blame]
epogera8f77452014-07-17 08:22:12 -07001#!/usr/bin/python
2# Copyright (c) 2014 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Utility to duplicate a config in some subset of our GM expectation files.
7
8Created for http://skbug.com/2752 ('split existing "gpu" GM results into "gl"
9and "gles"')
10
11Run with -h to see usage.
12
13Example command lines:
14 copy_config.py gl gles '.*Mac10.7.*'
15
16TODO(epoger): Once https://codereview.chromium.org/397103003/ is committed,
17we should add a unittest. Until then, we can test this as follows:
18
19OLD=expectations/gm && NEW=/tmp/expectations && \
20 rm -rf $NEW && \
21 cp -a $OLD $NEW && \
22 gm/copy_config.py 8888 8888-copy '.*Mac10.7.*' \
23 --expectations-root $NEW && \
24 diff --recursive $OLD $NEW
25"""
26__author__ = 'Elliot Poger'
27
28import argparse
29import os
30import re
31
32import gm_json
33
34DEFAULT_EXPECTATIONS_ROOT = os.path.join(
35 os.path.dirname(__file__), os.pardir, 'expectations', 'gm')
36IMAGE_FILENAME_RE = re.compile(gm_json.IMAGE_FILENAME_PATTERN)
37
38
39class Copier(object):
40
41 def __init__(self, args):
42 """
43 Params:
44 args: the Namespace object generated by argparse.parse_args()
45 """
46 self._args = args
47
48 def run(self):
49 """Perform all the duplications."""
50 for path in self._get_file_list():
51 self._duplicate_config(path=path,
52 old=self._args.old_config_name,
53 new=self._args.new_config_name)
54
55 def _duplicate_config(self, path, old, new):
56 """Duplicates all instances of a config within a GM expectations file.
57
58 Params:
59 path: path to file which will be modified in place
60 old: old config name
61 new: new config name
62 """
63 dic = gm_json.LoadFromFile(file_path=path)
64 expected_results = dic[gm_json.JSONKEY_EXPECTEDRESULTS]
65 orig_keys = expected_results.keys()
66 for key in orig_keys:
67 result = expected_results[key]
68 (testname, config) = IMAGE_FILENAME_RE.match(key).groups()
69 if config == old:
70 config = new
71 key = '%s_%s.png' % (testname, config)
72 expected_results[key] = result
73 gm_json.WriteToFile(json_dict=dic, file_path=path)
74
75 def _get_file_list(self):
76 """Returns the list of files we want to operate on (the complete path
77 to each file)."""
78 root = self._args.expectations_root
79 regex = re.compile(self._args.builder_name_pattern)
80 return [os.path.join(root, builder, 'expected-results.json')
81 for builder in os.listdir(root)
82 if regex.match(builder)]
83
84
85def main():
86 parser = argparse.ArgumentParser()
87 parser.add_argument('old_config_name',
88 help=('Config we want to duplicate.'))
89 parser.add_argument('new_config_name',
90 help=('Name of the new config we want to create.'))
91 parser.add_argument('builder_name_pattern',
92 help=('Regex pattern describing which builders we want '
93 'to make the duplication for; \'.*\' to perform '
94 'the duplication on all builders.'))
95 parser.add_argument('--expectations-root',
96 default=DEFAULT_EXPECTATIONS_ROOT,
97 help=('Root of the GM expectations dir; defaults to '
98 '%(default)s'))
99 args = parser.parse_args()
100 copier = Copier(args)
101 copier.run()
102
103if __name__ == '__main__':
104 main()