def get_judges(): from satori.tools import options, setup options.add_argument('judges_dir') options.add_argument('--contest', help='Only for selected contest') contest = None args = setup(logging.INFO) judges_dir = args.judges_dir if not os.path.exists(judges_dir): os.mkdir(judges_dir) if args.contest is not None: try: contest = Contest.filter(ContestStruct(id=int(args.contest)))[0] except: try: contest = Contest.filter(ContestStruct(name=args.contest))[0] except: pass tests = [] if contest is None: tests = [(t, '') for t in Test.filter()] else: for pm in ProblemMapping.filter(ProblemMappingStruct(contest=contest)): tests += [(t, pm.code) for t in pm.default_test_suite.get_tests() ] stats = dict() for test, code in tests: if not code in stats: stats[code] = set() judge = test.data_get('judge') stats[code].add(judge.value) ext = judge.filename.split('.')[-1] name = os.path.join(judges_dir,judge.value+'.'+ext) test.data_get_blob_path('judge', name); print stats
def passwd(): options.add_argument('usr', help='username') options.add_argument('pwd', help='password') opts = setup(logging.INFO) u = User.filter(UserStruct(login=opts.usr))[0] logging.info("Changing password for user %s" % (u.name, )) u.set_password(opts.pwd)
def get_judges(): from satori.tools import options, setup options.add_argument('judges_dir') options.add_argument('--contest', help='Only for selected contest') contest = None args = setup(logging.INFO) judges_dir = args.judges_dir if not os.path.exists(judges_dir): os.mkdir(judges_dir) if args.contest is not None: try: contest = Contest.filter(ContestStruct(id=int(args.contest)))[0] except: try: contest = Contest.filter(ContestStruct(name=args.contest))[0] except: pass tests = [] if contest is None: tests = [(t, '') for t in Test.filter()] else: for pm in ProblemMapping.filter(ProblemMappingStruct(contest=contest)): tests += [(t, pm.code) for t in pm.default_test_suite.get_tests()] stats = dict() for test, code in tests: if not code in stats: stats[code] = set() judge = test.data_get('judge') stats[code].add(judge.value) ext = judge.filename.split('.')[-1] name = os.path.join(judges_dir, judge.value + '.' + ext) test.data_get_blob_path('judge', name) print stats
def default_judges(): from satori.tools import options, setup options.add_argument('judges_dir') args = setup(logging.INFO) judges_dir = args.judges_dir for judge in [ os.path.join(judges_dir, entry) for entry in os.listdir(judges_dir) if os.path.isfile(os.path.join(judges_dir, entry)) ]: if judge[-3:] == '.py': name = os.path.basename(judge)[:-3] blob = Global.get_instance().judges_set_blob_path(name, judge)
def adduser(): options.add_argument('usr', help='username') options.add_argument('first', help='firstname') options.add_argument('last', help='lastname') options.add_argument('email', help='e-mail') options.add_argument('affiliation', help='affiliation') opts = setup(logging.INFO) u = User.create( UserStruct(login=opts.usr, firstname=opts.first, lastname=opts.last, email=opts.email, confirmed=True, activated=True, affiliation=opts.affiliation)) logging.info("Created user %s" % (u.name, ))
def main(): from satori.tools import options, setup options.add_argument('--ipython', help='Use IPython', action='store_true') flags = setup(logging.INFO) if flags.ipython: from satori.client.common import want_import want_import(globals(), "*") from IPython import embed embed() else: import code console = code.InteractiveConsole() console.runcode('from satori.client.common import want_import') console.runcode('want_import(globals(), "*")') console.interact()
def addchecker(): options.add_argument('login', help='login') options.add_argument('address', help='address') options.add_argument('pwd', help='password') opts = setup(logging.INFO) m = Machine.create( MachineStruct(login=opts.login, address=opts.address, netmask='255.255.255.255')) m.set_password(opts.pwd) Privilege.global_grant(m, 'JUDGE')
def uzi_team(): options.add_argument('contest', help='contest_name') options.add_argument('team', help='team_name') options.add_argument('contestants', nargs='+', metavar='contestant', help='contest name') opts = setup(logging.INFO) contest_name = opts.contest name = opts.team users_name = opts.contestants c = Contest.filter(ContestStruct(name=contest_name)) if len(c) >= 1: contest = c[0] else: logging.error('incorrect contest name ' + contest_name) sys.exit(1) users = set() for un in users_name: u = User.filter(UserStruct(login=un, activated=True)) if len(u) > 1: raise RuntimeError("To many matches for '" + un + "'") elif len(u) == 1: users.add(u[0]) else: u = User.filter(UserStruct(name=un, activated=True)) if len(u) > 1: raise RuntimeError("To many matches for '" + un + "'") if len(u) == 1: users.add(u[0]) else: raise RuntimeError('Unknown user name ' + un) users = list(users) if not len(users): raise RuntimeError('no users') print contest.name, name, [user.name for user in users], users Contestant.create(fields=ContestantStruct(contest=contest, name=name, accepted=True), user_list=users)
def team_rename(): options.add_argument('contest', help='contest_name') options.add_argument('team', help='old team_name') options.add_argument('name', help='new team_name') opts = setup(logging.INFO) contest_name = opts.contest old_name = opts.team new_name = opts.name c = Contest.filter(ContestStruct(name=contest_name)) if len(c) >= 1: contest = c[0] else: logging.error('incorrect contest name ' + contest_name) sys.exit(1) cn = Contestant.filter(ContestantStruct(contest=contest, name=old_name))[0] cn.modify(ContestantStruct(name=new_name))
def submit(): options.add_argument('-C', '--contest', type=int, help='contest ID') options.add_argument('problem_code') options.add_argument('submit_file', type=argparse.FileType('r')) opts = setup(logging.INFO) contestId = None if auth_setup.section: if config.has_option(auth_setup.section, 'contest'): contestId = config.getint(auth_setup.section, 'contest') if opts.contest: contestId = options.contest if not contestId: raise RuntimeError('The contest ID has not been specified') cc = Contest.filter(ContestStruct(id=contestId)) if not cc: raise RuntimeError('The specified contest is not found') contest = cc[0] pp = ProblemMapping.filter( ProblemMappingStruct(contest=contest, code=opts.problem_code)) if not pp: raise RuntimeError('The specified problem is not found') problem = pp[0] submit = Submit.create(SubmitStruct(problem=problem), opts.submit_file.read(), os.path.basename(opts.submit_file.name)) opts.submit_file.close() print 'Submit successfully created:' print ' Contest:', contest.name print ' Problem:', problem.code, '-', problem.title print ' File:', args[1] print ' Submit ID:', submit.id
def uzi_team(): options.add_argument('contest', help='contest_name') options.add_argument('team', help='team_name') options.add_argument('contestants', nargs='+', metavar='contestant', help='contest name') opts = setup(logging.INFO) contest_name=opts.contest name=opts.team users_name=opts.contestants c=Contest.filter(ContestStruct(name=contest_name)) if len(c) >= 1: contest=c[0] else: logging.error('incorrect contest name '+contest_name) sys.exit(1) users = set() for un in users_name: u=User.filter(UserStruct(login=un, activated=True)) if len(u) > 1: raise RuntimeError("To many matches for '" + un + "'") elif len(u) == 1: users.add(u[0]); else: u=User.filter(UserStruct(name=un, activated=True)) if len(u) > 1: raise RuntimeError("To many matches for '" + un + "'") if len(u) == 1: users.add(u[0]); else: raise RuntimeError('Unknown user name '+un) users = list(users) if not len(users) : raise RuntimeError('no users') print contest.name, name,[user.name for user in users],users Contestant.create(fields=ContestantStruct(contest=contest,name=name,accepted=True), user_list=users)
def athina_import(): import os, sys, getpass from satori.tools import options, setup options.add_argument('--name', help='Contest name') options.add_argument('--environment', help='Test environment name') options.add_argument('base_dir') args = setup(logging.INFO) base_dir = args.base_dir if not os.path.exists(os.path.join(base_dir, 'server', 'contest', 'users')): logging.error('provided path is invalid') sys.exit(1) def get_path(*args): return os.path.join(base_dir, 'server', 'contest', *args) while not args.name: args.name = raw_input('Contest name: ') print 'Contest name: ', args.name print 'Test environment: ', args.environment time_start = None with open(get_path('times', 'start'), 'r') as f: time_start = datetime.fromtimestamp( int(f.readline().strip(" \n\t\x00"))) time_stop = None with open(get_path('times', 'end'), 'r') as f: time_stop = datetime.fromtimestamp(int( f.readline().strip(" \n\t\x00"))) time_freeze = None with open(get_path('times', 'freeze'), 'r') as f: time_freeze = datetime.fromtimestamp( int(f.readline().strip(" \n\t\x00"))) users = {} if True: for login in os.listdir(get_path('users')): with open(get_path('users', login, 'fullname'), 'r') as f: fullname = f.readline().strip(" \n\t\x00") with open(get_path('users', login, 'password'), 'r') as f: password = f.readline().strip(" \n\t\x00") users[login] = { 'login': string.lower(login), 'name': fullname, 'password': password, } submits = {} if True: for d in range(10): for submit in os.listdir(get_path('data', str(d))): with open(get_path('problem', str(d), submit), 'r') as f: problem = f.readline().strip(" \n\t\x00") if problem[0:2] == "__": continue with open(get_path('data', str(d), submit), 'r') as f: data = f.read() with open(get_path('filename', str(d), submit), 'r') as f: filename = f.readline().strip(" \n\t\x00") with open(get_path('time', str(d), submit), 'r') as f: time = datetime.fromtimestamp( int(f.readline().strip(" \n\t\x00"))) with open(get_path('user', str(d), submit), 'r') as f: user = f.readline().strip().strip(" \n\t\x00") with open(get_path('log', str(d), submit), 'r') as f: test_result = {} for tr in [ x.strip(" \n\t\x00[]") for x in re.split( u'\][^[]*\[', u' '.join(f.readlines())) ]: m = re.match(u'^(\d+)\s*:\s*(.+?)\s+(\d+)$', tr) if m: test_result[int(m.group(1))] = { 'status': m.group(2), 'time': int(m.group(3)), } continue m = re.match(u'^(\d+)\s*:\s*(.*)$', tr) if m: test_result[int(m.group(1))] = { 'status': m.group(2), 'time': 0, } continue test_result[0] = { 'status': tr, 'time': 0, } with open(get_path('status', str(d), submit), 'r') as f: suite_result = f.readline().strip().strip(" \n\t\x00") submit = int(submit) submits[submit] = { 'submit': submit, 'data': data, 'filename': filename, 'problem': problem, 'time': time, 'user': user, 'test_results': test_result, 'suite_result': suite_result, } problems = {} if True: for dir in os.listdir(get_path()): if not os.path.isdir(get_path(dir)): continue if not os.path.exists(get_path(dir, 'testcount')): continue with open(get_path(dir, 'testcount')) as f: testcount = int(f.readline()) + 1 with open(get_path(dir, 'sizelimit')) as f: sizelimit = int(f.readline()) if os.path.exists(get_path(dir, 'checker')): checker = get_path(dir, 'checker') else: checker = None tests = {} for t in range(testcount): input = get_path(dir, str(t) + '.in') if not os.path.exists(input): input = None output = get_path(dir, str(t) + '.out') if not os.path.exists(output): output = None memlimit = get_path(dir, str(t) + '.mem') if os.path.exists(memlimit): with open(memlimit, 'r') as f: memlimit = int(f.readline()) else: memlimit = None timelimit = get_path(dir, str(t) + '.tle') if os.path.exists(timelimit): with open(timelimit, 'r') as f: timelimit = timedelta(seconds=float(f.readline()) * 0.01) else: timelimit = None tests[t] = { 'test': t, 'input': input, 'output': output, 'memlimit': memlimit, 'timelimit': timelimit, } problems[dir] = { 'problem': dir, 'testcount': testcount, 'sizelimit': sizelimit, 'checker': checker, 'tests': tests, } # print 'users: ', users # print 'problems: ', problems # print 'submits: ', submits mytoken = token_container.get_token() contest = Creator('Contest', name=args.name)() Privilege.grant(contest.contestant_role, contest, 'SUBMIT') Privilege.grant(contest.contestant_role, contest, 'VIEW') Privilege.grant(contest.contestant_role, contest, 'VIEW_TASKS') for login, user in sorted(users.iteritems()): firstname = user['name'].split()[0] lastname = ' '.join(user['name'].split()[1:]) user['object'] = Creator('User', login=args.name + '_' + user['login']).fields(firstname=firstname, lastname=lastname)() user['object'].set_password(user['password']) user['contestant'] = Creator('Contestant', contest=contest, login=user['object'].login).additional( user_list=[user['object']])() user['contestant'].set_password(user['password']) for name, problem in sorted(problems.iteritems()): problem['object'] = Creator('Problem', name=args.name + '_' + problem['problem'])() tests = [] for num, test in sorted(problem['tests'].iteritems()): oa = OaMap() if problem['checker'] != None: oa.set_blob_path('checker', problem['checker']) if test['input'] != None: oa.set_blob_path('input', test['input']) if test['output'] != None: oa.set_blob_path('hint', test['output']) if test['memlimit'] != None: oa.set_str('memory', str(test['memlimit']) + 'B') if test['timelimit'] != None: oa.set_str('time', str(seconds(test['timelimit'])) + 's') test['object'] = Creator( 'Test', problem=problem['object'], name=args.name + '_' + problem['problem'] + '_default_' + str(test['test'])).fields( environment=args.environment).additional( data=oa.get_map())() tests.append(test['object']) params = OaMap() params.set_str('reporter_show_tests', 'true') problem['testsuite'] = Creator( 'TestSuite', problem=problem['object'], name=args.name + '_' + problem['problem'] + '_default').fields( reporter='ACMReporter', dispatcher='SerialDispatcher', accumulators='').additional(test_list=tests, params=params.get_map(), test_params=[{}] * len(tests))() problem['mapping'] = Creator( 'ProblemMapping', contest=contest, problem=problem['object']).fields( code=problem['problem'], title=problem['problem'], default_test_suite=problem['testsuite'])() params = OaMap() params.set_str('time_start', time_start.strftime('%Y-%m-%d %H:%M:%S')) params.set_str('time_stop', time_freeze.strftime('%Y-%m-%d %H:%M:%S')) ranking = Creator('Ranking', contest=contest, name='Ranking').fields( is_public=True, aggregator='ACMAggregator').additional(params=params.get_map(), problem_test_suites={}, problem_params={})() params = OaMap() params.set_str('time_start', time_start.strftime('%Y-%m-%d %H:%M:%S')) params.set_str('time_stop', (time_stop + timedelta(hours=2)).strftime('%Y-%m-%d %H:%M:%S')) full_ranking = Creator( 'Ranking', contest=contest, name='Full Ranking').fields(is_public=False, aggregator='ACMAggregator').additional( params=params.get_map(), problem_test_suites={}, problem_params={})() Privilege.grant(contest.contestant_role, full_ranking, 'VIEW', PrivilegeTimes(start_on=time_stop + timedelta(hours=2))) marks_ranking = Creator('Ranking', contest=contest, name='Marks').fields( is_public=False, aggregator='MarksAggregator').additional(params=params.get_map(), problem_test_suites={}, problem_params={})() params = OaMap() params.set_str('time_start', time_start.strftime('%Y-%m-%d %H:%M:%S')) params.set_str('show_invisible', '1') admin_ranking = Creator( 'Ranking', contest=contest, name='Admin Ranking').fields(is_public=False, aggregator='ACMAggregator').additional( params=params.get_map(), problem_test_suites={}, problem_params={})() for id, submit in sorted(submits.iteritems()): user = users[submit['user']] test_results = {} problem = problems[submit['problem']] for num, test in problem['tests'].iteritems(): if num in submit['test_results']: oa = OaMap() oa.set_str('status', unicode(submit['test_results'][num]['status'])) oa.set_str('execute_time_cpu', unicode(submit['test_results'][num]['time'])) test_results[test['object']] = oa.get_map() submit['object'] = Creator( 'Submit', contestant=user['contestant'], time=submit['time'], problem=problems[submit['problem']]['mapping']).additional( filename=submit['filename'], content=submit['data'], test_results=test_results).function('inject')()
def athina_import_testsuite(): from satori.tools import options, setup options.add_argument('problem_name') options.add_argument('base_dir') args = setup() problem = Problem.filter(ProblemStruct(name=unicode(args.problem_name)))[0] base_dir = unicode(args.base_dir) if not os.path.exists(os.path.join(base_dir, 'testcount')): logging.error('provided path is invalid') sys.exit(1) def get_path(*args): return os.path.join(base_dir, *args) with open(get_path('testcount')) as f: testcount = int(f.readline()) + 1 with open(get_path('sizelimit')) as f: sizelimit = int(f.readline()) if os.path.exists(get_path('checker')): checker = get_path('checker') else: checker = None judge = get_path('judge') if not os.path.exists(judge): raise Exception('judge missing') tests = [] for t in range(testcount): input = get_path(str(t) + '.in') if not os.path.exists(input): input = None output = get_path(str(t) + '.out') if not os.path.exists(output): output = None memlimit = get_path(str(t) + '.mem') if os.path.exists(memlimit): with open(memlimit, 'r') as f: memlimit = int(f.readline()) else: memlimit = None timelimit = get_path(str(t) + '.tle') if os.path.exists(timelimit): with open(timelimit, 'r') as f: timelimit = timedelta(seconds=float(f.readline()) * 0.01) else: timelimit = None oa = OaMap() oa.set_blob_path('judge', judge) if checker != None: oa.set_blob_path('checker', checker) if input != None: oa.set_blob_path('input', input) if output != None: oa.set_blob_path('hint', output) if memlimit != None: oa.set_str('memory', str(memlimit) + 'B') if timelimit != None: oa.set_str('time', str(seconds(timelimit)) + 's') tests.append( Creator('Test', problem=problem, name=problem.name + '_import_' + str(t)).additional(data=oa.get_map())()) params = OaMap() testsuite = Creator('TestSuite', problem=problem, name=problem.name + '_import').fields( reporter='ACMReporter', dispatcher='SerialDispatcher', accumulators='').additional( test_list=tests, params=params.get_map(), test_params=[{}] * len(tests))()
def rejudge(): options.add_argument('--submit', type=int, help='submit ID') opts = setup(logging.INFO) s = Submit.filter(SubmitStruct(id=opts.submit))[0] s.rejudge_test_results()
from satori.judge.judge import JailBuilder, JailRun from satori.client.common import want_import import os import resource import stat import sys import shutil import time import subprocess import traceback from satori.tools import options, setup, authenticate options.add_argument('--debug', dest='debug', default='') options.add_argument('--jail-dir', dest='jail_dir', default='/jail') options.add_argument('--cgroup-dir', dest='cgroup_dir', default='/sys/fs/cgroup') options.add_argument('--template-dir', dest='template_dir', default='/template') options.add_argument('--template-src', dest='template_src', default='student.tcs.uj.edu.pl:/exports/judge') options.add_argument('--retry-time', dest='retry_time', default=5, type=int) options.add_argument('--cgroup', dest='cgroup', default='runner') options.add_argument('--memory', dest='cgroup_memory', default=8*1024*1024*1024, type=int) options.add_argument('--time', dest='real_time', default=5*60*1000, type=int) options.add_argument('--host-interface', dest='host_eth', default='vethsh') options.add_argument('--host-ip', dest='host_ip', default='192.168.100.101') options.add_argument('--guest-interface', dest='guest_eth', default='vethsg')
def passwd(): options.add_argument('usr', help='username') options.add_argument('pwd', help='password') opts = setup(logging.INFO) u=User.filter(UserStruct(login=opts.usr))[0] logging.info("Changing password for user %s"%(u.name,))