Exemple #1
0
def split_input(args: 'argparse.Namespace') -> None:
    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, shell=True, 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.percentformat(args.output, { 'i': str(index) })
                log.info('case found: %d', index)
                if args.header:
                    if args.header == args.header.strip():
                        acc = '\n' + acc
                    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
Exemple #2
0
def glob_with_format(directory: pathlib.Path, format: str) -> List[pathlib.Path]:
    table = {}
    table['s'] = '*'
    table['e'] = '*'
    pattern = str(directory / utils.percentformat(format, table))
    paths = list(map(pathlib.Path, glob.glob(pattern)))
    for path in paths:
        log.debug('testcase globbed: %s', path)
    return paths
Exemple #3
0
def match_with_format(directory: pathlib.Path, format: str,
                      path: pathlib.Path) -> Optional[Match[str]]:
    table = {}
    table['s'] = '(?P<name>.+)'
    table['e'] = '(?P<ext>in|out)'
    pattern = re.compile(
        '^' + re.escape(str(directory.resolve())) + '/' +
        utils.percentformat(re.escape(format).replace('\\%', '%'), table) +
        '$')
    return pattern.match(str(path.resolve()))
Exemple #4
0
def glob_with_format(directory: pathlib.Path,
                     format: str) -> List[pathlib.Path]:
    table = {}
    table['s'] = '*'
    table['e'] = '*'
    pattern = (
        glob.escape(str(directory)) + '/' +
        utils.percentformat(glob.escape(format).replace('\\%', '%'), table))
    paths = list(map(pathlib.Path, glob.glob(pattern)))
    for path in paths:
        log.debug('testcase globbed: %s', path)
    return paths
Exemple #5
0
def download(args: 'argparse.Namespace') -> None:
    # prepare values
    problem = onlinejudge.dispatch.problem_from_url(args.url)
    if problem is None:
        sys.exit(1)
    is_default_format = args.format is None and args.directory is None  # must be here since args.directory and args.format are overwritten
    if args.directory is None:
        args.directory = pathlib.Path('test')
    if args.format is None:
        if args.system:
            if problem.get_service().get_name() == 'yukicoder':
                args.format = '%b.%e'
            else:
                args.format = '%i.%e'
        else:
            args.format = 'sample-%i.%e'

    # get samples from the server
    with utils.with_cookiejar(utils.new_default_session(),
                              path=args.cookie) as sess:
        if args.system:
            samples = problem.download_system_cases(
                session=sess)  # type: ignore
        else:
            samples = problem.download_sample_cases(
                session=sess)  # type: ignore

    # append the history for submit command
    if not args.dry_run and is_default_format:
        history = onlinejudge.implementation.download_history.DownloadHistory()
        history.add(problem)

    # write samples to files
    for i, sample in enumerate(samples):
        log.faster_emit('')
        log.faster_info('sample %d', i)
        for kind in ['input', 'output']:
            ext = kind[:-3]
            data = getattr(sample, kind).data
            name = getattr(sample, kind).name
            table = {}
            table['i'] = str(i + 1)
            table['e'] = ext
            table['n'] = name
            table['b'] = os.path.basename(name)
            table['d'] = os.path.dirname(name)
            path = args.directory / utils.percentformat(
                args.format, table)  # type: pathlib.Path
            log.faster_status('%sput: %s', ext, name)
            log.faster_emit(colorama.Style.BRIGHT + data.rstrip() +
                            colorama.Style.RESET_ALL)
            if args.dry_run:
                continue
            if path.exists():
                log.faster_warning('file already exists: %s', path)
                if not args.overwrite:
                    log.faster_warning('skipped')
                    continue
            path.parent.mkdir(parents=True, exist_ok=True)
            with path.open('w') as fh:
                fh.write(data)
            log.faster_success('saved to: %s', path)

    # print json
    if args.json:
        print(json.dumps(list(map(convert_sample_to_dict, samples))))
Exemple #6
0
def path_from_format(directory: pathlib.Path, format: str, name: str, ext: str) -> pathlib.Path:
    table = {}
    table['s'] = name
    table['e'] = ext
    return directory / utils.percentformat(format, table)