blob: 943dc26143a2385666567f433e0e2ddc36f08980 [file] [log] [blame]
Caroline Ticef6ef4392017-04-06 17:16:05 -07001#!/usr/bin/env 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.
cmticefb27ee92013-12-06 13:25:44 -08009"""
10
Caroline Tice88272d42016-01-13 09:48:29 -080011from __future__ import print_function
12
Luis Lozanof2a3ef42015-12-15 13:49:30 -080013__author__ = 'cmtice@google.com (Caroline Tice)'
cmticefb27ee92013-12-06 13:25:44 -080014
15import os
16import sys
17import json
18
Caroline Tice88272d42016-01-13 09:48:29 -080019from cros_utils import misc
cmticefb27ee92013-12-06 13:25:44 -080020
21Defaults = {}
22
cmticefb27ee92013-12-06 13:25:44 -080023
24class TelemetryDefaults(object):
Caroline Tice88272d42016-01-13 09:48:29 -080025 """Class for handling telemetry default return result fields."""
cmticefb27ee92013-12-06 13:25:44 -080026
Caroline Ticeef4ca8a2015-08-25 12:53:38 -070027 DEFAULTS_FILE_NAME = 'crosperf/default-telemetry-results.json'
28
cmticefb27ee92013-12-06 13:25:44 -080029 def __init__(self):
30 # Get the Crosperf directory; that is where the defaults
31 # file should be.
Caroline Ticeef4ca8a2015-08-25 12:53:38 -070032 dirname, __ = misc.GetRoot(__file__)
33 fullname = os.path.join(dirname, self.DEFAULTS_FILE_NAME)
cmticefb27ee92013-12-06 13:25:44 -080034 self._filename = fullname
35 self._defaults = {}
36
Luis Lozanof2a3ef42015-12-15 13:49:30 -080037 def ReadDefaultsFile(self):
cmticefb27ee92013-12-06 13:25:44 -080038 if os.path.exists(self._filename):
Luis Lozanof2a3ef42015-12-15 13:49:30 -080039 with open(self._filename, 'r') as fp:
cmticefb27ee92013-12-06 13:25:44 -080040 self._defaults = json.load(fp)
41
Luis Lozanof2a3ef42015-12-15 13:49:30 -080042 def WriteDefaultsFile(self):
43 with open(self._filename, 'w') as fp:
cmtice27dc5832015-06-30 15:08:06 -070044 json.dump(self._defaults, fp, indent=2)
cmticefb27ee92013-12-06 13:25:44 -080045
Luis Lozanof2a3ef42015-12-15 13:49:30 -080046 def ListCurrentDefaults(self, benchmark=all):
cmticefb27ee92013-12-06 13:25:44 -080047 # Show user current defaults. By default, show all. The user
48 # can specify the name of a particular benchmark to see only that
49 # benchmark's default values.
50 if len(self._defaults) == 0:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080051 print('The benchmark default results are currently empty.')
cmticefb27ee92013-12-06 13:25:44 -080052 if benchmark == all:
53 for b in self._defaults.keys():
54 results = self._defaults[b]
55 out_str = b + ' : '
56 for r in results:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080057 out_str += r + ' '
58 print(out_str)
cmticefb27ee92013-12-06 13:25:44 -080059 elif benchmark in self._defaults:
60 results = self._defaults[benchmark]
61 out_str = benchmark + ' : '
62 for r in results:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080063 out_str += r + ' '
64 print(out_str)
cmticefb27ee92013-12-06 13:25:44 -080065 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080066 print("Error: Unrecognized benchmark '%s'" % benchmark)
cmticefb27ee92013-12-06 13:25:44 -080067
Luis Lozanof2a3ef42015-12-15 13:49:30 -080068 def AddDefault(self, benchmark, result):
cmticefb27ee92013-12-06 13:25:44 -080069 if benchmark in self._defaults:
70 resultList = self._defaults[benchmark]
71 else:
72 resultList = []
73 resultList.append(result)
74 self._defaults[benchmark] = resultList
Luis Lozanof2a3ef42015-12-15 13:49:30 -080075 print("Updated results set for '%s': " % benchmark)
76 print('%s : %s' % (benchmark, repr(self._defaults[benchmark])))
cmticefb27ee92013-12-06 13:25:44 -080077
Luis Lozanof2a3ef42015-12-15 13:49:30 -080078 def RemoveDefault(self, benchmark, result):
cmticefb27ee92013-12-06 13:25:44 -080079 if benchmark in self._defaults:
80 resultList = self._defaults[benchmark]
81 if result in resultList:
82 resultList.remove(result)
Luis Lozanof2a3ef42015-12-15 13:49:30 -080083 print("Updated results set for '%s': " % benchmark)
84 print('%s : %s' % (benchmark, repr(self._defaults[benchmark])))
cmticefb27ee92013-12-06 13:25:44 -080085 else:
Caroline Ticef6ef4392017-04-06 17:16:05 -070086 print("'%s' is not in '%s's default results list." % (result,
87 benchmark))
cmticefb27ee92013-12-06 13:25:44 -080088 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080089 print("Cannot find benchmark named '%s'" % benchmark)
cmticefb27ee92013-12-06 13:25:44 -080090
Luis Lozanof2a3ef42015-12-15 13:49:30 -080091 def GetDefault(self):
Yunlian Jiang00094152015-12-10 09:46:49 -080092 return self._defaults
93
Luis Lozanof2a3ef42015-12-15 13:49:30 -080094 def RemoveBenchmark(self, benchmark):
cmticefb27ee92013-12-06 13:25:44 -080095 if benchmark in self._defaults:
96 del self._defaults[benchmark]
Luis Lozanof2a3ef42015-12-15 13:49:30 -080097 print("Deleted benchmark '%s' from list of benchmarks." % benchmark)
cmticefb27ee92013-12-06 13:25:44 -080098 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -080099 print("Cannot find benchmark named '%s'" % benchmark)
cmticefb27ee92013-12-06 13:25:44 -0800100
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800101 def RenameBenchmark(self, old_name, new_name):
cmticefb27ee92013-12-06 13:25:44 -0800102 if old_name in self._defaults:
103 resultsList = self._defaults[old_name]
104 del self._defaults[old_name]
105 self._defaults[new_name] = resultsList
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800106 print("Renamed '%s' to '%s'." % (old_name, new_name))
cmticefb27ee92013-12-06 13:25:44 -0800107 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800108 print("Cannot find benchmark named '%s'" % old_name)
cmticefb27ee92013-12-06 13:25:44 -0800109
110 def UsageError(self, user_input):
111 # Print error message, then show options
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800112 print("Error:Invalid user input: '%s'" % user_input)
cmticefb27ee92013-12-06 13:25:44 -0800113 self.ShowOptions()
114
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800115 def ShowOptions(self):
Caroline Tice88272d42016-01-13 09:48:29 -0800116 print("""
cmticefb27ee92013-12-06 13:25:44 -0800117Below are the valid user options and their arguments, and an explanation
118of what each option does. You may either print out the full name of the
119option, or you may use the first letter of the option. Case (upper or
120lower) does not matter, for the command (case of the result name DOES matter):
121
122 (L)ist - List all current defaults
123 (L)ist <benchmark> - List current defaults for benchmark
124 (H)elp - Show this information.
125 (A)dd <benchmark> <result> - Add a default result for a particular
126 benchmark (appends to benchmark's list
127 of results, if list already exists)
128 (D)elete <benchmark> <result> - Delete a default result for a
129 particular benchmark
130 (R)emove <benchmark> - Remove an entire benchmark (and its
131 results)
132 (M)ove <old-benchmark> <new-benchmark> - Rename a benchmark
133 (Q)uit - Exit this program, saving changes.
134 (T)erminate - Exit this program; abandon changes.
135
Caroline Tice88272d42016-01-13 09:48:29 -0800136""")
cmticefb27ee92013-12-06 13:25:44 -0800137
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800138 def GetUserInput(self):
cmticefb27ee92013-12-06 13:25:44 -0800139 # Prompt user
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800140 print('Enter option> ')
cmticefb27ee92013-12-06 13:25:44 -0800141 # Process user input
142 inp = sys.stdin.readline()
143 inp = inp[:-1]
144 # inp = inp.lower()
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800145 words = inp.split(' ')
cmticefb27ee92013-12-06 13:25:44 -0800146 option = words[0]
147 option = option.lower()
148 if option == 'h' or option == 'help':
149 self.ShowOptions()
150 elif option == 'l' or option == 'list':
151 if len(words) == 1:
152 self.ListCurrentDefaults()
153 else:
154 self.ListCurrentDefaults(benchmark=words[1])
155 elif option == 'a' or option == 'add':
156 if len(words) < 3:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800157 self.UsageError(inp)
cmticefb27ee92013-12-06 13:25:44 -0800158 else:
159 benchmark = words[1]
160 resultList = words[2:]
161 for r in resultList:
162 self.AddDefault(benchmark, r)
163 elif option == 'd' or option == 'delete':
164 if len(words) != 3:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800165 self.UsageError(inp)
cmticefb27ee92013-12-06 13:25:44 -0800166 else:
167 benchmark = words[1]
168 result = words[2]
169 self.RemoveDefault(benchmark, result)
170 elif option == 'r' or option == 'remove':
171 if len(words) != 2:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800172 self.UsageError(inp)
cmticefb27ee92013-12-06 13:25:44 -0800173 else:
174 benchmark = words[1]
175 self.RemoveBenchmark(benchmark)
176 elif option == 'm' or option == 'move':
177 if len(words) != 3:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800178 self.UsageError(inp)
cmticefb27ee92013-12-06 13:25:44 -0800179 else:
180 old_name = words[1]
181 new_name = words[2]
182 self.RenameBenchmark(old_name, new_name)
183 elif option == 'q' or option == 'quit':
184 self.WriteDefaultsFile()
185
186 return (option == 'q' or option == 'quit' or option == 't' or
187 option == 'terminate')
188
189
190def Main():
191 defaults = TelemetryDefaults()
192 defaults.ReadDefaultsFile()
193 defaults.ShowOptions()
194 done = defaults.GetUserInput()
195 while not done:
196 done = defaults.GetUserInput()
197 return 0
198
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800199
200if __name__ == '__main__':
cmticefb27ee92013-12-06 13:25:44 -0800201 retval = Main()
202 sys.exit(retval)