update_telemetry_defaults.py revision ef4ca8a8cfe05ec09447896db42fb220d07834f8
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 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 197if __name__ == "__main__": 198 retval = Main() 199 sys.exit(retval) 200