def parse_arguments(): global short_mode short_mode = True optlist, args = getopt.gnu_getopt(sys.argv[1:], 'l:v') for flag, arg in optlist: if flag == '-l': sys.stdout.dumb_taps.append(open(arg, 'w')) if flag == '-v': short_mode = False last_dir = None while not os.path.exists('.box'): head, tail = os.getcwd(), None while not tail: head, tail = os.path.split(head) last_dir = tail os.chdir('..') if os.getcwd() == '/': ui.show_message('Error', 'Could not find .box/ directory.', ERROR) ui.usage() if last_dir: args.append(last_dir) try: cmd = args.pop(0) except IndexError: ui.usage() if args: targets = [arg.rstrip('/') for arg in args] else: targets = [directory.rstrip('/') for directory in glob('*/')] targets = sorted(targets) return cmd, targets
def build_binary(src_fn): u"Build the binary for `src_fn`." ui.start_task(src_fn) exe_fn = change_extension(src_fn, 'exe') # get compilers from environment gcc = os.getenv('CC') if not gcc: gcc = 'gcc' gpp = os.getenv('CXX') if not gpp: gpp = 'g++' if src_fn.endswith('.c'): cmd_line = '%s -O2 -lm -std=c99 -o %s %s' % (gcc, exe_fn, src_fn) elif src_fn.endswith('.cpp'): cmd_line = '%s -std=c++11 -O2 -o %s %s' % (gpp, exe_fn, src_fn) elif src_fn.endswith('.pas'): cmd_line = 'fpc -O2 -Tlinux -o%s %s' % (exe_fn, src_fn) elif src_fn.endswith('.java'): exe_fn = change_extension(src_fn, 'exe') cmd_line = ('javac %s && ln -sf `pwd`/.box/run_java.sh %s && ' 'chmod a+x %s' % (src_fn, exe_fn, exe_fn)) elif src_fn.endswith('.py'): exe_fn = change_extension(src_fn, 'exe') cmd_line = 'ln -sf `pwd`/%s %s && chmod a+x %s' % (src_fn, exe_fn, exe_fn) else: ui.show_message('Error', 'Unknown extension for source file.', ERROR) ui.end_task('failed') if subprocess.call(cmd_line, shell=True) == 0: ui.end_task('OK') else: ui.end_task('failed')
def box_build_output(name): u"Build output file for problem `name`." global short_mode if short_mode: OK_msg, failed_msg, short = '.', 'X', 'short' else: OK_msg, failed_msg, short = 'OK', 'failed', None if reference_solution(name) is None: ui.task_header(name, 'Generating problem output...') ui.show_message('Error', 'No good solutions available.', ERROR) hints.give_hint('output-good-solutions') return False else: ui.task_header( name, 'Generating problem output with %s...' % os.path.basename(reference_solution(name))) for input_fn in input_files(name): output_fn = change_extension(input_fn, 'sol') if not short_mode: ui.start_task(output_fn) if build_one_output(name, input_fn): ui.end_task(OK_msg, OK, short) else: ui.end_task(failed_msg, ERROR, short) if short: print return True
def box_build_pdf(name): u"Compile PDFs for all documents of problem `name`." ui.task_header(name, 'Compiling documents...') if not program_available('pdflatex'): ui.show_message('Error', 'Could not find `pdflatex`.', ERROR) hints.give_hint('pdflatex') return for tex_fn in glob('%s/documents/*.tex' % name): pdf_fn = change_extension(tex_fn, 'pdf') build_pdf(pdf_fn, tex_fn)
def handle_user(stdscr): while True: credentials = get_login_credentials(stdscr) email = credentials[0] password = credentials[1] try: return login_user(email, password) except NoSuchUserException: try: username = get_user_input( stdscr, 'You are creating a new account. Choose your new username') return register_user(username, email, password) except mongoengine.errors.ValidationError: show_message(stdscr, "{} is not a correct email address".format(email)) continue except IncorrectPasswordException: show_message(stdscr, "Incorrect password") continue
def run_checker(checker_fn): u"Run a single checker program against the input file." ui.task_header( name, 'Validating input with %s...' % os.path.basename(checker_fn)) if not os.access(checker_fn, os.X_OK): ui.show_message('Warning', '%s is not an executable.' % checker_fn, WARNING) return for input_fn in sample_input_files(name) + input_files(name): if not short_mode: ui.start_task(input_fn) with open(input_fn, 'r') as input_file: with open('/dev/null', 'w') as dev_null: if subprocess.call(checker_fn, stdin=input_file, stdout=dev_null, stderr=dev_null) != 0: ui.end_task(failed_msg, ERROR, short) else: ui.end_task(OK_msg, OK, short)
def box_check_output(name): u"Check the output file for problem `name`." global short_mode def run_checker(checker_fn): u"Run a single checker program against the input file." ui.task_header( name, 'Validating output with %s...' % os.path.basename(checker_fn)) if not os.access(checker_fn, os.X_OK): ui.show_message('Warning', '%s is not an executable.' % checker_fn, WARNING) return for output_fn in sample_output_files(name) + output_files(name): if not short_mode: ui.start_task(output_fn) with open(output_fn, 'r') as output_file: with open('/dev/null', 'w') as dev_null: if subprocess.call(checker_fn, stdin=output_file, stdout=dev_null, stderr=dev_null) != 0: ui.end_task(failed_msg, ERROR, short) else: ui.end_task(OK_msg, OK, short) if short_mode: OK_msg, failed_msg, short = '.', 'X', 'short' else: OK_msg, failed_msg, short = 'OK', 'failed', None ui.task_header(name, 'Building checkers...') build_binaries('%s/checkers/' % name) output_checkers = glob('%s/checkers/*output*.*exe' % name) if not output_checkers: ui.show_message('Warning', 'No output checkers available.', WARNING) return for checker_fn in output_checkers: run_checker(checker_fn) if short: print
def show_hints(): for hint in useful_hints: ui.show_message('Hint', HINTS[hint])
while True: try: sport_choice=int(raw_input()) break except ValueError: logger.debug("Asking for valid sport choice") print "Supply a valid value." while True: match_list=fetch.list_matches(sport_choice) n=0 for item in match_list: print "%d. %s"%(n+1,item) n+=1 while True: try: match_choice=int(raw_input()) break except ValueError: logger.debug("Asking for a valid sport choice") print "Supply a valid value." while True: ui.show_message(fetch.match_details(match_choice-1)) sleep(SLEEP)
def box_check_solutions(name, sol_groups, do_time=False, do_sample=False): u"Check all solutions for problem `name`." global short_mode def check_solution_language(fsolname): with open(fsolname, 'r') as fsol: first_line = fsol.readline() if first_line.find('#!') == 0: if first_line.find('python') > 0: t = 'python' else: t = 'java' else: t = 'c/c++' return t def run_check(name, sol_group, ctime_limit=100, jtime_limit=100, ptime_limit=100): for sol_fn, expected in solutions(name, sol_group): cur_cref_solutions_time,cur_jref_solutions_time,cur_pref_solutions_time=[],[],[] cur_cref_solutions_totaltime,cur_jref_solutions_totaltime,cur_pref_solutions_totaltime=[],[],[] solution_language = check_solution_language(sol_fn) ui.task_header( name, 'Testing %s [%s,%s] on REAL input...' % (os.path.basename(sol_fn), sol_group, solution_language)) basename, extension = os.path.splitext(sol_fn) mem_limit = 1024 file_limit = 1024 if solution_language == 'java': time_limit = jtime_limit mem_limit = 2048 elif solution_language == 'python': time_limit = ptime_limit else: time_limit = ctime_limit for input_fn in input_files(name): if not short_mode: ui.start_task(input_fn) result = run_solution(sol_fn, input_fn, reference_fn=change_extension( input_fn, 'sol'), time_limit=time_limit, mem_limit=mem_limit, file_limit=file_limit) if result.status == 'RE': ui.end_task(exec_msg, ERROR, short) #ui.end_task(result.detail, ERROR, short) elif result.status == 'TLE': color = [WARNING, OK][expected == result.status] ui.end_task(timeout_msg, color, short) elif result.status == 'WA': color = [ERROR, OK][expected == 'WA'] if do_time: ui.end_task('%4.2fs ' % result.running_time, color, short) else: ui.end_task(wrong_msg, color, short) elif result.status == 'AC': color = [ERROR, OK][expected == 'AC'] if do_time: ui.end_task('%4.2fs ' % result.running_time, color, short) else: ui.end_task(OK_msg, color, short) else: assert False if expected == 'AC': if solution_language == 'java': cur_jref_solutions_time.append(result.running_time) elif solution_language == 'python': cur_pref_solutions_time.append(result.running_time) else: # lang is c/c++ cur_cref_solutions_time.append(result.running_time) if len(cur_cref_solutions_time) != 0: cref_solutions_maxtime.append(max(cur_cref_solutions_time)) cref_solutions_mintime.append(min(cur_cref_solutions_time)) cref_solutions_totaltime.append(sum(cur_cref_solutions_time)) print ' Total time: %.2fs (worst case: %.2fs)' % ( sum(cur_cref_solutions_time), max(cur_cref_solutions_time)) if len(cur_jref_solutions_time) != 0: jref_solutions_maxtime.append(max(cur_jref_solutions_time)) jref_solutions_mintime.append(min(cur_jref_solutions_time)) jref_solutions_totaltime.append(sum(cur_jref_solutions_time)) print ' Total time: %.2fs (worst case: %.2fs)' % ( sum(cur_jref_solutions_time), max(cur_jref_solutions_time)) if len(cur_pref_solutions_time) != 0: pref_solutions_maxtime.append(max(cur_pref_solutions_time)) pref_solutions_mintime.append(min(cur_pref_solutions_time)) pref_solutions_totaltime.append(sum(cur_pref_solutions_time)) print ' Total time: %.2fs (worst case: %.2fs)' % ( sum(cur_pref_solutions_time), max(cur_pref_solutions_time)) return if short_mode: OK_msg, timeout_msg, wrong_msg, exec_msg, short = '.', 'T', 'W', 'X', 'short' else: OK_msg, timeout_msg, wrong_msg, exec_msg, short = 'OK', 'timeout', 'wrong', 'failed', None # problem specs, to be read or calculated # using this only for the sample jtime_limit = 60 ctime_limit = 60 ptime_limit = 60 if do_sample: for sol_fn, expected in solutions(name, "good"): solution_language = check_solution_language(sol_fn) ui.task_header( name, 'Testing %s [%s,%s] on sample input...' % (os.path.basename(sol_fn), "good", solution_language)) for input_fn in sample_input_files(name): if not short_mode: ui.start_task(input_fn) basename, extension = os.path.splitext(sol_fn) mem_limit = 1024 file_limit = 1024 if solution_language == 'java': time_limit = jtime_limit mem_limit = 2048 elif solution_language == 'python': time_limit = ptime_limit else: time_limit = ctime_limit result = run_solution(sol_fn, input_fn, reference_fn=change_extension( input_fn, 'sol'), time_limit=time_limit, mem_limit=mem_limit, file_limit=file_limit) if result.status == 'RE': #ui.end_task(exec_msg, ERROR, short) ui.end_task(result.detail, ERROR, short) elif result.status == 'TLE': color = [WARNING, OK][expected == result.status] ui.end_task(timeout_msg, color, short) elif result.status == 'WA': color = [ERROR, OK][expected == 'WA'] ui.end_task(wrong_msg, color, short) hints.give_hint('solution-wrong-sample') elif result.status == 'AC': color = [ERROR, OK][expected == 'AC'] ui.end_task(OK_msg, color, short) else: assert False cref_solutions_maxtime,cref_solutions_mintime,cref_solutions_totaltime=[],[],[] jref_solutions_maxtime,jref_solutions_mintime,jref_solutions_totaltime=[],[],[] pref_solutions_maxtime,pref_solutions_mintime,pref_solutions_totaltime=[],[],[] if "good" in sol_groups: # will calculate time limits run_check(name, 'good') if short_mode: print if len(cref_solutions_maxtime) == 0: # no C solutions print >> sys.stderr, "Must have at least one C solution to calculate time limits!" ctime_limit = timelimit(cref_solutions_maxtime, C_LANG) if len(jref_solutions_maxtime) == 0: # no Java solutions jtime_limit = timelimit(cref_solutions_maxtime, JAVA_LANG) else: jtime_limit = timelimit(jref_solutions_maxtime, JAVA_LANG) if len(pref_solutions_maxtime) == 0: # no Python solutions ptime_limit = timelimit(cref_solutions_maxtime, PYTHON_LANG) else: ptime_limit = timelimit(pref_solutions_maxtime, PYTHON_LANG) # write time limits in file attic/problem.desc try: os.mkdir('%s/attic' % name) except: pass with open('%s/attic/problem.desc' % name, 'w') as desc_fn: desc_fn.write( 'time_limit_c=%.2f\ntime_limit_java=%.2f\ntime_limit_python=%.2f\n' % (ctime_limit, jtime_limit, ptime_limit)) desc_fn.write( "C=(%s,%s,%s)\n" % (str(cref_solutions_mintime), str(cref_solutions_maxtime), str(cref_solutions_totaltime))) desc_fn.write( "Java=(%s,%s,%s)\n" % (str(jref_solutions_mintime), str(jref_solutions_maxtime), str(jref_solutions_totaltime))) desc_fn.write( "Python=(%s,%s,%s)\n" % (str(pref_solutions_mintime), str(pref_solutions_maxtime), str(pref_solutions_totaltime))) box_report(name) # read time_limits from file attic/problem.desc if "good" not in sol_groups if not os.path.exists('%s/attic/problem.desc' % name): ui.show_message( 'Error', 'Please run check-solutions-good before to calculate time limits.', ERROR) return context = {} execfile(os.path.join(name, 'attic', 'problem.desc'), context) ct, jt, pt = context['time_limit_c'], context['time_limit_java'], context[ 'time_limit_python'] if ct == 60 and jt == 60 and pt == 60: ui.show_message( 'Error', 'Please run check-solutions-good before to calculate time limits.', ERROR) return if "pass" in sol_groups: run_check(name, 'pass', ct, jt, pt) if short_mode: print if "wrong" in sol_groups: run_check(name, 'wrong', ct, jt, pt) if short_mode: print if "slow" in sol_groups: run_check(name, 'slow', ct, jt, pt) if short_mode: print