Skip to content

Commit

Permalink
Update the error message of download subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
kmyk committed Mar 14, 2021
1 parent 920a827 commit 60eedb5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
3 changes: 2 additions & 1 deletion onlinejudge_command/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ def run_program(args: argparse.Namespace, parser: argparse.ArgumentParser) -> in

# TODO: make functions for subcommand take a named tuple instead of the raw result of argparse. Using named tuples make code well-typed.
if args.subcommand in ['download', 'd', 'dl']:
subcommand_download.run(args)
if not subcommand_download.run(args):
return 1
elif args.subcommand in ['login', 'l']:
if not subcommand_login.run(args):
return 1
Expand Down
26 changes: 19 additions & 7 deletions onlinejudge_command/subcommand/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def convert_sample_to_dict(sample: TestCase) -> Dict[str, str]:
return data


def run(args: argparse.Namespace) -> None:
def run(args: argparse.Namespace) -> bool:
# prepare values
problem = dispatch.problem_from_url(args.url)
if problem is None:
Expand All @@ -92,13 +92,21 @@ def run(args: argparse.Namespace) -> None:
with utils.new_session_with_our_user_agent(path=args.cookie) as sess:
if args.yukicoder_token and isinstance(problem, YukicoderProblem):
sess.headers['Authorization'] = 'Bearer {}'.format(args.yukicoder_token)
if args.system:
samples = problem.download_system_cases(session=sess)
else:
samples = problem.download_sample_cases(session=sess)
try:
if args.system:
samples = problem.download_system_cases(session=sess)
else:
samples = problem.download_sample_cases(session=sess)
except requests.exceptions.HTTPError:
logger.exception('Failed to download test cases')
return False
except SampleParseError:
logger.exception('Failed to download test cases')
return False

if not samples:
raise SampleParseError("Sample not found")
logger.error("Sample not found")
return False

# append the history for submit subcommand
if not args.dry_run and is_default_format:
Expand Down Expand Up @@ -127,7 +135,9 @@ def iterate_files_to_write(sample: TestCase, *, i: int) -> Iterator[Tuple[str, p
for i, sample in enumerate(samples):
for _, path, _ in iterate_files_to_write(sample, i=i):
if path.exists():
raise FileExistsError('Failed to download since file already exists: ' + str(path))
logger.error('Failed to download since file already exists: %s', str(path))
logger.info(utils.HINT + 'We recommend adding your own test cases to test/ directory, and using one directory per one problem. Please see also https://github.com/online-judge-tools/oj/blob/master/docs/getting-started.md#random-testing. If you wanted to keep using one directory per one contest, you can run like `$ rm -rf test/ && oj d https://...`.')
return False

# write samples to files
for i, sample in enumerate(samples):
Expand All @@ -147,3 +157,5 @@ def iterate_files_to_write(sample: TestCase, *, i: int) -> Iterator[Tuple[str, p
if args.log_file:
with args.log_file.open(mode='w') as fhs:
json.dump(list(map(convert_sample_to_dict, samples)), fhs)

return True

0 comments on commit 60eedb5

Please sign in to comment.