update_telemetry_defaults.py revision f2a3ef46f75d2196a93d3ed27f4d1fcf22b54fbe
1#!/usr/bin/python 2# 3# Copyright 2013 Google Inc. All Rights Reserved. 4"""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 12__author__ = 'cmtice@google.com (Caroline Tice)' 13 14import os 15import sys 16import json 17 18from utils import misc 19 20Defaults = {} 21 22 23class TelemetryDefaults(object): 24 25 DEFAULTS_FILE_NAME = 'crosperf/default-telemetry-results.json' 26 27 def __init__(self): 28 # Get the Crosperf directory; that is where the defaults 29 # file should be. 30 dirname, __ = misc.GetRoot(__file__) 31 fullname = os.path.join(dirname, self.DEFAULTS_FILE_NAME) 32 self._filename = fullname 33 self._defaults = {} 34 35 def ReadDefaultsFile(self): 36 if os.path.exists(self._filename): 37 with open(self._filename, 'r') as fp: 38 self._defaults = json.load(fp) 39 40 def WriteDefaultsFile(self): 41 with open(self._filename, 'w') as fp: 42 json.dump(self._defaults, fp, indent=2) 43 44 def ListCurrentDefaults(self, benchmark=all): 45 # Show user current defaults. By default, show all. The user 46 # can specify the name of a particular benchmark to see only that 47 # benchmark's default values. 48 if len(self._defaults) == 0: 49 print('The benchmark default results are currently empty.') 50 if benchmark == all: 51 for b in self._defaults.keys(): 52 results = self._defaults[b] 53 out_str = b + ' : ' 54 for r in results: 55 out_str += r + ' ' 56 print(out_str) 57 elif benchmark in self._defaults: 58 results = self._defaults[benchmark] 59 out_str = benchmark + ' : ' 60 for r in results: 61 out_str += r + ' ' 62 print(out_str) 63 else: 64 print("Error: Unrecognized benchmark '%s'" % benchmark) 65 66 def AddDefault(self, benchmark, result): 67 if benchmark in self._defaults: 68 resultList = self._defaults[benchmark] 69 else: 70 resultList = [] 71 resultList.append(result) 72 self._defaults[benchmark] = resultList 73 print("Updated results set for '%s': " % benchmark) 74 print('%s : %s' % (benchmark, repr(self._defaults[benchmark]))) 75 76 def RemoveDefault(self, benchmark, result): 77 if benchmark in self._defaults: 78 resultList = self._defaults[benchmark] 79 if result in resultList: 80 resultList.remove(result) 81 print("Updated results set for '%s': " % benchmark) 82 print('%s : %s' % (benchmark, repr(self._defaults[benchmark]))) 83 else: 84 print("'%s' is not in '%s's default results list." % 85 (result, benchmark)) 86 else: 87 print("Cannot find benchmark named '%s'" % benchmark) 88 89 def GetDefault(self): 90 return self._defaults 91 92 def RemoveBenchmark(self, benchmark): 93 if benchmark in self._defaults: 94 del self._defaults[benchmark] 95 print("Deleted benchmark '%s' from list of benchmarks." % benchmark) 96 else: 97 print("Cannot find benchmark named '%s'" % benchmark) 98 99 def RenameBenchmark(self, old_name, new_name): 100 if old_name in self._defaults: 101 resultsList = self._defaults[old_name] 102 del self._defaults[old_name] 103 self._defaults[new_name] = resultsList 104 print("Renamed '%s' to '%s'." % (old_name, new_name)) 105 else: 106 print("Cannot find benchmark named '%s'" % old_name) 107 108 def UsageError(self, user_input): 109 # Print error message, then show options 110 print("Error:Invalid user input: '%s'" % user_input) 111 self.ShowOptions() 112 113 def ShowOptions(self): 114 print """ 115Below are the valid user options and their arguments, and an explanation 116of what each option does. You may either print out the full name of the 117option, or you may use the first letter of the option. Case (upper or 118lower) does not matter, for the command (case of the result name DOES matter): 119 120 (L)ist - List all current defaults 121 (L)ist <benchmark> - List current defaults for benchmark 122 (H)elp - Show this information. 123 (A)dd <benchmark> <result> - Add a default result for a particular 124 benchmark (appends to benchmark's list 125 of results, if list already exists) 126 (D)elete <benchmark> <result> - Delete a default result for a 127 particular benchmark 128 (R)emove <benchmark> - Remove an entire benchmark (and its 129 results) 130 (M)ove <old-benchmark> <new-benchmark> - Rename a benchmark 131 (Q)uit - Exit this program, saving changes. 132 (T)erminate - Exit this program; abandon changes. 133 134""" 135 136 def GetUserInput(self): 137 # Prompt user 138 print('Enter option> ') 139 # Process user input 140 inp = sys.stdin.readline() 141 inp = inp[:-1] 142 # inp = inp.lower() 143 words = inp.split(' ') 144 option = words[0] 145 option = option.lower() 146 if option == 'h' or option == 'help': 147 self.ShowOptions() 148 elif option == 'l' or option == 'list': 149 if len(words) == 1: 150 self.ListCurrentDefaults() 151 else: 152 self.ListCurrentDefaults(benchmark=words[1]) 153 elif option == 'a' or option == 'add': 154 if len(words) < 3: 155 self.UsageError(inp) 156 else: 157 benchmark = words[1] 158 resultList = words[2:] 159 for r in resultList: 160 self.AddDefault(benchmark, r) 161 elif option == 'd' or option == 'delete': 162 if len(words) != 3: 163 self.UsageError(inp) 164 else: 165 benchmark = words[1] 166 result = words[2] 167 self.RemoveDefault(benchmark, result) 168 elif option == 'r' or option == 'remove': 169 if len(words) != 2: 170 self.UsageError(inp) 171 else: 172 benchmark = words[1] 173 self.RemoveBenchmark(benchmark) 174 elif option == 'm' or option == 'move': 175 if len(words) != 3: 176 self.UsageError(inp) 177 else: 178 old_name = words[1] 179 new_name = words[2] 180 self.RenameBenchmark(old_name, new_name) 181 elif option == 'q' or option == 'quit': 182 self.WriteDefaultsFile() 183 184 return (option == 'q' or option == 'quit' or option == 't' or 185 option == 'terminate') 186 187 188def Main(): 189 defaults = TelemetryDefaults() 190 defaults.ReadDefaultsFile() 191 defaults.ShowOptions() 192 done = defaults.GetUserInput() 193 while not done: 194 done = defaults.GetUserInput() 195 return 0 196 197 198if __name__ == '__main__': 199 retval = Main() 200 sys.exit(retval) 201