Skip to content

Commit

Permalink
[export] Make sure that update_problems_yaml uses a single language
Browse files Browse the repository at this point in the history
Also slightly refactor `bt zip` and `bt samplezip` to use the same force_single_language function
  • Loading branch information
mpsijm committed Mar 16, 2024
1 parent ef41bf8 commit 4e276a7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
37 changes: 26 additions & 11 deletions bin/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ def revert():
return revert


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


# Write any .lang.pdf files to .pdf.
def remove_language_suffix(fname, statement_language):
if not statement_language:
Expand All @@ -46,7 +57,10 @@ def remove_language_suffix(fname, statement_language):
return out


def build_samples_zip(problems, statement_language):
def build_samples_zip(problems):
# Add contest PDF for only one language to the zip file
statement_language = force_single_language(problems)

zf = zipfile.ZipFile(
'samples.zip', mode="w", compression=zipfile.ZIP_DEFLATED, allowZip64=False
)
Expand Down Expand Up @@ -101,9 +115,12 @@ def build_samples_zip(problems, statement_language):
print("Wrote zip to samples.zip", file=sys.stderr)


def build_problem_zip(problem, output, statement_language):
def build_problem_zip(problem, output):
"""Make DOMjudge ZIP file for specified problem."""

# Add problem PDF for only one language to the zip file (note that Kattis export does not include PDF)
statement_language = None if config.args.kattis else force_single_language([problem])

deprecated = [ # may be removed at some point.
'domjudge-problem.ini',
]
Expand Down Expand Up @@ -335,25 +352,23 @@ def update_problems_yaml(problems, colors=None):
path = Path('problems.yaml')
data = path.is_file() and read_yaml(path) or []

# DOMjudge does not yet support multilingual problems.yaml files.
statement_language = force_single_language(problems)

change = False
for problem in problems:
found = False

default_language = (
'en'
if 'en' in problem.statement_languages
else next(iter(problem.statement_languages), None)
)
problem_name = (
problem.settings.name and default_language and problem.settings.name[default_language]
)
problem_name = problem.settings.name
if isinstance(problem_name, dict):
problem_name = problem_name[statement_language]

for d in data:
if d['id'] == problem.name:
found = True
if problem_name != d.get('name'):
change = True
d['name'] = problem.settings.name
d['name'] = problem_name

if 'rgb' not in d:
change = True
Expand Down
25 changes: 5 additions & 20 deletions bin/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -828,10 +828,7 @@ def run_parsed_arguments(args):
return

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

export.build_samples_zip(problems, statement_language)
export.build_samples_zip(problems)
return

if action == 'rename_problem':
Expand Down Expand Up @@ -926,19 +923,18 @@ def run_parsed_arguments(args):
config.args = old_args

if not config.args.kattis:
# Make sure that all problems use the same language for the PDFs
export.force_single_language(problems)

success &= latex.build_problem_pdfs(problem)
# Add problem PDF for only one language to the zip file
statement_language = force_single_language(problems)
else:
statement_language = None

if not config.args.force:
success &= problem.validate_data(validate.Mode.INPUT, constraints={})
success &= problem.validate_data(validate.Mode.ANSWER, constraints={})

# Write to problemname.zip, where we strip all non-alphanumeric from the
# problem directory name.
success &= export.build_problem_zip(problem, output, statement_language)
success &= export.build_problem_zip(problem, output)

if len(problems) > 1:
print(file=sys.stderr)
Expand Down Expand Up @@ -990,17 +986,6 @@ 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 4e276a7

Please sign in to comment.