forked from MakazhanAlpamys/Soup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.py
More file actions
266 lines (237 loc) · 8.62 KB
/
Copy pathenv.py
File metadata and controls
266 lines (237 loc) · 8.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
"""`soup env` — hermetic env lockfile + ABI status (v0.64.0 Part C).
Sub-commands:
- ``soup env lock`` — snapshot the current env into ``soup-env.lock``.
- ``soup env status`` — print currently-locked env summary.
- ``soup env check`` — compare current env against ``soup-env.lock`` and
report any ABI-sensitive drift (exit 3 on drift).
"""
from __future__ import annotations
from typing import Optional
import typer
from rich.console import Console
from rich.markup import escape
from rich.panel import Panel
from rich.table import Table
from soup_cli.utils.env_lock import (
DEFAULT_LOCK_FILE,
check_abi_compat,
current_declared_bounds_check,
read_lock,
render_install_plan,
snapshot_env,
write_lock,
write_requirements_txt,
)
from soup_cli.utils.paths import is_under_cwd
console = Console()
# Both an ABI drift against the lock and an installed package violating Soup's
# own declared dependency bound exit with this code (#368).
DRIFT_EXIT_CODE = 3
env_app = typer.Typer(
name="env",
help="Hermetic env lockfile + ABI drift detection (v0.64.0).",
no_args_is_help=True,
)
@env_app.command("lock")
def env_lock_cmd(
output: str = typer.Option(
DEFAULT_LOCK_FILE,
"--output",
"-o",
help="Path to write the lock file (default: ./soup-env.lock).",
),
) -> None:
"""Snapshot the current env into a lock file."""
if "\x00" in output:
console.print("[red]output path must not contain null bytes[/]")
raise typer.Exit(2)
if not is_under_cwd(output):
console.print(f"[red]output {escape(output)!r} is outside cwd[/]")
raise typer.Exit(2)
try:
lock = snapshot_env()
write_lock(lock, output)
except (TypeError, ValueError) as exc:
console.print(f"[red]{escape(str(exc))}[/]")
raise typer.Exit(2) from exc
console.print(
Panel(
f"[green]Locked {len(lock.entries)} packages to "
f"{escape(output)}[/]\n"
f"Python: {escape(lock.python_version)} | "
f"Platform: {escape(lock.platform)} | "
f"CUDA: {escape(lock.cuda_version or 'none')}",
title="env lock",
border_style="green",
)
)
@env_app.command("status")
def env_status_cmd(
lock_path: str = typer.Option(
DEFAULT_LOCK_FILE,
"--lock",
help="Path to the lock file (default: ./soup-env.lock).",
),
) -> None:
"""Print currently-locked env summary."""
try:
lock = read_lock(lock_path)
except FileNotFoundError:
console.print(
f"[yellow]No lock file at {escape(lock_path)}; "
"run `soup env lock` first.[/]"
)
raise typer.Exit(1) from None
except (TypeError, ValueError) as exc:
console.print(f"[red]{escape(str(exc))}[/]")
raise typer.Exit(2) from exc
header = Table(title="env status (locked)")
header.add_column("Field")
header.add_column("Value")
header.add_row("soup_version", escape(lock.soup_version))
header.add_row("python_version", escape(lock.python_version))
header.add_row("platform", escape(lock.platform))
header.add_row("cuda_version", escape(lock.cuda_version or "none"))
header.add_row("created_at", escape(lock.created_at))
header.add_row("entries", str(len(lock.entries)))
console.print(header)
if lock.entries:
body = Table(title="packages")
body.add_column("Name")
body.add_column("Version")
body.add_column("Source")
for e in lock.entries:
body.add_row(escape(e.name), escape(e.version), escape(e.source))
console.print(body)
@env_app.command("check")
def env_check_cmd(
lock_path: str = typer.Option(
DEFAULT_LOCK_FILE,
"--lock",
help="Path to the lock file to compare against.",
),
) -> None:
"""Compare the current env against the lock and report drift."""
# Audit installed packages against Soup's OWN declared bounds first — this
# is independent of any lock file, so it catches the `pip install vllm`
# transformers/torch downgrade even for a user who never ran `soup env lock`
# (#368). The bound is read from package metadata, not a hardcoded copy.
bounds = current_declared_bounds_check()
# #368 review finding 5 — run the lock diagnostic REGARDLESS of the bounds
# outcome, so a bounds violation no longer hides "no lock file" / ABI drift.
# `lock_exit` records the lock half's exit code (None = clean); the bounds
# violation takes precedence at the end but is printed after the lock line.
lock_exit = None
try:
locked = read_lock(lock_path)
except FileNotFoundError:
console.print(
f"[red]No lock file at {escape(lock_path)}; "
"run `soup env lock` first.[/]"
)
lock_exit = 1
except (TypeError, ValueError) as exc:
console.print(f"[red]{escape(str(exc))}[/]")
lock_exit = 2
else:
current = snapshot_env()
report = check_abi_compat(locked, current)
if report.ok:
console.print(
Panel(
"[green]ABI-clean.[/] No drift detected.",
title="env check",
border_style="green",
)
)
else:
body = "\n".join(f"- {escape(c)}" for c in report.changes)
console.print(
Panel(
f"[red]{report.drift_count} ABI-sensitive drift(s):[/]\n{body}",
title="env check",
border_style="red",
)
)
lock_exit = DRIFT_EXIT_CODE
if not bounds.ok:
rows = "\n".join(
f"- {escape(v.name)} {escape(v.installed)} violates declared bound "
f"{escape(v.specifier)}"
for v in bounds.violations
)
console.print(
Panel(
f"[red]{bounds.violation_count} package(s) violate this "
f"distribution's own declared bounds:[/]\n{rows}\n\n"
"[yellow]A later install (e.g. `pip install vllm`) likely moved "
"these packages outside the range soup-cli declares. Reinstall "
"soup-cli, or pin the listed package(s) back into range.[/]",
title="env check",
border_style="red",
)
)
raise typer.Exit(DRIFT_EXIT_CODE)
if lock_exit is not None:
raise typer.Exit(lock_exit)
@env_app.command("fix")
def env_fix_cmd(
lock_path: str = typer.Option(
DEFAULT_LOCK_FILE,
"--lock",
help="Path to the lock file to render an install plan from.",
),
fmt: str = typer.Option(
"uv-pip",
"--format",
help="Install-plan format: uv-pip (copy/paste uv commands) | requirements.",
),
output: Optional[str] = typer.Option(
None,
"--output",
"-o",
help="Optionally also write a requirements.txt to this path (under cwd).",
),
) -> None:
"""Render a reproducible install plan from ``soup-env.lock``.
Print-only by design — recreating a venv is environment-dependent, so
v0.71.1 emits the install commands for manual copy/paste (or scripting)
rather than shelling out to a package manager (v0.71.1 #209).
"""
try:
lock = read_lock(lock_path)
except FileNotFoundError:
console.print(
f"[red]No lock file at {escape(lock_path)}; "
"run `soup env lock` first.[/]"
)
raise typer.Exit(1) from None
except (TypeError, ValueError) as exc:
console.print(f"[red]{escape(str(exc))}[/]")
raise typer.Exit(2) from exc
try:
plan = render_install_plan(lock, fmt=fmt)
except (TypeError, ValueError) as exc:
console.print(f"[red]{escape(str(exc))}[/]")
raise typer.Exit(2) from exc
console.print(
Panel(
escape(plan.rstrip("\n")),
title=f"env fix — install plan ({escape(fmt)})",
border_style="green",
)
)
if output is not None:
if "\x00" in output:
console.print("[red]output path must not contain null bytes[/]")
raise typer.Exit(2)
if not is_under_cwd(output):
console.print(f"[red]output {escape(output)!r} is outside cwd[/]")
raise typer.Exit(2)
try:
write_requirements_txt(lock, output)
except (TypeError, ValueError) as exc:
console.print(f"[red]{escape(str(exc))}[/]")
raise typer.Exit(2) from exc
console.print(f"[green]Wrote requirements to {escape(output)}[/]")
__all__ = ["env_app"]