Skip to content

Commit

Permalink
Merge branch 'dev' into bs-blackbox-schema
Browse files Browse the repository at this point in the history
mbottini committed Apr 18, 2018

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
2 parents a0cc642 + 81ba264 commit 68bfc9e
Showing 3 changed files with 81 additions and 88 deletions.
100 changes: 20 additions & 80 deletions dialogue.py
Original file line number Diff line number Diff line change
@@ -4,9 +4,10 @@
# Invokes run.sh with configurations stored in the file
# 'runtype_options.json'


import json

EXIT_CONSTS = set(['q', 'quit', 'exit'])
YES_CONSTS = set(['y', 'yes'])

def write_json(filename, python_dict):
"""
@@ -27,84 +28,20 @@ def read_json(filename):
return json.load(f)


def create_run_dict(runtype, *args):
"""
Returns a dictionary of a run-type option object deserialized
from a human-readable configuration file (e.g., JSON, HJSON, YAML).
:param runtype: (HBIR || HBIR_RT || PRESET || LOADLEVEL)
"""
runtype_option_dict = read_json('runtype_options.json')
if runtype not in runtype_option_dict:
return {}
parameter_list = runtype_option_dict[runtype]

if(len(parameter_list) == len(args)):
return_dict = {tup[0] : tup[1] for tup in zip(parameter_list, args)}
return_dict['Run Type'] = runtype
return return_dict
return {}


def create_run_dict_list(runtype, args):
"""
Returns a dictionary of run-type option objects.
:param runtype: (HBIR || HBIR_RT || PRESET || LOADLEVEL)
"""
return create_run_dict(runtype, *args)


def create_run_sh_invocation(run_dict):
"""
Builds a string to be used to invoke the `run.sh` script in ~/workloads. The
command-line arguments of `run.sh` are initialized with the respective values in
run_dict.
:param run_dict: dict
"""
try:
runtype_option_dict = read_json('runtype_options.json')
parameters = runtype_option_dict[run_dict['Run Type']]
return './run.sh {}'.format(
' '.join(str(run_dict[p]) for p in parameters))
except: # Probably a dictionary failure.
return ''


def execute_run(run_dict):
"""
Invoke the `run.sh` script in ~/workloads with command-line arguments stored
in run_dict.
:param run_dict: dict
"""
# We'll eventually invoke this for realsies. For now, we're just going to
# print it.
invocation = create_run_sh_invocation(run_dict)
if invocation:
print(invocation)
return True
return False


def execute_runs(run_dict_list):
"""
A wrapper for `execute_run(...)`, which is invoked repeatedly for each set
of run-type options found in run_dict_list.
:param run_dict_list: [dict]
"""
return [execute_run(run_dict) for run_dict in run_dict_list]


# Utility functions.

def print_dict(d):
for key, value in sorted(d.items(), key=lambda x : x[0]):
print("{}: {}".format(key, value))


# Level-one layer of dialogue.
# All functions take run_dict, runtype_dict as arguments so that they can be
# called homogenously from a dictionary in `dialogue`.

def print_all_runs(run_dict, runtype_dict):
for k, v in sorted(run_dict.items(), key=lambda x : x[0]):
print('Run {}'.format(k))
print('\nTag {}\n'.format(k))
print_dict(v)


@@ -119,29 +56,32 @@ def create_run(run_dict, runtype_dict):
run_type = input('-> ')
if run_type not in runtype_dict.keys():
user_input = input('{} is not currently an option. Add it? ')
if user_input.lower() in ['y', 'yes']:
if user_input.lower() in YES_CONSTS:
create_runtype(runtype_dict)
return
run_dict[run_name] = create_run_dialogue(runtype_dict[run_type])


def create_runtype(run_dict, runtype_dict):
option_set = set()
# We need to preserve order for entry consistency's sake.
option_list = []
user_input = input('Input a name for the runtype. ')
while (user_input in runtype_dict and
user_input.lower() not in ['q', 'quit']):
user_input.lower() not in EXIT_CONSTS):
user_input = ('Name already exists. Input a different name. ')

runtype_name = user_input

while user_input.lower() not in ['q', 'quit']:
while user_input.lower() not in EXIT_CONSTS:
user_input = input('Input a new run attribute. ')
if user_input in option_set:
print('{} has already been added!'.format(user_input))
option_set.add(user_input)
option_list.append(user_input)

if runtype_name.lower() not in ['q', 'quit']:
runtype_dict[runtype_name] = list(option_set)
if runtype_name.lower() not in EXIT_CONSTS:
runtype_dict[runtype_name] = option_list


def delete_run(run_dict, runtype_dict):
@@ -207,7 +147,7 @@ def load_runlist(run_dict, runtype_dict):

def new_runlist(run_dict, runtype_dict):
user_input = input('Are you sure? This will remove all runs from the RunDict!')
if user_input.lower() in ['y', 'yes']:
if user_input.lower() in YES_CONSTS:
run_dict = {}
print('RunDict has been cleared.')
else:
@@ -235,8 +175,8 @@ def edit_run_dialogue(old_run):

def dialogue():
"""
A dialogue function for creating a RunDict.
Note that this currently does not allow you to create a new category of Run.
A dialogue function for creating, editing, and saving RunSet objects
as JSON objects.
"""

default_json = 'example_config.json'
@@ -286,10 +226,10 @@ def dialogue():

user_input = ""

while user_input.lower() not in ['q', 'quit']:
print("What would you like to do?")
while user_input.lower() not in EXIT_CONSTS:
print("\nWhat would you like to do?\n")
print_dict(option_description_dict)
user_input = input("-> ")
if user_input.lower() not in ['q', 'quit']:
if user_input.lower() not in EXIT_CONSTS:
function_dict.get(user_input, error)(run_dict, runtype_dict)
print('Exiting.')
45 changes: 37 additions & 8 deletions mainCLI.py
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@
import json
import os
from subprocess import call
from shutil import copy, rmtree

# external imports
from docopt import docopt
@@ -21,9 +22,9 @@


def to_list(s):
if s['run_type'].lower() in ["hbir", "hbir_rt"]:
if s["run_type"].lower() in ["hbir", "hbir_rt"]:
return [
s['run_type'], # RUNTYPE
s["run_type"], # RUNTYPE
s["kit_version"], # kitVersion
s["tag"], # tag
s["jdk"], # JDK
@@ -37,7 +38,7 @@ def to_list(s):
]
else:
return [
s['run_type'], # runtype
s["run_type"], # runtype
s["kit_version"], # kitversion
s["tag"], # tag
s["jdk"], # jdk
@@ -51,6 +52,15 @@ def to_list(s):
s["t"][2], # t3
]

def relative_to_main(relname):
return os.path.join(os.path.dirname(__file__), relname)

blackbox_artifacts = [
'.run_number',
'controller.out',
'specjbb2015.props',
'sut.txt',
]

def do_run(arguments):
"""
@@ -59,10 +69,30 @@ def do_run(arguments):
with open(arguments['<config>'], 'r') as f:
args = json.loads(f.read())
stringified = list(map(lambda arg: str(arg), to_list(args['specjbb'])))
call(
['bash', 'run.sh'] + stringified,
cwd=os.path.join(os.path.dirname(__file__), 'scripts')
)
workdir = args['specjbb'].get('workdir', 'scripts')
scripts_abspath = relative_to_main(workdir)
workdir_abspath = relative_to_main('scripts')

# if we don't already have the scripts available to us
# copy them into the new location
if workdir != 'scripts':
copy(scripts_abspath, workdir_abspath)

def cleanup():
# we need to cleanup the cwd or worktree for some reason
if workdir != 'scripts':
rmtree(workdir_abspath)
else:
for name in map(lambda name: os.path.join(scripts_abspath, name),
blackbox_artifacts):
os.remove(name)

try:
if not call(['bash', 'run.sh'] + stringified, cwd=workdir_abspath):
cleanup()
except:
cleanup()


def do_validate(arguments):
"""
@@ -84,7 +114,6 @@ def do_dialogue(arguments):
'dialogue' : do_dialogue
}


if __name__ == "__main__":
arguments = docopt(__doc__, version='SPECtate v0.1')

24 changes: 24 additions & 0 deletions tests/test_mainCLI.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import unittest
import json

import mainCLI as main

example_configuration = json.loads(open("example_config.json", "r").read())["specjbb"]

class TestMain(unittest.TestCase):
def test_to_list_gives_arguments(self):
self.assertIsNotNone(main.to_list(example_configuration))

def test_to_list_changes_based_on_run_type(self):
different = example_configuration.copy()
different.update({ "run_type": "HBIR_RT" })

if example_configuration == different:
self.skipTest()

self.assertNotEqual(main.to_list(example_configuration),
main.to_list(different))

def test_to_list_has_required_keys(self):
with self.assertRaises(Exception):
main.to_list({})

0 comments on commit 68bfc9e

Please sign in to comment.