rbx.cp 0.6.0__py3-none-any.whl → 0.7.0__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.
rbx/box/linting.py ADDED
@@ -0,0 +1,95 @@
1
+ import difflib
2
+ import pathlib
3
+ from typing import Optional, Type
4
+
5
+ import yamlfix
6
+ import yamlfix.model
7
+ from pydantic import BaseModel
8
+
9
+ from rbx import console
10
+ from rbx.box.cd import is_contest_package, is_preset_package, is_problem_package
11
+ from rbx.box.contest.schema import Contest
12
+ from rbx.box.presets import get_preset_yaml
13
+ from rbx.box.presets.schema import Preset
14
+ from rbx.box.schema import Package
15
+ from rbx.box.stats import find_problem_packages_from_contest
16
+ from rbx.utils import uploaded_schema_path
17
+
18
+
19
+ def fix_language_server(path: pathlib.Path, model_cls: Type[BaseModel]) -> bool:
20
+ stream = []
21
+ with path.open('r') as f:
22
+ for line in f:
23
+ if line.strip().startswith('# yaml-language-server:'):
24
+ continue
25
+ stream.append(line)
26
+ if line.startswith('---'):
27
+ stream.append(
28
+ f'# yaml-language-server: $schema={uploaded_schema_path(model_cls)}\n'
29
+ )
30
+ content = ''.join(stream)
31
+ orig_text = path.read_text()
32
+ path.write_text(content)
33
+ return orig_text != content
34
+
35
+
36
+ def fix_yaml(
37
+ path: pathlib.Path,
38
+ verbose: bool = True,
39
+ print_diff: bool = False,
40
+ model_cls: Optional[Type[BaseModel]] = None,
41
+ ):
42
+ orig_text = path.read_text()
43
+
44
+ # Config to go hand-to-hand with VSCode YAML extension,
45
+ # which we offer first class support to. Unfortunately,
46
+ # YAML extension is not perfect :(
47
+ config = yamlfix.model.YamlfixConfig(
48
+ quote_basic_values=True,
49
+ quote_representation='"',
50
+ comments_min_spaces_from_content=1,
51
+ )
52
+ _, changed = yamlfix.fix_files([str(path)], dry_run=False, config=config)
53
+
54
+ if model_cls is not None:
55
+ if fix_language_server(path, model_cls):
56
+ changed = True
57
+
58
+ if changed and verbose:
59
+ console.console.print(
60
+ f'Formatting [item]{path}[/item].',
61
+ )
62
+
63
+ if print_diff and changed:
64
+ unified_diff = difflib.unified_diff(
65
+ orig_text.splitlines(), path.read_text().splitlines()
66
+ )
67
+ console.console.print(
68
+ f'Diff for [item]{path}[/item].\n' + '\n'.join(unified_diff),
69
+ )
70
+
71
+
72
+ def fix_package(root: pathlib.Path = pathlib.Path(), print_diff: bool = False):
73
+ if is_preset_package(root):
74
+ fix_yaml(root / 'preset.rbx.yml', model_cls=Preset, print_diff=print_diff)
75
+ preset = get_preset_yaml(root)
76
+ if preset.problem is not None:
77
+ fix_yaml(
78
+ root / preset.problem / 'problem.rbx.yml',
79
+ model_cls=Package,
80
+ print_diff=print_diff,
81
+ )
82
+ if preset.contest is not None:
83
+ fix_package(root / preset.contest, print_diff=print_diff)
84
+ return
85
+
86
+ if is_problem_package(root):
87
+ fix_yaml(root / 'problem.rbx.yml', model_cls=Package, print_diff=print_diff)
88
+ if is_contest_package(root):
89
+ fix_yaml(root / 'contest.rbx.yml', model_cls=Contest, print_diff=print_diff)
90
+ for problem in find_problem_packages_from_contest(root):
91
+ fix_yaml(
92
+ problem / 'problem.rbx.yml',
93
+ model_cls=Package,
94
+ print_diff=print_diff,
95
+ )
rbx/box/package.py CHANGED
@@ -10,10 +10,9 @@ import ruyaml
10
10
  import typer
11
11
  from pydantic import ValidationError
12
12
 
13
- from rbx import config, console, utils
14
- from rbx.box import cd, environment
13
+ from rbx import console, utils
14
+ from rbx.box import cd
15
15
  from rbx.box.environment import get_sandbox_type
16
- from rbx.box.presets import get_installed_preset_or_null, get_preset_lock
17
16
  from rbx.box.schema import (
18
17
  CodeItem,
19
18
  ExpectedOutcome,
@@ -38,33 +37,6 @@ TEMP_DIR = None
38
37
  CACHE_STEP_VERSION = 1
39
38
 
40
39
 
41
- def warn_preset_deactivated(root: pathlib.Path = pathlib.Path()):
42
- preset_lock = get_preset_lock(root)
43
- if preset_lock is None:
44
- return
45
-
46
- preset = get_installed_preset_or_null(preset_lock.preset_name)
47
- if preset is None:
48
- console.console.print(
49
- f'[warning]WARNING: [item]{preset_lock.preset_name}[/item] is not installed. '
50
- 'Run [item]rbx presets sync && rbx activate[/item] to install and activate this preset.'
51
- )
52
- console.console.print()
53
- return
54
-
55
- if preset.env is not None and (
56
- not environment.get_environment_path(preset.name).is_file()
57
- or config.get_config().boxEnvironment != preset.name
58
- ):
59
- console.console.print(
60
- '[warning]WARNING: This package uses a preset that configures a custom environment, '
61
- f' but instead you are using the environment [item]{config.get_config().boxEnvironment}[/item]. '
62
- 'Run [item]rbx activate[/item] to use the environment configured by your preset.'
63
- )
64
- console.console.print()
65
- return
66
-
67
-
68
40
  @functools.cache
69
41
  def find_problem_yaml(root: pathlib.Path = pathlib.Path()) -> Optional[pathlib.Path]:
70
42
  root = root.resolve()
@@ -74,7 +46,6 @@ def find_problem_yaml(root: pathlib.Path = pathlib.Path()) -> Optional[pathlib.P
74
46
  problem_yaml_path = root / YAML_NAME
75
47
  if not problem_yaml_path.is_file():
76
48
  return None
77
- warn_preset_deactivated(root)
78
49
  return problem_yaml_path
79
50
 
80
51
 
@@ -130,9 +101,7 @@ def save_package(
130
101
  def get_ruyaml(root: pathlib.Path = pathlib.Path()) -> Tuple[ruyaml.YAML, ruyaml.Any]:
131
102
  problem_yaml_path = find_problem_yaml(root)
132
103
  if problem_yaml_path is None:
133
- console.console.print(
134
- f'Problem not found in {pathlib.Path().absolute()}', style='error'
135
- )
104
+ console.console.print(f'[error]Problem not found in {root.absolute()}[/error]')
136
105
  raise typer.Exit(1)
137
106
  res = ruyaml.YAML()
138
107
  return res, res.load(problem_yaml_path.read_text())