rbx.cp 0.5.73__py3-none-any.whl → 0.6.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (86) hide show
  1. rbx/annotations.py +21 -1
  2. rbx/box/cd.py +11 -1
  3. rbx/box/checkers.py +9 -1
  4. rbx/box/cli.py +59 -46
  5. rbx/box/code.py +142 -3
  6. rbx/box/contest/build_contest_statements.py +44 -34
  7. rbx/box/contest/contest_package.py +4 -7
  8. rbx/box/contest/main.py +7 -58
  9. rbx/box/contest/schema.py +52 -8
  10. rbx/box/contest/statements.py +53 -25
  11. rbx/box/creation.py +3 -36
  12. rbx/box/environment.py +21 -9
  13. rbx/box/fields.py +35 -0
  14. rbx/box/lang.py +27 -0
  15. rbx/box/linting.py +26 -0
  16. rbx/box/package.py +4 -35
  17. rbx/box/packaging/boca/packager.py +48 -5
  18. rbx/box/packaging/contest_main.py +13 -0
  19. rbx/box/packaging/main.py +13 -2
  20. rbx/box/packaging/packager.py +4 -4
  21. rbx/box/packaging/pkg/packager.py +142 -0
  22. rbx/box/packaging/polygon/packager.py +2 -24
  23. rbx/box/packaging/polygon/upload.py +35 -17
  24. rbx/box/presets/__init__.py +362 -281
  25. rbx/box/presets/lock_schema.py +1 -2
  26. rbx/box/presets/schema.py +13 -5
  27. rbx/box/remote.py +2 -2
  28. rbx/box/retries.py +8 -0
  29. rbx/box/schema.py +82 -19
  30. rbx/box/solutions.py +77 -15
  31. rbx/box/statements/build_statements.py +44 -27
  32. rbx/box/statements/builders.py +18 -10
  33. rbx/box/statements/expander.py +49 -0
  34. rbx/box/statements/latex_jinja.py +61 -4
  35. rbx/box/statements/schema.py +33 -9
  36. rbx/box/stats.py +92 -0
  37. rbx/box/tasks.py +6 -3
  38. rbx/box/testcase_utils.py +19 -47
  39. rbx/box/tooling/__init__.py +0 -0
  40. rbx/box/tooling/boca/__init__.py +0 -0
  41. rbx/box/tooling/boca/main.py +13 -0
  42. rbx/box/tooling/boca/scrape.py +34 -0
  43. rbx/box/{packaging/boca/upload.py → tooling/boca/scraper.py} +77 -8
  44. rbx/box/tooling/main.py +8 -0
  45. rbx/box/ui/utils/run_ui.py +1 -1
  46. rbx/box/ui/widgets/interaction_box.py +19 -1
  47. rbx/grading/caching.py +18 -2
  48. rbx/grading/judge/sandbox.py +60 -5
  49. rbx/grading/judge/sandboxes/isolate.py +1 -0
  50. rbx/grading/judge/sandboxes/stupid_sandbox.py +11 -5
  51. rbx/grading/judge/sandboxes/timeit.py +36 -15
  52. rbx/grading/processing_context.py +62 -78
  53. rbx/grading/steps.py +92 -40
  54. rbx/resources/packagers/boca/checker.sh +4 -1
  55. rbx/resources/packagers/boca/compile/c +2 -6
  56. rbx/resources/packagers/boca/compile/cc +2 -6
  57. rbx/resources/packagers/boca/compile/cpp +2 -6
  58. rbx/resources/packagers/boca/compile/java +1 -6
  59. rbx/resources/packagers/boca/compile/kt +24 -28
  60. rbx/resources/packagers/boca/compile/py2 +2 -6
  61. rbx/resources/packagers/boca/compile/py3 +2 -6
  62. rbx/resources/packagers/boca/interactive/c +15 -83
  63. rbx/resources/packagers/boca/interactive/cc +15 -83
  64. rbx/resources/packagers/boca/interactive/cpp +15 -83
  65. rbx/resources/packagers/boca/interactive/java +15 -88
  66. rbx/resources/packagers/boca/interactive/kt +15 -88
  67. rbx/resources/packagers/boca/interactive/py2 +15 -88
  68. rbx/resources/packagers/boca/interactive/py3 +15 -88
  69. rbx/resources/packagers/boca/interactor_compile.sh +5 -2
  70. rbx/resources/packagers/boca/interactor_run.sh +174 -0
  71. rbx/resources/packagers/boca/safeexec.c +530 -0
  72. rbx/resources/packagers/boca/safeexec_compile.sh +49 -0
  73. rbx/resources/presets/default/contest/contest.rbx.yml +9 -8
  74. rbx/resources/presets/default/problem/problem.rbx.yml +38 -26
  75. rbx/resources/presets/default/problem/random.txt +3 -1
  76. rbx/resources/presets/default/problem/rbx.h +92 -0
  77. rbx/resources/presets/default/problem/statement/statement.rbx.tex +4 -7
  78. rbx/resources/presets/default/problem/validator.cpp +8 -8
  79. rbx/resources/templates/rbx.h +2 -3
  80. {rbx_cp-0.5.73.dist-info → rbx_cp-0.6.1.dist-info}/METADATA +23 -6
  81. {rbx_cp-0.5.73.dist-info → rbx_cp-0.6.1.dist-info}/RECORD +84 -71
  82. {rbx_cp-0.5.73.dist-info → rbx_cp-0.6.1.dist-info}/WHEEL +1 -1
  83. rbx/resources/packagers/boca/compile/pas +0 -172
  84. rbx/resources/presets/default/problem/statement/projecao.png +0 -0
  85. {rbx_cp-0.5.73.dist-info → rbx_cp-0.6.1.dist-info}/LICENSE +0 -0
  86. {rbx_cp-0.5.73.dist-info → rbx_cp-0.6.1.dist-info}/entry_points.txt +0 -0
rbx/annotations.py CHANGED
@@ -1,7 +1,7 @@
1
1
  import importlib.resources
2
2
  import pathlib
3
3
  import re
4
- from typing import List, Optional
4
+ from typing import Any, Dict, List, Optional
5
5
 
6
6
  import typer
7
7
  import typer.core
@@ -112,6 +112,26 @@ Checker = Annotated[
112
112
  ]
113
113
 
114
114
 
115
+ def parse_dictionary(value: Optional[str]) -> Dict[str, Any]:
116
+ if value is None:
117
+ return {}
118
+ res = {}
119
+ for item in value.split(','):
120
+ key, value = item.split('=', 1)
121
+ res[key] = value
122
+ return res
123
+
124
+
125
+ def parse_dictionary_items(items: Optional[List[str]]) -> Dict[str, Any]:
126
+ if items is None:
127
+ return {}
128
+ res = {}
129
+ for item in items:
130
+ key, value = item.split('=', 1)
131
+ res[key] = value
132
+ return res
133
+
134
+
115
135
  class AliasGroup(typer.core.TyperGroup):
116
136
  _CMD_SPLIT_P = re.compile(r', ?')
117
137
 
rbx/box/cd.py CHANGED
@@ -1,7 +1,7 @@
1
1
  import contextlib
2
2
  import functools
3
3
  import pathlib
4
- from typing import Optional
4
+ from typing import List, Optional
5
5
 
6
6
  import typer
7
7
 
@@ -25,6 +25,16 @@ def find_package(root: pathlib.Path = pathlib.Path()) -> Optional[pathlib.Path]:
25
25
  return root
26
26
 
27
27
 
28
+ def find_all_ancestor_packages(
29
+ root: pathlib.Path = pathlib.Path(),
30
+ ) -> List[pathlib.Path]:
31
+ packages = []
32
+ while (pkg := find_package(root)) is not None:
33
+ packages.append(pkg)
34
+ root = pkg.parent
35
+ return packages
36
+
37
+
28
38
  def is_problem_package(root: pathlib.Path = pathlib.Path()) -> bool:
29
39
  dir = find_package(root)
30
40
  if dir is None:
rbx/box/checkers.py CHANGED
@@ -141,9 +141,16 @@ def _is_checker_exitcode(exitcode: int) -> bool:
141
141
  return exitcode in [0, 1, 2, 3]
142
142
 
143
143
 
144
+ def _get_last_line(message: str) -> str:
145
+ if not message:
146
+ return ''
147
+ return message.strip().split('\n')[-1]
148
+
149
+
144
150
  def process_checker_run_log(
145
151
  checker_run_log: Optional[RunLog], message: str
146
152
  ) -> CheckerResult:
153
+ message = _get_last_line(message)
147
154
  if (
148
155
  checker_run_log is not None
149
156
  and checker_run_log.exitstatus == SandboxBase.EXIT_SANDBOX_ERROR
@@ -352,7 +359,8 @@ async def check_communication(
352
359
  return _extra_check_and_sanitize(result)
353
360
 
354
361
  # Just a defensive pattern to ensure result is not None, should never happen.
355
- result = check_with_no_output(interactor_run_log)
362
+ if result is None:
363
+ result = check_with_no_output(interactor_run_log)
356
364
  if result.outcome != Outcome.ACCEPTED:
357
365
  if result.outcome == Outcome.RUNTIME_ERROR:
358
366
  result.outcome = Outcome.JUDGE_FAILED
rbx/box/cli.py CHANGED
@@ -26,7 +26,7 @@ from rbx.box import (
26
26
  )
27
27
  from rbx.box.contest import main as contest
28
28
  from rbx.box.contest.contest_package import find_contest_yaml
29
- from rbx.box.environment import VerificationLevel, get_environment_path
29
+ from rbx.box.environment import VerificationLevel, get_app_environment_path
30
30
  from rbx.box.header import generate_header
31
31
  from rbx.box.packaging import main as packaging
32
32
  from rbx.box.schema import CodeItem, ExpectedOutcome, TestcaseGroup
@@ -42,6 +42,7 @@ from rbx.box.solutions import (
42
42
  from rbx.box.statements import build_statements
43
43
  from rbx.box.testcase_utils import TestcaseEntry
44
44
  from rbx.box.testcases import main as testcases
45
+ from rbx.box.tooling import main as tooling
45
46
 
46
47
  app = typer.Typer(no_args_is_help=True, cls=annotations.AliasGroup)
47
48
  app.add_typer(
@@ -93,6 +94,13 @@ app.add_typer(
93
94
  help='Manage testcases (sub-command).',
94
95
  rich_help_panel='Management',
95
96
  )
97
+ app.add_typer(
98
+ tooling.app,
99
+ name='tool, tooling',
100
+ cls=annotations.AliasGroup,
101
+ help='Manage tooling (sub-command).',
102
+ rich_help_panel='Misc',
103
+ )
96
104
 
97
105
 
98
106
  @app.callback()
@@ -106,7 +114,8 @@ def main(
106
114
  ),
107
115
  capture: bool = typer.Option(
108
116
  True,
109
- '--capture',
117
+ '--nocapture',
118
+ flag_value=False,
110
119
  help='Whether to save extra logs and outputs from interactive solutions.',
111
120
  ),
112
121
  ):
@@ -324,7 +333,7 @@ async def run(
324
333
  )
325
334
 
326
335
 
327
- async def _time_impl(check: bool, detailed: bool) -> Optional[int]:
336
+ async def _time_impl(check: bool, detailed: bool, runs: int = 0) -> Optional[int]:
328
337
  if package.get_main_solution() is None:
329
338
  console.console.print(
330
339
  '[warning]No main solution found, so cannot estimate a time limit.[/warning]'
@@ -344,6 +353,7 @@ async def _time_impl(check: bool, detailed: bool) -> Optional[int]:
344
353
  check=check,
345
354
  verification=VerificationLevel(verification),
346
355
  timelimit_override=-1, # Unlimited for time limit estimation
356
+ nruns=runs,
347
357
  )
348
358
 
349
359
  console.console.print()
@@ -388,6 +398,12 @@ async def time(
388
398
  '-d',
389
399
  help='Whether to print a detailed view of the tests using tables.',
390
400
  ),
401
+ runs: int = typer.Option(
402
+ 0,
403
+ '--runs',
404
+ '-r',
405
+ help='Number of runs to perform for each solution. Zero means the config default.',
406
+ ),
391
407
  ):
392
408
  main_solution = package.get_main_solution()
393
409
  if check and main_solution is None:
@@ -402,7 +418,7 @@ async def time(
402
418
  if not await builder.build(verification=verification, output=check):
403
419
  return None
404
420
 
405
- await _time_impl(check, detailed)
421
+ await _time_impl(check, detailed, runs)
406
422
 
407
423
 
408
424
  @app.command(
@@ -805,6 +821,7 @@ def header():
805
821
  generate_header()
806
822
 
807
823
 
824
+ # TODO: warn when using a preset (or show it)
808
825
  @app.command(
809
826
  'environment, env',
810
827
  rich_help_panel='Configuration',
@@ -822,15 +839,14 @@ def environment_command(
822
839
  ] = None,
823
840
  ):
824
841
  if env is None:
825
- cfg = config.get_config()
826
- console.console.print(f'Current environment: [item]{cfg.boxEnvironment}[/item]')
827
842
  console.console.print(
828
- f'Location: {environment.get_environment_path(cfg.boxEnvironment)}'
843
+ f'Current environment: [item]{environment.get_active_environment_description()}[/item]'
829
844
  )
845
+ console.console.print(f'Location: {environment.get_active_environment_path()}')
830
846
  return
831
847
  if install_from is not None:
832
848
  environment.install_environment(env, pathlib.Path(install_from))
833
- if not get_environment_path(env).is_file():
849
+ if not get_app_environment_path(env).is_file():
834
850
  console.console.print(
835
851
  f'[error]Environment [item]{env}[/item] does not exist.[/error]'
836
852
  )
@@ -843,7 +859,7 @@ def environment_command(
843
859
  )
844
860
  return
845
861
  console.console.print(
846
- f'Changing box environment from [item]{cfg.boxEnvironment}[/item] to [item]{env}[/item]...'
862
+ f'Changing global environment from [item]{cfg.boxEnvironment}[/item] to [item]{env}[/item]...'
847
863
  )
848
864
  cfg.boxEnvironment = env
849
865
  config.save_config(cfg)
@@ -852,43 +868,6 @@ def environment_command(
852
868
  clear()
853
869
 
854
870
 
855
- @app.command(
856
- 'activate',
857
- rich_help_panel='Configuration',
858
- help='Activate the environment of the current preset used by the package.',
859
- )
860
- @cd.within_closest_package
861
- def activate():
862
- preset_lock = presets.get_preset_lock()
863
- if preset_lock is None:
864
- console.console.print(
865
- '[warning]No configured preset to be activated for this package.[/warning]'
866
- )
867
- raise typer.Exit(1)
868
-
869
- preset = presets.get_installed_preset_or_null(preset_lock.preset_name)
870
- if preset is None:
871
- if preset_lock.uri is None:
872
- console.console.print(
873
- '[error]Preset is not installed. Install it manually, or specify a URI in [item].preset-lock.yml[/item].[/error]'
874
- )
875
- raise typer.Exit(1)
876
- presets.install(preset_lock.uri)
877
-
878
- preset = presets.get_installed_preset(preset_lock.preset_name)
879
-
880
- # Install the environment from the preset if it's not already installed.
881
- presets.optionally_install_environment_from_preset(
882
- preset, root=presets.get_preset_installation_path(preset_lock.name)
883
- )
884
-
885
- # Activate the environment.
886
- if preset.env is not None:
887
- environment_command(preset.name)
888
-
889
- console.console.print(f'[success]Preset [item]{preset.name}[/item] is activated.')
890
-
891
-
892
871
  @app.command(
893
872
  'languages',
894
873
  rich_help_panel='Configuration',
@@ -909,6 +888,40 @@ def languages():
909
888
  console.console.print()
910
889
 
911
890
 
891
+ @app.command(
892
+ 'stats',
893
+ rich_help_panel='Management',
894
+ help='Show stats about current and related packages.',
895
+ )
896
+ @cd.within_closest_package
897
+ def stats(
898
+ transitive: bool = typer.Option(
899
+ False,
900
+ '--transitive',
901
+ '-t',
902
+ help='Show stats about all reachable packages.',
903
+ ),
904
+ ):
905
+ from rbx.box import stats
906
+
907
+ if transitive:
908
+ stats.print_reachable_package_stats()
909
+ else:
910
+ stats.print_package_stats()
911
+
912
+
913
+ @app.command(
914
+ 'fix',
915
+ rich_help_panel='Management',
916
+ help='Format files of the current package.',
917
+ )
918
+ @cd.within_closest_package
919
+ def fix():
920
+ from rbx.box import linting
921
+
922
+ linting.fix_package()
923
+
924
+
912
925
  @app.command(
913
926
  'clear, clean',
914
927
  rich_help_panel='Management',
rbx/box/code.py CHANGED
@@ -14,6 +14,7 @@ import typer
14
14
  from rbx import console
15
15
  from rbx.box import download, package, setter_config, state
16
16
  from rbx.box.environment import (
17
+ CompilationConfig,
17
18
  ExecutionConfig,
18
19
  FileMapping,
19
20
  get_compilation_config,
@@ -29,7 +30,7 @@ from rbx.box.formatting import get_formatted_memory
29
30
  from rbx.box.sanitizers import warning_stack
30
31
  from rbx.box.schema import CodeItem
31
32
  from rbx.grading import steps, steps_with_caching
32
- from rbx.grading.judge.sandbox import SandboxParams
33
+ from rbx.grading.judge.sandbox import SandboxBase, SandboxParams
33
34
  from rbx.grading.steps import (
34
35
  DigestHolder,
35
36
  DigestOrDest,
@@ -39,7 +40,10 @@ from rbx.grading.steps import (
39
40
  GradingFileOutput,
40
41
  RunLog,
41
42
  RunLogMetadata,
43
+ get_exe_from_command,
44
+ is_cpp_command,
42
45
  is_cxx_command,
46
+ maybe_get_bits_stdcpp_for_commands,
43
47
  )
44
48
 
45
49
 
@@ -391,12 +395,111 @@ def _prepare_run(
391
395
  )
392
396
 
393
397
 
398
+ def _should_precompile(commands: List[str]) -> bool:
399
+ return any(is_cpp_command(command) for command in commands)
400
+
401
+
402
+ def _precompile_header(
403
+ compilation_options: CompilationConfig,
404
+ sanitized: SanitizationLevel,
405
+ sandbox: SandboxBase,
406
+ sandbox_params: SandboxParams,
407
+ artifacts: GradingArtifacts,
408
+ input_artifact: GradingFileInput,
409
+ force_warnings: bool = False,
410
+ verbose: bool = False,
411
+ include_other_headers: bool = False,
412
+ ) -> GradingFileInput:
413
+ """
414
+ Precompile a header file (.h).
415
+
416
+ Assumes input artifact is a header file (.h) and compilation commands are C++.
417
+ """
418
+ assert compilation_options.commands is not None
419
+
420
+ dependency_cache = package.get_dependency_cache()
421
+
422
+ # TODO: deduplicate code with compile_item.
423
+ commands = get_mapped_commands(
424
+ compilation_options.commands,
425
+ FileMapping(
426
+ compilable='precompilable.h',
427
+ executable='precompilable.h.gch',
428
+ ),
429
+ )
430
+ commands = add_warning_flags(commands, force_warnings)
431
+ commands = substitute_commands(commands, sanitized=sanitized.should_sanitize())
432
+
433
+ if sanitized.should_sanitize():
434
+ commands = add_sanitizer_flags(commands)
435
+
436
+ precompilation_artifacts = GradingArtifacts()
437
+
438
+ # Keep only header files.
439
+ if include_other_headers:
440
+ precompilation_artifacts.inputs = [
441
+ input
442
+ for input in artifacts.inputs
443
+ if input.src is not None and input.src.suffix == '.h'
444
+ ]
445
+ precompilation_artifacts.inputs.append(
446
+ GradingFileInput(
447
+ src=input_artifact.src,
448
+ dest=PosixPath('precompilable.h'),
449
+ )
450
+ )
451
+
452
+ # Pull only the precompiled header file.
453
+ precompiled_digest = DigestHolder()
454
+ precompilation_artifacts.outputs.append(
455
+ GradingFileOutput(
456
+ src=PosixPath('precompilable.h.gch'),
457
+ digest=precompiled_digest,
458
+ executable=True,
459
+ )
460
+ )
461
+
462
+ if not steps_with_caching.compile(
463
+ commands,
464
+ params=sandbox_params,
465
+ artifacts=precompilation_artifacts,
466
+ sandbox=sandbox,
467
+ dependency_cache=dependency_cache,
468
+ ):
469
+ console.console.print(
470
+ f'[error]Failed to precompile header file: [item]{input_artifact.src}[/item][/error]'
471
+ )
472
+ raise typer.Exit(1)
473
+
474
+ if verbose:
475
+ console.console.print(
476
+ f'[status]Precompiled header file: [item]{input_artifact.src}[/item]'
477
+ )
478
+
479
+ if (
480
+ precompilation_artifacts.logs is not None
481
+ and precompilation_artifacts.logs.preprocess is not None
482
+ ):
483
+ for log in precompilation_artifacts.logs.preprocess:
484
+ console.console.print(f'[status]Command:[/status] {log.get_command()}')
485
+ console.console.print(f'[status]Summary:[/status] {log.get_summary()}')
486
+
487
+ assert precompiled_digest.value is not None
488
+
489
+ return GradingFileInput(
490
+ digest=precompiled_digest,
491
+ dest=input_artifact.dest.with_suffix('.h.gch'),
492
+ executable=True,
493
+ )
494
+
495
+
394
496
  # Compile code item and return its digest in the storage.
395
497
  def compile_item(
396
498
  code: CodeItem,
397
499
  sanitized: SanitizationLevel = SanitizationLevel.PREFER,
398
500
  force_warnings: bool = False,
399
501
  verbose: bool = False,
502
+ precompile: bool = True,
400
503
  ) -> str:
401
504
  _check_stack_limit()
402
505
 
@@ -461,6 +564,41 @@ def compile_item(
461
564
  for input in artifacts.inputs:
462
565
  _ignore_warning_in_cxx_input(input)
463
566
 
567
+ # Add system bits/stdc++.h to the compilation.
568
+ bits_artifact = maybe_get_bits_stdcpp_for_commands(commands)
569
+ if bits_artifact is not None:
570
+ artifacts.inputs.append(bits_artifact)
571
+ commands = [
572
+ command + ' -I.'
573
+ for command in commands
574
+ if is_cxx_command(get_exe_from_command(command))
575
+ ]
576
+
577
+ # Precompile C++ interesting header files.
578
+ if precompile and _should_precompile(commands):
579
+ precompilation_inputs = []
580
+ for input in artifacts.inputs:
581
+ if (
582
+ input.src is not None
583
+ and input.src.suffix == '.h'
584
+ and input.dest.name in ['stdc++.h', 'jngen.h', 'testlib.h']
585
+ ):
586
+ precompilation_inputs.append(
587
+ _precompile_header(
588
+ compilation_options,
589
+ sanitized,
590
+ sandbox,
591
+ sandbox_params,
592
+ artifacts,
593
+ input,
594
+ force_warnings,
595
+ verbose=False,
596
+ )
597
+ )
598
+ if precompilation_inputs:
599
+ artifacts.inputs.extend(precompilation_inputs)
600
+
601
+ # Compile the code.
464
602
  if not steps_with_caching.compile(
465
603
  commands,
466
604
  params=sandbox_params,
@@ -473,6 +611,7 @@ def compile_item(
473
611
  assert compiled_digest.value is not None
474
612
 
475
613
  if verbose and artifacts.logs is not None and artifacts.logs.preprocess is not None:
614
+ console.console.print(f'[status]Compiled item: [item]{code.path}[/item]')
476
615
  for log in artifacts.logs.preprocess:
477
616
  console.console.print(f'[status]Command:[/status] {log.get_command()}')
478
617
  console.console.print(f'[status]Summary:[/status] {log.get_summary()}')
@@ -589,8 +728,8 @@ async def run_communication(
589
728
  interactor_prepared.metadata.retryIndex = retry_index
590
729
  solution_prepared.metadata.retryIndex = retry_index
591
730
 
592
- interactor_prefix = 'INTERACTOR:'
593
- solution_prefix = 'SOLUTION:'
731
+ interactor_prefix = '<'
732
+ solution_prefix = '>'
594
733
 
595
734
  if merged_capture is not None:
596
735
  package.get_merged_capture_path().write_text(
@@ -2,7 +2,7 @@ import dataclasses
2
2
  import pathlib
3
3
  import tempfile
4
4
  import typing
5
- from typing import List, Optional, Tuple
5
+ from typing import Any, Dict, List, Optional, Tuple
6
6
 
7
7
  import typer
8
8
 
@@ -85,36 +85,41 @@ def get_statement_builder_contest(
85
85
 
86
86
  def get_problems_for_statement(
87
87
  contest: Contest,
88
- language: str,
88
+ contest_statement: ContestStatement,
89
89
  requires_matching_statement: bool = True,
90
90
  ) -> List[ExtractedProblem]:
91
91
  pkgs = get_problems(contest)
92
- if not pkgs:
92
+ if not pkgs and not requires_matching_statement:
93
93
  console.console.print(
94
94
  '[error]No problems found in the contest, cannot infer statement type.[/error]'
95
95
  )
96
96
  raise typer.Exit(1)
97
97
 
98
+ def matches(statement: Statement) -> bool:
99
+ if not requires_matching_statement:
100
+ return True
101
+ if contest_statement.match is None:
102
+ return statement.language == contest_statement.language
103
+ return statement.name == contest_statement.match
104
+
98
105
  res = []
99
106
  for pkg, problem in zip(pkgs, contest.problems):
100
- found = False
101
- for statement in pkg.statements:
102
- if statement.language == language or not requires_matching_statement:
103
- found = True
104
- res.append(
105
- ExtractedProblem(
106
- package=pkg,
107
- statement=statement,
108
- problem=problem,
109
- samples=_get_samples(problem),
110
- )
111
- )
112
- break
113
- if not found:
107
+ matching_statements = [
108
+ statement for statement in pkg.expanded_statements if matches(statement)
109
+ ]
110
+ if not matching_statements:
114
111
  console.console.print(
115
- f'[error]No statement found for language {language} in problem {problem.short_name}[/error]'
112
+ f'[error]No statement found for language {contest_statement.language} in problem {problem.short_name}[/error]'
116
113
  )
117
114
  raise typer.Exit(1)
115
+ res.append(
116
+ ExtractedProblem(
117
+ package=pkg,
118
+ statement=matching_statements[0],
119
+ problem=problem,
120
+ samples=_get_samples(problem),
121
+ )
122
+ )
118
123
 
119
124
  return res
120
125
 
@@ -146,14 +151,17 @@ def _build_problem_statements(
146
151
  root: pathlib.Path,
147
152
  output_type: StatementType,
148
153
  use_samples: bool = True,
149
- is_editorial: bool = False,
154
+ custom_vars: Optional[Dict[str, Any]] = None,
150
155
  ) -> List[ExtractedProblem]:
151
156
  console.console.print('Building problem-level statements...')
152
- extracted_problems = get_problems_for_statement(contest, statement.language)
157
+ extracted_problems = get_problems_for_statement(contest, statement)
153
158
  res = []
154
159
  contest_cwd_absolute = pathlib.Path().resolve()
155
160
  contest_assets = get_relative_assets(statement.path, statement.assets)
156
161
 
162
+ extra_vars = dict(statement.override.vars if statement.override is not None else {})
163
+ extra_vars.update(custom_vars or {})
164
+
157
165
  for extracted_problem in extracted_problems:
158
166
  console.console.print(
159
167
  f'Building statement for problem {extracted_problem.problem.short_name}...'
@@ -174,7 +182,8 @@ def _build_problem_statements(
174
182
  overridden_assets=contest_assets, # overridden assets
175
183
  overridden_params_root=contest_cwd_absolute,
176
184
  use_samples=use_samples,
177
- is_editorial=is_editorial,
185
+ # Use custom var overriding and problem-level overriding.
186
+ custom_vars=extra_vars,
178
187
  )
179
188
  dest_dir = root / '.problems' / extracted_problem.problem.short_name
180
189
  dest_path = dest_dir / f'statement{output_type.get_file_suffix()}'
@@ -201,7 +210,7 @@ def build_contest_only(
201
210
  input: bytes,
202
211
  input_type: StatementType,
203
212
  output_type: Optional[StatementType] = None,
204
- is_editorial: bool = False,
213
+ custom_vars: Optional[Dict[str, Any]] = None,
205
214
  ) -> Tuple[bytes, StatementType]:
206
215
  console.console.print('Building contest-level statement.')
207
216
  bdrs = get_builders(
@@ -224,10 +233,11 @@ def build_contest_only(
224
233
  output = bdr.build(
225
234
  input=last_content,
226
235
  context=StatementBuilderContext(
236
+ lang=statement.language,
227
237
  languages=get_environment_languages_for_statement(),
228
238
  params=params,
229
239
  root=pathlib.Path(td),
230
- editorial=is_editorial,
240
+ custom_vars=custom_vars,
231
241
  vars={**contest.expanded_vars, **statement.expanded_vars},
232
242
  ),
233
243
  item=get_statement_builder_contest(statement, extracted_problems),
@@ -245,7 +255,7 @@ def build_statement_rooted(
245
255
  root: pathlib.Path,
246
256
  output_type: Optional[StatementType] = None,
247
257
  use_samples: bool = True,
248
- is_editorial: bool = False,
258
+ custom_vars: Optional[Dict[str, Any]] = None,
249
259
  ) -> Tuple[bytes, StatementType]:
250
260
  # Validate.
251
261
  if not statement.path.is_file():
@@ -257,7 +267,7 @@ def build_statement_rooted(
257
267
  if statement.joiner is None:
258
268
  joiner = None
259
269
  extracted_problems = get_problems_for_statement(
260
- contest, statement.language, requires_matching_statement=False
270
+ contest, statement, requires_matching_statement=False
261
271
  )
262
272
  else:
263
273
  # Build problem-level statements.
@@ -268,7 +278,7 @@ def build_statement_rooted(
268
278
  root,
269
279
  output_type=joiner.joined_type(),
270
280
  use_samples=use_samples,
271
- is_editorial=is_editorial,
281
+ custom_vars=custom_vars,
272
282
  )
273
283
 
274
284
  # Build contest-level statement into joiner input type.
@@ -279,7 +289,7 @@ def build_statement_rooted(
279
289
  statement.path.read_bytes(),
280
290
  statement.type,
281
291
  output_type=joiner.joined_type() if joiner is not None else output_type,
282
- is_editorial=is_editorial,
292
+ custom_vars=custom_vars,
283
293
  )
284
294
 
285
295
  if joiner is None:
@@ -313,7 +323,7 @@ def build_statement_rooted(
313
323
  last_content,
314
324
  last_output,
315
325
  output_type=output_type,
316
- is_editorial=is_editorial,
326
+ custom_vars=custom_vars,
317
327
  )
318
328
 
319
329
  return last_content, last_output
@@ -324,7 +334,7 @@ def build_statement(
324
334
  contest: Contest,
325
335
  output_type: Optional[StatementType] = None,
326
336
  use_samples: bool = True,
327
- is_editorial: bool = False,
337
+ custom_vars: Optional[Dict[str, Any]] = None,
328
338
  ) -> pathlib.Path:
329
339
  with tempfile.TemporaryDirectory() as td:
330
340
  root = pathlib.Path(td)
@@ -334,17 +344,17 @@ def build_statement(
334
344
  root,
335
345
  output_type=output_type,
336
346
  use_samples=use_samples,
337
- is_editorial=is_editorial,
347
+ custom_vars=custom_vars,
338
348
  )
339
349
 
340
- statement_path = pathlib.Path(
341
- f'build/{statement.path.stem}{last_output.get_file_suffix()}'
350
+ statement_path = (pathlib.Path('build') / statement.name).with_suffix(
351
+ last_output.get_file_suffix()
342
352
  )
343
353
  statement_path.parent.mkdir(parents=True, exist_ok=True)
344
354
  statement_path.write_bytes(typing.cast(bytes, last_content))
345
355
  console.console.print(
346
- f'Statement built successfully for language '
356
+ f'[success]Statement [item]{statement.name}[/item] built successfully for language '
347
357
  f'[item]{statement.language}[/item] at '
348
- f'{href(statement_path)}'
358
+ f'{href(statement_path)}[/success]'
349
359
  )
350
360
  return statement_path