-
Notifications
You must be signed in to change notification settings - Fork 5
/
uploadproblems.py
executable file
·314 lines (258 loc) · 10.5 KB
/
uploadproblems.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/usr/bin/python3
import argparse
import datetime
import json
import logging
import os
import subprocess
import tempfile
import zipfile
from typing import Any, Mapping, Set
import omegaup.api
import problems
import repository
def createProblemZip(problemConfig: Mapping[str, Any], problemPath: str,
zipPath: str) -> None:
"""Creates a problem .zip on the provided path."""
with zipfile.ZipFile(zipPath, 'w',
compression=zipfile.ZIP_DEFLATED) as archive:
def _addFile(f: str) -> None:
logging.debug('writing %s', f)
archive.write(f, os.path.relpath(f, problemPath))
def _recursiveAdd(directory: str) -> None:
for (root, _,
filenames) in os.walk(os.path.join(problemPath, directory)):
for f in filenames:
_addFile(os.path.join(root, f))
testplan = os.path.join(problemPath, 'testplan')
if os.path.isfile(testplan):
_addFile(testplan)
if problemConfig['validator']['name'] == 'custom':
validators = [
x for x in os.listdir(problemPath) if x.startswith('validator')
]
if not validators:
raise Exception('Custom validator missing!')
if len(validators) != 1:
raise Exception('More than one validator found!')
validator = os.path.join(problemPath, validators[0])
_addFile(validator)
for directory in ('statements', 'solutions', 'cases'):
_recursiveAdd(directory)
for directory in ('examples', 'interactive'):
if not os.path.isdir(os.path.join(problemPath, directory)):
continue
_recursiveAdd(directory)
def uploadProblemZip(
client: omegaup.api.Client,
problemConfig: Mapping[str, Any],
canCreate: bool,
zipPath: str,
commitMessage: str,
timeout: datetime.timedelta,
) -> None:
"""Uploads a problem with the given .zip and configuration."""
misc = problemConfig['misc']
alias = misc['alias']
limits = problemConfig['limits']
validator = problemConfig['validator']
payload = {
'message': commitMessage,
'problem_alias': alias,
'title': problemConfig['title'],
'source': problemConfig['source'],
'visibility': misc['visibility'],
'languages': misc['languages'],
'time_limit': limits['TimeLimit'],
'memory_limit': limits['MemoryLimit'] // 1024,
'input_limit': limits['InputLimit'],
'output_limit': limits['OutputLimit'],
'extra_wall_time': limits['ExtraWallTime'],
'overall_wall_time_limit': limits['OverallWallTimeLimit'],
'validator': validator['name'],
'validator_time_limit': validator['limits']['TimeLimit'],
'email_clarifications': misc['email_clarifications'],
'group_score_policy': misc.get('group_score_policy',
'sum-if-not-zero'),
}
exists = client.problem.details(problem_alias=alias,
check_=False)['status'] == 'ok'
if not exists:
if not canCreate:
raise Exception("Problem doesn't exist!")
logging.info("Problem doesn't exist. Creating problem.")
endpoint = '/api/problem/create/'
else:
endpoint = '/api/problem/update/'
languages = payload.get('languages', '')
if languages == 'all':
payload['languages'] = ','.join((
'c11-clang',
'c11-gcc',
'cpp11-clang',
'cpp11-gcc',
'cpp17-clang',
'cpp17-gcc',
'cpp20-clang',
'cpp20-gcc',
'cs',
'go',
'hs',
'java',
'js',
'kt',
'lua',
'pas',
'py2',
'py3',
'rb',
'rs',
))
elif languages == 'karel':
payload['languages'] = 'kj,kp'
elif languages == 'none':
payload['languages'] = ''
files = {'problem_contents': open(zipPath, 'rb')}
client.query(endpoint, payload, files, timeout)
targetAdmins = misc.get('admins', [])
targetAdminGroups = misc.get('admin-groups', [])
if targetAdmins or targetAdminGroups:
allAdmins = client.problem.admins(problem_alias=alias)
if targetAdmins is not None:
admins = {
a['username'].lower()
for a in allAdmins['admins'] if a['role'] == 'admin'
}
desiredAdmins = {admin.lower() for admin in targetAdmins}
clientAdmin: Set[str] = set()
if client.username:
clientAdmin.add(client.username.lower())
adminsToRemove = admins - desiredAdmins - clientAdmin
adminsToAdd = desiredAdmins - admins - clientAdmin
for admin in adminsToAdd:
logging.info('Adding problem admin: %s', admin)
client.problem.addAdmin(problem_alias=alias, usernameOrEmail=admin)
for admin in adminsToRemove:
logging.info('Removing problem admin: %s', admin)
client.problem.removeAdmin(problem_alias=alias,
usernameOrEmail=admin)
if targetAdminGroups is not None:
adminGroups = {
a['alias'].lower()
for a in allAdmins['group_admins'] if a['role'] == 'admin'
}
desiredGroups = {group.lower() for group in targetAdminGroups}
groupsToRemove = adminGroups - desiredGroups
groupsToAdd = desiredGroups - adminGroups
for group in groupsToAdd:
logging.info('Adding problem admin group: %s', group)
client.problem.addGroupAdmin(problem_alias=alias, group=group)
for group in groupsToRemove:
logging.info('Removing problem admin group: %s', group)
client.problem.removeGroupAdmin(problem_alias=alias, group=group)
if 'tags' in misc:
tags = {
t['name'].lower()
for t in client.problem.tags(problem_alias=alias)['tags']
}
desiredTags = {t.lower() for t in misc['tags']}
tagsToRemove = tags - desiredTags
tagsToAdd = desiredTags - tags
for tag in tagsToRemove:
if tag.startsWith('problemRestrictedTag'):
logging.info('Skipping restricted tag: %s', tag)
continue
client.problem.removeTag(problem_alias=alias, name=tag)
for tag in tagsToAdd:
logging.info('Adding problem tag: %s', tag)
client.problem.addTag(problem_alias=alias,
name=tag,
public=payload.get('public', False))
def uploadProblem(
client: omegaup.api.Client,
problemPath: str,
commitMessage: str,
canCreate: bool,
timeout: datetime.timedelta,
) -> None:
with open(os.path.join(problemPath, 'settings.json'), 'r') as f:
problemConfig = json.load(f)
logging.info('Uploading problem: %s', problemConfig['title'])
with tempfile.NamedTemporaryFile() as tempFile:
createProblemZip(problemConfig, problemPath, tempFile.name)
uploadProblemZip(client,
problemConfig,
canCreate,
tempFile.name,
commitMessage=commitMessage,
timeout=timeout)
logging.info('Success uploading %s', problemConfig['title'])
def _main() -> None:
env = os.environ
parser = argparse.ArgumentParser(
description='Deploy a problem to omegaUp.')
parser.add_argument('--ci',
action='store_true',
help='Signal that this is being run from the CI.')
parser.add_argument(
'--all',
action='store_true',
help='Consider all problems, instead of only those that have changed')
parser.add_argument('--verbose',
action='store_true',
help='Verbose logging')
parser.add_argument('--url',
default='https://omegaup.com',
help='URL of the omegaUp host.')
parser.add_argument('--api-token',
type=str,
default=env.get('OMEGAUP_API_TOKEN'))
parser.add_argument('-u',
'--username',
type=str,
default=env.get('OMEGAUPUSER'),
required=('OMEGAUPUSER' not in env
and 'OMEGAUP_API_TOKEN' not in env))
parser.add_argument('-p',
'--password',
type=str,
default=env.get('OMEGAUPPASS'),
required=('OMEGAUPPASS' not in env
and 'OMEGAUP_API_TOKEN' not in env))
parser.add_argument('--can-create',
action='store_true',
help=("Whether it's allowable to create the "
"problem if it does not exist."))
parser.add_argument("--timeout",
type=int,
default=60,
help="Timeout for deploy API call (in seconds)")
parser.add_argument('problem_paths',
metavar='PROBLEM',
type=str,
nargs='*')
args = parser.parse_args()
logging.basicConfig(format='%(asctime)s: %(message)s',
level=logging.DEBUG if args.verbose else logging.INFO)
logging.getLogger('urllib3').setLevel(logging.CRITICAL)
client = omegaup.api.Client(username=args.username,
password=args.password,
api_token=args.api_token,
url=args.url)
if env.get('GITHUB_ACTIONS'):
commit = env['GITHUB_SHA']
else:
commit = subprocess.check_output(['git', 'rev-parse', 'HEAD'],
universal_newlines=True).strip()
rootDirectory = repository.repositoryRoot()
for problem in problems.problems(allProblems=args.all,
rootDirectory=rootDirectory,
problemPaths=args.problem_paths):
uploadProblem(
client,
os.path.join(rootDirectory, problem.path),
commitMessage=f'Deployed automatically from commit {commit}',
canCreate=args.can_create,
timeout=datetime.timedelta(seconds=args.timeout))
if __name__ == '__main__':
_main()