rbx.cp 0.5.40__py3-none-any.whl → 0.5.42__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 (53) hide show
  1. rbx/box/builder.py +6 -6
  2. rbx/box/checkers.py +100 -25
  3. rbx/box/cli.py +860 -0
  4. rbx/box/code.py +199 -84
  5. rbx/box/contest/statements.py +4 -2
  6. rbx/box/generators.py +55 -49
  7. rbx/box/generators_test.py +7 -7
  8. rbx/box/main.py +1 -864
  9. rbx/box/package.py +42 -1
  10. rbx/box/packaging/boca/packager.py +2 -1
  11. rbx/box/packaging/main.py +17 -9
  12. rbx/box/packaging/moj/packager.py +49 -10
  13. rbx/box/retries.py +5 -5
  14. rbx/box/schema.py +20 -4
  15. rbx/box/solutions.py +46 -108
  16. rbx/box/solutions_test.py +5 -6
  17. rbx/box/statements/build_statements.py +4 -2
  18. rbx/box/stresses.py +23 -12
  19. rbx/box/tasks.py +258 -0
  20. rbx/box/testcase_extractors.py +21 -21
  21. rbx/box/testcases/main.py +19 -14
  22. rbx/box/unit.py +10 -7
  23. rbx/box/validators.py +10 -10
  24. rbx/box/validators_test.py +3 -3
  25. rbx/grading/judge/sandbox.py +8 -0
  26. rbx/grading/judge/sandboxes/stupid_sandbox.py +12 -7
  27. rbx/grading/judge/sandboxes/timeit.py +8 -2
  28. rbx/grading/steps.py +76 -2
  29. rbx/grading/steps_with_caching.py +45 -3
  30. rbx/grading/steps_with_caching_run_test.py +51 -49
  31. rbx/resources/packagers/moj/scripts/compare.sh +25 -6
  32. rbx/test.py +6 -4
  33. rbx/testdata/interactive/checker.cpp +21 -0
  34. rbx/testdata/interactive/gen.cpp +11 -0
  35. rbx/testdata/interactive/interactor.cpp +63 -0
  36. rbx/testdata/interactive/problem.rbx.yml +40 -0
  37. rbx/testdata/interactive/sols/af_ac_pe.cpp +75 -0
  38. rbx/testdata/interactive/sols/af_ac_re.cpp +76 -0
  39. rbx/testdata/interactive/sols/af_ac_too_many_iter.cpp +72 -0
  40. rbx/testdata/interactive/sols/af_inf_cout_with_flush.cpp +79 -0
  41. rbx/testdata/interactive/sols/af_inf_cout_without_flush.cpp +78 -0
  42. rbx/testdata/interactive/sols/af_ml.cpp +78 -0
  43. rbx/testdata/interactive/sols/af_tl_after_ans.cpp +74 -0
  44. rbx/testdata/interactive/sols/af_wa.cpp +74 -0
  45. rbx/testdata/interactive/sols/interactive-binary-search_mm_naive_cin.cpp +17 -0
  46. rbx/testdata/interactive/sols/main.cpp +26 -0
  47. rbx/testdata/interactive/testplan.txt +6 -0
  48. rbx/testdata/interactive/validator.cpp +16 -0
  49. {rbx_cp-0.5.40.dist-info → rbx_cp-0.5.42.dist-info}/METADATA +2 -1
  50. {rbx_cp-0.5.40.dist-info → rbx_cp-0.5.42.dist-info}/RECORD +53 -35
  51. {rbx_cp-0.5.40.dist-info → rbx_cp-0.5.42.dist-info}/LICENSE +0 -0
  52. {rbx_cp-0.5.40.dist-info → rbx_cp-0.5.42.dist-info}/WHEEL +0 -0
  53. {rbx_cp-0.5.40.dist-info → rbx_cp-0.5.42.dist-info}/entry_points.txt +0 -0
rbx/box/cli.py ADDED
@@ -0,0 +1,860 @@
1
+ import pathlib
2
+ import shlex
3
+ import shutil
4
+ import sys
5
+ import tempfile
6
+ from typing import Annotated, Optional
7
+
8
+ import rich
9
+ import rich.prompt
10
+ import syncer
11
+ import typer
12
+
13
+ from rbx import annotations, config, console, utils
14
+ from rbx.box import (
15
+ cd,
16
+ compile,
17
+ creation,
18
+ download,
19
+ environment,
20
+ generators,
21
+ package,
22
+ presets,
23
+ setter_config,
24
+ state,
25
+ validators,
26
+ )
27
+ from rbx.box.contest import main as contest
28
+ from rbx.box.contest.contest_package import find_contest_yaml
29
+ from rbx.box.environment import VerificationLevel, get_environment_path
30
+ from rbx.box.packaging import main as packaging
31
+ from rbx.box.schema import CodeItem, ExpectedOutcome, TestcaseGroup
32
+ from rbx.box.solutions import (
33
+ estimate_time_limit,
34
+ get_exact_matching_solutions,
35
+ get_matching_solutions,
36
+ pick_solutions,
37
+ print_run_report,
38
+ run_and_print_interactive_solutions,
39
+ run_solutions,
40
+ )
41
+ from rbx.box.statements import build_statements
42
+ from rbx.box.testcase_utils import TestcaseEntry
43
+ from rbx.box.testcases import main as testcases
44
+
45
+ app = typer.Typer(no_args_is_help=True, cls=annotations.AliasGroup)
46
+ app.add_typer(
47
+ setter_config.app,
48
+ name='config, cfg',
49
+ cls=annotations.AliasGroup,
50
+ help='Manage setter configuration (sub-command).',
51
+ rich_help_panel='Configuration',
52
+ )
53
+ app.add_typer(
54
+ build_statements.app,
55
+ name='statements, st',
56
+ cls=annotations.AliasGroup,
57
+ help='Manage statements (sub-command).',
58
+ rich_help_panel='Deploying',
59
+ )
60
+ app.add_typer(
61
+ download.app,
62
+ name='download',
63
+ cls=annotations.AliasGroup,
64
+ help='Download an asset from supported repositories (sub-command).',
65
+ rich_help_panel='Management',
66
+ )
67
+ app.add_typer(
68
+ presets.app,
69
+ name='presets',
70
+ cls=annotations.AliasGroup,
71
+ help='Manage presets (sub-command).',
72
+ rich_help_panel='Configuration',
73
+ )
74
+ app.add_typer(
75
+ packaging.app,
76
+ name='package, pkg',
77
+ cls=annotations.AliasGroup,
78
+ help='Build problem packages (sub-command).',
79
+ rich_help_panel='Deploying',
80
+ )
81
+ app.add_typer(
82
+ contest.app,
83
+ name='contest',
84
+ cls=annotations.AliasGroup,
85
+ help='Manage contests (sub-command).',
86
+ rich_help_panel='Management',
87
+ )
88
+ app.add_typer(
89
+ testcases.app,
90
+ name='testcases, tc, t',
91
+ cls=annotations.AliasGroup,
92
+ help='Manage testcases (sub-command).',
93
+ rich_help_panel='Management',
94
+ )
95
+
96
+
97
+ @app.callback()
98
+ def main(
99
+ sanitized: bool = typer.Option(
100
+ False,
101
+ '--sanitized',
102
+ '-s',
103
+ help='Whether to compile and run testlib components with sanitizers enabled. '
104
+ 'If you want to run the solutions with sanitizers enabled, use the "-s" flag in the corresponding run command.',
105
+ ),
106
+ ):
107
+ state.STATE.run_through_cli = True
108
+ state.STATE.sanitized = sanitized
109
+ if sanitized:
110
+ console.console.print(
111
+ '[warning]Sanitizers are running just for testlib components.\n'
112
+ 'If you want to run the solutions with sanitizers enabled, use the [item]-s[/item] flag in the corresponding run command.[/warning]'
113
+ )
114
+
115
+
116
+ # @app.command('ui', hidden=True)
117
+ # @package.within_problem
118
+ # def ui():
119
+ # ui_pkg.start()
120
+
121
+
122
+ @app.command(
123
+ 'edit, e',
124
+ rich_help_panel='Configuration',
125
+ help='Open problem.rbx.yml in your default editor.',
126
+ )
127
+ @package.within_problem
128
+ def edit():
129
+ console.console.print('Opening problem definition in editor...')
130
+ # Call this function just to raise exception in case we're no in
131
+ # a problem package.
132
+ package.find_problem()
133
+ config.open_editor(package.find_problem_yaml() or pathlib.Path())
134
+
135
+
136
+ @app.command(
137
+ 'build, b', rich_help_panel='Deploying', help='Build all tests for the problem.'
138
+ )
139
+ @package.within_problem
140
+ @syncer.sync
141
+ async def build(verification: environment.VerificationParam):
142
+ from rbx.box import builder
143
+
144
+ await builder.build(verification=verification)
145
+
146
+
147
+ @app.command(
148
+ 'run, r',
149
+ rich_help_panel='Testing',
150
+ help='Build and run solution(s).',
151
+ )
152
+ @package.within_problem
153
+ @syncer.sync
154
+ async def run(
155
+ verification: environment.VerificationParam,
156
+ solution: Annotated[
157
+ Optional[str],
158
+ typer.Argument(
159
+ help='Path to solution to run. If not specified, will run all solutions.'
160
+ ),
161
+ ] = None,
162
+ outcome: Optional[str] = typer.Option(
163
+ None,
164
+ '--outcome',
165
+ '-o',
166
+ help='Include only solutions whose expected outcomes intersect with this.',
167
+ ),
168
+ check: bool = typer.Option(
169
+ True,
170
+ '--nocheck',
171
+ flag_value=False,
172
+ help='Whether to not build outputs for tests and run checker.',
173
+ ),
174
+ detailed: bool = typer.Option(
175
+ False,
176
+ '--detailed',
177
+ '-d',
178
+ help='Whether to print a detailed view of the tests using tables.',
179
+ ),
180
+ timeit: bool = typer.Option(
181
+ False,
182
+ '--time',
183
+ '-t',
184
+ help='Whether to use estimate a time limit based on accepted solutions.',
185
+ ),
186
+ sanitized: bool = typer.Option(
187
+ False,
188
+ '--sanitized',
189
+ '-s',
190
+ help='Whether to compile the solutions with sanitizers enabled.',
191
+ ),
192
+ choice: bool = typer.Option(
193
+ False,
194
+ '--choice',
195
+ '--choose',
196
+ '-c',
197
+ help='Whether to pick solutions interactively.',
198
+ ),
199
+ ):
200
+ main_solution = package.get_main_solution()
201
+ if check and main_solution is None:
202
+ console.console.print(
203
+ '[warning]No main solution found, running without checkers.[/warning]'
204
+ )
205
+ check = False
206
+
207
+ tracked_solutions = None
208
+ if outcome is not None:
209
+ tracked_solutions = {
210
+ str(solution.path)
211
+ for solution in get_matching_solutions(ExpectedOutcome(outcome))
212
+ }
213
+ if solution:
214
+ tracked_solutions = {solution}
215
+
216
+ if choice:
217
+ tracked_solutions = set(pick_solutions(tracked_solutions))
218
+ if not tracked_solutions:
219
+ console.console.print('[error]No solutions selected. Exiting.[/error]')
220
+ raise typer.Exit(1)
221
+
222
+ from rbx.box import builder
223
+
224
+ if not await builder.build(verification=verification, output=check):
225
+ return
226
+
227
+ if verification <= VerificationLevel.VALIDATE.value:
228
+ console.console.print(
229
+ '[warning]Verification level is set to [item]validate (-v1)[/item], so rbx only build tests and validated them.[/warning]'
230
+ )
231
+ return
232
+
233
+ override_tl = None
234
+ if timeit:
235
+ if sanitized:
236
+ console.console.print(
237
+ '[error]Sanitizers are known to be time-hungry, so they cannot be used for time estimation.\n'
238
+ 'Remove either the [item]-s[/item] flag or the [item]-t[/item] flag to run solutions without sanitizers.[/error]'
239
+ )
240
+ raise typer.Exit(1)
241
+
242
+ # Never use sanitizers for time estimation.
243
+ override_tl = await _time_impl(check=check, detailed=False)
244
+ if override_tl is None:
245
+ raise typer.Exit(1)
246
+
247
+ if sanitized:
248
+ console.console.print(
249
+ '[warning]Sanitizers are running, so the time limit for the problem will be dropped, '
250
+ 'and the environment default time limit will be used instead.[/warning]'
251
+ )
252
+
253
+ if sanitized and tracked_solutions is None:
254
+ console.console.print(
255
+ '[warning]Sanitizers are running, and no solutions were specified to run. Will only run [item]ACCEPTED[/item] solutions.'
256
+ )
257
+ tracked_solutions = {
258
+ str(solution.path)
259
+ for solution in get_exact_matching_solutions(ExpectedOutcome.ACCEPTED)
260
+ }
261
+
262
+ with utils.StatusProgress('Running solutions...') as s:
263
+ solution_result = run_solutions(
264
+ progress=s,
265
+ tracked_solutions=tracked_solutions,
266
+ check=check,
267
+ verification=VerificationLevel(verification),
268
+ timelimit_override=override_tl,
269
+ sanitized=sanitized,
270
+ )
271
+
272
+ console.console.print()
273
+ console.console.rule('[status]Run report[/status]', style='status')
274
+ await print_run_report(
275
+ solution_result,
276
+ console.console,
277
+ VerificationLevel(verification),
278
+ detailed=detailed,
279
+ skip_printing_limits=sanitized,
280
+ )
281
+
282
+
283
+ async def _time_impl(check: bool, detailed: bool) -> Optional[int]:
284
+ if package.get_main_solution() is None:
285
+ console.console.print(
286
+ '[warning]No main solution found, so cannot estimate a time limit.[/warning]'
287
+ )
288
+ return None
289
+
290
+ verification = VerificationLevel.ALL_SOLUTIONS.value
291
+
292
+ with utils.StatusProgress('Running ACCEPTED solutions...') as s:
293
+ tracked_solutions = {
294
+ str(solution.path)
295
+ for solution in get_exact_matching_solutions(ExpectedOutcome.ACCEPTED)
296
+ }
297
+ solution_result = run_solutions(
298
+ progress=s,
299
+ tracked_solutions=tracked_solutions,
300
+ check=check,
301
+ verification=VerificationLevel(verification),
302
+ timelimit_override=600, # 10 minute time limit for estimation
303
+ )
304
+
305
+ console.console.print()
306
+ console.console.rule(
307
+ '[status]Run report (for time estimation)[/status]', style='status'
308
+ )
309
+ ok = await print_run_report(
310
+ solution_result,
311
+ console.console,
312
+ VerificationLevel(verification),
313
+ detailed=detailed,
314
+ skip_printing_limits=True,
315
+ )
316
+
317
+ if not ok:
318
+ console.console.print(
319
+ '[error]Failed to run ACCEPTED solutions, so cannot estimate a reliable time limit.[/error]'
320
+ )
321
+ return None
322
+
323
+ console.console.print()
324
+ return await estimate_time_limit(console.console, solution_result)
325
+
326
+
327
+ @app.command(
328
+ 'time, t',
329
+ rich_help_panel='Testing',
330
+ help='Estimate a time limit for the problem based on a time limit formula and timings of accepted solutions.',
331
+ )
332
+ @package.within_problem
333
+ @syncer.sync
334
+ async def time(
335
+ check: bool = typer.Option(
336
+ True,
337
+ '--nocheck',
338
+ flag_value=False,
339
+ help='Whether to not build outputs for tests and run checker.',
340
+ ),
341
+ detailed: bool = typer.Option(
342
+ False,
343
+ '--detailed',
344
+ '-d',
345
+ help='Whether to print a detailed view of the tests using tables.',
346
+ ),
347
+ ):
348
+ main_solution = package.get_main_solution()
349
+ if check and main_solution is None:
350
+ console.console.print(
351
+ '[warning]No main solution found, running without checkers.[/warning]'
352
+ )
353
+ check = False
354
+
355
+ from rbx.box import builder
356
+
357
+ verification = VerificationLevel.ALL_SOLUTIONS.value
358
+ if not await builder.build(verification=verification, output=check):
359
+ return None
360
+
361
+ await _time_impl(check, detailed)
362
+
363
+
364
+ @app.command(
365
+ 'irun, ir',
366
+ rich_help_panel='Testing',
367
+ help='Build and run solution(s) by passing testcases in the CLI.',
368
+ )
369
+ @package.within_problem
370
+ @syncer.sync
371
+ async def irun(
372
+ verification: environment.VerificationParam,
373
+ solution: Annotated[
374
+ Optional[str],
375
+ typer.Argument(
376
+ help='Path to solution to run. If not specified, will run all solutions.'
377
+ ),
378
+ ] = None,
379
+ outcome: Optional[str] = typer.Option(
380
+ None,
381
+ '--outcome',
382
+ '-o',
383
+ help='Include only solutions whose expected outcomes intersect with this.',
384
+ ),
385
+ check: bool = typer.Option(
386
+ True,
387
+ '--nocheck',
388
+ flag_value=False,
389
+ help='Whether to not build outputs for tests and run checker.',
390
+ ),
391
+ generator: Optional[str] = typer.Option(
392
+ None,
393
+ '--generator',
394
+ '-g',
395
+ help='Generator call to use to generate a single test for execution.',
396
+ ),
397
+ testcase: Optional[str] = typer.Option(
398
+ None,
399
+ '--testcase',
400
+ '--test',
401
+ '-tc',
402
+ '-t',
403
+ help='Testcase to run, in the format "[group]/[index]". If not specified, will run interactively.',
404
+ ),
405
+ output: bool = typer.Option(
406
+ False,
407
+ '--output',
408
+ '-o',
409
+ help='Whether to ask user for custom output.',
410
+ ),
411
+ print: bool = typer.Option(
412
+ False, '--print', '-p', help='Whether to print outputs to terminal.'
413
+ ),
414
+ sanitized: bool = typer.Option(
415
+ False,
416
+ '--sanitized',
417
+ '-s',
418
+ help='Whether to compile the solutions with sanitizers enabled.',
419
+ ),
420
+ choice: bool = typer.Option(
421
+ False,
422
+ '--choice',
423
+ '--choose',
424
+ '-c',
425
+ help='Whether to pick solutions interactively.',
426
+ ),
427
+ ):
428
+ if not print:
429
+ console.console.print(
430
+ '[warning]Outputs will be written to files. If you wish to print them to the terminal, use the "-p" parameter.'
431
+ )
432
+ if verification < VerificationLevel.ALL_SOLUTIONS.value:
433
+ console.console.print(
434
+ '[warning]Verification level should be at least [item]all solutions (-v4)[/item] to run solutions interactively.'
435
+ )
436
+ return
437
+
438
+ tracked_solutions = None
439
+ if outcome is not None:
440
+ tracked_solutions = {
441
+ str(solution.path)
442
+ for solution in get_matching_solutions(ExpectedOutcome(outcome))
443
+ }
444
+ if solution:
445
+ tracked_solutions = {solution}
446
+
447
+ if choice:
448
+ tracked_solutions = set(pick_solutions(tracked_solutions))
449
+ if not tracked_solutions:
450
+ console.console.print('[error]No solutions selected. Exiting.[/error]')
451
+ raise typer.Exit(1)
452
+
453
+ if sanitized and tracked_solutions is None:
454
+ console.console.print(
455
+ '[warning]Sanitizers are running, and no solutions were specified to run. Will only run [item]ACCEPTED[/item] solutions.'
456
+ )
457
+ tracked_solutions = {
458
+ str(solution.path)
459
+ for solution in get_exact_matching_solutions(ExpectedOutcome.ACCEPTED)
460
+ }
461
+
462
+ with utils.StatusProgress('Running solutions...') as s:
463
+ await run_and_print_interactive_solutions(
464
+ progress=s,
465
+ tracked_solutions=tracked_solutions,
466
+ check=check,
467
+ verification=VerificationLevel(verification),
468
+ generator=generators.get_call_from_string(generator)
469
+ if generator is not None
470
+ else None,
471
+ testcase_entry=TestcaseEntry.parse(testcase) if testcase else None,
472
+ custom_output=output,
473
+ print=print,
474
+ sanitized=sanitized,
475
+ )
476
+
477
+
478
+ @app.command(
479
+ 'create, c',
480
+ rich_help_panel='Management',
481
+ help='Create a new problem package.',
482
+ )
483
+ def create(
484
+ name: str,
485
+ preset: Annotated[
486
+ Optional[str], typer.Option(help='Preset to use when creating the problem.')
487
+ ] = None,
488
+ ):
489
+ if find_contest_yaml() is not None:
490
+ console.console.print(
491
+ '[error]Cannot [item]rbx create[/item] a problem inside a contest.[/error]'
492
+ )
493
+ console.console.print(
494
+ '[error]Instead, use [item]rbx contest add[/item] to add a problem to a contest.[/error]'
495
+ )
496
+ raise typer.Exit(1)
497
+
498
+ if preset is not None:
499
+ creation.create(name, preset=preset)
500
+ return
501
+ creation.create(name)
502
+
503
+
504
+ @app.command(
505
+ 'stress',
506
+ rich_help_panel='Testing',
507
+ help='Run a stress test.',
508
+ )
509
+ @package.within_problem
510
+ def stress(
511
+ name: Annotated[
512
+ str,
513
+ typer.Argument(
514
+ help='Name of the stress test to run (specified in problem.rbx.yml), '
515
+ 'or the generator to run, in case -g is specified.'
516
+ ),
517
+ ],
518
+ generator_args: Annotated[
519
+ Optional[str],
520
+ typer.Option(
521
+ '--generator',
522
+ '-g',
523
+ help='Run generator [name] with these args.',
524
+ ),
525
+ ] = None,
526
+ finder: Annotated[
527
+ Optional[str],
528
+ typer.Option(
529
+ '--finder',
530
+ '-f',
531
+ help='Run a stress with this finder expression.',
532
+ ),
533
+ ] = None,
534
+ timeout: Annotated[
535
+ int,
536
+ typer.Option(
537
+ '--timeout',
538
+ '--time',
539
+ '-t',
540
+ help='For how many seconds to run the stress test.',
541
+ ),
542
+ ] = 10,
543
+ findings: Annotated[
544
+ int,
545
+ typer.Option('--findings', '-n', help='How many breaking tests to look for.'),
546
+ ] = 1,
547
+ verbose: bool = typer.Option(
548
+ False,
549
+ '-v',
550
+ '--verbose',
551
+ help='Whether to print verbose output for checkers and finders.',
552
+ ),
553
+ sanitized: bool = typer.Option(
554
+ False,
555
+ '--sanitized',
556
+ '-s',
557
+ help='Whether to compile the solutions with sanitizers enabled.',
558
+ ),
559
+ ):
560
+ if finder and not generator_args or generator_args and not finder:
561
+ console.console.print(
562
+ '[error]Options --generator/-g and --finder/-f should be specified together.'
563
+ )
564
+ raise typer.Exit(1)
565
+
566
+ from rbx.box import stresses
567
+
568
+ with utils.StatusProgress('Running stress...') as s:
569
+ report = stresses.run_stress(
570
+ name,
571
+ timeout,
572
+ args=generator_args,
573
+ finder=finder,
574
+ findingsLimit=findings,
575
+ progress=s,
576
+ verbose=verbose,
577
+ sanitized=sanitized,
578
+ )
579
+
580
+ stresses.print_stress_report(report)
581
+
582
+ if not report.findings:
583
+ return
584
+
585
+ # Add found tests.
586
+ res = rich.prompt.Confirm.ask(
587
+ 'Do you want to add the tests that were found to a test group?',
588
+ console=console.console,
589
+ )
590
+ if not res:
591
+ return
592
+ testgroup = None
593
+ while testgroup is None or testgroup:
594
+ groups_by_name = {
595
+ name: group
596
+ for name, group in package.get_test_groups_by_name().items()
597
+ if group.generatorScript is not None
598
+ and group.generatorScript.path.suffix == '.txt'
599
+ }
600
+
601
+ import questionary
602
+
603
+ testgroup = questionary.select(
604
+ 'Choose the testgroup to add the tests to.\nOnly test groups that have a .txt generatorScript are shown below: ',
605
+ choices=list(groups_by_name) + ['(create new script)', '(skip)'],
606
+ ).ask()
607
+
608
+ if testgroup == '(create new script)':
609
+ new_script_name = questionary.text(
610
+ 'Enter the name of the new .txt generatorScript file: '
611
+ ).ask()
612
+ new_script_path = pathlib.Path(new_script_name).with_suffix('.txt')
613
+ new_script_path.parent.mkdir(parents=True, exist_ok=True)
614
+ new_script_path.touch()
615
+
616
+ # Temporarily create a new testgroup with the new script.
617
+ testgroup = new_script_path.stem
618
+ groups_by_name[testgroup] = TestcaseGroup(
619
+ name=testgroup, generatorScript=CodeItem(path=new_script_path)
620
+ )
621
+ ru, problem_yml = package.get_ruyaml()
622
+ if 'testcases' not in problem_yml:
623
+ problem_yml['testcases'] = []
624
+ problem_yml['testcases'].append(
625
+ {
626
+ 'name': testgroup,
627
+ 'generatorScript': new_script_path.name,
628
+ }
629
+ )
630
+ dest = package.find_problem_yaml()
631
+ assert dest is not None
632
+ utils.save_ruyaml(dest, ru, problem_yml)
633
+ package.clear_package_cache()
634
+
635
+ if testgroup not in groups_by_name:
636
+ break
637
+ try:
638
+ subgroup = groups_by_name[testgroup]
639
+ assert subgroup.generatorScript is not None
640
+ generator_script = pathlib.Path(subgroup.generatorScript.path)
641
+
642
+ finding_lines = []
643
+ for finding in report.findings:
644
+ line = finding.generator.name
645
+ if finding.generator.args is not None:
646
+ line = f'{line} {finding.generator.args}'
647
+ finding_lines.append(line)
648
+
649
+ with generator_script.open('a') as f:
650
+ stress_text = f'# Obtained by running `rbx {shlex.join(sys.argv[1:])}`'
651
+ finding_text = '\n'.join(finding_lines)
652
+ f.write(f'\n{stress_text}\n{finding_text}\n')
653
+
654
+ console.console.print(
655
+ f"Added [item]{len(report.findings)}[/item] tests to test group [item]{testgroup}[/item]'s generatorScript at [item]{subgroup.generatorScript.path}[/item]"
656
+ )
657
+ except typer.Exit:
658
+ continue
659
+ break
660
+
661
+
662
+ @app.command(
663
+ 'compile',
664
+ rich_help_panel='Testing',
665
+ help='Compile an asset given its path.',
666
+ )
667
+ @package.within_problem
668
+ def compile_command(
669
+ path: Annotated[
670
+ Optional[str],
671
+ typer.Argument(help='Path to the asset to compile.'),
672
+ ] = None,
673
+ sanitized: bool = typer.Option(
674
+ False,
675
+ '--sanitized',
676
+ '-s',
677
+ help='Whether to compile the asset with sanitizers enabled.',
678
+ ),
679
+ warnings: bool = typer.Option(
680
+ False,
681
+ '--warnings',
682
+ '-w',
683
+ help='Whether to compile the asset with warnings enabled.',
684
+ ),
685
+ ):
686
+ if path is None:
687
+ import questionary
688
+
689
+ path = questionary.path("What's the path to your asset?").ask()
690
+ if path is None:
691
+ console.console.print('[error]No path specified.[/error]')
692
+ raise typer.Exit(1)
693
+
694
+ compile.any(path, sanitized, warnings)
695
+
696
+
697
+ @app.command(
698
+ 'validate',
699
+ rich_help_panel='Testing',
700
+ help='Run the validator in a one-off fashion, interactively.',
701
+ )
702
+ @package.within_problem
703
+ @syncer.sync
704
+ async def validate(
705
+ path: Annotated[
706
+ Optional[str],
707
+ typer.Option('--path', '-p', help='Path to the testcase to validate.'),
708
+ ] = None,
709
+ ):
710
+ validator_tuple = validators.compile_main_validator()
711
+ if validator_tuple is None:
712
+ console.console.print('[error]No validator found for this problem.[/error]')
713
+ raise typer.Exit(1)
714
+
715
+ validator, validator_digest = validator_tuple
716
+
717
+ input = console.multiline_prompt('Testcase input')
718
+
719
+ if path is None:
720
+ with tempfile.TemporaryDirectory() as tmpdir:
721
+ tmppath = pathlib.Path(tmpdir) / '000.in'
722
+ tmppath.write_text(input)
723
+
724
+ info = await validators.validate_one_off(
725
+ pathlib.Path(tmppath), validator, validator_digest
726
+ )
727
+ else:
728
+ info = await validators.validate_one_off(
729
+ pathlib.Path(path), validator, validator_digest
730
+ )
731
+
732
+ validators.print_validation_report([info])
733
+
734
+
735
+ @app.command(
736
+ 'unit',
737
+ rich_help_panel='Testing',
738
+ help='Run unit tests for the validator and checker.',
739
+ )
740
+ def unit_tests():
741
+ from rbx.box import unit
742
+
743
+ with utils.StatusProgress('Running unit tests...') as s:
744
+ unit.run_unit_tests(s)
745
+
746
+
747
+ @app.command(
748
+ 'environment, env',
749
+ rich_help_panel='Configuration',
750
+ help='Set or show the current box environment.',
751
+ )
752
+ def environment_command(
753
+ env: Annotated[Optional[str], typer.Argument()] = None,
754
+ install_from: Annotated[
755
+ Optional[str],
756
+ typer.Option(
757
+ '--install',
758
+ '-i',
759
+ help='Whether to install this environment from the given file.',
760
+ ),
761
+ ] = None,
762
+ ):
763
+ if env is None:
764
+ cfg = config.get_config()
765
+ console.console.print(f'Current environment: [item]{cfg.boxEnvironment}[/item]')
766
+ console.console.print(
767
+ f'Location: {environment.get_environment_path(cfg.boxEnvironment)}'
768
+ )
769
+ return
770
+ if install_from is not None:
771
+ environment.install_environment(env, pathlib.Path(install_from))
772
+ if not get_environment_path(env).is_file():
773
+ console.console.print(
774
+ f'[error]Environment [item]{env}[/item] does not exist.[/error]'
775
+ )
776
+ raise typer.Exit(1)
777
+
778
+ cfg = config.get_config()
779
+ if env == cfg.boxEnvironment:
780
+ console.console.print(
781
+ f'Environment is already set to [item]{env}[/item].',
782
+ )
783
+ return
784
+ console.console.print(
785
+ f'Changing box environment from [item]{cfg.boxEnvironment}[/item] to [item]{env}[/item]...'
786
+ )
787
+ cfg.boxEnvironment = env
788
+ config.save_config(cfg)
789
+
790
+ # Also clear cache when changing environments.
791
+ clear()
792
+
793
+
794
+ @app.command(
795
+ 'activate',
796
+ rich_help_panel='Configuration',
797
+ help='Activate the environment of the current preset used by the package.',
798
+ )
799
+ @cd.within_closest_package
800
+ def activate():
801
+ preset_lock = presets.get_preset_lock()
802
+ if preset_lock is None:
803
+ console.console.print(
804
+ '[warning]No configured preset to be activated for this package.[/warning]'
805
+ )
806
+ raise typer.Exit(1)
807
+
808
+ preset = presets.get_installed_preset_or_null(preset_lock.preset_name)
809
+ if preset is None:
810
+ if preset_lock.uri is None:
811
+ console.console.print(
812
+ '[error]Preset is not installed. Install it manually, or specify a URI in [item].preset-lock.yml[/item].[/error]'
813
+ )
814
+ raise typer.Exit(1)
815
+ presets.install(preset_lock.uri)
816
+
817
+ preset = presets.get_installed_preset(preset_lock.preset_name)
818
+
819
+ # Install the environment from the preset if it's not already installed.
820
+ presets.optionally_install_environment_from_preset(
821
+ preset, root=presets.get_preset_installation_path(preset_lock.name)
822
+ )
823
+
824
+ # Activate the environment.
825
+ if preset.env is not None:
826
+ environment_command(preset.name)
827
+
828
+ console.console.print(f'[success]Preset [item]{preset.name}[/item] is activated.')
829
+
830
+
831
+ @app.command(
832
+ 'languages',
833
+ rich_help_panel='Configuration',
834
+ help='List the languages available in this environment',
835
+ )
836
+ def languages():
837
+ env = environment.get_environment()
838
+
839
+ console.console.print(
840
+ f'[success]There are [item]{len(env.languages)}[/item] language(s) available.'
841
+ )
842
+
843
+ for language in env.languages:
844
+ console.console.print(
845
+ f'[item]{language.name}[/item], aka [item]{language.readable_name or language.name}[/item]:'
846
+ )
847
+ console.console.print(language)
848
+ console.console.print()
849
+
850
+
851
+ @app.command(
852
+ 'clear, clean',
853
+ rich_help_panel='Management',
854
+ help='Clears cache and build directories.',
855
+ )
856
+ @cd.within_closest_package
857
+ def clear():
858
+ console.console.print('Cleaning cache and build directories...')
859
+ shutil.rmtree('.box', ignore_errors=True)
860
+ shutil.rmtree('build', ignore_errors=True)