From 5792ebcdd354eb858eb696323b7840c10c4fbfb1 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Thu, 27 Sep 2018 11:35:41 +0200 Subject: Improve reporting in test scripts --- kpov_judge/test_prepare_disks.py | 2 +- kpov_judge/test_task.py | 57 ++++++++++++++++++++-------------------- 2 files changed, 30 insertions(+), 29 deletions(-) diff --git a/kpov_judge/test_prepare_disks.py b/kpov_judge/test_prepare_disks.py index bc4bae5..8d0d8c1 100755 --- a/kpov_judge/test_prepare_disks.py +++ b/kpov_judge/test_prepare_disks.py @@ -144,7 +144,7 @@ class SSHGuestFs: if __name__ == '__main__': if len(sys.argv) != 1: - print("Usage: {0}") + print("Usage: " + sys.argv[0]) print("Run prepare_disks on running computers over ssh") print("The task name and params are read from ~/.kpov_params.yaml") yaml_config_file = os.path.expanduser("~/.kpov_params.yaml") diff --git a/kpov_judge/test_task.py b/kpov_judge/test_task.py index d2a003c..d79c0e5 100755 --- a/kpov_judge/test_task.py +++ b/kpov_judge/test_task.py @@ -52,7 +52,6 @@ def get_params_dialog(params, meta, param_name_list=None, dialog=None, language else: init = params[name] ret, s = dialog.inputbox(description, init=init) - # print ret, type(ret) if ret == 'ok': if m.get('w', True): params[name] = s @@ -69,22 +68,15 @@ def add_meta_to_argparser(argparser, meta, defaults = {}): desc = v['descriptions'][language].encode("utf-8") except: desc = k - # print("{} {}".format(desc, type(desc))) argparser.add_argument('--'+k, nargs='?', help=desc, type=str, default=defaults.get(k, None)) return argparser def http_auth(url, username, password): password_mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm() -# Add the username and password. -# If we knew the realm, we could use it instead of None. password_mgr.add_password(None, url, username, password) handler = urllib.request.HTTPBasicAuthHandler(password_mgr) -# create "opener" (OpenerDirector instance) opener = urllib.request.build_opener(handler) -# use the opener to fetch a URL -# opener.open(SERVER_URL) -# Install the opener. -# Now all calls to urllib2.urlopen use our opener. + # Install the opener. Now all calls to urllib2.urlopen use our opener. urllib.request.install_opener(opener) def load_task(stream): @@ -101,7 +93,7 @@ def load_task(stream): def locate_task(params, argparser, dialog): # first the URL where all tasks are stored url_meta = { - 'task_url': {'descriptions': {'si': 'URL z nalogami', 'en': 'Root URL for all tasks'}, } + 'task_url': {'descriptions': {'si': 'URL z nalogami', 'en': 'Root URL for all tasks'}} } if 'task_url' not in params: params['task_url'] = TASK_URL @@ -112,7 +104,7 @@ def locate_task(params, argparser, dialog): params = get_params_dialog(params, url_meta, dialog = dialog) # then the student's ID (and password if neccessarry) fetch_params_meta = { - 'username':{'descriptions': {'si': 'Uporabniško ime', 'en': 'Username'}, }, + 'username': {'descriptions': {'si': 'Uporabniško ime', 'en': 'Username'}}, } if params['task_url'] is not None and ( params['task_url'].startswith('http')): @@ -148,6 +140,7 @@ if __name__ == '__main__': basic_argparser.add_argument('-pf','--params_file', nargs='?', help='a local file containing saved param values', default=PARAMS_FILE) basic_args, unknown_args = basic_argparser.parse_known_args() + # get default parameters including language params = load_params(basic_args.params_file) basic_argparser.add_argument('-l', '--language', nargs='?', @@ -159,6 +152,7 @@ if __name__ == '__main__': dialog = Dialog(dialog="dialog") else: dialog = None + # continue with the parameters needed to get the task argparser, params = locate_task(params, basic_argparser, dialog=dialog) # TODO: if the task name is missing or invalid, try to get a list of tasks @@ -168,13 +162,18 @@ if __name__ == '__main__': task_name = params['task_name'] if task_url.startswith('http'): http_auth(task_url, params['username'], params['password']) - print("fetching {task_url}/{task_name}/task.py".format(**params)) - req = urllib.request.Request("{task_url}/{task_name}/task.py".format(**params)) - source = urllib.request.urlopen(req) + print() + print("Fetching {task_url}/{task_name}/task.py…".format(**params)) + source = urllib.request.urlopen("{task_url}/{task_name}/task.py".format(**params)) task, task_check, task_params_meta, gen_params = load_task(source) except Exception as e: - print(params) - print(e) + import traceback + traceback.print_exc() + print() + for k, v in params.items(): + if k != 'password': + print('{}: {}'.format(k, v)) + print() if basic_args.help: argparser.print_help() else: @@ -182,6 +181,7 @@ if __name__ == '__main__': with open(basic_args.params_file, 'w') as f: yaml.dump(params, f) exit(1) + # get task parameters params['task_params'] = params.get('task_params', dict()) params['task_params'][params['task_name']] = params['task_params'].get(params['task_name'], dict()) @@ -203,11 +203,11 @@ if __name__ == '__main__': task_params[k] = args[k] if dialog is not None: task_params = get_params_dialog(task_params, task_params_meta, dialog=dialog, language = params['language']) - # print(task_params) if basic_args.help: # params_argparser.print_help() print(params_argparser.format_help()) exit(0) + # run task.task() public_params = dict() for k in inspect.getargs(task.__code__)[0]: @@ -217,17 +217,18 @@ if __name__ == '__main__': with open(basic_args.params_file, 'w') as f: # print "dumping", params yaml.dump(params, f) + try: + print() + print('Running task… ', end='') task_result = task(**public_params) - # run task.task_check() + print('Checking task… ', end='') + score, hints = task_check(task_result, task_params) + print('Done!') + print('Score: {}'.format(score)) + print('Hints:') + for hint in hints: + print(hint) except Exception as e: - print("Error running task(...):") - print(e) - exit(1) - try: - result, hints = task_check(task_result, task_params) - print(result, hints) - except Exception as e: - print("Error running task_check(results, params):") - print(e) - + import traceback + traceback.print_exc() -- cgit v1.2.1