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