Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
def get_directory(*, problem: onlinejudge.type.Problem,
                  contest: Optional[onlinejudge.type.Contest],
                  config: Dict[str, Any]) -> pathlib.Path:
    # prepare params
    service = problem.get_service()

    for name in ('contest_id', 'contest_slug'):
        contest_id = getattr(problem, name, None)
        if contest_id:
            break
    else:
        contest_id = ''

    for name in ('problem_id', 'problem_slug', 'problem_no', 'task_id',
                 'task_slug', 'task_no', 'alphabet', 'index'):
        problem_id = getattr(problem, name, None)
        if problem_id:
            break
    else:
        problem_id, = urllib.parse.urlparse(
            problem.get_url()).path.lstrip('/').replace('/', '-'),

    params = {
        'service_name': service.get_name(),
        'service_domain': urllib.parse.urlparse(service.get_url()).netloc,
        'contest_id': contest_id,
        'problem_id': problem_id,
    }

    # generate the path
    problem_pattern = config.get('problem_directory', '.')
    if 'problem_directory' not in config:
        logger.info(
            'setting "problem_directory" is not found in your config; use %s',
            repr(problem_pattern))
    problem_directory = pathlib.Path(
        problem_pattern.format(**params)).expanduser()
    if contest is None:
        return problem_directory

    contest_pattern = config.get('contest_directory', '{problem_id}')
    if 'contest_directory' not in config:
        logger.info(
            'setting "contest_directory" is not found in your config; use %s',
            repr(contest_pattern))
    contest_directory = pathlib.Path(
        contest_pattern.format(**params)).expanduser()
    return contest_directory / problem_directory
 def add(self, problem: onlinejudge.type.Problem, directory: pathlib.Path = pathlib.Path.cwd()) -> None:
     self.path.parent.mkdir(parents=True, exist_ok=True)
     with open(str(self.path), 'a') as fh:
         fh.write(json.dumps({
             'timestamp': int(time.time()),  # this should not be int, but Python's strptime is too weak and datetime.fromisoformat is from 3.7
             'directory': str(directory),
             'url': problem.get_url(),
         }) + '\n')
     log.status('append history to: %s', self.path)
     self._flush()
Ejemplo n.º 4
0
def get_directory(*, problem: onlinejudge.type.Problem,
                  contest: Optional[onlinejudge.type.Contest]) -> pathlib.Path:
    # prepare params
    service = problem.get_service()

    for name in ('contest_id', 'contest_slug'):
        contest_id = getattr(problem, name, None)
        if contest_id:
            break
    else:
        contest_id = ''

    for name in ('problem_id', 'problem_slug', 'problem_no', 'task_id',
                 'task_slug', 'task_no', 'alphabet', 'index'):
        problem_id = getattr(problem, name, None)
        if problem_id:
            break
    else:
        problem_id, = urllib.parse.urlparse(
            problem.get_url()).path.lstrip('/').replace('/', '-'),

    params = {
        'service_name': service.get_name(),
        'service_domain': urllib.parse.urlparse(service.get_url()).netloc,
        'contest_id': contest_id,
        'problem_id': problem_id,
    }

    # generate the path
    problem_pattern = "{problem_id}"  #config.get('problem_directory', '.')
    problem_directory = pathlib.Path(
        problem_pattern.format(**params)).expanduser()
    if contest is None:
        return problem_directory

    contest_pattern = "./Contests/{service_name}_{contest_id}"
    contest_directory = pathlib.Path(
        contest_pattern.format(**params)).expanduser()
    return contest_directory / problem_directory
Ejemplo n.º 5
0
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)