1#!/usr/bin/python2 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 12from __future__ import print_function 13 14__author__ = 'cmtice@google.com (Caroline Tice)' 15 16import os 17import sys 18import json 19 20from cros_utils import misc 21 22Defaults = {} 23 24 25class TelemetryDefaults(object): 26 """Class for handling telemetry default return result fields.""" 27 28 DEFAULTS_FILE_NAME = 'crosperf/default-telemetry-results.json' 29 30 def __init__(self): 31 # Get the Crosperf directory; that is where the defaults 32 # file should be. 33 dirname, __ = misc.GetRoot(__file__) 34 fullname = os.path.join(dirname, self.DEFAULTS_FILE_NAME) 35 self._filename = fullname 36 self._defaults = {} 37 38 def ReadDefaultsFile(self): 39 if os.path.exists(self._filename): 40 with open(self._filename, 'r') as fp: 41 self._defaults = json.load(fp) 42 43 def WriteDefaultsFile(self): 44 with open(self._filename, 'w') as fp: 45 json.dump(self._defaults, fp, indent=2) 46 47 def ListCurrentDefaults(self, benchmark=all): 48 # 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: 52 print('The benchmark default results are currently empty.') 53 if benchmark == all: 54 for b in self._defaults.keys(): 55 results = self._defaults[b] 56 out_str = b + ' : ' 57 for r in results: 58 out_str += r + ' ' 59 print(out_str) 60 elif benchmark in self._defaults: 61 results = self._defaults[benchmark] 62 out_str = benchmark + ' : ' 63 for r in results: 64 out_str += r + ' ' 65 print(out_str) 66 else: 67 print("Error: Unrecognized benchmark '%s'" % benchmark) 68 69 def AddDefault(self, benchmark, result): 70 if benchmark in self._defaults: 71 resultList = self._defaults[benchmark] 72 else: 73 resultList = [] 74 resultList.append(result) 75 self._defaults[benchmark] = resultList 76 print("Updated results set for '%s': " % benchmark) 77 print('%s : %s' % (benchmark, repr(self._defaults[benchmark]))) 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 200 201if __name__ == '__main__': 202 retval = Main() 203 sys.exit(retval) 204