smelt-cli 0.2.0__py3-none-win_amd64.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.
- smelt/__init__.py +6 -0
- smelt/__main__.py +5 -0
- smelt/analysis/__init__.py +45 -0
- smelt/analysis/annotations.py +105 -0
- smelt/analysis/cache.py +168 -0
- smelt/analysis/context.py +147 -0
- smelt/analysis/contracts.py +210 -0
- smelt/analysis/coverage.py +377 -0
- smelt/analysis/definition_blocks.py +569 -0
- smelt/analysis/enum_values.py +217 -0
- smelt/analysis/errors.py +11 -0
- smelt/analysis/exports.py +166 -0
- smelt/analysis/files.py +279 -0
- smelt/analysis/function_usage.py +123 -0
- smelt/analysis/graphs.py +77 -0
- smelt/analysis/identity.py +64 -0
- smelt/analysis/import_cache.py +98 -0
- smelt/analysis/imports.py +452 -0
- smelt/analysis/literals.py +17 -0
- smelt/analysis/member_state.py +108 -0
- smelt/analysis/members.py +975 -0
- smelt/analysis/ordering.py +399 -0
- smelt/analysis/ordering_rewrite.py +232 -0
- smelt/analysis/package_exports.py +58 -0
- smelt/analysis/parsing.py +126 -0
- smelt/analysis/python_version.py +164 -0
- smelt/analysis/retained.py +84 -0
- smelt/analysis/syntax.py +109 -0
- smelt/analysis/test_awaits.py +91 -0
- smelt/analysis/test_cases.py +84 -0
- smelt/analysis/test_deadlines.py +90 -0
- smelt/analysis/test_outcomes.py +71 -0
- smelt/analysis/test_subprocesses.py +107 -0
- smelt/analysis/test_syntax.py +1425 -0
- smelt/analysis/test_timing.py +76 -0
- smelt/analysis/type_dispatch.py +193 -0
- smelt/analysis/workspace.py +146 -0
- smelt/cli/__init__.py +3 -0
- smelt/cli/app.py +300 -0
- smelt/cli/commands/__init__.py +62 -0
- smelt/cli/commands/adopt.py +274 -0
- smelt/cli/commands/check.py +151 -0
- smelt/cli/commands/discover.py +51 -0
- smelt/cli/commands/info.py +112 -0
- smelt/cli/session.py +163 -0
- smelt/cli/session_slots.py +43 -0
- smelt/cli/support.py +90 -0
- smelt/config/__init__.py +36 -0
- smelt/config/discovery.py +93 -0
- smelt/config/errors.py +51 -0
- smelt/config/loader.py +282 -0
- smelt/config/models.py +717 -0
- smelt/config/patterns.py +47 -0
- smelt/config/schema.py +23 -0
- smelt/config/source.py +56 -0
- smelt/config/validation.py +180 -0
- smelt/diagnostics/__init__.py +0 -0
- smelt/diagnostics/debt.py +226 -0
- smelt/diagnostics/dedupe.py +41 -0
- smelt/diagnostics/groups.py +109 -0
- smelt/diagnostics/render/__init__.py +0 -0
- smelt/diagnostics/render/machine.py +237 -0
- smelt/diagnostics/render/text.py +321 -0
- smelt/diagnostics/report.py +104 -0
- smelt/diagnostics/suppressions.py +80 -0
- smelt/diagnostics/violation.py +120 -0
- smelt/docs.py +98 -0
- smelt/engine/__init__.py +0 -0
- smelt/engine/briefing.py +630 -0
- smelt/engine/cache.py +65 -0
- smelt/engine/changes.py +260 -0
- smelt/engine/check.py +724 -0
- smelt/engine/coverage.py +151 -0
- smelt/engine/evaluation.py +291 -0
- smelt/engine/file_checks.py +94 -0
- smelt/engine/git_source.py +140 -0
- smelt/engine/inference.py +854 -0
- smelt/engine/mirror_inference.py +95 -0
- smelt/engine/native_git.py +190 -0
- smelt/engine/ordering_fixes.py +132 -0
- smelt/engine/paths.py +163 -0
- smelt/engine/result_cache.py +208 -0
- smelt/engine/rule_settings.py +164 -0
- smelt/engine/schema.py +58 -0
- smelt/engine/staged.py +75 -0
- smelt/engine/workspace.py +5 -0
- smelt/model/__init__.py +9 -0
- smelt/model/architecture.py +234 -0
- smelt/py.typed +0 -0
- smelt/rules/__init__.py +0 -0
- smelt/rules/activation.py +129 -0
- smelt/rules/base.py +131 -0
- smelt/rules/catalog.json +600 -0
- smelt/rules/common.py +164 -0
- smelt/rules/dependencies/__init__.py +0 -0
- smelt/rules/dependencies/composition_root.py +86 -0
- smelt/rules/dependencies/cycles.py +329 -0
- smelt/rules/dependencies/fanout.py +81 -0
- smelt/rules/dependencies/features.py +165 -0
- smelt/rules/dependencies/interfaces.py +84 -0
- smelt/rules/dependencies/layers.py +138 -0
- smelt/rules/dependencies/third_party.py +132 -0
- smelt/rules/member_names.py +13 -0
- smelt/rules/meta.py +74 -0
- smelt/rules/registry.py +132 -0
- smelt/rules/structure/__init__.py +0 -0
- smelt/rules/structure/assignments.py +114 -0
- smelt/rules/structure/contracts.py +71 -0
- smelt/rules/structure/coverage.py +100 -0
- smelt/rules/structure/enum_values.py +50 -0
- smelt/rules/structure/headers.py +143 -0
- smelt/rules/structure/import_style.py +145 -0
- smelt/rules/structure/layout.py +202 -0
- smelt/rules/structure/members.py +116 -0
- smelt/rules/structure/naming.py +132 -0
- smelt/rules/structure/ordering.py +83 -0
- smelt/rules/structure/package_imports.py +117 -0
- smelt/rules/structure/type_dispatch.py +79 -0
- smelt/rules/testing/__init__.py +0 -0
- smelt/rules/testing/awaits.py +46 -0
- smelt/rules/testing/cases.py +47 -0
- smelt/rules/testing/location.py +537 -0
- smelt/rules/testing/mocking.py +140 -0
- smelt/rules/testing/outcomes.py +119 -0
- smelt/rules/testing/private_access.py +93 -0
- smelt/rules/testing/public_attributes.py +62 -0
- smelt/rules/testing/public_functions.py +52 -0
- smelt/rules/testing/timing.py +62 -0
- smelt_cli-0.2.0.data/scripts/smelt.exe +0 -0
- smelt_cli-0.2.0.dist-info/METADATA +103 -0
- smelt_cli-0.2.0.dist-info/RECORD +133 -0
- smelt_cli-0.2.0.dist-info/WHEEL +4 -0
- smelt_cli-0.2.0.dist-info/sboms/smelt-cli.cyclonedx.json +5462 -0
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
from dataclasses import dataclass
|
|
5
|
+
from typing import TYPE_CHECKING
|
|
6
|
+
|
|
7
|
+
if TYPE_CHECKING:
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
DEFAULT_MIRROR = "{path}/test_{module}.py"
|
|
11
|
+
ROOT_MIRROR = "{root}/{path}/test_{module}.py"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
_IMPORTLIB = re.compile(r"import[-_]mode\W*importlib")
|
|
15
|
+
_PYTEST_CONFIGS = ("pyproject.toml", "pytest.ini", "setup.cfg", "tox.ini")
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@dataclass(frozen=True)
|
|
19
|
+
class MirrorGuess:
|
|
20
|
+
pattern: str
|
|
21
|
+
mirrored: int # test files that already sit at their mirrored path
|
|
22
|
+
total: int
|
|
23
|
+
# test roots where pytest cannot import two test_router.py side by side
|
|
24
|
+
name_clashes: tuple[str, ...] = ()
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def infer_mirror(
|
|
28
|
+
root: Path, test_roots: list[str], packages: list[Path]
|
|
29
|
+
) -> MirrorGuess | None:
|
|
30
|
+
"""The mirror pattern most existing tests follow, or None if none follows one."""
|
|
31
|
+
modules = {package.name: _module_paths(package) for package in packages}
|
|
32
|
+
counts = {DEFAULT_MIRROR: 0, ROOT_MIRROR: 0}
|
|
33
|
+
total = 0
|
|
34
|
+
for test_root in test_roots:
|
|
35
|
+
directory = root / test_root
|
|
36
|
+
if not directory.is_dir():
|
|
37
|
+
continue
|
|
38
|
+
for path in sorted(directory.rglob("test_*.py")):
|
|
39
|
+
total += 1
|
|
40
|
+
parts = list(path.relative_to(directory).parent.parts)
|
|
41
|
+
subject = path.stem.removeprefix("test_")
|
|
42
|
+
if any(_mirrors(parts, subject, known) for known in modules.values()):
|
|
43
|
+
counts[DEFAULT_MIRROR] += 1
|
|
44
|
+
if parts and _mirrors(parts[1:], subject, modules.get(parts[0], set())):
|
|
45
|
+
counts[ROOT_MIRROR] += 1
|
|
46
|
+
pattern = max(counts, key=lambda key: counts[key])
|
|
47
|
+
if counts[pattern] == 0:
|
|
48
|
+
return None
|
|
49
|
+
return MirrorGuess(
|
|
50
|
+
pattern, counts[pattern], total, _clashing_test_roots(root, test_roots)
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _clashing_test_roots(root: Path, test_roots: list[str]) -> tuple[str, ...]:
|
|
55
|
+
"""Test roots whose mirrored test files would collide under pytest's defaults.
|
|
56
|
+
|
|
57
|
+
Mirroring repeats file names (``auth/test_router.py``, ``user/test_router.py``).
|
|
58
|
+
pytest's default import mode only tells them apart inside packages, so it needs
|
|
59
|
+
``__init__.py`` files or ``--import-mode=importlib``.
|
|
60
|
+
"""
|
|
61
|
+
found: list[str] = []
|
|
62
|
+
for test_root in test_roots:
|
|
63
|
+
directory = root / test_root
|
|
64
|
+
if not directory.is_dir() or any(directory.rglob("__init__.py")):
|
|
65
|
+
continue
|
|
66
|
+
configs = [
|
|
67
|
+
base / name
|
|
68
|
+
for base in dict.fromkeys((root, directory.parent))
|
|
69
|
+
for name in _PYTEST_CONFIGS
|
|
70
|
+
]
|
|
71
|
+
if not any(
|
|
72
|
+
config.is_file()
|
|
73
|
+
and _IMPORTLIB.search(config.read_text(encoding="utf-8", errors="replace"))
|
|
74
|
+
for config in configs
|
|
75
|
+
):
|
|
76
|
+
found.append(test_root)
|
|
77
|
+
return tuple(found)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _module_paths(package: Path) -> set[str]:
|
|
81
|
+
"""``billing/invoice`` and ``billing/__init__`` style paths inside a package."""
|
|
82
|
+
return {
|
|
83
|
+
path.relative_to(package).with_suffix("").as_posix()
|
|
84
|
+
for path in package.rglob("*.py")
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def _mirrors(directories: list[str], subject: str, known: set[str]) -> bool:
|
|
89
|
+
if "/".join([*directories, subject]) in known:
|
|
90
|
+
return True
|
|
91
|
+
return (
|
|
92
|
+
bool(directories)
|
|
93
|
+
and directories[-1] == subject
|
|
94
|
+
and ("/".join([*directories, "__init__"]) in known)
|
|
95
|
+
)
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
"""Optional read-only native Git service, owned by the current Python process."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import atexit
|
|
6
|
+
import base64
|
|
7
|
+
import contextlib
|
|
8
|
+
import json
|
|
9
|
+
import os
|
|
10
|
+
import subprocess
|
|
11
|
+
from concurrent.futures import ThreadPoolExecutor
|
|
12
|
+
from contextvars import ContextVar
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
from typing import TYPE_CHECKING, Any
|
|
15
|
+
|
|
16
|
+
if TYPE_CHECKING:
|
|
17
|
+
from collections.abc import Iterator
|
|
18
|
+
|
|
19
|
+
_LIMIT = 64 * 1024 * 1024
|
|
20
|
+
_ENABLED = ContextVar("native_git_enabled", default=False)
|
|
21
|
+
_CAPTURED: ContextVar[tuple[Path, str | None, bytes] | None] = ContextVar(
|
|
22
|
+
"native_git_index", default=None
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class GitService:
|
|
27
|
+
def __init__(self, executable: str) -> None:
|
|
28
|
+
self.executable = executable
|
|
29
|
+
self.process = subprocess.Popen( # noqa: S603 - supplied by the installed native launcher
|
|
30
|
+
[executable, "--internal-git"],
|
|
31
|
+
cwd=Path(executable).parent,
|
|
32
|
+
stdin=subprocess.PIPE,
|
|
33
|
+
stdout=subprocess.PIPE,
|
|
34
|
+
stderr=subprocess.DEVNULL,
|
|
35
|
+
creationflags=getattr(subprocess, "CREATE_NO_WINDOW", 0),
|
|
36
|
+
)
|
|
37
|
+
self.executor = ThreadPoolExecutor(
|
|
38
|
+
max_workers=1, thread_name_prefix="smelt-git"
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
def request(self, message: dict[str, Any]) -> dict[str, Any]:
|
|
42
|
+
return self.executor.submit(self._exchange, message).result(timeout=10)
|
|
43
|
+
|
|
44
|
+
def close(self) -> None:
|
|
45
|
+
if self.process.poll() is None:
|
|
46
|
+
self.process.kill()
|
|
47
|
+
self.process.wait(timeout=5)
|
|
48
|
+
self.executor.shutdown(wait=True, cancel_futures=True)
|
|
49
|
+
for stream in (self.process.stdin, self.process.stdout):
|
|
50
|
+
if stream is not None:
|
|
51
|
+
# Closing a buffered input after the child crashed can itself
|
|
52
|
+
# raise BrokenPipeError. Still close the output pipe as well.
|
|
53
|
+
with contextlib.suppress(OSError):
|
|
54
|
+
stream.close()
|
|
55
|
+
|
|
56
|
+
def _exchange(self, message: dict[str, Any]) -> dict[str, Any]:
|
|
57
|
+
if self.process.stdin is None or self.process.stdout is None:
|
|
58
|
+
raise OSError("closed native Git service")
|
|
59
|
+
self.process.stdin.write(json.dumps(message).encode() + b"\n")
|
|
60
|
+
self.process.stdin.flush()
|
|
61
|
+
line = self.process.stdout.readline(_LIMIT)
|
|
62
|
+
if not line.endswith(b"\n"):
|
|
63
|
+
raise OSError("incomplete native Git response")
|
|
64
|
+
response = json.loads(line)
|
|
65
|
+
if not isinstance(response, dict):
|
|
66
|
+
raise ValueError("invalid native Git response")
|
|
67
|
+
return response
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def close() -> None:
|
|
71
|
+
global _service # noqa: PLW0603 - one owned service per Python process
|
|
72
|
+
if _service is not None:
|
|
73
|
+
with contextlib.suppress(OSError, subprocess.TimeoutExpired):
|
|
74
|
+
_service.close()
|
|
75
|
+
_service = None
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
_service: GitService | None = None
|
|
79
|
+
_failed = False
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
atexit.register(close)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
@contextlib.contextmanager
|
|
86
|
+
def check_scope(*, enabled: bool) -> Iterator[None]:
|
|
87
|
+
active = _ENABLED.set(enabled)
|
|
88
|
+
captured = _CAPTURED.set(None)
|
|
89
|
+
try:
|
|
90
|
+
yield
|
|
91
|
+
finally:
|
|
92
|
+
_CAPTURED.reset(captured)
|
|
93
|
+
_ENABLED.reset(active)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def request(operation: str, root: Path, **values: object) -> dict[str, Any] | None:
|
|
97
|
+
global _service, _failed # noqa: PLW0603 - serialized helper ownership
|
|
98
|
+
executable = _executable()
|
|
99
|
+
if not executable:
|
|
100
|
+
return None
|
|
101
|
+
try:
|
|
102
|
+
if _service is None:
|
|
103
|
+
_service = GitService(executable)
|
|
104
|
+
response = _service.request(
|
|
105
|
+
{
|
|
106
|
+
"operation": operation,
|
|
107
|
+
"root": str(root),
|
|
108
|
+
"index_file": os.environ.get("GIT_INDEX_FILE"),
|
|
109
|
+
**values,
|
|
110
|
+
}
|
|
111
|
+
)
|
|
112
|
+
return None if "fallback" in response else response
|
|
113
|
+
except (OSError, ValueError, TimeoutError, subprocess.SubprocessError):
|
|
114
|
+
_failed = True
|
|
115
|
+
close()
|
|
116
|
+
return None
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def capture(root: Path) -> tuple[Path, str | None] | None:
|
|
120
|
+
response = request("capture", root)
|
|
121
|
+
if response is None:
|
|
122
|
+
return None
|
|
123
|
+
try:
|
|
124
|
+
repository = Path(response["repository"]).resolve()
|
|
125
|
+
head = response["head"]
|
|
126
|
+
if head is not None and not isinstance(head, str):
|
|
127
|
+
return None
|
|
128
|
+
records = base64.b64decode(response["records"], validate=True)
|
|
129
|
+
_CAPTURED.set((repository, os.environ.get("GIT_INDEX_FILE"), records))
|
|
130
|
+
return repository, head
|
|
131
|
+
except (KeyError, TypeError, ValueError):
|
|
132
|
+
return None
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def index_records(repository: Path) -> bytes | None:
|
|
136
|
+
if not _executable():
|
|
137
|
+
return None
|
|
138
|
+
captured = _CAPTURED.get()
|
|
139
|
+
if captured is not None and captured[:2] == (
|
|
140
|
+
repository,
|
|
141
|
+
os.environ.get("GIT_INDEX_FILE"),
|
|
142
|
+
):
|
|
143
|
+
return captured[2]
|
|
144
|
+
response = request("index", repository)
|
|
145
|
+
try:
|
|
146
|
+
return (
|
|
147
|
+
base64.b64decode(response["records"], validate=True) if response else None
|
|
148
|
+
)
|
|
149
|
+
except (KeyError, TypeError, ValueError):
|
|
150
|
+
return None
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def read_blobs(repository: Path, identifiers: list[str]) -> dict[str, bytes] | None:
|
|
154
|
+
response = request("blobs", repository, identifiers=identifiers)
|
|
155
|
+
if response is None:
|
|
156
|
+
return None
|
|
157
|
+
try:
|
|
158
|
+
contents = response["blobs"]
|
|
159
|
+
if set(contents) != set(identifiers):
|
|
160
|
+
return None
|
|
161
|
+
return {
|
|
162
|
+
oid: base64.b64decode(contents[oid], validate=True) for oid in identifiers
|
|
163
|
+
}
|
|
164
|
+
except (KeyError, TypeError, ValueError):
|
|
165
|
+
return None
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def tree_records(repository: Path, revision: str) -> bytes | None:
|
|
169
|
+
response = request("tree", repository, identifiers=[revision])
|
|
170
|
+
try:
|
|
171
|
+
return (
|
|
172
|
+
base64.b64decode(response["records"], validate=True) if response else None
|
|
173
|
+
)
|
|
174
|
+
except (KeyError, TypeError, ValueError):
|
|
175
|
+
return None
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def _executable() -> str | None:
|
|
179
|
+
if (
|
|
180
|
+
not _ENABLED.get()
|
|
181
|
+
or _failed
|
|
182
|
+
or os.environ.get("SMELT_NATIVE_GIT") == "0"
|
|
183
|
+
or any(
|
|
184
|
+
name.startswith("GIT_")
|
|
185
|
+
and name not in {"GIT_PAGER", "GIT_OPTIONAL_LOCKS", "GIT_INDEX_FILE"}
|
|
186
|
+
for name in os.environ
|
|
187
|
+
)
|
|
188
|
+
):
|
|
189
|
+
return None
|
|
190
|
+
return os.environ.get("SMELT_NATIVE_CLIENT")
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"""Explicit, scoped ordering fixes; analysis and normal checks remain read-only."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import codecs
|
|
6
|
+
import io
|
|
7
|
+
import os
|
|
8
|
+
import stat
|
|
9
|
+
import tempfile
|
|
10
|
+
import tokenize
|
|
11
|
+
from collections import defaultdict
|
|
12
|
+
from dataclasses import dataclass, field
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
from typing import TYPE_CHECKING, Any
|
|
15
|
+
|
|
16
|
+
from smelt.analysis.ordering_rewrite import rewrite_order
|
|
17
|
+
from smelt.analysis.parsing import AnalysisError
|
|
18
|
+
|
|
19
|
+
if TYPE_CHECKING:
|
|
20
|
+
from smelt.engine.check import CheckOutcome
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@dataclass
|
|
24
|
+
class OrderingFixes:
|
|
25
|
+
files: list[str] = field(default_factory=list)
|
|
26
|
+
sections: int = 0
|
|
27
|
+
skipped: list[dict[str, str]] = field(default_factory=list)
|
|
28
|
+
|
|
29
|
+
def skip(self, path: str, reason: str) -> None:
|
|
30
|
+
self.skipped.append({"path": path, "reason": reason})
|
|
31
|
+
|
|
32
|
+
def to_json(self) -> dict[str, Any]:
|
|
33
|
+
return {"files": self.files, "sections": self.sections, "skipped": self.skipped}
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@dataclass(frozen=True)
|
|
37
|
+
class _Prepared:
|
|
38
|
+
path: str
|
|
39
|
+
target: Path
|
|
40
|
+
original: bytes
|
|
41
|
+
replacement: bytes
|
|
42
|
+
sections: int
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def apply_ordering_fixes(outcome: CheckOutcome) -> OrderingFixes:
|
|
46
|
+
"""Fix only visible SMT313 sections after scope, selection, ignores and debt."""
|
|
47
|
+
fixes = OrderingFixes()
|
|
48
|
+
selected: dict[str, set[str]] = defaultdict(set)
|
|
49
|
+
for violation in outcome.report.violations:
|
|
50
|
+
if violation.code != "SMT313" or violation.path is None:
|
|
51
|
+
continue
|
|
52
|
+
expected = violation.expected or {}
|
|
53
|
+
if not expected.get("fixable") or not isinstance(expected.get("section"), str):
|
|
54
|
+
fixes.skip(
|
|
55
|
+
violation.path,
|
|
56
|
+
str(expected.get("fix_reason") or "section requires manual review"),
|
|
57
|
+
)
|
|
58
|
+
continue
|
|
59
|
+
selected[violation.path].add(expected["section"])
|
|
60
|
+
pending: list[_Prepared] = []
|
|
61
|
+
for path, keys in sorted(selected.items()):
|
|
62
|
+
try:
|
|
63
|
+
prepared = _prepare(outcome, path, keys)
|
|
64
|
+
if prepared.replacement != prepared.original:
|
|
65
|
+
pending.append(prepared)
|
|
66
|
+
except (AnalysisError, OSError, UnicodeError, SyntaxError) as exc:
|
|
67
|
+
fixes.skip(path, str(exc))
|
|
68
|
+
for prepared in pending:
|
|
69
|
+
try:
|
|
70
|
+
_write(prepared, outcome.context.root)
|
|
71
|
+
fixes.files.append(prepared.path)
|
|
72
|
+
fixes.sections += prepared.sections
|
|
73
|
+
except (AnalysisError, OSError) as exc:
|
|
74
|
+
fixes.skip(prepared.path, str(exc))
|
|
75
|
+
return fixes
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _local_target(target: Path, root: Path) -> bool:
|
|
79
|
+
return target.resolve().is_relative_to(root) and not any(
|
|
80
|
+
part.is_symlink()
|
|
81
|
+
for part in (target, *target.parents)
|
|
82
|
+
if part.is_relative_to(root)
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def _prepare(outcome: CheckOutcome, path: str, keys: set[str]) -> _Prepared:
|
|
87
|
+
ctx = outcome.context
|
|
88
|
+
source = ctx.files.source_for_path(path)
|
|
89
|
+
if source is None or not _local_target(source.absolute, ctx.root):
|
|
90
|
+
raise AnalysisError(
|
|
91
|
+
"safe fixes require a regular source file inside the project root"
|
|
92
|
+
)
|
|
93
|
+
result = ctx.ordering.files[path]
|
|
94
|
+
sections = [
|
|
95
|
+
section for section in result.sections if section.key in keys and section.edges
|
|
96
|
+
]
|
|
97
|
+
original = source.absolute.read_bytes()
|
|
98
|
+
encoding, _ = tokenize.detect_encoding(io.BytesIO(original).readline)
|
|
99
|
+
if codecs.lookup(encoding).name not in {"utf-8", "utf-8-sig"}:
|
|
100
|
+
raise AnalysisError("safe ordering fixes support UTF-8 source only")
|
|
101
|
+
text = original.decode(encoding)
|
|
102
|
+
if text.replace("\r\n", "\n").replace("\r", "\n") != ctx.files.read_text(path):
|
|
103
|
+
raise AnalysisError("source changed since the check; run the check again")
|
|
104
|
+
rewritten = rewrite_order(text, result, sections)
|
|
105
|
+
return _Prepared(
|
|
106
|
+
path, source.absolute, original, rewritten.encode(encoding), len(sections)
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def _write(prepared: _Prepared, root: Path) -> None:
|
|
111
|
+
target = prepared.target
|
|
112
|
+
if not _local_target(target, root) or target.read_bytes() != prepared.original:
|
|
113
|
+
raise AnalysisError("source changed since fix preparation; no replacement made")
|
|
114
|
+
temporary: Path | None = None
|
|
115
|
+
try:
|
|
116
|
+
with tempfile.NamedTemporaryFile(
|
|
117
|
+
dir=target.parent, prefix=".smelt-order-", suffix=".tmp", delete=False
|
|
118
|
+
) as handle:
|
|
119
|
+
temporary = Path(handle.name)
|
|
120
|
+
handle.write(prepared.replacement)
|
|
121
|
+
handle.flush()
|
|
122
|
+
os.fsync(handle.fileno())
|
|
123
|
+
temporary.chmod(stat.S_IMODE(target.stat().st_mode))
|
|
124
|
+
if not _local_target(target, root) or target.read_bytes() != prepared.original:
|
|
125
|
+
raise AnalysisError(
|
|
126
|
+
"source changed during fix preparation; no replacement made"
|
|
127
|
+
)
|
|
128
|
+
temporary.replace(target)
|
|
129
|
+
finally:
|
|
130
|
+
if temporary is not None and temporary.exists():
|
|
131
|
+
temporary.chmod(stat.S_IMODE(temporary.stat().st_mode) | stat.S_IWUSR)
|
|
132
|
+
temporary.unlink()
|
smelt/engine/paths.py
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import TYPE_CHECKING
|
|
4
|
+
|
|
5
|
+
from smelt.config.errors import ConfigIssue, did_you_mean
|
|
6
|
+
from smelt.config.models import CrossFeatureAllowance
|
|
7
|
+
from smelt.config.patterns import module_matches
|
|
8
|
+
|
|
9
|
+
if TYPE_CHECKING:
|
|
10
|
+
from smelt.analysis.context import AnalysisContext
|
|
11
|
+
from smelt.config.loader import LoadedConfig
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def project_metadata(loaded: LoadedConfig) -> dict[str, str]:
|
|
15
|
+
root = (
|
|
16
|
+
loaded.source.resolve(loaded.root) if loaded.source else loaded.root.resolve()
|
|
17
|
+
)
|
|
18
|
+
repository = next(
|
|
19
|
+
(
|
|
20
|
+
directory
|
|
21
|
+
for directory in (root, *root.parents)
|
|
22
|
+
if (directory / ".git").exists()
|
|
23
|
+
),
|
|
24
|
+
root,
|
|
25
|
+
)
|
|
26
|
+
prefix = root.relative_to(repository).as_posix()
|
|
27
|
+
return {
|
|
28
|
+
"root": root.as_posix(),
|
|
29
|
+
"config": (
|
|
30
|
+
loaded.source.resolve(loaded.path)
|
|
31
|
+
if loaded.source
|
|
32
|
+
else loaded.path.resolve()
|
|
33
|
+
).as_posix(),
|
|
34
|
+
"repository_root": repository.as_posix(),
|
|
35
|
+
"repository_prefix": "" if prefix == "." else prefix,
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def missing_paths(ctx: AnalysisContext) -> list[ConfigIssue]:
|
|
40
|
+
"""Config entries that point at nothing, which would silently switch rules off."""
|
|
41
|
+
issues: list[ConfigIssue] = []
|
|
42
|
+
project = ctx.config.project
|
|
43
|
+
for index, directory in enumerate(project.source_roots):
|
|
44
|
+
if not ctx.store.is_dir(ctx.root / directory):
|
|
45
|
+
issues.append(
|
|
46
|
+
ConfigIssue(
|
|
47
|
+
f"project.source_roots[{index}]",
|
|
48
|
+
f'directory "{directory}" does not exist',
|
|
49
|
+
)
|
|
50
|
+
)
|
|
51
|
+
if ctx.config.tests.layout == "mirror":
|
|
52
|
+
for index, directory in enumerate(project.test_roots):
|
|
53
|
+
if not ctx.store.is_dir(ctx.root / directory):
|
|
54
|
+
issues.append(
|
|
55
|
+
ConfigIssue(
|
|
56
|
+
f"project.test_roots[{index}]",
|
|
57
|
+
f'directory "{directory}" does not exist',
|
|
58
|
+
)
|
|
59
|
+
)
|
|
60
|
+
for index, package in enumerate(project.root_packages):
|
|
61
|
+
if package in ctx.files.missing_root_packages:
|
|
62
|
+
roots = ", ".join(project.source_roots)
|
|
63
|
+
issues.append(
|
|
64
|
+
ConfigIssue(
|
|
65
|
+
f"project.root_packages[{index}]",
|
|
66
|
+
f'package "{package}" is in none of the source roots ({roots})',
|
|
67
|
+
)
|
|
68
|
+
)
|
|
69
|
+
issues.extend(_missing_modules(ctx))
|
|
70
|
+
issues.extend(_conflicting_package_policies(ctx))
|
|
71
|
+
matched_contracts = {contract.name for contract in ctx.contracts.resolved}
|
|
72
|
+
issues.extend(
|
|
73
|
+
ConfigIssue(
|
|
74
|
+
f"structure.contracts.{name}.match",
|
|
75
|
+
f'"{contract.match}" matches no analyzed package',
|
|
76
|
+
)
|
|
77
|
+
for name, contract in ctx.config.structure.contracts.items()
|
|
78
|
+
if name not in matched_contracts
|
|
79
|
+
)
|
|
80
|
+
for index, allowance in enumerate(ctx.config.architecture.cross_feature.allow):
|
|
81
|
+
if not isinstance(allowance, CrossFeatureAllowance):
|
|
82
|
+
continue
|
|
83
|
+
for key, value in (("from", allowance.source), ("to", allowance.target)):
|
|
84
|
+
feature = value.split(".")[0]
|
|
85
|
+
if feature not in ctx.model.features:
|
|
86
|
+
issues.append(
|
|
87
|
+
ConfigIssue(
|
|
88
|
+
f"architecture.cross_feature.allow[{index}].{key}",
|
|
89
|
+
f'unknown feature "{feature}"{did_you_mean(feature, ctx.model.features)}',
|
|
90
|
+
)
|
|
91
|
+
)
|
|
92
|
+
return issues
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def _conflicting_package_policies(ctx: AnalysisContext) -> list[ConfigIssue]:
|
|
96
|
+
policies: dict[tuple[str, str], object] = {}
|
|
97
|
+
issues: list[ConfigIssue] = []
|
|
98
|
+
for contract in ctx.contracts.resolved:
|
|
99
|
+
for key, label in (
|
|
100
|
+
("imports", "import styles"),
|
|
101
|
+
("module_docstrings", "module docstring policies"),
|
|
102
|
+
("future_annotations", "future annotation policies"),
|
|
103
|
+
("private_modules", "private module naming policies"),
|
|
104
|
+
("ordering", "definition ordering policies"),
|
|
105
|
+
("type_dispatch", "type dispatch policies"),
|
|
106
|
+
):
|
|
107
|
+
policy = getattr(contract.spec, key)
|
|
108
|
+
if policy is None:
|
|
109
|
+
continue
|
|
110
|
+
previous = policies.setdefault((contract.package, key), policy)
|
|
111
|
+
if previous != policy:
|
|
112
|
+
issues.append(
|
|
113
|
+
ConfigIssue(
|
|
114
|
+
f"structure.contracts.{contract.name}.{key}",
|
|
115
|
+
f"conflicting {label} for {contract.package}; use one policy per package",
|
|
116
|
+
)
|
|
117
|
+
)
|
|
118
|
+
return issues
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def _missing_modules(ctx: AnalysisContext) -> list[ConfigIssue]:
|
|
122
|
+
arch = ctx.config.architecture
|
|
123
|
+
files = ctx.files
|
|
124
|
+
packages = sorted(files.packages)
|
|
125
|
+
known = {*files.sources, *files.packages}
|
|
126
|
+
issues: list[ConfigIssue] = []
|
|
127
|
+
features = arch.features
|
|
128
|
+
if features is not None:
|
|
129
|
+
key = "root" if features.root is not None else "pattern"
|
|
130
|
+
package = features.root or (features.pattern or "").rsplit(".", 1)[0]
|
|
131
|
+
if package not in files.packages:
|
|
132
|
+
issues.append(
|
|
133
|
+
ConfigIssue(
|
|
134
|
+
f"architecture.features.{key}",
|
|
135
|
+
f'package "{package}" does not exist{did_you_mean(package, packages)}',
|
|
136
|
+
)
|
|
137
|
+
)
|
|
138
|
+
for key in ("shared", "composition_root"):
|
|
139
|
+
for index, module in enumerate(getattr(arch, key)):
|
|
140
|
+
if module not in known:
|
|
141
|
+
issues.append(
|
|
142
|
+
ConfigIssue(
|
|
143
|
+
f"architecture.{key}[{index}]",
|
|
144
|
+
f'module "{module}" does not exist{did_you_mean(module, known)}',
|
|
145
|
+
)
|
|
146
|
+
)
|
|
147
|
+
issues.extend(
|
|
148
|
+
ConfigIssue(
|
|
149
|
+
f"architecture.modules.{module}",
|
|
150
|
+
f'module "{module}" does not exist{did_you_mean(module, known)}',
|
|
151
|
+
)
|
|
152
|
+
for module in arch.modules
|
|
153
|
+
if module not in known
|
|
154
|
+
)
|
|
155
|
+
for index, pattern in enumerate(arch.wiring):
|
|
156
|
+
if not any(module_matches(pattern, module) for module in known):
|
|
157
|
+
issues.append(
|
|
158
|
+
ConfigIssue(
|
|
159
|
+
f"architecture.wiring[{index}]",
|
|
160
|
+
f'"{pattern}" matches no module',
|
|
161
|
+
)
|
|
162
|
+
)
|
|
163
|
+
return issues
|