blob: 9ee7d8b0ee0da11d4c281bf175cdf970123550eb [file] [log] [blame]
Caroline Tice88272d42016-01-13 09:48:29 -08001#!/usr/bin/python2
cmticefb27ee92013-12-06 13:25:44 -08002#
3# Copyright 2013 Google Inc. All Rights Reserved.
cmticefb27ee92013-12-06 13:25:44 -08004"""Script to maintain the Telemetry benchmark default results file.
5
6This script allows the user to see and update the set of default
7results to be used in generating reports from running the Telemetry
8benchmarks.
9
10"""
11
Caroline Tice88272d42016-01-13 09:48:29 -080012from __future__ import print_function
13
Luis Lozanof2a3ef42015-12-15 13:49:30 -080014__author__ = 'cmtice@google.com (Caroline Tice)'
cmticefb27ee92013-12-06 13:25:44 -080015
16import os
17import sys
18import json
19
Caroline Tice88272d42016-01-13 09:48:29 -080020from cros_utils import misc
cmticefb27ee92013-12-06 13:25:44 -080021
22Defaults = {}
23
cmticefb27ee92013-12-06 13:25:44 -080024
25class TelemetryDefaults(object):
Caroline Tice88272d42016-01-13 09:48:29 -080026 """Class for handling telemetry default return result fields."""
cmticefb27ee92013-12-06 13:25:44 -080027
Caroline Ticeef4ca8a2015-08-25 12:53:38 -070028 DEFAULTS_FILE_NAME = 'crosperf/default-telemetry-results.json'
29
cmticefb27ee92013-12-06 13:25:44 -080030 def __init__(self):
31 # Get the Crosperf directory; that is where the defaults
32 # file should be.
Caroline Ticeef4ca8a2015-08-25 12:53:38 -070033 dirname, __ = misc.GetRoot(__file__)
34 fullname = os.path.join(dirname, self.DEFAULTS_FILE_NAME)
cmticefb27ee92013-12-06 13:25:44 -080035 self._filename = fullname
36 self._defaults = {}
37
Luis Lozanof2a3ef42015-12-15 13:49:30 -080038 def ReadDefaultsFile(self):
cmticefb27ee92013-12-06 13:25:44 -080039 if os.path.exists(self._filename):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080040 with open(self._filename, 'r') as fp:
cmticefb27ee92013-12-06 13:25:44 -080041 self._defaults = json.load(fp)
42
Luis Lozanof2a3ef42015-12-15 13:49:30 -080043 def WriteDefaultsFile(self):
44 with open(self._filename, 'w') as fp:
cmtice27dc5832015-06-30 15:08:06 -070045 json.dump(self._defaults, fp, indent=2)
cmticefb27ee92013-12-06 13:25:44 -080046
Luis Lozanof2a3ef42015-12-15 13:49:30 -080047 def ListCurrentDefaults(self, benchmark=all):
cmticefb27ee92013-12-06 13:25:44 -080048 # Show user current defaults. By default, show all. The user
49 # can specify the name of a particular benchmark to see only that
50 # benchmark's default values.
51 if len(self._defaults) == 0:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080052 print('The benchmark default results are currently empty.')
cmticefb27ee92013-12-06 13:25:44 -080053 if benchmark == all:
54 for b in self._defaults.keys():
55 results = self._defaults[b]
56 out_str = b + ' : '
57 for r in results:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080058 out_str += r + ' '
59 print(out_str)
cmticefb27ee92013-12-06 13:25:44 -080060 elif benchmark in self._defaults:
61 results = self._defaults[benchmark]
62 out_str = benchmark + ' : '
63 for r in results:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080064 out_str += r + ' '
65 print(out_str)
cmticefb27ee92013-12-06 13:25:44 -080066 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080067 print("Error: Unrecognized benchmark '%s'" % benchmark)
cmticefb27ee92013-12-06 13:25:44 -080068
Luis Lozanof2a3ef42015-12-15 13:49:30 -080069 def AddDefault(self, benchmark, result):
cmticefb27ee92013-12-06 13:25:44 -080070 if benchmark in self._defaults:
71 resultList = self._defaults[benchmark]
72 else:
73 resultList = []
74 resultList.append(result)
75 self._defaults[benchmark] = resultList
Luis Lozanof2a3ef42015-12-15 13:49:30 -080076 print("Updated results set for '%s': " % benchmark)
77 print('%s : %s' % (benchmark, repr(self._defaults[benchmark])))
cmticefb27ee92013-12-06 13:25:44 -080078
Luis Lozanof2a3ef42015-12-15 13:49:30 -080079 def RemoveDefault(self, benchmark, result):
cmticefb27ee92013-12-06 13:25:44 -080080 if benchmark in self._defaults:
81 resultList = self._defaults[benchmark]
82 if result in resultList:
83 resultList.remove(result)
Luis Lozanof2a3ef42015-12-15 13:49:30 -080084 print("Updated results set for '%s': " % benchmark)
85 print('%s : %s' % (benchmark, repr(self._defaults[benchmark])))
cmticefb27ee92013-12-06 13:25:44 -080086 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080087 print("'%s' is not in '%s's default results list." %
88 (result, benchmark))
cmticefb27ee92013-12-06 13:25:44 -080089 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080090 print("Cannot find benchmark named '%s'" % benchmark)
cmticefb27ee92013-12-06 13:25:44 -080091
Luis Lozanof2a3ef42015-12-15 13:49:30 -080092 def GetDefault(self):
Yunlian Jiang00094152015-12-10 09:46:49 -080093 return self._defaults
94
Luis Lozanof2a3ef42015-12-15 13:49:30 -080095 def RemoveBenchmark(self, benchmark):
cmticefb27ee92013-12-06 13:25:44 -080096 if benchmark in self._defaults:
97 del self._defaults[benchmark]
Luis Lozanof2a3ef42015-12-15 13:49:30 -080098 print("Deleted benchmark '%s' from list of benchmarks." % benchmark)
cmticefb27ee92013-12-06 13:25:44 -080099 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800100 print("Cannot find benchmark named '%s'" % benchmark)
cmticefb27ee92013-12-06 13:25:44 -0800101
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800102 def RenameBenchmark(self, old_name, new_name):
cmticefb27ee92013-12-06 13:25:44 -0800103 if old_name in self._defaults:
104 resultsList = self._defaults[old_name]
105 del self._defaults[old_name]
106 self._defaults[new_name] = resultsList
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800107 print("Renamed '%s' to '%s'." % (old_name, new_name))
cmticefb27ee92013-12-06 13:25:44 -0800108 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800109 print("Cannot find benchmark named '%s'" % old_name)
cmticefb27ee92013-12-06 13:25:44 -0800110
111 def UsageError(self, user_input):
112 # Print error message, then show options
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800113 print("Error:Invalid user input: '%s'" % user_input)
cmticefb27ee92013-12-06 13:25:44 -0800114 self.ShowOptions()
115
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800116 def ShowOptions(self):
Caroline Tice88272d42016-01-13 09:48:29 -0800117 print("""
cmticefb27ee92013-12-06 13:25:44 -0800118Below are the valid user options and their arguments, and an explanation
119of what each option does. You may either print out the full name of the
120option, or you may use the first letter of the option. Case (upper or
121lower) does not matter, for the command (case of the result name DOES matter):
122
123 (L)ist - List all current defaults
124 (L)ist <benchmark> - List current defaults for benchmark
125 (H)elp - Show this information.
126 (A)dd <benchmark> <result> - Add a default result for a particular
127 benchmark (appends to benchmark's list
128 of results, if list already exists)
129 (D)elete <benchmark> <result> - Delete a default result for a
130 particular benchmark
131 (R)emove <benchmark> - Remove an entire benchmark (and its
132 results)
133 (M)ove <old-benchmark> <new-benchmark> - Rename a benchmark
134 (Q)uit - Exit this program, saving changes.
135 (T)erminate - Exit this program; abandon changes.
136
Caroline Tice88272d42016-01-13 09:48:29 -0800137""")
cmticefb27ee92013-12-06 13:25:44 -0800138
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800139 def GetUserInput(self):
cmticefb27ee92013-12-06 13:25:44 -0800140 # Prompt user
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800141 print('Enter option> ')
cmticefb27ee92013-12-06 13:25:44 -0800142 # Process user input
143 inp = sys.stdin.readline()
144 inp = inp[:-1]
145 # inp = inp.lower()
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800146 words = inp.split(' ')
cmticefb27ee92013-12-06 13:25:44 -0800147 option = words[0]
148 option = option.lower()
149 if option == 'h' or option == 'help':
150 self.ShowOptions()
151 elif option == 'l' or option == 'list':
152 if len(words) == 1:
153 self.ListCurrentDefaults()
154 else:
155 self.ListCurrentDefaults(benchmark=words[1])
156 elif option == 'a' or option == 'add':
157 if len(words) < 3:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800158 self.UsageError(inp)
cmticefb27ee92013-12-06 13:25:44 -0800159 else:
160 benchmark = words[1]
161 resultList = words[2:]
162 for r in resultList:
163 self.AddDefault(benchmark, r)
164 elif option == 'd' or option == 'delete':
165 if len(words) != 3:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800166 self.UsageError(inp)
cmticefb27ee92013-12-06 13:25:44 -0800167 else:
168 benchmark = words[1]
169 result = words[2]
170 self.RemoveDefault(benchmark, result)
171 elif option == 'r' or option == 'remove':
172 if len(words) != 2:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800173 self.UsageError(inp)
cmticefb27ee92013-12-06 13:25:44 -0800174 else:
175 benchmark = words[1]
176 self.RemoveBenchmark(benchmark)
177 elif option == 'm' or option == 'move':
178 if len(words) != 3:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800179 self.UsageError(inp)
cmticefb27ee92013-12-06 13:25:44 -0800180 else:
181 old_name = words[1]
182 new_name = words[2]
183 self.RenameBenchmark(old_name, new_name)
184 elif option == 'q' or option == 'quit':
185 self.WriteDefaultsFile()
186
187 return (option == 'q' or option == 'quit' or option == 't' or
188 option == 'terminate')
189
190
191def Main():
192 defaults = TelemetryDefaults()
193 defaults.ReadDefaultsFile()
194 defaults.ShowOptions()
195 done = defaults.GetUserInput()
196 while not done:
197 done = defaults.GetUserInput()
198 return 0
199
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800200
201if __name__ == '__main__':
cmticefb27ee92013-12-06 13:25:44 -0800202 retval = Main()
203 sys.exit(retval)