Skip to content

Commit

Permalink
[export] Only add PDFs for a single language to contest zip files
Browse files Browse the repository at this point in the history
  • Loading branch information
mpsijm committed Oct 22, 2023
1 parent 7e09c74 commit 6c2e868
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 29 deletions.
41 changes: 27 additions & 14 deletions bin/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,25 @@ def revert():
return revert


def build_samples_zip(problems):
# Write any .lang.pdf files to .pdf.
def remove_language_suffix(fname, statement_language):
out = Path(fname)
if out.suffixes == ['.' + statement_language, '.pdf']:
out = out.with_suffix('').with_suffix('.pdf')
return out


def build_samples_zip(problems, statement_language):
zf = zipfile.ZipFile(
'samples.zip', mode="w", compression=zipfile.ZIP_DEFLATED, allowZip64=False
)
for fname in glob(Path('.'), 'contest*.pdf'):
for fname in glob(Path('.'), f'contest*.{statement_language}.pdf'):
if Path(fname).is_file():
zf.write(fname, fname, compress_type=zipfile.ZIP_DEFLATED)
zf.write(
fname,
remove_language_suffix(fname, statement_language),
compress_type=zipfile.ZIP_DEFLATED,
)

for problem in problems:
outputdir = Path(problem.label)
Expand Down Expand Up @@ -142,10 +154,7 @@ def build_problem_zip(problem, output, statement_language):
# NOTE: Directories are skipped because ZIP only supports files.
if f.is_file():
out = f.relative_to(problem.path)
# Write any .lang.pdf files to .pdf.
if out.suffixes == ['.' + statement_language, '.pdf']:
out = out.with_suffix('')
out = out.with_suffix('.pdf')
out = remove_language_suffix(out, statement_language)
# For Kattis, prepend the problem shortname to all files.
if config.args.kattis:
out = problem.name / out
Expand Down Expand Up @@ -179,10 +188,10 @@ def build_problem_zip(problem, output, statement_language):


# Assumes the current working directory has: the zipfiles and
# contest*.pdf
# solutions*.pdf
# contest*.{lang}.pdf
# solutions*.{lang}.pdf
# Output is <outfile>
def build_contest_zip(problems, zipfiles, outfile, args):
def build_contest_zip(problems, zipfiles, outfile, statement_language, args):
print("writing ZIP file %s" % outfile, file=sys.stderr)

update_problems_yaml(problems)
Expand All @@ -194,19 +203,23 @@ def build_contest_zip(problems, zipfiles, outfile, args):

# For general zip export, also create pdfs and a samples zip.
if not args.kattis:
build_samples_zip(problems)
build_samples_zip(problems, statement_language)

for fname in (
[
'problems.yaml',
'contest.yaml',
'samples.zip',
]
+ glob(Path('.'), 'contest*.pdf')
+ glob(Path('.'), 'solutions*.pdf')
+ glob(Path('.'), f'contest*.{statement_language}.pdf')
+ glob(Path('.'), f'solutions*.{statement_language}.pdf')
):
if Path(fname).is_file():
zf.write(fname, fname, compress_type=zipfile.ZIP_DEFLATED)
zf.write(
fname,
remove_language_suffix(fname, statement_language),
compress_type=zipfile.ZIP_DEFLATED,
)

# For Kattis export, delete the original zipfiles.
if args.kattis:
Expand Down
5 changes: 4 additions & 1 deletion bin/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,10 @@ def build_contest_pdf(contest, problems, tmpdir, language, solutions=False, web=
return build_latex_pdf(builddir, Path(main_file), language)


def build_contest_pdfs(contest, problems, tmpdir, solutions=False, web=False):
def build_contest_pdfs(contest, problems, tmpdir, lang=None, solutions=False, web=False):
if lang:
return build_contest_pdf(contest, problems, tmpdir, lang, solutions, web)

"""Build contest PDFs for all available languages"""
statement_languages = set.intersection(*(set(p.statement_languages) for p in problems))
if not statement_languages:
Expand Down
46 changes: 32 additions & 14 deletions bin/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,10 @@ def run_parsed_arguments(args):
return

if action == 'samplezip':
export.build_samples_zip(problems)
# Add contest PDF for only one language to the zip file
statement_language = force_single_language(problems)

export.build_samples_zip(problems, statement_language)
return

if action == 'rename_problem':
Expand Down Expand Up @@ -953,14 +956,8 @@ def run_parsed_arguments(args):
success &= problem.validate_format('input_format', constraints={})
success &= problem.validate_format('output_format', constraints={})

"""Build contest PDFs for all available languages"""
if config.args.language:
statement_language = config.args.language
else:
all_languages = set.union(*(set(p.statement_languages) for p in problems))
if len(all_languages) > 1:
fatal('Multiple languages found, please specify one with --language')
statement_language = all_languages.pop()
# Add problem PDF for only one language to the zip file
statement_language = force_single_language(problems)

# Write to problemname.zip, where we strip all non-alphanumeric from the
# problem directory name.
Expand All @@ -987,19 +984,29 @@ def run_parsed_arguments(args):
)

if action in ['zip']:
statement_language = None
if not config.args.kattis:
success &= latex.build_contest_pdfs(contest, problems, tmpdir)
success &= latex.build_contest_pdfs(contest, problems, tmpdir, web=True)
# Add contest/solutions PDF for only one language to the zip file
statement_language = force_single_language(problems)

success &= latex.build_contest_pdfs(contest, problems, tmpdir, statement_language)
success &= latex.build_contest_pdfs(
contest, problems, tmpdir, statement_language, web=True
)
if not config.args.no_solutions:
success &= latex.build_contest_pdfs(contest, problems, tmpdir, solutions=True)
success &= latex.build_contest_pdfs(
contest, problems, tmpdir, solutions=True, web=True
contest, problems, tmpdir, statement_language, solutions=True
)
success &= latex.build_contest_pdfs(
contest, problems, tmpdir, statement_language, solutions=True, web=True
)

outfile = contest + '.zip'
if config.args.kattis:
outfile = contest + '-kattis.zip'
export.build_contest_zip(problems, problem_zips, outfile, config.args)
export.build_contest_zip(
problems, problem_zips, outfile, statement_language, config.args
)
if action in ['update_problems_yaml']:
export.update_problems_yaml(
problems,
Expand All @@ -1010,6 +1017,17 @@ def run_parsed_arguments(args):
sys.exit(1)


def force_single_language(problems):
if config.args.language:
statement_language = config.args.language
else:
all_languages = set.union(*(set(p.statement_languages) for p in problems))
if len(all_languages) > 1:
fatal('Multiple languages found, please specify one with --language')
statement_language = all_languages.pop()
return statement_language


def read_personal_config():
args = {}

Expand Down

0 comments on commit 6c2e868

Please sign in to comment.