-
Notifications
You must be signed in to change notification settings - Fork 0
/
satori
executable file
·590 lines (485 loc) · 19 KB
/
satori
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
#!/usr/bin/env python2.7
# -*- mode: python -*-
import argparse
import getpass
import json
import os
import io
import requests
import webbrowser
import random
import sys
import time
import subprocess
from pyquery import PyQuery as pq
from lxml.html import etree
__version__ = '0.1'
BASE = 'https://satori.tcs.uj.edu.pl'
LOG_REQUESTS = True
def parse_html(html):
parser = etree.HTMLParser()
tree = etree.parse(io.BytesIO(html), parser)
return tree
class SatoriError(Exception):
__module__ = 'satori'
def match_code(query, code):
query = query.lower()
code = code.lower().encode('utf8')
return query == code or query + '*' == code
class Cache(object):
def __init__(self, path):
self.path = path
self.data = None
def load(self):
if self.data is not None:
return
try:
with open(self.path) as f:
self.data = json.load(f)
except IOError:
self.data = {}
def save(self):
dir = os.path.dirname(self.path)
if not os.path.exists(dir):
os.mkdir(dir)
suf = '.tmp%d' % random.randrange(10000)
with open(self.path + suf, 'w') as f:
json.dump(self.data, f)
os.rename(self.path + suf, self.path)
def __setitem__(self, key, value):
self.load()
self.data[key] = value
self.save()
def __getitem__(self, key):
self.load()
return self.data[key]
def __contains__(self, key):
self.load()
return key in self.data
def get(self, key, default=None):
self.load()
return self.data.get(key, default)
def cached(cache_name):
def wrapper1(func):
def wrapper(self, *args):
key = repr(args)
cache = getattr(self, cache_name)
if key not in cache:
cache[key] = func(self, *args)
return cache[key]
return wrapper
return wrapper1
class Session(object):
def __init__(self, path='~/.config/satori.json',
cache_path='~/.cache/satori'):
self.path = os.path.expanduser(path)
self.cache_path = os.path.expanduser(cache_path)
self.settings = {}
self.match_contest_cache = Cache(self.cache_path + '/match_contest.json')
self.match_submit_problem_cache = Cache(self.cache_path + '/match_submit_problem.json')
self.match_problem_cache = Cache(self.cache_path + '/match_problem.json')
def load(self):
try:
with open(self.path, 'r') as f:
self.settings = json.load(f)
except IOError:
pass
def save(self):
with open(self.path + '.tmp', 'w') as f:
json.dump(self.settings, f)
os.rename(self.path + '.tmp', self.path)
def login(self, username, password):
self.settings['username'] = username
self.settings['password'] = password.encode('hex')
# HTTP
def _do_request(self, method, path, parse=True, cookie=True, data={}, files={}):
if LOG_REQUESTS:
log_line = '{} {}'.format(method, path)
color1 = '\33[0;33m'
color2 = '\33[1;30m'
endcolor = '\33[m'
sys.stderr.write(color1 + log_line + '...' + endcolor)
sys.stderr.flush()
r = requests.request(method, BASE + path,
files=files,
allow_redirects=False,
data=data,
headers={'Cookie':
'satori_token='
+ self.settings['satori_token']} if cookie else {})
if LOG_REQUESTS:
sys.stderr.write(color2 + '\r' + log_line + ' -> {}'.format(r.status_code) + endcolor + '\n')
return r
def request(self, method, path, **kwargs):
parse = kwargs.get('parse', True)
if not 'satori_token' in self.settings:
self._login()
r = self._do_request(method, path, **kwargs)
if r.status_code == 302 and r.headers['location'].startswith(BASE + '/login'):
self._login()
self.save()
r = self._do_request(method, path)
if parse:
return pq(r.content)
else:
return r
def _login(self):
if 'username' not in self.settings:
raise SatoriError('Please login with `satori login`.')
resp = self._do_request('POST', '/login',
cookie=False,
parse=False,
data={
'login': self.settings['username'],
'password': self.settings['password'].decode('hex')})
if resp.status_code == 302:
cookie = resp.headers['set-cookie']
token = cookie.split('=')[1].split(';')[0]
self.settings['satori_token'] = token
else:
raise SatoriError('invalid login')
# API
def get_contests(self, other=False, archived=False):
resp = self.request('GET', '/contest/select' + ('?participating_limit=0&participating_filter_archived=1' if archived else ''))
tables = resp.find('.results')
if not other:
tables = pq(tables[0])
for row in tables.find('tr'):
row = pq(row)
name = row.find('a.stdlink').text()
link = row.find('a.stdlink').attr('href')
if link.startswith('/contest/'):
id = link.split('/')[2]
yield name, int(id)
@cached('match_contest_cache')
def match_contest(self, query):
try:
return int(query)
except ValueError:
for name, id in self.get_contests(query):
if query.lower() in name.lower():
return id
raise SatoriError('no contest %r found' % query)
def print_contests(self, **kwargs):
for name, id in self.get_contests(**kwargs):
print u'{}\t{}'.format(id, name)
def get_problems(self, contest):
url = '/contest/%d/problems' % self.match_contest(contest)
for row in self.request('GET', url).find('.results').find('tr'):
row = pq(row)
cols = [ pq(c) for c in row.find('td') ]
if not cols: continue
code = cols[0].text().strip()
name = cols[1].text()
desc = cols[3].text()
pdf = cols[2].find('a').attr('href')
if pdf:
id = int(pdf.split('/')[3])
else:
id = None
url = cols[1].find('.stdlink')
if url: url = url.attr('href')
yield id, pdf, url, code, name, desc
def print_problems(self, contest):
for id, pdf, url, code, title, desc in self.get_problems(contest):
print u'{:<9} {: <5} {: <30} {}' \
.format(id or '', code, title, desc)
def get_submit_problems(self, contest):
url = '/contest/%d/submit' % self.match_contest(contest)
body = self.request('GET', url)
opts = body.find('[name=problem]').find('option')
for opt in opts:
opt = pq(opt)
if opt.attr('value'):
code, name = opt.text().split(':', 1)
yield int(opt.attr('value')), code, name
def print_submit_problems(self, contest):
for id, code, desc in self.get_submit_problems(contest):
print u'{:<9} {: <5} {}' \
.format(id or '', code, desc)
@cached('match_submit_problem_cache')
def match_submit_problem(self, contest, problem):
for id, code, desc in self.get_submit_problems(contest):
if match_code(problem, code):
return (id, code)
raise SatoriError('unknown problem %r' % problem)
@cached('match_problem_cache')
def match_problem(self, contest, problem):
try:
return int(problem)
except ValueError:
pass
for id, pdf, url, code, title, desc in self.get_problems(contest):
if match_code(problem, code):
return (id, pdf, url)
raise SatoriError('unknown problem %r' % problem)
def cache_write(self, name, data):
if not os.path.exists(self.cache_path):
os.mkdir(self.cache_path)
path = self.cache_path + '/' + name
with open(path, 'w') as f:
f.write(data)
return path
def get_pdf(self, contest, problem):
id, pdf, url = self.match_problem(contest, problem)
if not pdf:
return None
ret = self.request('GET', pdf, parse=False)
ret.raise_for_status()
return self.cache_write('%d.pdf' % id, ret.content)
def get_status(self, contest, id):
url = '/contest/%d/results/%d' % (self.match_contest(contest), id)
body = self.request('GET', url)
header = body.find('table.results').find('tr')[1]
header = pq(header).find('td')
problem = pq(header[2]).text()
status = pq(header[-1]).text()
tests = []
for row in body.find('.mainsphinx table.docutils tr')[1:]:
row = pq(row)
cols = map(pq, row.find('td'))
tests.append((cols[0].text(), cols[1].text()))
return problem, status, tests
def print_status(self, contest, id, out=None):
out = out or sys.stdout
problem, status, tests = self.get_status(contest, id)
print >>out, 'Test report for %s' % problem
print >>out, 'Status:', status
for name, status in tests:
print >>out, ' - {: <10} {}'.format(name, status)
def submit(self, contest, problem, file):
name = file.split('/')[-1]
url = '/contest/%d/submit' % self.match_contest(contest)
problem, problemcode = self.match_submit_problem(contest, problem)
ret = self.request('POST', url,
data={'problem': problem},
files={'codefile': (name, open(file), 'text/plain')},
parse=False)
if ret.status_code != 302:
err_path = self.cache_path + '/error.html'
with open(err_path, 'w') as f:
f.write(ret.content)
raise SatoriError('submit failed (check error: {})'.format(err_path))
return problemcode
def get_last_submit(self, contest):
url = '/contest/%d/results' % self.match_contest(contest)
body = self.request('GET', url)
tr = body.find('table.results').find('tr')[1]
return int(pq(tr).find('a').text())
def get_submits(self, contest):
url = '/contest/%d/results?results_limit=2000' % self.match_contest(contest)
body = self.request('GET', url)
rows = body.find('table.results').find('tr')[1:]
for row in rows:
status = pq(row).find('.submitstatus').text()
id = int(pq(row).find('a').text())
name = row[1].text
yield id, name, status
def print_submits(self, contest):
for id, name, status in self.get_submits(contest):
print u'{}\t{}\t{}'.format(id, name, status)
def download_submit(self, submit):
resp = self.request('GET', '/download/Submit/%d/data/content/info.txt' % submit, parse=False)
return resp.content
def notify_status(problem, status):
subprocess.check_call(['notify-send', 'New status for {}: {}'.format(problem, status)])
def wait(sess, contest, id, notify=True):
contest = sess.match_contest(contest)
last_status = ''
while True:
problem, status, tests = sess.get_status(contest, id)
if status != last_status:
if notify: notify_status(problem, status)
last_status = status
if status and status != 'QUE':
break
time.sleep(10)
if notify:
with open(sess.cache_path + '/last.txt', 'a') as out:
sess.print_status(contest, id, out)
print >>out
if not notify:
sess.print_status(contest, id)
def download_submits(sess, contest, dir):
from magic import Magic
import mimetypes
magic = Magic(mime=True, uncompress=False)
mimetype_to_ext = dict( (v, k) for k, v in mimetypes.types_map.items() )
mimetype_to_ext['text/x-c'] = '.cpp'
submits = list(sess.get_submits(contest))
for id, name, status in submits:
status = status.split()[0]
if '/' in name:
raise ValueError('bad task name')
if '/' in status:
raise ValueError('bad status name')
if not status:
status = 'UNK'
data = sess.download_submit(id)
mime = magic.from_buffer(data).split(';')[0]
ext = mimetype_to_ext.get(mime, '.txt')
path = dir + '/%s/%d_%s%s' % (name, id, status, ext)
dirs = os.path.dirname(path)
if not os.path.exists(dirs):
os.makedirs(dirs)
with open(path, 'wb') as f:
f.write(data)
def main():
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(dest='command')
subparsers.add_parser(
'login',
help='Set Satori credentials.')
contests_parser = subparsers.add_parser(
'contests',
help='List contests')
contests_parser.add_argument('--show-other', action='store_true')
contests_parser.add_argument('--show-archived', action='store_true')
problems_parser = subparsers.add_parser(
'problems',
help='List problems.')
problems_parser.add_argument('--submit',
action='store_true',
help='show submittable problems.')
problems_parser.add_argument('contest')
problem_parser = subparsers.add_parser(
'problem',
help='Open problem statement.')
problem_parser.add_argument('contest')
problem_parser.add_argument('problem')
problem_parser.add_argument('--pdf', action='store_true',
help='Open PDF instead of problem page.')
downloadproblems_parser = subparsers.add_parser(
'downloadproblems',
help='Download all problem PDFs from a given contest.')
downloadproblems_parser.add_argument('contest')
status_parser = subparsers.add_parser(
'status',
help='Show submit status.')
status_parser.add_argument('contest')
status_parser.add_argument('id', type=int, default=0, nargs='?')
wait_parser = subparsers.add_parser(
'wait',
help='Wait for submit status change and show notification.')
wait_parser.add_argument('contest')
wait_parser.add_argument('id', type=int, default=0, nargs='?')
submits_parser = subparsers.add_parser(
'submits',
help='List submits.')
submits_parser.add_argument('contest')
downloadsubmits_parser = subparsers.add_parser(
'downloadsubmits',
help='Downloads your submissions.')
downloadsubmits_parser.add_argument('contest')
downloadsubmits_parser.add_argument('dir')
submit_parser = subparsers.add_parser(
'submit',
help='Submit a solution..')
submit_parser.add_argument('contest')
submit_parser.add_argument('problem')
submit_parser.add_argument('file')
subparsers.add_parser(
'last',
help='Show results of recently submitted tasks.')
subparsers.add_parser(
'clear-cache',
help='Clear cache.')
ns = parser.parse_args()
sess = Session()
sess.load()
if ns.command == 'login':
sess.login(raw_input('Username: '),
getpass.getpass('Password: '))
sess.print_contests()
sess.save()
elif ns.command == 'contests':
sess.print_contests(other=ns.show_other, archived=ns.show_archived)
elif ns.command == 'problems':
if ns.submit:
sess.print_submit_problems(ns.contest)
else:
sess.print_problems(ns.contest)
elif ns.command == 'problem':
def open_pdf():
path = sess.get_pdf(ns.contest, ns.problem)
if not path:
print 'Problem statement missing.'
else:
webbrowser.open('file://' + path)
if ns.pdf:
open_pdf()
else:
id, pdf, url = sess.match_problem(ns.contest, ns.problem)
if not url:
print 'No HTML for this problem'
open_pdf()
else:
webbrowser.open(BASE + url)
elif ns.command == 'downloadproblems':
for id, pdf, url, code, title, desc in sess.get_problems(ns.contest):
if '/' in code or '\0' in code:
raise Exception('invalid task code')
if not pdf:
print 'skip task', title
continue
ret = sess.request('GET', pdf, parse=False)
ret.raise_for_status()
with open(code + '.pdf', 'wb') as f:
f.write(ret.content)
elif ns.command == 'status':
id = ns.id
if id == 0:
id = sess.get_last_submit(ns.contest)
sess.print_status(ns.contest, id)
elif ns.command == 'wait':
id = ns.id
if id == 0:
id = sess.get_last_submit(ns.contest)
wait(sess, ns.contest, id, notify=False)
elif ns.command == 'submit':
sess.submit(ns.contest, ns.problem, ns.file)
submit_id = sess.get_last_submit(ns.contest)
print 'Submitted as', submit_id
check_new_version()
pid = os.fork()
if pid == 0:
sys.stdout = sys.stderr = open(sess.cache_path + '/waiter.log', 'a', 1)
wait(sess, ns.contest, submit_id, notify=True)
os._exit(0)
else:
print 'Forked waiter with PID', pid
elif ns.command == 'submits':
sess.print_submits(ns.contest)
elif ns.command == 'downloadsubmits':
download_submits(sess, ns.contest, ns.dir)
elif ns.command == 'last':
subprocess.check_call(['tail', '-n', '200', sess.cache_path + '/last.txt'])
elif ns.command == 'clear-cache':
path = os.path.expanduser('~/.cache/satori')
for file in os.listdir(path):
os.unlink(path + '/' + file)
def check_new_version():
try:
data = requests.get('https://raw.githubusercontent.com/zielmicha/satori-cli/master/version').content.strip()
if data != __version__:
print 'New version of satori-cli is available. '
print 'Download it from https://github.com/zielmicha/satori-cli'
except Exception as ex:
print 'Failed to check for new version:', ex
if __name__ == '__main__':
HTTP_DEBUG = False
if HTTP_DEBUG:
import httplib as http_client
http_client.HTTPConnection.debuglevel = 1
import logging
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
try:
main()
except SatoriError as err:
sys.exit('Error: ' + str(err))
except KeyboardInterrupt:
sys.exit('Interrupted')