def split_input(args): with open(args.input) as fh: inf = fh.read() if args.footer == split_input_auto_footer: args.footer = inf.splitlines(keepends=True)[-1] with subprocess.Popen(args.command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=sys.stderr) as proc: index = 0 acc = '' for line in inf.splitlines(keepends=True): if args.ignore: args.ignore -= 1 else: acc += line proc.stdin.write(line.encode()) proc.stdin.flush() time.sleep(args.time) if non_block_read(proc.stdout): # if output exists index += 1 path = utils.parcentformat(args.output, {'i': str(index)}) log.info('case found: %d', index) if args.header: acc = args.header + acc if args.footer: acc = acc + args.footer log.emit(log.bold(acc)) with open(path, 'w') as fh: fh.write(acc) log.success('saved to: %s', path) acc = '' while non_block_read(proc.stdout): # consume all pass
def generate_output(args): if not args.test: args.test = glob_with_format(args.format) # by default if args.ignore_backup: args.test = drop_backup_or_hidden_files(args.test) tests = construct_relationship_of_files(args.test, args.format) for name, it in sorted(tests.items()): log.emit('') log.info('%s', name) if 'out' in it: log.info('output file already exists.') log.info('skipped.') continue with open(it['in']) as inf: begin = time.perf_counter() answer, proc = utils.exec_command(args.command, shell=args.shell, stdin=inf) end = time.perf_counter() log.status('time: %f sec', end - begin) if proc.returncode != 0: log.failure(log.red('RE') + ': return code %d', proc.returncode) log.info('skipped.') continue log.emit(log.bold(answer.decode().rstrip())) path = path_from_format(args.format, match_with_format(args.format, it['in']).groupdict()['name'], 'out') with open(path, 'w') as fh: fh.buffer.write(answer) log.success('saved to: %s', path)
def generate_scanner(args): if not args.silent: log.warning("This feature is " + log.red("experimental") + ".") if args.silent: for handler in log.logger.handlers: log.removeHandler(handler) problem = onlinejudge.dispatch.problem_from_url(args.url) if problem is None: sys.exit(1) with utils.with_cookiejar(utils.new_default_session(), path=args.cookie) as sess: it = problem.get_input_format(session=sess) if not it: log.error("input format not found") sys.exit(1) try: log.debug("original data: %s", repr(it)) it = list(tokenize(it)) log.debug("tokenized: %s", str(it)) it = list(parse(it)) log.debug("parsed: %s", str(it)) it = postprocess(it) log.debug("postprocessed: %s", str(it)) it = export(it, use_scanf=args.scanf, repeat_macro=args.repeat_macro) log.debug("result: %s", repr(it)) except: log.error("something wrong") raise log.success("success:") print(log.bold(it.rstrip())) # to stdout
def generate_output(args: 'argparse.Namespace') -> None: if not args.test: args.test = cutils.glob_with_format(args.directory, args.format) # by default if args.ignore_backup: args.test = cutils.drop_backup_or_hidden_files(args.test) tests = cutils.construct_relationship_of_files(args.test, args.directory, args.format) for name, it in sorted(tests.items()): log.emit('') log.info('%s', name) if 'out' in it: log.info('output file already exists.') log.info('skipped.') continue with it['in'].open() as inf: begin = time.perf_counter() answer, proc = utils.exec_command(args.command, shell=True, stdin=inf) end = time.perf_counter() log.status('time: %f sec', end - begin) if proc.returncode != 0: log.failure(log.red('RE') + ': return code %d', proc.returncode) log.info('skipped.') continue log.emit(log.bold(answer.decode().rstrip())) match_result = cutils.match_with_format(args.directory, args.format, it['in']) # type: Optional[Match[Any]] if match_result is not None: matched_name = match_result.groupdict()['name'] # type: str else: assert False path = cutils.path_from_format(args.directory, args.format, name=matched_name, ext='out') if not path.parent.is_dir(): os.makedirs(str(path.parent), exist_ok=True) with path.open('wb') as fh: fh.write(answer) log.success('saved to: %s', path)
def generate_scanner(args: 'argparse.Namespace') -> None: if not args.silent: log.warning('This feature is ' + log.red('experimental') + '.') if args.silent: for handler in log.logger.handlers: log.removeHandler(handler) problem = onlinejudge.dispatch.problem_from_url(args.url) if problem is None: sys.exit(1) with utils.with_cookiejar(utils.new_default_session(), path=args.cookie) as sess: it = problem.get_input_format(session=sess) # type: Any if not it: log.error('input format not found') sys.exit(1) try: log.debug('original data: %s', repr(it)) it = list(tokenize(it)) log.debug('tokenized: %s', str(it)) it = list(parse(it)) log.debug('parsed: %s', str(it)) it = postprocess(it) log.debug('postprocessed: %s', str(it)) it = export(it, use_scanf=args.scanf, repeat_macro=args.repeat_macro) log.debug('result: %s', repr(it)) except: log.error('something wrong') raise log.success('success:') print(log.bold(it.rstrip())) # to stdout
def submit(args): problem = onlinejudge.dispatch.problem_from_url(args.url) if problem is None: sys.exit(1) # code with open(args.file) as fh: code = fh.buffer.read() try: s = code.decode() # for logging except UnicodeDecodeError as e: log.failure('%s: %s', e.__class__.__name__, str(e)) s = repr(code)[1:] log.info('code:') log.emit(log.bold(s)) # session with utils.session(cookiejar=args.cookie) as sess: # language langs = problem.get_language_dict(session=sess) if args.language not in langs: log.error('language is unknown') log.info('supported languages are:') for lang in sorted(langs.keys()): log.emit('%s (%s)', lang, langs[lang]['description']) sys.exit(1) # confirm if args.wait: log.status('sleep(%.2f)', args.wait) time.sleep(args.wait) if not args.yes: sys.stdout.write('Are you sure? [y/N] ') sys.stdout.flush() c = sys.stdin.read(1) if c != 'y': log.info('terminated.') return # submit url = problem.submit(code, language=args.language, session=sess) if url and args.open: if not isinstance(args.open, str): args.open = None for browser in default_url_opener: args.open = shutil.which(browser) if args.open: break if not args.open: log.failure('couldn\'t open the url. please specify a browser') else: log.info('open the submission page with: %s', args.open) subprocess.check_call([args.open, url], stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr)
def generate_scanner(args): if not args.silent: log.warning('This feature is ' + log.red('experimental') + '.') if args.silent: for handler in log.logger.handlers: log.removeHandler(handler) problem = onlinejudge.dispatch.problem_from_url(args.url) if problem is None: sys.exit(1) with utils.session(cookiejar=args.cookie) as sess: it = problem.get_input_format(session=sess) if not it: log.error('input format not found') sys.exit(1) try: it = tokenize(it) it = parse(it) it = export(it, use_scanf=args.scanf, repeat_macro=args.repeat_macro) except: log.error('somethin wrong') raise log.success('success:') print(log.bold(it.rstrip())) # to stdout
def generate_output(args): if not args.test: args.test = cutils.glob_with_format(args.directory, args.format) # by default if args.ignore_backup: args.test = cutils.drop_backup_or_hidden_files(args.test) tests = cutils.construct_relationship_of_files(args.test, args.directory, args.format) for name, it in sorted(tests.items()): log.emit("") log.info("%s", name) if "out" in it: log.info("output file already exists.") log.info("skipped.") continue with open(it["in"]) as inf: begin = time.perf_counter() answer, proc = utils.exec_command(args.command, shell=True, stdin=inf) end = time.perf_counter() log.status("time: %f sec", end - begin) if proc.returncode != 0: log.failure(log.red("RE") + ": return code %d", proc.returncode) log.info("skipped.") continue log.emit(log.bold(answer.decode().rstrip())) path = cutils.path_from_format( args.directory, args.format, name=cutils.match_with_format(args.directory, args.format, it["in"]).groupdict()["name"], ext="out", ) with open(path, "w") as fh: fh.buffer.write(answer) log.success("saved to: %s", path)
def print_input(): nonlocal is_printed_input if not is_printed_input: is_printed_input = True with open(it['in']) as inf: log.emit('input:\n%s', log.bold(inf.read()))
def test(args): # prepare if not args.test: args.test = cutils.glob_with_format(args.directory, args.format) # by default if args.ignore_backup: args.test = cutils.drop_backup_or_hidden_files(args.test) tests = cutils.construct_relationship_of_files(args.test, args.directory, args.format) if args.error: # float mode match = lambda a, b: compare_as_floats(a, b, args.error) else: def match(a, b): if a == b: return True if args.rstrip and a.rstrip(rstrip_targets) == b.rstrip( rstrip_targets): log.warning("WA if no rstrip") return True return False rstrip_targets = " \t\r\n\f\v\0" # ruby's one, follow AnarchyGolf slowest, slowest_name = -1, "" ac_count = 0 for name, it in sorted(tests.items()): is_printed_input = not args.print_input def print_input(): nonlocal is_printed_input if not is_printed_input: is_printed_input = True with open(it["in"]) as inf: log.emit("input:\n%s", log.bold(inf.read())) log.emit("") log.info("%s", name) # run the binary with open(it["in"]) as inf: begin = time.perf_counter() answer, proc = utils.exec_command(args.command, shell=True, stdin=inf, timeout=args.tle) end = time.perf_counter() answer = answer.decode() if slowest < end - begin: slowest = end - begin slowest_name = name log.status("time: %f sec", end - begin) proc.terminate() # check TLE, RE or not result = "AC" if proc.returncode is None: log.failure(log.red("TLE")) result = "TLE" print_input() elif proc.returncode != 0: log.failure(log.red("RE") + ": return code %d", proc.returncode) result = "RE" print_input() # check WA or not if "out" in it: with open(it["out"]) as outf: correct = outf.read() # compare if args.mode == "all": if not match(answer, correct): log.failure(log.red("WA")) print_input() if not args.silent: log.emit("output:\n%s", log.bold(answer)) log.emit("expected:\n%s", log.bold(correct)) result = "WA" elif args.mode == "line": answer = answer.splitlines() correct = correct.splitlines() for i, (x, y) in enumerate( zip(answer + [None] * len(correct), correct + [None] * len(answer))): if x is None and y is None: break elif x is None: print_input() log.failure( log.red("WA") + ': line %d: line is nothing: expected "%s"', i + 1, log.bold(y), ) result = "WA" elif y is None: print_input() log.failure( log.red("WA") + ': line %d: unexpected line: output "%s"', i + 1, log.bold(x), ) result = "WA" elif not match(x, y): print_input() log.failure( log.red("WA") + ': line %d: output "%s": expected "%s"', i + 1, log.bold(x), log.bold(y), ) result = "WA" else: assert False else: if not args.silent: log.emit(log.bold(answer)) if result == "AC": log.success(log.green("AC")) ac_count += 1 # summarize log.emit("") log.status("slowest: %f sec (for %s)", slowest, slowest_name) if ac_count == len(tests): log.success("test " + log.green("success") + ": %d cases", len(tests)) else: log.failure("test " + log.red("failed") + ": %d AC / %d cases", ac_count, len(tests)) sys.exit(1)
def submit(args): # parse url problem = onlinejudge.dispatch.problem_from_url(args.url) if problem is None: sys.exit(1) # read code with open(args.file) as fh: code = fh.buffer.read() try: s = code.decode() # for logging except UnicodeDecodeError as e: log.failure('%s: %s', e.__class__.__name__, str(e)) s = repr(code)[1:] log.info('code:') log.emit(log.bold(s)) # prepare kwargs kwargs = {} if problem.get_service().get_name() == 'topcoder': if args.full_submission: kwargs['kind'] = 'full' else: kwargs['kind'] = 'example' # make session if problem.get_service().get_name() == 'topcoder': sess = utils.run_webdriver(args.webdriver, target_url=problem.get_service().get_url(), headless=not args.verbose, cookie_path=args.cookie) else: sess = utils.with_cookiejar(utils.new_default_session(), path=args.cookie) with sess as sess: # language langs = problem.get_language_dict(session=sess) if args.language not in langs: log.error('language is unknown') log.info('supported languages are:') for lang in sorted(langs.keys()): log.emit('%s (%s)', lang, langs[lang]['description']) sys.exit(1) # confirm if args.wait: log.status('sleep(%.2f)', args.wait) time.sleep(args.wait) if not args.yes: sys.stdout.write('Are you sure? [y/N] ') sys.stdout.flush() c = sys.stdin.read(1) if c != 'y': log.info('terminated.') return # submit submission = problem.submit(code, language=args.language, session=sess, **kwargs) # show result if submission is None: log.failure('submission failed') else: if args.open: if not isinstance(args.open, str): args.open = None for browser in default_url_opener: args.open = shutil.which(browser) if args.open: break if not args.open: log.failure( 'couldn\'t open the url. please specify a browser') else: log.info('open the submission page with: %s', args.open) subprocess.check_call( [args.open, submission.get_url()], stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr)
def submit(args: 'argparse.Namespace') -> None: # guess url history = onlinejudge.implementation.download_history.DownloadHistory() if args.file.parent.resolve() == pathlib.Path.cwd(): guessed_urls = history.get() else: log.warning( 'cannot guess URL since the given file is not in the current directory' ) guessed_urls = [] if args.url is None: if len(guessed_urls) == 1: args.url = guessed_urls[0] log.info('guessed problem: %s', args.url) else: log.error('failed to guess the URL to submit') log.info('please manually specify URL as: $ oj submit URL FILE') sys.exit(1) # parse url problem = onlinejudge.dispatch.problem_from_url(args.url) if problem is None: sys.exit(1) # read code with args.file.open('rb') as fh: code = fh.read() # type: bytes format_config = { 'dos2unix': args.format_dos2unix or args.golf, 'rstrip': args.format_dos2unix or args.golf, } code = format_code(code, **format_config) # report code try: s = code.decode() except UnicodeDecodeError as e: log.failure('%s: %s', e.__class__.__name__, str(e)) s = repr(code)[1:] log.info('code (%d byte):', len(code)) lines = s.splitlines(keepends=True) if len(lines) < 30: log.emit(log.bold(s)) else: log.emit(log.bold(''.join(lines[:10]))) log.emit('... (%s lines) ...', len(lines[10:-10])) log.emit(log.bold(''.join(lines[-10:]))) with utils.with_cookiejar(utils.new_default_session(), path=args.cookie) as sess: # guess or select language ids langs = problem.get_language_dict(session=sess) matched_lang_ids = None # type: Optional[List[str]] if args.language in langs: matched_lang_ids = [args.language] else: if args.guess: kwargs = { 'language_dict': langs, 'cxx_latest': args.guess_cxx_latest, 'cxx_compiler': args.guess_cxx_compiler, 'python_version': args.guess_python_version, 'python_interpreter': args.guess_python_interpreter, } matched_lang_ids = guess_lang_ids_of_file( args.file, code, **kwargs) if not matched_lang_ids: log.info('failed to guess languages from the file name') matched_lang_ids = list(langs.keys()) if args.language is not None: log.info( 'you can use `--no-guess` option if you want to do an unusual submission' ) matched_lang_ids = select_ids_of_matched_languages( args.language.split(), matched_lang_ids, language_dict=langs) else: if args.language is None: matched_lang_ids = None else: matched_lang_ids = select_ids_of_matched_languages( args.language.split(), list(langs.keys()), language_dict=langs) # report selected language ids if matched_lang_ids is not None and len(matched_lang_ids) == 1: args.language = matched_lang_ids[0] log.info('chosen language: %s (%s)', args.language, langs[args.language]['description']) else: if matched_lang_ids is None: log.error('language is unknown') log.info('supported languages are:') elif len(matched_lang_ids) == 0: log.error('no languages are matched') log.info('supported languages are:') else: log.error('Matched languages were not narrowed down to one.') log.info('You have to choose:') for lang_id in sorted(matched_lang_ids or langs.keys()): log.emit('%s (%s)', lang_id, langs[lang_id]['description']) sys.exit(1) # confirm guessed_unmatch = ([problem.get_url()] != guessed_urls) if guessed_unmatch: samples_text = ('samples of "{}'.format('", "'.join(guessed_urls)) if guessed_urls else 'no samples') log.warning( 'the problem "%s" is specified to submit, but %s were downloaded in this directory. this may be mis-operation', problem.get_url(), samples_text) if args.wait: log.status('sleep(%.2f)', args.wait) time.sleep(args.wait) if not args.yes: if guessed_unmatch: problem_id = problem.get_url().rstrip('/').split( '/')[-1].split('?')[-1] # this is too ad-hoc key = problem_id[:3] + (problem_id[-1] if len(problem_id) >= 4 else '') sys.stdout.write('Are you sure? Please type "{}" '.format(key)) sys.stdout.flush() c = sys.stdin.readline().rstrip() if c != key: log.info('terminated.') return else: sys.stdout.write('Are you sure? [y/N] ') sys.stdout.flush() c = sys.stdin.read(1) if c.lower() != 'y': log.info('terminated.') return # submit kwargs = {} if problem.get_service().get_name() == 'topcoder': if args.full_submission: kwargs['kind'] = 'full' else: kwargs['kind'] = 'example' try: submission = problem.submit_code(code, language=args.language, session=sess, **kwargs) # type: ignore except onlinejudge.type.SubmissionError: log.failure('submission failed') sys.exit(1) # show result if args.open: if args.open_browser: browser = args.open_browser else: for browser in default_url_opener: if shutil.which(browser): break else: browser = None log.failure( 'couldn\'t find browsers to open the url. please specify a browser' ) if browser: log.status('open the submission page with: %s', browser) subprocess.check_call([browser, submission.get_url()], stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr)
def submit(args: 'argparse.Namespace') -> None: # parse url problem = onlinejudge.dispatch.problem_from_url(args.url) if problem is None: sys.exit(1) # read code with open(args.file) as fh: code = fh.buffer.read() format_config = { 'dos2unix': args.format_dos2unix or args.golf, 'rstrip': args.format_dos2unix or args.golf, } code = format_code(code, **format_config) # report code try: s = code.decode() except UnicodeDecodeError as e: log.failure('%s: %s', e.__class__.__name__, str(e)) s = repr(code)[1:] log.info('code (%d byte):', len(code)) log.emit(log.bold(s)) with utils.with_cookiejar(utils.new_default_session(), path=args.cookie) as sess: # guess or select language ids langs = problem.get_language_dict(session=sess) matched_lang_ids: Optional[List[str]] = None if args.guess: kwargs = { 'language_dict': langs, 'cxx_latest': args.guess_cxx_latest, 'cxx_compiler': args.guess_cxx_compiler, 'python_version': args.guess_python_version, 'python_interpreter': args.guess_python_interpreter, } matched_lang_ids = guess_lang_ids_of_file(args.file, code, **kwargs) if args.language is not None: matched_lang_ids = select_ids_of_matched_languages( args.language.split(), matched_lang_ids, language_dict=langs) else: if args.language is None: matched_lang_ids = None elif args.language in langs: matched_lang_ids = [args.language] else: matched_lang_ids = select_ids_of_matched_languages( args.language.split(), list(langs.keys()), language_dict=langs) # report selected language ids if matched_lang_ids is not None and len(matched_lang_ids) == 1: args.language = matched_lang_ids[0] log.info('choosed language: %s (%s)', args.language, langs[args.language]['description']) else: if matched_lang_ids is None: log.error('language is unknown') log.info('supported languages are:') elif len(matched_lang_ids) == 0: log.error('no languages are matched') log.info('supported languages are:') else: log.error('Matched languages were not narrowed down to one.') log.info('You have to choose:') for lang_id in sorted(matched_lang_ids or langs.keys()): log.emit('%s (%s)', lang_id, langs[lang_id]['description']) sys.exit(1) # confirm if args.wait: log.status('sleep(%.2f)', args.wait) time.sleep(args.wait) if not args.yes: sys.stdout.write('Are you sure? [y/N] ') sys.stdout.flush() c = sys.stdin.read(1) if c != 'y': log.info('terminated.') return # submit kwargs = {} if problem.get_service().get_name() == 'topcoder': if args.full_submission: kwargs['kind'] = 'full' else: kwargs['kind'] = 'example' submission = problem.submit(code, language=args.language, session=sess, **kwargs) # type: ignore # show result if submission is None: log.failure('submission failed') else: if args.open: if args.open_browser: browser = args.open_browser else: for browser in default_url_opener: if shutil.which(browser): break else: browser = None log.failure( 'couldn\'t find browsers to open the url. please specify a browser' ) if browser: log.info('open the submission page with: %s', browser) subprocess.check_call( [browser, submission.get_url()], stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr)
def submit(args): # parse url problem = onlinejudge.dispatch.problem_from_url(args.url) if problem is None: sys.exit(1) # read code with open(args.file) as fh: code = fh.buffer.read() format_config = { "dos2unix": args.format_dos2unix or args.golf, "rstrip": args.format_dos2unix or args.golf, } code = format_code(code, **format_config) # report code try: s = code.decode() except UnicodeDecodeError as e: log.failure("%s: %s", e.__class__.__name__, str(e)) s = repr(code)[1:] log.info("code (%d byte):", len(code)) log.emit(log.bold(s)) with utils.with_cookiejar(utils.new_default_session(), path=args.cookie) as sess: # guess or select language ids langs = problem.get_language_dict(session=sess) if args.guess: kwargs = { "language_dict": langs, "cxx_latest": args.guess_cxx_latest, "cxx_compiler": args.guess_cxx_compiler, "python_version": args.guess_python_version, "python_interpreter": args.guess_python_interpreter, } matched_lang_ids = guess_lang_ids_of_file(args.file, code, **kwargs) if args.language is not None: matched_lang_ids = select_ids_of_matched_languages( args.language.split(), matched_lang_ids, language_dict=langs) else: if args.language is None: matched_lang_ids = None elif args.language in langs: matched_lang_ids = [args.language] else: matched_lang_ids = select_ids_of_matched_languages( args.language.split(), langs.keys(), language_dict=langs) # report selected language ids if matched_lang_ids is not None and len(matched_lang_ids) == 1: args.language = matched_lang_ids[0] log.info( "choosed language: %s (%s)", args.language, langs[args.language]["description"], ) else: if matched_lang_ids is None: log.error("language is unknown") log.info("supported languages are:") elif len(matched_lang_ids) == 0: log.error("no languages are matched") log.info("supported languages are:") else: log.error("Matched languages were not narrowed down to one.") log.info("You have to choose:") for lang_id in sorted(matched_lang_ids or langs.keys()): log.emit("%s (%s)", lang_id, langs[lang_id]["description"]) sys.exit(1) # confirm if args.wait: log.status("sleep(%.2f)", args.wait) time.sleep(args.wait) if not args.yes: sys.stdout.write("Are you sure? [y/N] ") sys.stdout.flush() c = sys.stdin.read(1) if c != "y": log.info("terminated.") return # submit kwargs = {} if problem.get_service().get_name() == "topcoder": if args.full_submission: kwargs["kind"] = "full" else: kwargs["kind"] = "example" submission = problem.submit(code, language=args.language, session=sess, **kwargs) # show result if submission is None: log.failure("submission failed") else: if args.open: if args.open_browser: browser = args.open_browser else: for browser in default_url_opener: if shutil.which(browser): break else: browser = None log.failure( "couldn't find browsers to open the url. please specify a browser" ) if browser: log.info("open the submission page with: %s", browser) subprocess.check_call( [browser, submission.get_url()], stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr, )
def test(args): # prepare if not args.test: args.test = glob_with_format(args.format) # by default if args.ignore_backup: args.test = drop_backup_or_hidden_files(args.test) tests = construct_relationship_of_files(args.test, args.format) if args.error: # float mode match = lambda a, b: compare_as_floats(a, b, args.error) else: def match(a, b): if a == b: return True if args.rstrip and a.rstrip(rstrip_targets) == b.rstrip(rstrip_targets): log.warning('WA if no rstrip') return True return False rstrip_targets = ' \t\r\n\f\v\0' # ruby's one, follow AnarchyGolf slowest, slowest_name = -1, '' ac_count = 0 for name, it in sorted(tests.items()): is_printed_input = not args.print_input def print_input(): nonlocal is_printed_input if not is_printed_input: is_printed_input = True with open(it['in']) as inf: log.emit('input:\n%s', log.bold(inf.read())) log.emit('') log.info('%s', name) # run the binary with open(it['in']) as inf: begin = time.perf_counter() answer, proc = utils.exec_command(args.command, shell=args.shell, stdin=inf, timeout=args.tle) end = time.perf_counter() answer = answer.decode() if slowest < end - begin: slowest = end - begin slowest_name = name log.status('time: %f sec', end - begin) proc.terminate() # check TLE, RE or not result = 'AC' if proc.returncode is None: log.failure(log.red('TLE')) result = 'TLE' print_input() elif proc.returncode != 0: log.failure(log.red('RE') + ': return code %d', proc.returncode) result = 'RE' print_input() # check WA or not if 'out' in it: with open(it['out']) as outf: correct = outf.read() # compare if args.mode == 'all': if not match(answer, correct): log.failure(log.red('WA')) print_input() if not args.silent: log.emit('output:\n%s', log.bold(answer)) log.emit('expected:\n%s', log.bold(correct)) result = 'WA' elif args.mode == 'line': answer = answer .splitlines() correct = correct.splitlines() for i, (x, y) in enumerate(zip(answer + [ None ] * len(correct), correct + [ None ] * len(answer))): if x is None and y is None: break elif x is None: print_input() log.failure(log.red('WA') + ': line %d: line is nothing: expected "%s"', i, log.bold(y)) result = 'WA' elif y is None: print_input() log.failure(log.red('WA') + ': line %d: unexpected line: output "%s"', i, log.bold(x)) result = 'WA' elif not match(x, y): print_input() log.failure(log.red('WA') + ': line %d: output "%s": expected "%s"', i, log.bold(x), log.bold(y)) result = 'WA' else: assert False else: if not args.silent: log.emit(log.bold(answer)) if result == 'AC': log.success(log.green('AC')) ac_count += 1 # summarize log.emit('') log.status('slowest: %f sec (for %s)', slowest, slowest_name) if ac_count == len(tests): log.success('test ' + log.green('success') + ': %d cases', len(tests)) else: log.failure('test ' + log.red('failed') + ': %d AC / %d cases', ac_count, len(tests)) sys.exit(1)
def test(args: 'argparse.Namespace') -> None: # prepare if not args.test: args.test = cutils.glob_with_format(args.directory, args.format) # by default if args.ignore_backup: args.test = cutils.drop_backup_or_hidden_files(args.test) # 自分で書き換えた箇所 f = open('onlinejudge/communication.py', 'r') com_prob = f.read().split()[1] f.close() path = com_prob.upper() + '/test' args.test = [] for p in os.listdir(path): args.test.append(pathlib.Path(com_prob.upper() + '/test/' + p)) tests = cutils.construct_relationship_of_files(args.test, args.directory, args.format) if args.error: # float mode match = lambda a, b: compare_as_floats(a, b, args.error) else: def match(a, b): # 自分で書き換えた箇所 a = a.replace(chr(13), '') b = b.replace(chr(13), '') if a == b: return 'AC' if args.rstrip and a.rstrip(rstrip_targets) == b.rstrip(rstrip_targets): log.faster_warning('WA if no rstrip') return 'AC' # 自分で書き換えた箇所 # 小数出力の誤差許容 def make_outlist(s): outlist = [] tmp_token = '' for i in range(len(s)): if s[i] == ' ' or s[i] == '\n' or i == len(s)-1: if tmp_token != '': outlist.append(tmp_token) tmp_token = '' continue else: tmp_token += s[i] return outlist outlist_a = make_outlist(a) outlist_b = make_outlist(b) if len(outlist_a) == len(outlist_b): flg = False for i in range(len(outlist_a)): def isnum(s): ''' 小数点およびマイナスも含めて数字であるか ''' try: int(s) except: return re.fullmatch(r'[-]?[0-9]+[.][0-9]+', s) != None return True if isnum(outlist_a[i]) and isnum(outlist_b[i]): # b が correct だと想定 if '.' in outlist_b[i]: flg = True permissible_error = float('1e-6') if not (abs(float(outlist_a[i]) - float(outlist_b[i])) <= permissible_error or abs(float(outlist_a[i]) - float(outlist_b[i])) <= abs(float(outlist_b[i])) * permissible_error): return 'WA' else: return 'WA' return ['AC_with_error', flg] return 'WA' rstrip_targets = ' \t\r\n\f\v\0' # ruby's one, follow AnarchyGolf slowest = -1 # type: Union[int, float] slowest_name = '' ac_count = 0 ac_with_error_count = 0 error_permission = False history = [] # type: List[Dict[str, Any]] for name, it in sorted(tests.items()): is_printed_input = not args.print_input def print_input(): nonlocal is_printed_input if not is_printed_input: is_printed_input = True with open(it['in']) as inf: log.faster_emit('input:\n%s', log.bold(inf.read())) log.faster_emit('') log.faster_info('%s', name) # run the binary with it['in'].open() as inf: begin = time.perf_counter() answer_byte, proc = utils.exec_command(args.command, shell=True, stdin=inf, timeout=args.tle) end = time.perf_counter() elapsed = end - begin answer = answer_byte.decode() if slowest < elapsed: slowest = elapsed slowest_name = name log.faster_status('time: %f sec', elapsed) proc.terminate() # check TLE, RE or not result = 'AC' if proc.returncode is None: log.faster_failure(log.red('TLE')) result = 'TLE' print_input() elif proc.returncode != 0: log.faster_failure(log.red('RE') + ': return code %d', proc.returncode) result = 'RE' print_input() # check WA or not if 'out' in it: with it['out'].open() as outf: correct = outf.read() # compare if args.mode == 'all': if match(answer, correct) == 'WA': log.faster_failure(log.red('WA')) print_input() if not args.silent: log.faster_emit('output:\n%s', log.bold(answer)) log.faster_emit('expected:\n%s', log.bold(correct)) result = 'WA' elif match(answer, correct) == ['AC_with_error', True]: print_input() if not args.silent: log.faster_emit('output:\n%s', log.bold(answer)) log.faster_emit('expected:\n%s', log.bold(correct)) result = 'AC_with_error' error_permission = True elif match(answer, correct) == ['AC_with_error', False]: print_input() if not args.silent: log.faster_emit('output:\n%s', log.bold(answer)) log.faster_emit('expected:\n%s', log.bold(correct)) result = 'AC_with_error' elif args.mode == 'line': answer_words = answer .splitlines() correct_words = correct.splitlines() for i, (x, y) in enumerate(zip(answer_words + [ None ] * len(correct_words), correct_words + [ None ] * len(answer_words))): # type: ignore if x is None and y is None: break elif x is None: print_input() log.faster_failure(log.red('WA') + ': line %d: line is nothing: expected "%s"', i + 1, log.bold(y)) result = 'WA' elif y is None: print_input() log.faster_failure(log.red('WA') + ': line %d: unexpected line: output "%s"', i + 1, log.bold(x)) result = 'WA' elif match(x, y) == 'WA': print_input() log.faster_failure(log.red('WA') + ': line %d: output "%s": expected "%s"', i + 1, log.bold(x), log.bold(y)) result = 'WA' else: assert False else: if not args.silent: log.faster_emit(log.bold(answer)) if result == 'AC': log.faster_success(log.green('AC')) ac_count += 1 elif result == 'AC_with_error': log.faster_success(log.green('AC (with error)')) ac_count += 1 ac_with_error_count += 1 # push the result testcase = { 'name': name, 'input': str(it['in'].resolve()), } if 'out' in it: testcase['output'] = str(it['out'].resolve()) history += [ { 'result': result, 'testcase': testcase, 'output': answer, 'exitcode': proc.returncode, 'elapsed': elapsed, } ] # summarize log.faster_emit('') log.faster_status('slowest: %f sec (for %s)', slowest, slowest_name) if ac_count == len(tests): # 自分で書き換えた箇所 if ac_with_error_count > 0: if error_permission == True: log.faster_success('test ' + log.green('success (with error)') + ': %d cases', len(tests)) else: log.faster_failure('test ' + log.green('success (with error)') + ': %d cases', len(tests)) log.faster_error('your answer has error while correct answer has no error') log.faster_warning('please remove error from your answer') f = open('onlinejudge/communication.py', 'r') com_prev = f.readlines() f.close() f = open('onlinejudge/communication.py', 'w') f.writelines([' '.join(com_prev[0].split())] + [' ', 'WA', '\n'] + com_prev[1].split()) f.close() else: log.faster_success('test ' + log.green('success') + ': %d cases', len(tests)) # 自分で書き換えた箇所 f = open('onlinejudge/communication.py', 'r') com_prev = f.readlines() f.close() f = open('onlinejudge/communication.py', 'w') f.writelines([' '.join(com_prev[0].split())] + [' ', 'AC', '\n'] + com_prev[1].split()) f.close() else: log.faster_failure('test ' + log.red('failed') + ': %d AC / %d cases', ac_count, len(tests)) # 自分で書き換えた箇所 f = open('onlinejudge/communication.py', 'r') com_prev = f.readlines() f.close() f = open('onlinejudge/communication.py', 'w') f.writelines([' '.join(com_prev[0].split())] + [' ', 'WA', '\n'] + com_prev[1].split()) f.close() if args.json: print(json.dumps(history)) if ac_count != len(tests): sys.exit(1)
def test(args): if not args.test: args.test = glob_with_format(args.format) # by default tests = construct_relationship_of_files(args.test, args.format) # run tests if args.error: # float mode match = lambda a, b: compare_as_floats(a, b, args.error) else: match = lambda a, b: a == b rstrip_targets = ' \t\r\n\f\v\0' # ruby's one, follow AnarchyGolf slowest, slowest_name = -1, '' ac_count = 0 for name, it in sorted(tests.items()): log.emit('') log.info('%s', name) with open(it['in']) as inf: # run begin = time.perf_counter() answer, proc = utils.exec_command(args.command, shell=args.shell, stdin=inf, timeout=args.tle) end = time.perf_counter() answer = answer.decode() if args.rstrip: answer = answer.rstrip(rstrip_targets) if slowest < end - begin: slowest = end - begin slowest_name = name log.status('time: %f sec', end - begin) # check is_ac = True if proc.returncode is None: log.failure(log.red('TLE')) is_ac = False elif proc.returncode != 0: log.failure(log.red('RE') + ': return code %d', proc.returncode) is_ac = False if 'out' in it: with open(it['out']) as outf: correct = outf.read() if args.rstrip: correct = correct.rstrip(rstrip_targets) # compare if args.mode == 'all': if not match(answer, correct): log.failure(log.red('WA')) if not args.silent: log.emit('output:\n%s', log.bold(answer)) log.emit('expected:\n%s', log.bold(correct)) is_ac = False elif args.mode == 'line': answer = answer .splitlines() correct = correct.splitlines() for i, (x, y) in enumerate(zip(answer + [ None ] * len(correct), correct + [ None ] * len(answer))): if x is None and y is None: break elif x is None: log.failure(log.red('WA') + ': line %d: line is nothing: expected "%s"', i, log.bold(y)) is_ac = False elif y is None: log.failure(log.red('WA') + ': line %d: unexpected line: output "%s"', i, log.bold(x)) is_ac = False elif not match(x, y): log.failure(log.red('WA') + ': line %d: output "%s": expected "%s"', i, log.bold(x), log.bold(y)) is_ac = False else: assert False else: if not args.silent: log.emit(log.bold(answer)) if is_ac: log.success(log.green('AC')) ac_count += 1 # summarize log.emit('') log.status('slowest: %f sec (for %s)', slowest, slowest_name) if ac_count == len(tests): log.success('test ' + log.green('success') + ': %d cases', len(tests)) else: log.failure('test ' + log.red('failed') + ': %d AC / %d cases', ac_count, len(tests)) sys.exit(1)