def execute(self, line): args = self.arg_parser.parse_args(line) id, lang, src, tl, ml = self.get_submission_data(args.submission_id) id = args.problem or id lang = args.language or lang tl = args.time_limit or tl ml = args.memory_limit or ml if id not in map(itemgetter(0), judgeenv.get_supported_problems()): raise InvalidCommandException("unknown problem '%s'" % id) elif lang not in executors: raise InvalidCommandException("unknown language '%s'" % lang) elif tl <= 0: raise InvalidCommandException('--time-limit must be >= 0') elif ml <= 0: raise InvalidCommandException('--memory-limit must be >= 0') src = self.open_editor(lang, src) self.judge.submission_id_counter += 1 self.judge.graded_submissions.append((id, lang, src, tl, ml)) self.judge.begin_grading(self.judge.submission_id_counter, id, lang, src, tl, ml, False, {}, blocking=True)
def execute(self, line: str) -> None: args = self.arg_parser.parse_args(line) problem_id, lang, src, tl, ml = self.get_submission_data( args.submission_id) problem_id = args.problem or problem_id lang = args.language or lang tl = args.time_limit or tl ml = args.memory_limit or ml if id not in judgeenv.get_supported_problems(): raise InvalidCommandException(f"unknown problem '{problem_id}'") elif lang not in executors: raise InvalidCommandException(f"unknown language '{lang}'") elif tl <= 0: raise InvalidCommandException('--time-limit must be >= 0') elif ml <= 0: raise InvalidCommandException('--memory-limit must be >= 0') src = self.open_editor(lang, src) self.judge.submission_id_counter += 1 self.judge.graded_submissions.append((problem_id, lang, src, tl, ml)) try: self.judge.begin_grading( Submission(self.judge.submission_id_counter, problem_id, lang, src, tl, ml, False, {}), blocking=True, report=print, ) except KeyboardInterrupt: self.judge.abort_grading()
def open_editor(self, lang: str, src: str = '') -> str: file_suffix = '.' + executors[lang].Executor.ext editor = os.environ.get('EDITOR') if editor: with tempfile.NamedTemporaryFile(mode='w+', suffix=file_suffix) as temp: temp.write(src) temp.flush() subprocess.call([editor, temp.name]) temp.seek(0) src = temp.read() else: print_ansi( '#ansi[$EDITOR not set, falling back to stdin](yellow)\n') lines = [] try: while True: s = input() if s.strip() == ':q': raise EOFError lines.append(s) except EOFError: # Ctrl+D src = '\n'.join(lines) except Exception as io: raise InvalidCommandException(str(io)) return src
def execute(self, line: str) -> None: args = self.arg_parser.parse_args(line) problem_id: str = args.problem_id language_id: Optional[str] = args.language_id time_limit: float = args.time_limit memory_limit: int = args.memory_limit source_file: Optional[str] = args.source_file if language_id not in executors.executors: source_file = language_id language_id = None # source file / language id optional if problem_id not in judgeenv.get_supported_problems(): raise InvalidCommandException(f"unknown problem '{problem_id}'") elif not language_id: if source_file: language_id = executors.from_filename( source_file).Executor.name else: raise InvalidCommandException('no language is selected') elif language_id not in executors.executors: raise InvalidCommandException(f"unknown language '{language_id}'") elif time_limit <= 0: raise InvalidCommandException('--time-limit must be >= 0') elif memory_limit <= 0: raise InvalidCommandException('--memory-limit must be >= 0') assert language_id is not None src = self.get_source( source_file) if source_file else self.open_editor(language_id) self.judge.submission_id_counter += 1 self.judge.graded_submissions.append( (problem_id, language_id, src, time_limit, memory_limit)) try: self.judge.begin_grading( Submission(self.judge.submission_id_counter, problem_id, language_id, src, time_limit, memory_limit, False, {}), blocking=True, report=print, ) except KeyboardInterrupt: self.judge.abort_grading()
def get_submission_data(self, submission_id: int) -> GradedSubmission: # don't wrap around if submission_id > 0: try: return self.judge.graded_submissions[submission_id - 1] except IndexError: pass raise InvalidCommandException(f"invalid submission '{submission_id}'")
def get_submission_data(self, submission_id): # don't wrap around if submission_id > 0: try: return self.judge.graded_submissions[submission_id - 1] except IndexError: pass raise InvalidCommandException("invalid submission '%d'" % submission_id)
def execute(self, line): args = self.arg_parser.parse_args(line) if args.limit is not None and args.limit <= 0: raise InvalidCommandException("--limit must be >= 0") submissions = self.judge.graded_submissions if not args.limit else self.judge.graded_submissions[:args.limit] for i, (problem, lang, src, tl, ml) in enumerate(submissions): print_ansi('#ansi[%s](yellow)/#ansi[%s](green) in %s' % (problem, i + 1, lang)) print()
def execute(self, line): _args = self.arg_parser.parse_args(line) if _args.limit is not None and _args.limit <= 0: raise InvalidCommandException('--limit must be >= 0') all_problems = judgeenv.get_supported_problems() if _args.filter: r = re.compile(_args.filter) all_problems = list( filter(lambda p: r.match(p) is not None, all_problems)) if _args.limit: all_problems = all_problems[:_args.limit] if len(all_problems): max_len = max(len(p) for p in all_problems) for row in zip_longest(*[iter(all_problems)] * 4, fillvalue=''): print(' '.join(('%*s' % (-max_len, row[i])) for i in range(4))) print() else: raise InvalidCommandException('No problems matching filter found.')
def execute(self, line: str) -> None: args = self.arg_parser.parse_args(line) if args.limit is not None and args.limit <= 0: raise InvalidCommandException('--limit must be >= 0') submissions = self.judge.graded_submissions if not args.limit else self.judge.graded_submissions[:args . limit] for i, (problem, lang, src, tl, ml) in enumerate(submissions): print_ansi( f'#ansi[{problem}](yellow)/#ansi[{i + 1}](green) in {lang}') print()
def execute(self, line): args = self.arg_parser.parse_args(line) problem_id = args.problem_id language_id = args.language_id time_limit = args.time_limit memory_limit = args.memory_limit source_file = args.source_file if language_id not in executors: source_file = language_id language_id = None # source file / language id optional if problem_id not in map(itemgetter(0), judgeenv.get_supported_problems()): raise InvalidCommandException("unknown problem '%s'" % problem_id) elif not language_id: if source_file: filename, dot, ext = source_file.partition('.') if not ext: raise InvalidCommandException('invalid file name') else: # TODO: this should be a proper lookup elsewhere ext = ext.upper() language_id = { 'PY': 'PY2', 'CPP': 'CPP11', 'JAVA': 'JAVA8' }.get(ext, ext) else: raise InvalidCommandException("no language is selected") elif language_id not in executors: raise InvalidCommandException("unknown language '%s'" % language_id) elif time_limit <= 0: raise InvalidCommandException('--time-limit must be >= 0') elif memory_limit <= 0: raise InvalidCommandException('--memory-limit must be >= 0') src = self.get_source( source_file) if source_file else self.open_editor(language_id) self.judge.submission_id_counter += 1 self.judge.graded_submissions.append( (problem_id, language_id, src, time_limit, memory_limit)) self.judge.begin_grading( self.judge.submission_id_counter, problem_id, language_id, src, time_limit, memory_limit, False, {}, blocking=True, report=print, )
def execute(self, line: str) -> None: args = self.arg_parser.parse_args(line) problem_id: str = args.problem_id language_id: Optional[str] = args.language_id time_limit: float = args.time_limit memory_limit: int = args.memory_limit source_file: Optional[str] = args.source_file if language_id not in executors: source_file = language_id language_id = None # source file / language id optional if problem_id not in judgeenv.get_supported_problems(): raise InvalidCommandException(f"unknown problem '{problem_id}'") elif not language_id: if source_file: filename, dot, ext = source_file.partition('.') if not ext: raise InvalidCommandException('invalid file name') else: # TODO: this should be a proper lookup elsewhere ext = ext.upper() language_id = { 'PY': 'PY2', 'CPP': 'CPP11', 'JAVA': 'JAVA8' }.get(ext, ext) else: raise InvalidCommandException('no language is selected') elif language_id not in executors: raise InvalidCommandException(f"unknown language '{language_id}'") elif time_limit <= 0: raise InvalidCommandException('--time-limit must be >= 0') elif memory_limit <= 0: raise InvalidCommandException('--memory-limit must be >= 0') assert language_id is not None src = self.get_source( source_file) if source_file else self.open_editor(language_id) self.judge.submission_id_counter += 1 self.judge.graded_submissions.append( (problem_id, language_id, src, time_limit, memory_limit)) try: self.judge.begin_grading( Submission(self.judge.submission_id_counter, problem_id, language_id, src, time_limit, memory_limit, False, {}), blocking=True, report=print, ) except KeyboardInterrupt: self.judge.abort_grading()
def execute(self, line): args = self.arg_parser.parse_args(line) problem_ids = args.problem_ids supported_problems = set(get_supported_problems()) unknown_problems = ', '.join( map( lambda x: "'%s'" % x, filter(lambda problem_id: problem_id not in supported_problems, problem_ids))) if unknown_problems: raise InvalidCommandException("unknown problem(s) %s" % unknown_problems) tester = ProblemTester() total_fails = 0 for problem_id in problem_ids: fails = tester.test_problem(problem_id) if fails: print_ansi( 'Problem #ansi[%s](cyan|bold) #ansi[failed %d case(s)](red|bold).' % (problem_id, fails)) else: print_ansi( 'Problem #ansi[%s](cyan|bold) passed with flying colours.' % problem_id) print() total_fails += fails print() print('Test complete.') if fails: print_ansi('#ansi[A total of %d test(s) failed](red|bold)' % fails) else: print_ansi('#ansi[All tests passed.](green|bold)') return fails
def execute(self, line: str) -> int: args = self.arg_parser.parse_args(line) problem_ids = args.problem_ids supported_problems = set(get_supported_problems()) unknown_problems = ', '.join(f"'{i}'" for i in problem_ids if i not in supported_problems) if unknown_problems: raise InvalidCommandException( f'unknown problem(s) {unknown_problems}') tester = ProblemTester() total_fails = 0 for problem_id in problem_ids: fails = tester.run_problem_tests(problem_id) if fails: print_ansi( f'Problem #ansi[{problem_id}](cyan|bold) #ansi[failed {fails} case(s)](red|bold).' ) else: print_ansi( f'Problem #ansi[{problem_id}](cyan|bold) passed with flying colours.' ) print() total_fails += fails print() print('Test complete.') if total_fails: print_ansi( f'#ansi[A total of {total_fails} test(s) failed](red|bold)') else: print_ansi('#ansi[All tests passed.](green|bold)') return total_fails
def get_source(self, source_file: str) -> str: try: with open(os.path.realpath(source_file)) as f: return f.read() except Exception as io: raise InvalidCommandException(str(io))