|
16 | 16 | import typing as t |
17 | 17 | from difflib import ndiff |
18 | 18 | from functools import cached_property |
19 | | -from rich.syntax import Syntax |
20 | 19 | from sqlmesh.core import constants as c |
21 | 20 | from sqlmesh.core.console import get_console |
22 | 21 | from sqlmesh.core.macros import RuntimeStage |
| 22 | +from sqlmesh.core.model.common import python_env_payloads |
23 | 23 | from sqlmesh.core.snapshot import Snapshot, SnapshotId, SnapshotTableInfo |
24 | 24 | from sqlmesh.utils.errors import SQLMeshError |
25 | 25 | from sqlmesh.utils.pydantic import PydanticModel |
@@ -331,58 +331,47 @@ def requirements_diff(self) -> str: |
331 | 331 | ) |
332 | 332 | ) |
333 | 333 |
|
334 | | - def environment_statements_diff(self) -> t.List[Syntax]: |
335 | | - PYTHON_ENV = "python_env" |
336 | | - |
337 | | - def extract_python_env(python_env: t.Dict[str, Executable]) -> t.List[str]: |
338 | | - return [ |
339 | | - v.payload if v.is_import or v.is_definition else f"{k} = {v.payload}" |
340 | | - for k, v in python_env.items() |
341 | | - ] |
342 | | - |
| 334 | + def environment_statements_diff(self) -> t.List[t.Tuple[str, str]]: |
343 | 335 | def extract_statements(statements: t.List[EnvironmentStatements], attr: str) -> t.List[str]: |
344 | 336 | return [ |
345 | 337 | expr |
346 | 338 | for statement in statements |
347 | 339 | for expr in ( |
348 | | - extract_python_env(statement.python_env) |
349 | | - if attr == PYTHON_ENV |
| 340 | + python_env_payloads( |
| 341 | + sorted(statement.python_env.items(), key=lambda x: (x[1].kind, x[0])) |
| 342 | + ) |
| 343 | + if attr == "python_env" |
350 | 344 | else getattr(statement, attr) |
351 | 345 | ) |
352 | 346 | ] |
353 | 347 |
|
354 | | - def format_diff(attribute: str) -> t.Optional[Syntax]: |
| 348 | + def format_diff(attribute: str) -> t.Optional[t.Tuple[str, str]]: |
355 | 349 | previous = extract_statements(self.previous_environment_statements, attribute) |
356 | 350 | current = extract_statements(self.environment_statements, attribute) |
357 | 351 |
|
358 | 352 | if previous == current: |
359 | 353 | return None |
360 | 354 |
|
| 355 | + diff_lines = list(ndiff(previous, current)) |
361 | 356 | diff_text = ( |
362 | 357 | f"=== {attribute} ===\n" |
363 | | - if not attribute == PYTHON_ENV |
| 358 | + if not attribute == "python_env" |
364 | 359 | else "=== dependencies ===\n" |
365 | 360 | ) |
366 | 361 |
|
367 | | - diff_lines = list(ndiff(previous, current)) |
368 | 362 | if any(line.startswith(("-", "+")) for line in diff_lines): |
369 | 363 | diff_text += " " + "\n ".join(diff_lines) + "\n" |
370 | 364 |
|
371 | | - return Syntax( |
372 | | - diff_text, "python" if attribute == PYTHON_ENV else "sql", line_numbers=False |
373 | | - ) |
| 365 | + return "python" if attribute == "python_env" else "sql", diff_text |
374 | 366 |
|
375 | 367 | return [ |
376 | 368 | diff |
377 | | - for diff in ( |
378 | | - format_diff(attribute) |
379 | | - for attribute in [ |
380 | | - RuntimeStage.BEFORE_ALL.value, |
381 | | - RuntimeStage.AFTER_ALL.value, |
382 | | - PYTHON_ENV, |
383 | | - ] |
384 | | - ) |
385 | | - if diff is not None |
| 369 | + for attribute in [ |
| 370 | + RuntimeStage.BEFORE_ALL.value, |
| 371 | + RuntimeStage.AFTER_ALL.value, |
| 372 | + "python_env", |
| 373 | + ] |
| 374 | + if (diff := format_diff(attribute)) is not None |
386 | 375 | ] |
387 | 376 |
|
388 | 377 | @property |
|
0 commit comments