-
Notifications
You must be signed in to change notification settings - Fork 0
/
mooncloud_utils.py
executable file
·114 lines (104 loc) · 5.09 KB
/
mooncloud_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python
import math
import sys
import time
import requests
import argparse
class MoonCloudUtils:
mooncloud_api_login_endpoint="https://api.v2.moon-cloud.eu/user/login-token/"
mooncloud_api_evaluation_start_endpoint="https://api.v2.moon-cloud.eu/evaluation-rules/{uer_id}/start/"
mooncloud_api_evaluation_endpoint="https://api.v2.moon-cloud.eu/evaluation-rules/{uer_id}/"
max_attempt_number=10
@staticmethod
def getToken(user,password):
response=requests.post(url=MoonCloudUtils.mooncloud_api_login_endpoint,headers={"Content-Type":"application/json"},json={
"username":user,
"password":password
})
response.raise_for_status()
return response.json()["token"]
@staticmethod
def startEvaluation(token,uer_id,wait_result) -> bool:
execution_result=None
initial_result_number=None
if wait_result:
initial_result_number=MoonCloudUtils.getEvaluationResult(token=token,uer_id=uer_id)[1]
response=requests.put(url=MoonCloudUtils.mooncloud_api_evaluation_start_endpoint.format(uer_id=uer_id),headers={
"Content-Type":"application/json",
"Authorization":"Token "+token})
response.raise_for_status()
print("Evaluation started correctly")
start_evaluation_response_obj=response.json()
if wait_result:
new_result_flag=False
tmp_result=None
t_counter=0
while not new_result_flag and t_counter<MoonCloudUtils.max_attempt_number:
print("Waiting for evaluation completion...")
time.sleep(10)
tmp_result,tmp_result_number=MoonCloudUtils.getEvaluationResult(token=token,uer_id=uer_id)
if tmp_result_number==(initial_result_number+1):
new_result_flag=True
else:
t_counter=t_counter+1
if t_counter>=MoonCloudUtils.max_attempt_number:
raise TimeoutError("Max attempt number exceeded")
execution_result={
"evaluation_started":True if start_evaluation_response_obj["result"]=="started" else False,
"evaluation_result":True if tmp_result["result"]==True else False
}
else:
execution_result={
"evaluation_started":True if start_evaluation_response_obj["result"]=="started" else False
}
return execution_result
@staticmethod
def getEvaluationResult(token,uer_id):
response=requests.get(url=MoonCloudUtils.mooncloud_api_evaluation_endpoint.format(uer_id=uer_id),headers={
"Content-Type":"application/json",
"Authorization":"Token "+token})
response.raise_for_status()
response_obj=response.json()
tests_ex_res_len_sum=0
tests_ex_res_len_avg=0
for test in response_obj["tests"]:
tests_ex_res_len_sum=tests_ex_res_len_sum+len(test["execution_results"])
tests_ex_res_len_avg=math.floor(tests_ex_res_len_sum/len(response_obj["tests"]))
return response_obj,tests_ex_res_len_avg
def exitWithError(error_str):
print("Error occured: "+error_str, file=sys.stderr)
exit(1)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Get token by providing username and password")
subparsers = parser.add_subparsers(title="commands", dest="command")
# get-token option
get_token_parser = subparsers.add_parser("get-token", help="Get token")
get_token_parser.add_argument("--username", required=True, help="Username")
get_token_parser.add_argument("--password", required=True, help="Password")
# start-evaluation option
start_evaluation_parser = subparsers.add_parser("start-evaluation", help="Get token")
start_evaluation_parser.add_argument("--token", required=True, help="Token")
start_evaluation_parser.add_argument("--uer-id", required=True, help="UER ID")
start_evaluation_parser.add_argument("--wait-result", required=False, action="store_true", help="Wait for evaluation result")
args = parser.parse_args()
if args.command == "get-token":
try:
print(MoonCloudUtils.getToken(user=args.username,password=args.password))
except Exception as e:
exitWithError(str(e))
elif args.command == "start-evaluation":
if not args.wait_result:
try:
response=MoonCloudUtils.startEvaluation(token=args.token,uer_id=args.uer_id,wait_result=False)
assert response["evaluation_started"], "An error occured during evaluation starting"
print(response)
except Exception as e:
exitWithError(str(e))
else:
try:
response=MoonCloudUtils.startEvaluation(token=sys.argv[3],uer_id=sys.argv[5],wait_result=True)
assert response["evaluation_started"], "An error occured during evaluation starting"
assert response["evaluation_result"], "Evaluation failed"
print(response)
except Exception as e:
exitWithError(str(e))