-
Notifications
You must be signed in to change notification settings - Fork 0
/
submit.py
366 lines (301 loc) · 10.4 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
#!/usr/bin/env python
from __future__ import print_function
import argparse
import os
import re
import sys
import webbrowser
import requests
import requests.exceptions
# Python 2/3 compatibility
if sys.version_info[0] >= 3:
import configparser
else:
# Python 2, import modules with Python 3 names
import ConfigParser as configparser
# End Python 2/3 compatibility
_DEFAULT_CONFIG = '/usr/local/etc/kattisrc'
_LANGUAGE_GUESS = {
'.c': 'C',
'.c++': 'C++',
'.cc': 'C++',
'.c#': 'C#',
'.cpp': 'C++',
'.cs': 'C#',
'.cxx': 'C++',
'.cbl': 'COBOL',
'.cob': 'COBOL',
'.cpy': 'COBOL',
'.fs': 'F#',
'.go': 'Go',
'.h': 'C++',
'.hs': 'Haskell',
'.java': 'Java',
'.js': 'JavaScript',
'.kt': 'Kotlin',
'.lisp': 'Common Lisp',
'.cl': 'Common Lisp',
'.m': 'Objective-C',
'.ml': 'OCaml',
'.pas': 'Pascal',
'.php': 'PHP',
'.pl': 'Prolog',
'.rb': 'Ruby',
'.rs': 'Rust',
'.scala': 'Scala',
}
_GUESS_MAINCLASS = {'Java', 'Scala', 'Kotlin'}
_GUESS_MAINFILE = {'Python 2', 'Python 3', 'PHP', 'JavaScript', 'Rust', 'Pascal'}
_HEADERS = {'User-Agent': 'kattis-cli-submit'}
class ConfigError(Exception):
pass
def get_url(cfg, option, default):
if cfg.has_option('kattis', option):
return cfg.get('kattis', option)
else:
return 'https://%s/%s' % (cfg.get('kattis', '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)
if not cfg.read([os.path.join(os.path.expanduser("~"), '.kattisrc'),
os.path.join(os.path.dirname(sys.argv[0]), '.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=''):
"""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'}
sub_files = []
for f in files:
with open(f) 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 open_submission(submit_response, cfg):
submissions_url = get_url(cfg, 'submissionsurl', 'submissions')
m = re.search(r'Submission ID: (\d+)', submit_response)
if m:
submission_id = m.group(1)
# print('Open in browser (y/N)?')
# if sys.stdin.readline().upper()[:-1] == 'Y':
if True:
url = '%s/%s' % (submissions_url, submission_id)
webbrowser.open(url)
def main():
parser = argparse.ArgumentParser(prog='kattis', description='Submit a solution to Kattis')
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('''\
No language specified, and I failed to guess language from filename
extension "%s"''' % (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)
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)
try:
open_submission(plain_result, cfg)
except configparser.NoOptionError:
pass
if __name__ == '__main__':
main()