#!/usr/bin/env python from listcontexts import ListContextsAsJSON from listtestcases1 import ListTestCases1AsJSON from listtestcases2 import ListTestCases2AsJSON from getstats1 import GetStats1AsJSON from getstats2 import GetStats2AsJSON from getresultdetails2 import GetResultDetails2AsJSON from gettimeseriesstats import GetTimeSeriesStatsAsJSON from gettimeseriesdetails import GetTimeSeriesDetailsAsJSON from getsnapshots import GetSnapshotsAsJSON from getrankings import GetRankingsAsJSON from settimeseriesnote import SetTimeSeriesNote from dbaccess import setDatabase from misc import printErrorAsJSON import sys import os import re import urllib # === Global functions =============================================== # Returns a 2-tuple consisting of: # 1: an option dictionary, and # 2: a flag that is true iff the QUERY_STRING environment variable is # present (i.e. that the script is invoked as a CGI-script for a # HTTP GET request). # # The option dictionary is extracted from either the QUERY_STRING environment # variable (first priority) or command-line arguments (second priority). # In the latter case, the options must be of the form # ... -- ... -- ... def getOptions(): def getOptDictFromQueryString(qs): options = {} for sq in qs.split("&"): keyval = sq.split("=") options[keyval[0]] = urllib.unquote(keyval[1]) return options def getOptDictFromCommandLine(): options = {} p = re.compile("^--(.+)$") key = None for arg in sys.argv[1:]: if key != None: options[key] = arg m = p.match(arg) if m: key = m.group(1) else: key = None return options qs = "QUERY_STRING" if qs in os.environ: return (getOptDictFromQueryString(os.environ[qs]), True) else: return (getOptDictFromCommandLine(), False) # Returns a command instance. def createCommand(options, http_get): def printUsageError(): error = ( "usage: " + sys.argv[0] + " \\\n" + " --db D --cmd contexts [--rankedonly R] | \\\n" + " --db D --cmd testcases1 --host H --platform P --branch B " + "--sha1 S | \\\n" + " --db D --cmd testcases2 --host1 H --platform1 P --branch1 B " + "--sha11 S --host2 H --platform2 P --branch2 B --sha12 S | \\\n" + " --db D --cmd stats1 --host H --platform P --branch B " + "--sha1 S [--testcasefilter 'TC1 TC2 ...'] | \\\n" + " --db D --cmd stats2 --host1 H --platform1 P --branch1 B " + "--sha11 S --host2 H --platform2 P --branch2 B --sha12 S " + "[--testcasefilter 'TC1 TC2 ...'] | \\\n" + " --db D --cmd result_details2 --host1 H --platform1 P " + "--branch1 B --sha11 S --host2 H --platform2 P --branch2 B " + "--sha12 S --benchmark BM --metric M | \\\n" + " --db D --cmd timeseriesstats --host H --platform P --branch B " + "--sha11 S --sha12 S [--difftol T] [--durtolmin T] " + "[--durtolmax T] [--testcasefilter 'TC1 TC2 ...'] | \\\n" + " --db D --cmd timeseriesdetails --host H --platform P " + "--branch B --sha11 S --sha12 S --difftol T --durtolmin T " + "--durtolmax T --benchmark BM --metric M | \\\n" + " --db D --cmd snapshots --host H --platform P " + "--branch B --sha11 S --sha12 S | \\\n" + " --db D --cmd rankings --host H --platform P " + "--branch B --sha1 S [--testcasefilter 'TC1 TC2 ...'] " + "[--maxsize M] | \\\n" + " --db D --cmd settimeseriesnote --host H --platform P " + "--branch B --benchmark B --metric M --note N") if http_get: printErrorAsJSON("usage error") else: print error # Check for mandatory 'db' argument: if "db" in options: db = options["db"] setDatabase(db) else: printUsageError() sys.exit(1) # Check for mandatory 'cmd' argument: if "cmd" in options: cmd = options["cmd"] else: printUsageError() sys.exit(1) # Extract the test case filter needed by some commands: test_case_filter = ( tuple(options["testcasefilter"].split()) if "testcasefilter" in options else None) # Return the command if possible: # --- 'contexts' --------------------------------- if cmd == "contexts": if "rankedonly" in options: try: ranked_only = (int(options["rankedonly"]) != 0) except: ranked_only = (options["rankedonly"].lower() == "true") else: ranked_only = False return ListContextsAsJSON(ranked_only) # --- 'testcases1' --------------------------------- elif cmd == "testcases1": if ("host" in options and "platform" in options and "branch" in options and "sha1" in options): host = options["host"] platform = options["platform"] branch = options["branch"] sha1 = options["sha1"] return ListTestCases1AsJSON(host, platform, branch, sha1) # --- 'testcases2' --------------------------------- elif cmd == "testcases2": if ("host1" in options and "platform1" in options and "branch1" in options and "sha11" in options and "host2" in options and "platform2" in options and "branch2" in options and "sha12" in options): host1 = options["host1"] platform1 = options["platform1"] branch1 = options["branch1"] sha11 = options["sha11"] host2 = options["host2"] platform2 = options["platform2"] branch2 = options["branch2"] sha12 = options["sha12"] return ListTestCases2AsJSON( host1, platform1, branch1, sha11, host2, platform2, branch2, sha12) # --- 'stats1' --------------------------------- elif cmd == "stats1": if ("host" in options and "platform" in options and "branch" in options and "sha1" in options): host = options["host"] platform = options["platform"] branch = options["branch"] sha1 = options["sha1"] return GetStats1AsJSON( host, platform, branch, sha1, test_case_filter) # --- 'stats2' --------------------------------- elif cmd == "stats2": if ("host1" in options and "platform1" in options and "branch1" in options and "sha11" in options and "host2" in options and "platform2" in options and "branch2" in options and "sha12" in options): host1 = options["host1"] platform1 = options["platform1"] branch1 = options["branch1"] sha11 = options["sha11"] host2 = options["host2"] platform2 = options["platform2"] branch2 = options["branch2"] sha12 = options["sha12"] return GetStats2AsJSON( host1, platform1, branch1, sha11, host2, platform2, branch2, sha12, test_case_filter) # --- 'result_details2' --------------------------------- elif cmd == "result_details2": if ("host1" in options and "platform1" in options and "branch1" in options and "sha11" in options and "host2" in options and "platform2" in options and "branch2" in options and "sha12" in options and "benchmark" in options and "metric" in options): host1 = options["host1"] platform1 = options["platform1"] branch1 = options["branch1"] sha11 = options["sha11"] host2 = options["host2"] platform2 = options["platform2"] branch2 = options["branch2"] sha12 = options["sha12"] benchmark = options["benchmark"] metric = options["metric"] return GetResultDetails2AsJSON( host1, platform1, branch1, sha11, host2, platform2, branch2, sha12, benchmark, metric) # --- 'timeseriesstats' --------------------------------- elif cmd == "timeseriesstats": if ("host" in options and "platform" in options and "branch" in options and "sha11" in options and "sha12" in options): host = options["host"] platform = options["platform"] branch = options["branch"] sha11 = options["sha11"] sha12 = options["sha12"] if "difftol" in options: try: difftol = float(options["difftol"]) assert difftol >= 1.0 except: raise BaseException( "'difftol' not a real value >= 1.0: " + str(options["difftol"])) else: difftol = 1.1 # 10% if "durtolmin" in options: try: durtolmin = int(options["durtolmin"]) assert durtolmin >= 1 except: raise BaseException( "'durtolmin' not a positive integer: " + str(options["durtolmin"])) else: durtolmin = 3 if "durtolmax" in options: try: durtolmax = int(options["durtolmax"]) assert durtolmax >= durtolmin except: raise BaseException( "'durtolmax' not >= " + durtolmin + ": " + str(options["durtolmax"])) else: durtolmax = 50 return GetTimeSeriesStatsAsJSON( host, platform, branch, sha11, sha12, difftol, durtolmin, durtolmax, test_case_filter) # --- 'timeseriesdetails' --------------------------------- elif cmd == "timeseriesdetails": if ("host" in options and "platform" in options and "branch" in options and "sha11" in options and "sha12" in options and "difftol" in options and "durtolmin" in options and "durtolmax" in options and "benchmark" in options and "metric" in options): host = options["host"] platform = options["platform"] branch = options["branch"] sha11 = options["sha11"] sha12 = options["sha12"] difftol = float(options["difftol"]) durtolmin = float(options["durtolmin"]) durtolmax = float(options["durtolmax"]) benchmark = options["benchmark"] metric = options["metric"] return GetTimeSeriesDetailsAsJSON( host, platform, branch, sha11, sha12, difftol, durtolmin, durtolmax, benchmark, metric) # --- 'snapshots' --------------------------------- elif cmd == "snapshots": if ("host" in options and "platform" in options and "branch" in options and "sha11" in options and "sha12" in options): host = options["host"] platform = options["platform"] branch = options["branch"] sha11 = options["sha11"] sha12 = options["sha12"] return GetSnapshotsAsJSON(host, platform, branch, sha11, sha12) # --- 'rankings' --------------------------------- elif cmd == "rankings": if ("host" in options and "platform" in options and "branch" in options and "sha1" in options): host = options["host"] platform = options["platform"] branch = options["branch"] sha1 = options["sha1"] if "maxsize" in options: try: maxsize = int(options["maxsize"]) except: raise BaseException("'maxsize' not an integer") else: maxsize = 10 return GetRankingsAsJSON( host, platform, branch, sha1, test_case_filter, maxsize) # --- 'settimeseriesnote' --------------------------------- # ### Hm ... this command doesn't really get statistics, so maybe # rename getstats.py to something more generic # (like bmclient.py) ....... 2 B DONE! elif cmd == "settimeseriesnote": if ("host" in options and "platform" in options and "branch" in options and "benchmark" in options and "metric" in options and "note" in options): host = options["host"] platform = options["platform"] branch = options["branch"] benchmark = options["benchmark"] metric = options["metric"] note = options["note"] return SetTimeSeriesNote( host, platform, branch, benchmark, metric, note) # No match: printUsageError() sys.exit(1) # === Main program =================================================== options, http_get = getOptions() command = createCommand(options, http_get) command.execute()