update_telemetry_defaults.py revision 0009415847e04be4670c025e1b475adc7de23cc8
1#!/usr/bin/python 2# 3# Copyright 2013 Google Inc. All Rights Reserved. 4 5"""Script to maintain the Telemetry benchmark default results file. 6 7This script allows the user to see and update the set of default 8results to be used in generating reports from running the Telemetry 9benchmarks. 10 11""" 12 13__author__ = "cmtice@google.com (Caroline Tice)" 14 15import os 16import sys 17import json 18 19from utils import misc 20 21Defaults = {} 22 23 24class TelemetryDefaults(object): 25 26 DEFAULTS_FILE_NAME = 'crosperf/default-telemetry-results.json' 27 28 def __init__(self): 29 # Get the Crosperf directory; that is where the defaults 30 # file should be. 31 dirname, __ = misc.GetRoot(__file__) 32 fullname = os.path.join(dirname, self.DEFAULTS_FILE_NAME) 33 self._filename = fullname 34 self._defaults = {} 35 36 def ReadDefaultsFile (self): 37 if os.path.exists(self._filename): 38 with open(self._filename, "r") as fp: 39 self._defaults = json.load(fp) 40 41 def WriteDefaultsFile (self): 42 with open(self._filename, "w") as fp: 43 json.dump(self._defaults, fp, indent=2) 44 45 def ListCurrentDefaults (self, benchmark=all): 46 # Show user current defaults. By default, show all. The user 47 # can specify the name of a particular benchmark to see only that 48 # benchmark's default values. 49 if len(self._defaults) == 0: 50 print ("The benchmark default results are currently empty.") 51 if benchmark == all: 52 for b in self._defaults.keys(): 53 results = self._defaults[b] 54 out_str = b + ' : ' 55 for r in results: 56 out_str += r + ' ' 57 print (out_str) 58 elif benchmark in self._defaults: 59 results = self._defaults[benchmark] 60 out_str = benchmark + ' : ' 61 for r in results: 62 out_str += r + ' ' 63 print (out_str) 64 else: 65 print ("Error: Unrecognized benchmark '%s'" % benchmark) 66 67 68 def AddDefault (self, benchmark, result): 69 if benchmark in self._defaults: 70 resultList = self._defaults[benchmark] 71 else: 72 resultList = [] 73 resultList.append(result) 74 self._defaults[benchmark] = resultList 75 print ("Updated results set for '%s': " % benchmark) 76 print ("%s : %s" % (benchmark, repr(self._defaults[benchmark]))) 77 78 79 def RemoveDefault (self, benchmark, result): 80 if benchmark in self._defaults: 81 resultList = self._defaults[benchmark] 82 if result in resultList: 83 resultList.remove(result) 84 print ("Updated results set for '%s': " % benchmark) 85 print ("%s : %s" % (benchmark, repr(self._defaults[benchmark]))) 86 else: 87 print ("'%s' is not in '%s's default results list." % 88 (result, benchmark)) 89 else: 90 print ("Cannot find benchmark named '%s'" % benchmark) 91 92 def GetDefault (self): 93 return self._defaults 94 95 def RemoveBenchmark (self, benchmark): 96 if benchmark in self._defaults: 97 del self._defaults[benchmark] 98 print ("Deleted benchmark '%s' from list of benchmarks." % benchmark) 99 else: 100 print ("Cannot find benchmark named '%s'" % benchmark) 101 102 def RenameBenchmark (self, old_name, new_name): 103 if old_name in self._defaults: 104 resultsList = self._defaults[old_name] 105 del self._defaults[old_name] 106 self._defaults[new_name] = resultsList 107 print ("Renamed '%s' to '%s'." % (old_name, new_name)) 108 else: 109 print ("Cannot find benchmark named '%s'" % old_name) 110 111 def UsageError(self, user_input): 112 # Print error message, then show options 113 print ("Error:Invalid user input: '%s'" % user_input) 114 self.ShowOptions() 115 116 def ShowOptions (self): 117 print """ 118Below 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 137""" 138 139 def GetUserInput (self): 140 # Prompt user 141 print ("Enter option> ") 142 # Process user input 143 inp = sys.stdin.readline() 144 inp = inp[:-1] 145 # inp = inp.lower() 146 words = inp.split(" ") 147 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: 158 self.UsageError (inp) 159 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: 166 self.UsageError (inp) 167 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: 173 self.UsageError (inp) 174 else: 175 benchmark = words[1] 176 self.RemoveBenchmark(benchmark) 177 elif option == 'm' or option == 'move': 178 if len(words) != 3: 179 self.UsageError (inp) 180 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 200if __name__ == "__main__": 201 retval = Main() 202 sys.exit(retval) 203