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