def prepare_problem(problem: onlinejudge.type.Problem, *, contest: Optional[onlinejudge.type.Contest] = None, config: Dict[str, Any], session: requests.Session) -> None: table = config.get('templates') if table is None: table = { 'main.cpp': 'main.cpp', 'main.py': 'main.py', 'generate.py': 'generate.py', } logger.info('setting "templates" is not found in your config; use %s', repr(table)) dir = get_directory(problem=problem, contest=contest, config=config) logger.info('use directory: %s', str(dir)) dir.parent.mkdir(parents=True, exist_ok=True) with chdir(dir): url = problem.get_url() html = network.download_html(url, session=session) sample_cases = network.download_sample_cases(url, session=session) # analyze resources = analyzer.prepare_from_html(html, url=url, sample_cases=sample_cases) analyzed = analyzer.run(resources) for dest_str, template in table.items(): dest = pathlib.Path(dest_str) # generate try: code = generator.run(analyzed, template_file=template) except NotImplementedError as e: logger.error('generator failed: %s', e) continue # write dest.parent.mkdir(parents=True, exist_ok=True) if dest.exists(): logger.error('file already exists: %s', str(dest)) else: logger.info('write file: %s', str(dest)) with open(dest, 'wb') as fh: fh.write(code) if code.startswith(b'#!'): os.chmod(dest, os.stat(dest).st_mode | stat.S_IEXEC) # download try: subprocess.check_call( ['oj', 'download', problem.get_url()], stdout=sys.stdout, stderr=sys.stderr) except subprocess.CalledProcessError as e: logger.error('samples downloader failed: %s', e)
def main(args: Optional[List[str]] = None) -> None: parser = argparse.ArgumentParser() parser.add_argument('url') parser.add_argument('-t', '--template', default='main.cpp') parser.add_argument('-v', '--verbose', action='store_true') parser.add_argument('-c', '--cookie', default=onlinejudge.utils.default_cookie_path) parsed = parser.parse_args(args=args) # configure logging handler = colorlog.StreamHandler() handler.setFormatter( colorlog.ColoredFormatter( '%(log_color)s%(levelname)s%(reset)s:%(name)s:%(message)s')) level = INFO if parsed.verbose: level = DEBUG basicConfig(level=level, handlers=[handler]) # download url = parsed.url problem = onlinejudge.dispatch.problem_from_url(url) if problem is not None: url = problem.get_url() # normalize url url = url.replace('judge.yosupo.jp', 'old.yosupo.jp') # TODO: support the new pages logger.debug('url: %s', url) with onlinejudge.utils.with_cookiejar( onlinejudge.utils.get_default_session(), path=parsed.cookie) as session: html = network.download_html(url, session=session) sample_cases = network.download_sample_cases(url, session=session) logger.debug('sample cases: %s', sample_cases) # analyze resources = analyzer.prepare_from_html(html, url=url, sample_cases=sample_cases) logger.debug('analyzer resources: %s', resources._replace(html=b'...skipped...')) analyzed = analyzer.run(resources) logger.debug( 'analyzed result: %s', analyzed._replace(resources=analyzed.resources._replace( html=b'...skipped...'))) # generate code = generator.run(analyzed, template_file=parsed.template) sys.stdout.buffer.write(code)
def test_1395_a(self) -> None: """The answer is `Yes` or `No`. """ url = 'https://codeforces.com/contest/1395/problem/A' expected = { VarName("YES"): ConstantDecl(name=VarName("YES"), value="Yes", type=VarType.String), VarName("NO"): ConstantDecl(name=VarName("NO"), value="No", type=VarType.String), } sample_cases = download_sample_cases(url) assert sample_cases is not None self.assertEqual( analyzer.list_constants_from_sample_cases(sample_cases), expected)
def prepare_problem(problem: onlinejudge.type.Problem, *, contest: Optional[onlinejudge.type.Contest] = None, session: requests.Session) -> None: dir = get_directory(problem=problem, contest=contest) logger.info('use directory: %s', str(dir)) dir.parent.mkdir(parents=True, exist_ok=True) with chdir(dir): url = problem.get_url() html = network.download_html(url, session=session) sample_cases = network.download_sample_cases(url, session=session) dest_str = "main.cpp" dest = pathlib.Path(dest_str) with open("../../../template/template.cpp") as codefile: code = codefile.read().encode('utf-8') # write dest.parent.mkdir(parents=True, exist_ok=True) if dest.exists(): logger.error('file already exists: %s', str(dest)) else: logger.info('write file: %s', str(dest)) with open(dest, 'wb') as fh: fh.write(code) if code.startswith(b'#!'): os.chmod(dest, os.stat(dest).st_mode | stat.S_IEXEC) # download try: subprocess.check_call( ['oj', 'download', problem.get_url()], stdout=sys.stdout, stderr=sys.stderr) except subprocess.CalledProcessError as e: logger.error('samples downloader failed: %s', e)