diff --git a/roles/host.py b/roles/host.py index 8087dabc94d4a4e1d499bc0486a0d425cadb3c6c..f0e6b39855d80c2ff96f3ad832d38c465dca94cd 100644 --- a/roles/host.py +++ b/roles/host.py @@ -6,6 +6,7 @@ import time import requests from utils import Problem +import utils.adcclilib as adccli class Host(object): @@ -149,6 +150,48 @@ class Host(object): problem_key = params['problem'] solution_id = params['solution'] return self.get_solution_for_viewer(problem_key, solution_id) + elif cmd == 'adccli/login': + with open('path-to-adccli-login', 'r') as fp: + d = json.load(fp) + r = adccli.login(d['url'], d['username'], d['password']) + res = {'message': r} + return json.dumps(res) + elif cmd == 'adccli/logout': + r = adccli.logout() + res = {'message': r} + return json.dumps(res) + elif cmd == 'adccli/whoami': + with open('path-to-adccli-login', 'r') as fp: + d = json.load(fp) + r = adccli.whoami() + res = {'message': r, 'status': r == d['username']} + return json.dumps(res) + elif cmd == 'adccli/download-problem': + out_abspath = os.path.abspath(self.config['problem_path']) + r = adccli.get_q_all(out_abspath) + self._load_problems(self.config['problem_path']) + res = {'message': r} + return json.dumps(res) + elif cmd == 'adccli/upload-solution': + qname = params['problem'] + if not "best_json" in questions[qname]: + res = {'message': "Required to 'save' before submission"} + else: + out_path = "{}/{}".format(self.config['solution_path'], "submit") + # qnumber = qname.replace("NL_Q", "").replace(".txt", "") + # ans_file_path = "{}/T01_A{}.txt".format(out_path, qnumber) + # ans_file_abspath = os.path.abspath(ans_file_path) + + # r = adccli.put_a(qnumber, ans_file_abspath) + # mes = r + "\n" + + # json_name = questions[qname]['best_json'] + # data = questions[qname]['answers'][json_name] + # r = adccli.put_a_info(qnumber, data['cputime'], "0", "Test") + # mes += r + mes = '' + res = {'message': mes} + return json.dumps(res) else: return None diff --git a/utils/adcclilib.py b/utils/adcclilib.py new file mode 100644 index 0000000000000000000000000000000000000000..fb03db34b7bf8148f0a26f4a642c203c20353d31 --- /dev/null +++ b/utils/adcclilib.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python3 + +import subprocess + +PYTHON2 = "/usr/bin/python" +ADCCLI = "/home/pi/adc2018/conmgr/client/adccli" + +def _exec_adccli(cmd): + exec_cmd = "{} {} {}".format(PYTHON2, ADCCLI, cmd).strip() + print("ADCCLI: {}".format(exec_cmd)) + p = subprocess.run(exec_cmd, stdout=subprocess.PIPE, shell=True) + res = p.stdout.decode().strip() + print(res) + return res + +def login(url, username, password): + cmd = "--URL='{}' --username='{}' --password='{}' login".format(url, username, password) + return _exec_adccli(cmd) + +def logout(): + cmd = "logout" + return _exec_adccli(cmd) + +def whoami(): + cmd = "whoami" + return _exec_adccli(cmd) + +def put_message(message): + cmd = "put-user-alive '{}'" + return _exec_adccli(cmd) + +def post_user_q(qnum, filepath): + cmd = "post-user-q {} {}".format(qnum, filepath) + return _exec_adccli(cmd) + +def get_q_all(outpath): + cmd = "get-q" + question_list = _exec_adccli(cmd).split("\n") + + print("--- ADCCLI questions ---") + print(question_list) + + for v in question_list: + if v.startswith("Q"): + qnumber = int(v.replace("Q", "")) + out_file_path = "{}/NL_Q{:02d}.txt".format(outpath, qnumber) + cmd = "--output {} get-q {}".format(out_file_path, qnumber) + r = _exec_adccli(cmd) + + return question_list + +def put_a(qnum, filepath): + cmd = "put-a {} {}".format(qnum, filepath) + return _exec_adccli(cmd) + +def put_a_info(qnum, cpu, mem, misc): + cmd = "put-a-info {} {} {} '{}'".format(qnum, cpu, mem, misc) + return _exec_adccli(cmd) +