Repository navigation
Expand file tree
/
Copy pathmigrate.py
More file actions
168 lines (135 loc) · 4.9 KB
/
Copy pathmigrate.py
File metadata and controls
168 lines (135 loc) · 4.9 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import glob
import os
import pathlib
import time
import requests
import yaml
from dotenv import load_dotenv
load_dotenv(verbose=True)
dotenv_path = pathlib.Path(__file__).parent.parent / ".env"
load_dotenv(dotenv_path)
ADMIN_USERNAME = os.getenv("ADMIN_USERNAME")
ADMIN_PASSWORD = os.getenv("ADMIN_PASSWORD")
WAIT = 0.1
host = os.getenv("AIBLECODE_API_HOST")
curr = (pathlib.Path(__file__).parent / "problem").absolute()
res = requests.post(
f"{host}/token",
data={
"grant_type": "password",
"username": ADMIN_USERNAME,
"password": ADMIN_PASSWORD,
"scope": "",
},
)
print(res)
token = res.cookies
time.sleep(WAIT)
for category in glob.glob(f"{curr}/*"):
if not os.path.isdir(category):
continue
with open(f"{category}/metadata.yml") as f:
category_meta = yaml.load(f, Loader=yaml.SafeLoader)
category_path = category.replace(f"{curr}/", "")
title = category_meta["category"]["title"]
description = category_meta["category"]["description"]
res = requests.post(
f"{host}/create_category",
json={
"path_id": category_path,
"title": title,
"description": description,
},
cookies=token,
)
time.sleep(WAIT)
if res.status_code != 200:
print(f"Failed to create category: {category_path}")
print(res.text)
exit(1)
print("Success to create category:", category_path)
for problem in glob.glob(f"{category}/*"):
if not os.path.isdir(problem):
continue
with open(f"{problem}/metadata.yml") as f:
problem_meta = yaml.load(f, Loader=yaml.SafeLoader)
problem_path = problem.replace(f"{category}/", "")
ignore = problem_meta["problem"].get("ignore", False)
title = problem_meta["problem"]["title"]
statement = open(f"{problem}/statement.md").read()
level = problem_meta["problem"]["level"]
time_limit = problem_meta["problem"]["time_limit"]
memory_limit = problem_meta["problem"]["memory_limit"]
res = requests.post(
f"{host}/create_problem",
json={
"path_id": problem_path,
"title": title,
"statement": statement,
"category_path_id": category_path,
"level": level,
"time_limit": time_limit,
"memory_limit": memory_limit,
},
cookies=token,
)
time.sleep(WAIT)
if res.status_code != 200:
print(f"Failed to create problem: {problem_path}")
print(res.text)
exit(1)
print("Success to create problem:", problem_path)
judge = problem_meta["problem"].get("judge", None)
if judge:
res = requests.post(
f"{host}/set_judge",
json={
"category_path_id": category_path,
"problem_path_id": problem_path,
"judge_type": judge,
"code": open(f"{problem}/codes/judge.py").read(),
},
cookies=token,
)
time.sleep(WAIT)
if res.status_code != 200:
print(f"Failed to create judge: {problem_path}")
print(res.text)
exit(1)
print("Success to create judge:", problem_path)
input_files = sorted(glob.glob(f"{problem}/testcases/in/*"))
output_files = sorted(glob.glob(f"{problem}/testcases/out/*"))
input_set = set(
[f.replace(f"{problem}/testcases/in/", "") for f in input_files]
)
output_set = set(
[f.replace(f"{problem}/testcases/out/", "") for f in output_files]
)
if input_set != output_set:
print(f"Input and output files do not match for {problem_path}")
print("Extra input files:", *(input_set - output_set))
print("Extra output files:", *(output_set - input_set))
exit(1)
for path in sorted(input_set & output_set):
input_file = f"{problem}/testcases/in/{path}"
output_file = f"{problem}/testcases/out/{path}"
res = requests.post(
f"{host}/create_testcase",
json={
"category_path_id": category_path,
"problem_path_id": problem_path,
"name": path,
"input": open(input_file).read(),
"output": open(output_file).read(),
},
cookies=token,
)
time.sleep(WAIT)
print(res)
if res.status_code != 200:
print(f"Failed to create testcase: {path}")
print(res.text)
exit(1)
print("Success to create testcase:", path)
requests.post(f"{host}/logout", cookies=token)
print("Migration completed")