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 match_with_format(directory, format, path): table = {} table["s"] = "(?P<name>.+)" table["e"] = "(?P<ext>in|out)" pattern = re.compile("^" + utils.parcentformat(format, table) + "$") path = os.path.normpath(os.path.relpath(path, directory)) return pattern.match(path)
def match_with_format(directory: str, format: str, path: str) -> Optional[Match[str]]: table = {} table['s'] = '(?P<name>.+)' table['e'] = '(?P<ext>in|out)' pattern = re.compile('^' + utils.parcentformat(format, table) + '$') path = os.path.normpath(os.path.relpath(path, directory)) return pattern.match(path)
def glob_with_format(format): table = {} table['s'] = '*' table['e'] = '*' pattern = utils.parcentformat(format, table) paths = glob.glob(pattern) for path in paths: log.debug('testcase globbed: %s', path) return paths
def glob_with_format(directory: str, format: str) -> List[str]: table = {} table['s'] = '*' table['e'] = '*' pattern = os.path.join(directory, utils.parcentformat(format, table)) paths = glob.glob(pattern) for path in paths: log.debug('testcase globbed: %s', path) return paths
def download(args: 'argparse.Namespace') -> None: # prepare values problem = onlinejudge.dispatch.problem_from_url(args.url) if problem is None: sys.exit(1) kwargs = {} if args.system: supported_service_names = ['aoj', 'yukicoder'] if problem.get_service().get_name() not in supported_service_names: log.error('--system for %s is not supported', problem.get_service().get_name()) sys.exit(1) kwargs['is_system'] = True if args.format is None: if kwargs.get('is_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: samples = problem.download(session=sess, **kwargs) # type: ignore # write samples to files for i, sample in enumerate(samples): log.emit('') log.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 = os.path.join(args.directory, utils.parcentformat(args.format, table)) log.status('%sput: %s', ext, name) log.emit(colorama.Style.BRIGHT + data.rstrip() + colorama.Style.RESET_ALL) if args.dry_run: continue if os.path.exists(path): log.warning('file already exists: %s', path) if not args.overwrite: log.warning('skipped') continue os.makedirs(os.path.dirname(path), exist_ok=True) with open(path, 'w') as fh: fh.write(data) log.success('saved to: %s', path)
def download(args): # prepare values problem = onlinejudge.dispatch.problem_from_url(args.url) if problem is None: sys.exit(1) kwargs = {} if args.system: supported_service_names = ["aoj", "yukicoder"] if problem.get_service().get_name() not in supported_service_names: log.error("--system for %s is not supported", problem.get_service().get_name()) sys.exit(1) kwargs["is_system"] = True if args.format is None: if kwargs.get("is_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: samples = problem.download(session=sess, **kwargs) # write samples to files for i, sample in enumerate(samples): log.emit("") log.info("sample %d", i) for kind in ["input", "output"]: ext = kind[:-3] data = sample[kind]["data"] name = 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 = os.path.join(args.directory, utils.parcentformat(args.format, table)) log.status("%sput: %s", ext, name) log.emit(colorama.Style.BRIGHT + data.rstrip() + colorama.Style.RESET_ALL) if args.dry_run: continue if os.path.exists(path): log.warning("file already exists: %s", path) if not args.overwrite: log.warning("skipped") continue os.makedirs(os.path.dirname(path), exist_ok=True) with open(path, "w", encoding="utf-8") as fh: fh.write(data) log.success("saved to: %s", path)
def glob_with_format(directory, format): table = {} table["s"] = "*" table["e"] = "*" pattern = os.path.join(directory, utils.parcentformat(format, table)) paths = glob.glob(pattern) for path in paths: log.debug("testcase globbed: %s", path) return paths
def download(args): problem = onlinejudge.dispatch.problem_from_url(args.url) if problem is None: sys.exit(1) kwargs = {} if problem.get_service().get_name() == 'yukicoder': for x in args.extra_option: if x == 'all': kwargs['is_all'] = True if args.format is None: if problem.get_service().get_name() == 'yukicoder' and kwargs.get( 'is_all'): args.format = 'test/%b.%e' else: args.format = 'test/sample-%i.%e' with utils.session(cookiejar=args.cookie) as sess: samples = problem.download(session=sess, **kwargs) for i, sample in enumerate(samples): log.emit('') log.info('sample %d', i) for ext, (s, name) in zip(['in', 'out'], sample): 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 = utils.parcentformat(args.format, table) log.status('%sput: %s', ext, name) log.emit(colorama.Style.BRIGHT + s.rstrip() + colorama.Style.RESET_ALL) if args.dry_run: continue if os.path.exists(path): log.warning('file already exists: %s', path) if not args.overwrite: log.warning('skipped') continue os.makedirs(os.path.dirname(path), exist_ok=True) with open(path, 'w') as fh: fh.write(s) log.success('saved to: %s', path)
def path_from_format(format, name, ext): table = {} table['s'] = name table['e'] = ext return utils.parcentformat(format, table)
def match_with_format(format, path): table = {} table['s'] = '(?P<name>.+)' table['e'] = '(?P<ext>in|out)' pattern = re.compile('^' + utils.parcentformat(format, table) + '$') return pattern.match(os.path.normpath(path))
def path_from_format(directory: str, format: str, name: str, ext: str) -> str: table = {} table['s'] = name table['e'] = ext return os.path.join(directory, utils.parcentformat(format, table))
def path_from_format(directory, format, name, ext): table = {} table["s"] = name table["e"] = ext return os.path.join(directory, utils.parcentformat(format, table))