-
Notifications
You must be signed in to change notification settings - Fork 35
/
submit.py
executable file
·513 lines (432 loc) · 15 KB
/
submit.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
#!/usr/bin/env python
import argparse
import configparser
import os
import re
import requests
import sys
import time
from lxml.html import fragment_fromstring
_DEFAULT_CONFIG = '/usr/local/etc/kattisrc'
_LANGUAGE_GUESS = {
'.4th': 'Forth',
'.c': 'C',
'.c++': 'C++',
'.cc': 'C++',
'.c#': 'C#',
'.cpp': 'C++',
'.cs': 'C#',
'.cxx': 'C++',
'.cbl': 'COBOL',
'.cob': 'COBOL',
'.cpy': 'COBOL',
'.forth': 'Forth',
'.frt': 'Forth',
'.fs': 'F#',
'.fth': 'Forth',
'.go': 'Go',
'.hs': 'Haskell',
'.java': 'Java',
'.js': 'JavaScript (Node.js)',
'.ts': 'TypeScript',
'.kt': 'Kotlin',
'.lisp': 'Common Lisp',
'.cl': 'Common Lisp',
'.m': 'Objective-C',
'.ml': 'OCaml',
'.pas': 'Pascal',
'.php': 'PHP',
'.pl': 'Prolog',
'.py': 'Python 3',
'.pyc': 'Python 3',
'.rb': 'Ruby',
'.rkt': 'Racket',
'.rs': 'Rust',
'.scala': 'Scala',
'.f90': 'Fortran',
'.f': 'Fortran',
'.for': 'Fortran',
'.sh': 'Bash',
'.apl': 'APL',
'.ss': 'Gerbil',
'.jl': 'Julia',
'.vb': 'Visual Basic',
'.dart': 'Dart',
'.zig': 'Zig',
'.swift': 'Swift',
'.nim': 'Nim',
'.lua': 'Lua',
'.pm': 'Perl',
'.sno': 'SNOBOL',
'.odin': 'Odin',
'.a68': 'Algol 68',
'.cr': 'Crystal',
'.sim': 'Simula 67',
'.d': 'D',
'.mod': 'Modula-2',
'.st': 'Smalltalk',
'.adb': 'Ada',
'.ads': 'Ada',
'.erl': 'Erlang',
'.ex': 'Elixir',
}
_GUESS_MAINCLASS = { 'Elixir', 'Erlang', 'Java', 'Kotlin', 'Modula-2', 'Scala' }
_GUESS_MAINFILE = {
'Ada', 'Algol 68', 'APL', 'Bash', 'Crystal', 'Dart', 'Forth', 'Gerbil', 'JavaScript (Node.js)',
'JavaScript (SpiderMonkey)', 'Julia', 'Common Lisp', 'Lua', 'Nim', 'Octave', 'Pascal', 'Perl', 'PHP',
'Python 2', 'Python 3', 'Racket', 'Ruby', 'Rust', 'Simula', 'Smalltalk', 'SNOBOL', 'TypeScript', 'Zig',
}
_HEADERS = { 'User-Agent': 'kattis-cli-submit' }
_RUNNING_STATUS = 5
_COMPILE_ERROR_STATUS = 8
_ACCEPTED_STATUS = 16
_STATUS_MAP = {
0: 'New', # <invalid value>
1: 'New',
2: 'Waiting for compile',
3: 'Compiling',
4: 'Waiting for run',
_RUNNING_STATUS: 'Running',
6: 'Judge Error',
# 7: '<invalid value>',
_COMPILE_ERROR_STATUS: 'Compile Error',
9: 'Run Time Error',
10: 'Memory Limit Exceeded',
11: 'Output Limit Exceeded',
12: 'Time Limit Exceeded',
13: 'Illegal Function',
14: 'Wrong Answer',
# 15: '<invalid value>',
_ACCEPTED_STATUS: 'Accepted',
}
class ConfigError(Exception):
pass
def get_url(cfg, option, default):
if cfg.has_option('kattis', option):
return cfg.get('kattis', option)
else:
hostname = cfg.get('kattis', 'hostname')
return f'https://{hostname}/{default}'
def get_config():
"""Returns a ConfigParser object for the .kattisrc file(s)
"""
cfg = configparser.ConfigParser()
if os.path.exists(_DEFAULT_CONFIG):
cfg.read(_DEFAULT_CONFIG)
try:
file = __file__
except NameError:
file = sys.argv[0]
if not cfg.read([os.path.join(os.path.expanduser("~"), '.kattisrc'),
os.path.join(os.path.dirname(file), '.kattisrc'),
os.path.join(os.path.dirname(os.path.realpath(file)), '.kattisrc')]):
raise ConfigError('''\
I failed to read in a config file from your home directory or from the
same directory as this script. To download a .kattisrc file please visit
https://<kattis>/download/kattisrc
The file should look something like this:
[user]
username: yourusername
token: *********
[kattis]
hostname: <kattis>
loginurl: https://<kattis>/login
submissionurl: https://<kattis>/submit
submissionsurl: https://<kattis>/submissions''')
return cfg
def is_python2(files):
python2 = re.compile(r'^\s*\bprint\b *[^ \(\),\]]|\braw_input\b')
for filename in files:
try:
with open(filename) as f:
for index, line in enumerate(f):
if index == 0 and line.startswith('#!'):
if 'python2' in line:
return True
if 'python3' in line:
return False
if python2.search(line.split('#')[0]):
return True
except IOError:
return False
return False
def guess_language(ext, files):
if ext == ".C":
return "C++"
ext = ext.lower()
if ext == ".h":
if any(f.endswith(".c") for f in files):
return "C"
else:
return "C++"
if ext == ".py":
if is_python2(files):
return "Python 2"
else:
return "Python 3"
return _LANGUAGE_GUESS.get(ext, None)
def guess_mainfile(language, files):
for filename in files:
if os.path.splitext(os.path.basename(filename))[0] in ['main', 'Main']:
return filename
for filename in files:
try:
with open(filename) as f:
conts = f.read()
if language in ['Java', 'Rust', 'Scala', 'Kotlin'] and re.search(r' main\s*\(', conts):
return filename
if language == 'Pascal' and re.match(r'^\s*[Pp]rogram\b', conts):
return filename
except IOError:
pass
return files[0]
def guess_mainclass(language, files):
if language in _GUESS_MAINFILE and len(files) > 1:
return os.path.basename(guess_mainfile(language, files))
if language in _GUESS_MAINCLASS:
mainfile = os.path.basename(guess_mainfile(language, files))
name = os.path.splitext(mainfile)[0]
if language == 'Kotlin':
return name[0].upper() + name[1:] + 'Kt'
return name
return None
def login(login_url, username, password=None, token=None):
"""Log in to Kattis.
At least one of password or token needs to be provided.
Returns a requests.Response with cookies needed to be able to submit
"""
login_args = {'user': username, 'script': 'true'}
if password:
login_args['password'] = password
if token:
login_args['token'] = token
return requests.post(login_url, data=login_args, headers=_HEADERS)
def login_from_config(cfg):
"""Log in to Kattis using the access information in a kattisrc file
Returns a requests.Response with cookies needed to be able to submit
"""
username = cfg.get('user', 'username')
password = token = None
try:
password = cfg.get('user', 'password')
except configparser.NoOptionError:
pass
try:
token = cfg.get('user', 'token')
except configparser.NoOptionError:
pass
if password is None and token is None:
raise ConfigError('''\
Your .kattisrc file appears corrupted. It must provide a token (or a
KATTIS password).
Please download a new .kattisrc file''')
loginurl = get_url(cfg, 'loginurl', 'login')
return login(loginurl, username, password, token)
def submit(submit_url, cookies, problem, language, files, mainclass='', tag='', assignment=None, contest=None):
"""Make a submission.
The url_opener argument is an OpenerDirector object to use (as
returned by the login() function)
Returns the requests.Result from the submission
"""
data = {'submit': 'true',
'submit_ctr': 2,
'language': language,
'mainclass': mainclass,
'problem': problem,
'tag': tag,
'script': 'true'}
if assignment is not None:
data['assignment'] = assignment
if contest is not None:
data['contest'] = contest
sub_files = []
for f in files:
with open(f, 'rb') as sub_file:
sub_files.append(('sub_file[]',
(os.path.basename(f),
sub_file.read(),
'application/octet-stream')))
return requests.post(submit_url, data=data, files=sub_files, cookies=cookies, headers=_HEADERS)
def confirm_or_die(problem, language, files, mainclass, tag):
print('Problem:', problem)
print('Language:', language)
print('Files:', ', '.join(files))
if mainclass:
if language in _GUESS_MAINFILE:
print('Main file:', mainclass)
else:
print('Mainclass:', mainclass)
if tag:
print('Tag:', tag)
print('Submit (y/N)?')
if sys.stdin.readline().upper()[:-1] != 'Y':
print('Cancelling')
sys.exit(1)
def get_submission_url(submit_response, cfg):
m = re.search(r'Submission ID: (\d+)', submit_response)
if m:
submissions_url = get_url(cfg, 'submissionsurl', 'submissions')
submission_id = m.group(1)
return f'{submissions_url}/{submission_id}'
def get_submission_status(submission_url, cookies):
reply = requests.get(submission_url + '?json', cookies=cookies, headers=_HEADERS)
return reply.json()
_RED_COLOR = 31
_GREEN_COLOR = 32
def color(s, c):
return f'\x1b[{c}m{s}\x1b[0m'
def show_judgement(submission_url, cfg):
print()
login_reply = login_from_config(cfg)
while True:
status = get_submission_status(submission_url, login_reply.cookies)
status_id = status['status_id']
testcases_done = status['testcase_index']
testcases_total = status['row_html'].count('<i') - 1
status_text = _STATUS_MAP.get(status_id, f'Unknown status {status_id}')
if status_id < _RUNNING_STATUS:
print(f'\r{status_text}...', end='')
else:
print('\rTest cases: ', end='')
if status_id == _COMPILE_ERROR_STATUS:
print(f'\r{color(status_text, _RED_COLOR)}', end='')
try:
root = fragment_fromstring(status['feedback_html'], create_parent=True)
error = root.find('.//pre').text
print(color(':', _RED_COLOR))
print(error, end='')
except:
pass
elif status_id < _RUNNING_STATUS:
print(f'\r{status_text}...', end='')
else:
print('\rTest cases: ', end='')
if testcases_total == 0:
print('???', end='')
else:
s = '.' * (testcases_done - 1)
if status_id == _RUNNING_STATUS:
s += '?'
elif status_id == _ACCEPTED_STATUS:
s += '.'
else:
s += 'x'
print(f'[{s: <{testcases_total}}] {testcases_done} / {testcases_total}', end='')
sys.stdout.flush()
if status_id > _RUNNING_STATUS:
# Done
print()
success = status_id == _ACCEPTED_STATUS
try:
root = fragment_fromstring(status['row_html'], create_parent=True)
cpu_time = root.find('.//*[@data-type="cpu"]').text
status_text += " (" + cpu_time + ")"
except:
pass
if status_id != _COMPILE_ERROR_STATUS:
print(color(status_text, _GREEN_COLOR if success else _RED_COLOR))
return success
time.sleep(0.25)
def main():
parser = argparse.ArgumentParser(prog='kattis', description='Submit a solution to Kattis')
group = parser.add_mutually_exclusive_group()
group.add_argument('-a', '--assignment',
help='''Short name of assignment you want to submit to
Overrides default guess (server guesses based on assignments you are in)''')
group.add_argument('-c', '--contest',
help='''Short name of contest you want to submit to
Overrides default guess (server guesses based on contests you are in)''')
parser.add_argument('-p', '--problem',
help=''''Which problem to submit to.
Overrides default guess (first part of first filename)''')
parser.add_argument('-m', '--mainclass',
help='''Sets mainclass.
Overrides default guess (first part of first filename)''')
parser.add_argument('-l', '--language',
help='''Sets language.
Overrides default guess (based on suffix of first filename)''')
parser.add_argument('-t', '--tag',
help=argparse.SUPPRESS)
parser.add_argument('-f', '--force',
help='Force, no confirmation prompt before submission',
action='store_true')
parser.add_argument('files', nargs='+')
args = parser.parse_args()
files = args.files
try:
cfg = get_config()
except ConfigError as exc:
print(exc)
sys.exit(1)
problem, ext = os.path.splitext(os.path.basename(files[0]))
language = guess_language(ext, files)
mainclass = guess_mainclass(language, files)
tag = args.tag
problem = problem.lower()
if args.problem:
problem = args.problem
if args.mainclass is not None:
mainclass = args.mainclass
if args.language:
language = args.language
if language is None:
print(f'''\
No language specified, and I failed to guess language from filename
extension "{ext}"''')
sys.exit(1)
files = sorted(list(set(args.files)))
try:
login_reply = login_from_config(cfg)
except ConfigError as exc:
print(exc)
sys.exit(1)
except requests.exceptions.RequestException as err:
print('Login connection failed:', err)
sys.exit(1)
if not login_reply.status_code == 200:
print('Login failed.')
if login_reply.status_code == 403:
print('Incorrect username or password/token (403)')
elif login_reply.status_code == 404:
print('Incorrect login URL (404)')
else:
print('Status code:', login_reply.status_code)
sys.exit(1)
submit_url = get_url(cfg, 'submissionurl', 'submit')
if not args.force:
confirm_or_die(problem, language, files, mainclass, tag)
try:
result = submit(submit_url,
login_reply.cookies,
problem,
language,
files,
mainclass,
tag,
args.assignment,
args.contest)
except requests.exceptions.RequestException as err:
print('Submit connection failed:', err)
sys.exit(1)
if result.status_code != 200:
print('Submission failed.')
if result.status_code == 403:
print('Access denied (403)')
elif result.status_code == 404:
print('Incorrect submit URL (404)')
else:
print('Status code:', result.status_code)
sys.exit(1)
plain_result = result.content.decode('utf-8').replace('<br />', '\n')
print(plain_result)
submission_url = None
try:
submission_url = get_submission_url(plain_result, cfg)
except configparser.NoOptionError:
pass
if submission_url:
print(submission_url)
if not show_judgement(submission_url, cfg):
sys.exit(1)
if __name__ == '__main__':
main()