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
smelt/engine/coverage.py
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
"""What a check covered and under which policy, for machine readers of a report.
|
|
2
|
+
|
|
3
|
+
A stored report alone should tell a clean inventory from a clean diff, and say
|
|
4
|
+
which boundaries were exempt or not analyzed at all.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import hashlib
|
|
10
|
+
import json
|
|
11
|
+
from collections import Counter
|
|
12
|
+
from typing import TYPE_CHECKING, Any
|
|
13
|
+
|
|
14
|
+
from smelt.engine.workspace import workspace_members
|
|
15
|
+
from smelt.model import ModuleKind
|
|
16
|
+
|
|
17
|
+
if TYPE_CHECKING:
|
|
18
|
+
from smelt.analysis.context import AnalysisContext
|
|
19
|
+
from smelt.config.models import SmeltConfig
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def policy_hash(config: SmeltConfig) -> str:
|
|
23
|
+
"""Equal for two reports checked under the same resolved config."""
|
|
24
|
+
payload = json.dumps(config.model_dump(mode="json"), sort_keys=True)
|
|
25
|
+
return "sha256:" + hashlib.sha256(payload.encode()).hexdigest()[:16]
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def effective_coverage(
|
|
29
|
+
ctx: AnalysisContext,
|
|
30
|
+
*,
|
|
31
|
+
include_inventory: bool = False,
|
|
32
|
+
include_ordering: bool = False,
|
|
33
|
+
include_test_quality: bool = False,
|
|
34
|
+
) -> dict[str, Any]:
|
|
35
|
+
config = ctx.config
|
|
36
|
+
project = config.project
|
|
37
|
+
architecture = config.architecture
|
|
38
|
+
inventory = ctx.coverage
|
|
39
|
+
report = {
|
|
40
|
+
"mode": project.coverage,
|
|
41
|
+
**inventory.summary(),
|
|
42
|
+
"policy_hash": policy_hash(config),
|
|
43
|
+
"rules": dict(config.rules),
|
|
44
|
+
"root_packages": list(project.root_packages),
|
|
45
|
+
"source_roots": list(project.source_roots),
|
|
46
|
+
"test_roots": list(project.test_roots),
|
|
47
|
+
"exclude": list(project.exclude_patterns),
|
|
48
|
+
"exclusions": [
|
|
49
|
+
entry if isinstance(entry, str) else entry.model_dump(mode="json")
|
|
50
|
+
for entry in project.exclude
|
|
51
|
+
],
|
|
52
|
+
"omitted_workspace_members": _omitted_members(ctx),
|
|
53
|
+
"modules": _classification(ctx),
|
|
54
|
+
"exempt": {
|
|
55
|
+
"composition_root": list(architecture.composition_root),
|
|
56
|
+
"wiring": list(architecture.wiring),
|
|
57
|
+
},
|
|
58
|
+
"third_party": {
|
|
59
|
+
name: layer.third_party.model_dump(mode="json")
|
|
60
|
+
for name, layer in architecture.layers.items()
|
|
61
|
+
},
|
|
62
|
+
"imports": architecture.imports.model_dump(mode="json"),
|
|
63
|
+
"cross_feature": {
|
|
64
|
+
"default": architecture.cross_feature.default,
|
|
65
|
+
"allow": architecture.cross_feature.entries(),
|
|
66
|
+
},
|
|
67
|
+
"contracts": ctx.contracts.describe(),
|
|
68
|
+
"python_targets": ctx.python_targets.describe(),
|
|
69
|
+
"tests": {
|
|
70
|
+
**config.tests.model_dump(mode="json"),
|
|
71
|
+
"layout": config.tests.layout,
|
|
72
|
+
"mirror": config.tests.mirror,
|
|
73
|
+
"unmirrored": list(config.tests.unmirrored),
|
|
74
|
+
},
|
|
75
|
+
}
|
|
76
|
+
if include_inventory:
|
|
77
|
+
report["inventory"] = [entry.to_json() for entry in inventory.entries]
|
|
78
|
+
if include_ordering and ctx.ordering.files:
|
|
79
|
+
report["ordering"] = ctx.ordering.summary()
|
|
80
|
+
if include_test_quality and ctx.test_syntax.modules:
|
|
81
|
+
report["test_quality"] = {
|
|
82
|
+
"mode": "syntactic",
|
|
83
|
+
"files": len(ctx.test_syntax.modules),
|
|
84
|
+
"collected_tests": ctx.test_syntax.collected_tests(),
|
|
85
|
+
"fixture_traversal": False,
|
|
86
|
+
"fixture_return_annotations": True,
|
|
87
|
+
"fixture_return_inference": "consistent known constructor/alias or fixed tuple; no execution",
|
|
88
|
+
"operation_families": [
|
|
89
|
+
"unittest.mock",
|
|
90
|
+
"pytest-mock",
|
|
91
|
+
"pytest.MonkeyPatch.setattr/delattr",
|
|
92
|
+
],
|
|
93
|
+
"instance_provenance": [
|
|
94
|
+
"constructor",
|
|
95
|
+
"annotation",
|
|
96
|
+
"fixture-return",
|
|
97
|
+
"fixture-inferred",
|
|
98
|
+
],
|
|
99
|
+
}
|
|
100
|
+
return report
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _classification(ctx: AnalysisContext) -> dict[str, int]:
|
|
104
|
+
"""Source modules by architecture kind; wiring and central ones also counted apart."""
|
|
105
|
+
counts: Counter[str] = Counter({kind.value: 0 for kind in ModuleKind})
|
|
106
|
+
wiring = central = 0
|
|
107
|
+
for module in ctx.files.sources:
|
|
108
|
+
info = ctx.model.info(module)
|
|
109
|
+
kind = info.kind if info else ModuleKind.UNCLASSIFIED
|
|
110
|
+
counts[kind.value] += 1
|
|
111
|
+
wiring += bool(info and info.wiring)
|
|
112
|
+
central += bool(info and info.central)
|
|
113
|
+
return {
|
|
114
|
+
"total": len(ctx.files.sources),
|
|
115
|
+
**dict(counts),
|
|
116
|
+
"wiring": wiring,
|
|
117
|
+
"central": central,
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def _omitted_members(ctx: AnalysisContext) -> list[dict[str, Any]]:
|
|
122
|
+
"""Workspace members, declared next to the config, whose code is not analyzed."""
|
|
123
|
+
configured = set(ctx.config.project.root_packages)
|
|
124
|
+
analyzed = {
|
|
125
|
+
directory
|
|
126
|
+
for source in ctx.files.sources.values()
|
|
127
|
+
for directory in ctx.files.resolve_path(source.absolute).parents
|
|
128
|
+
}
|
|
129
|
+
omitted: list[dict[str, Any]] = []
|
|
130
|
+
for member in workspace_members(ctx.root, store=ctx.store):
|
|
131
|
+
if member.skipped:
|
|
132
|
+
omitted.append({"path": member.path, "reason": member.skipped})
|
|
133
|
+
continue
|
|
134
|
+
if member.source_root is None:
|
|
135
|
+
continue
|
|
136
|
+
missing = [name for name in member.packages if name not in configured]
|
|
137
|
+
uncovered = [
|
|
138
|
+
f"{member.source_root}/{name}"
|
|
139
|
+
for name in member.packages
|
|
140
|
+
if name in configured
|
|
141
|
+
and ctx.files.resolve_path(ctx.root / member.source_root / name)
|
|
142
|
+
not in analyzed
|
|
143
|
+
]
|
|
144
|
+
reasons = []
|
|
145
|
+
if missing:
|
|
146
|
+
reasons.append("not in project.root_packages: " + ", ".join(missing))
|
|
147
|
+
if uncovered:
|
|
148
|
+
reasons.append("no analyzed source files in: " + ", ".join(uncovered))
|
|
149
|
+
if reasons:
|
|
150
|
+
omitted.append({"path": member.path, "reason": "; ".join(reasons)})
|
|
151
|
+
return omitted
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
"""Explicit version comparisons and optional pytest runs, outside normal checks."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import hashlib
|
|
6
|
+
import json
|
|
7
|
+
import math
|
|
8
|
+
import os
|
|
9
|
+
import subprocess
|
|
10
|
+
import sys
|
|
11
|
+
from collections import Counter, defaultdict, deque
|
|
12
|
+
from dataclasses import dataclass, field
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
from time import perf_counter
|
|
15
|
+
from typing import Any
|
|
16
|
+
|
|
17
|
+
# scripts/evaluate.py is a consumer outside project.root_packages.
|
|
18
|
+
__all__ = ["EvaluationOptions", "evaluate"]
|
|
19
|
+
|
|
20
|
+
_LAUNCHER = """import sys
|
|
21
|
+
from pathlib import Path
|
|
22
|
+
root = Path(sys.argv.pop(1)).resolve()
|
|
23
|
+
expected = root / 'smelt' / '__init__.py'
|
|
24
|
+
if not expected.is_file():
|
|
25
|
+
raise SystemExit(f'missing Smelt package: {expected}')
|
|
26
|
+
sys.path.insert(0, str(root))
|
|
27
|
+
import smelt
|
|
28
|
+
if Path(smelt.__file__).resolve() != expected.resolve():
|
|
29
|
+
raise SystemExit('unexpected Smelt import origin')
|
|
30
|
+
from smelt.cli import main
|
|
31
|
+
raise SystemExit(main(sys.argv[1:]))
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@dataclass(frozen=True)
|
|
36
|
+
class EvaluationOptions:
|
|
37
|
+
config: Path
|
|
38
|
+
baseline: Path
|
|
39
|
+
candidate: Path
|
|
40
|
+
reports: Path
|
|
41
|
+
python: Path = field(default_factory=lambda: Path(sys.executable))
|
|
42
|
+
select: tuple[str, ...] = ("SMT42",)
|
|
43
|
+
timeout: float = 60
|
|
44
|
+
pytest_targets: tuple[str, ...] = ()
|
|
45
|
+
test_python: Path | None = None
|
|
46
|
+
pytest_args: tuple[str, ...] = ()
|
|
47
|
+
python_paths: tuple[Path, ...] = ()
|
|
48
|
+
source_revision: str | None = None
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def evaluate(options: EvaluationOptions) -> dict[str, Any]:
|
|
52
|
+
"""Never fix sources, alter policy, install packages, suppress findings or record debt."""
|
|
53
|
+
if not math.isfinite(options.timeout) or options.timeout <= 0:
|
|
54
|
+
raise ValueError("timeout must be finite and positive")
|
|
55
|
+
config = options.config.resolve()
|
|
56
|
+
policy_hash = hashlib.sha256(config.read_bytes()).hexdigest()
|
|
57
|
+
output = options.reports.resolve()
|
|
58
|
+
output.mkdir(parents=True, exist_ok=True)
|
|
59
|
+
checks: dict[str, Any] = {}
|
|
60
|
+
reports: dict[str, dict[str, Any]] = {}
|
|
61
|
+
for label, package in (
|
|
62
|
+
("baseline", options.baseline),
|
|
63
|
+
("candidate", options.candidate),
|
|
64
|
+
):
|
|
65
|
+
command = [
|
|
66
|
+
str(options.python),
|
|
67
|
+
"-c",
|
|
68
|
+
_LAUNCHER,
|
|
69
|
+
str(package.resolve()),
|
|
70
|
+
"check",
|
|
71
|
+
"--config",
|
|
72
|
+
str(config),
|
|
73
|
+
"--format",
|
|
74
|
+
"json",
|
|
75
|
+
]
|
|
76
|
+
if options.select:
|
|
77
|
+
command.extend(["--select", ",".join(options.select)])
|
|
78
|
+
result = _run_process(command, config.parent, options.timeout)
|
|
79
|
+
report = _report(result)
|
|
80
|
+
checks[label] = result
|
|
81
|
+
(output / f"{label}.stdout.txt").write_text(result["stdout"], encoding="utf-8")
|
|
82
|
+
(output / f"{label}.stderr.txt").write_text(result["stderr"], encoding="utf-8")
|
|
83
|
+
if report is not None:
|
|
84
|
+
reports[label] = report
|
|
85
|
+
payload = (
|
|
86
|
+
report
|
|
87
|
+
if report is not None
|
|
88
|
+
else {
|
|
89
|
+
"schema_version": 1,
|
|
90
|
+
"status": "error",
|
|
91
|
+
"error": {"kind": result["error"], "exit": result["exit"]},
|
|
92
|
+
}
|
|
93
|
+
)
|
|
94
|
+
(output / f"{label}.json").write_text(
|
|
95
|
+
json.dumps(payload, indent=2) + "\n", encoding="utf-8"
|
|
96
|
+
)
|
|
97
|
+
try:
|
|
98
|
+
policy_unchanged = (
|
|
99
|
+
hashlib.sha256(config.read_bytes()).hexdigest() == policy_hash
|
|
100
|
+
)
|
|
101
|
+
except OSError:
|
|
102
|
+
policy_unchanged = False
|
|
103
|
+
analysis_ok = len(reports) == len(checks) and policy_unchanged
|
|
104
|
+
tests: dict[str, Any] = {"executed": False}
|
|
105
|
+
# Do not execute test code after invalid/incomplete analysis input.
|
|
106
|
+
if analysis_ok and options.pytest_targets:
|
|
107
|
+
environment = dict(os.environ)
|
|
108
|
+
if options.python_paths:
|
|
109
|
+
environment["PYTHONPATH"] = os.pathsep.join(
|
|
110
|
+
[
|
|
111
|
+
*(str(path.resolve()) for path in options.python_paths),
|
|
112
|
+
environment.get("PYTHONPATH", ""),
|
|
113
|
+
]
|
|
114
|
+
)
|
|
115
|
+
command = [
|
|
116
|
+
str(options.test_python or options.python),
|
|
117
|
+
"-m",
|
|
118
|
+
"pytest",
|
|
119
|
+
"-q",
|
|
120
|
+
"-p",
|
|
121
|
+
"no:cacheprovider",
|
|
122
|
+
*options.pytest_args,
|
|
123
|
+
*options.pytest_targets,
|
|
124
|
+
"--basetemp",
|
|
125
|
+
str(output / "pytest-temp"),
|
|
126
|
+
]
|
|
127
|
+
tests = {
|
|
128
|
+
"executed": True,
|
|
129
|
+
**_run_process(command, config.parent, options.timeout, env=environment),
|
|
130
|
+
}
|
|
131
|
+
(output / "pytest.stdout.txt").write_text(tests["stdout"], encoding="utf-8")
|
|
132
|
+
(output / "pytest.stderr.txt").write_text(tests["stderr"], encoding="utf-8")
|
|
133
|
+
if tests.get("executed") and tests["exit"] not in (0, 1) and not tests.get("error"):
|
|
134
|
+
tests["error"] = "pytest-incomplete"
|
|
135
|
+
if not tests.get("executed"):
|
|
136
|
+
for name in ("pytest.stdout.txt", "pytest.stderr.txt"):
|
|
137
|
+
(output / name).write_text("", encoding="utf-8")
|
|
138
|
+
failed_to_run = not analysis_ok or bool(tests.get("error"))
|
|
139
|
+
exit_code = (
|
|
140
|
+
2 if failed_to_run else 1 if tests.get("executed") and tests["exit"] != 0 else 0
|
|
141
|
+
)
|
|
142
|
+
document = {
|
|
143
|
+
"schema_version": 1,
|
|
144
|
+
"status": "error"
|
|
145
|
+
if failed_to_run
|
|
146
|
+
else "tests-failed"
|
|
147
|
+
if exit_code
|
|
148
|
+
else "complete",
|
|
149
|
+
"exit_code": exit_code,
|
|
150
|
+
"config": str(config),
|
|
151
|
+
"config_sha256": policy_hash,
|
|
152
|
+
"policy_unchanged": policy_unchanged,
|
|
153
|
+
"source_revision": options.source_revision,
|
|
154
|
+
"analysis": checks,
|
|
155
|
+
"comparison": _compare_reports(reports["baseline"], reports["candidate"])
|
|
156
|
+
if analysis_ok
|
|
157
|
+
else None,
|
|
158
|
+
"pytest": tests,
|
|
159
|
+
"limits": [
|
|
160
|
+
"Static findings require semantic review.",
|
|
161
|
+
"Only explicitly requested pytest targets execute; no mutation testing is implied.",
|
|
162
|
+
"Use a fixed snapshot/revision for source stability; config bytes are checked for concurrent changes.",
|
|
163
|
+
],
|
|
164
|
+
}
|
|
165
|
+
(output / "evaluation.json").write_text(
|
|
166
|
+
json.dumps(document, indent=2) + "\n", encoding="utf-8"
|
|
167
|
+
)
|
|
168
|
+
return document
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def _identity(finding: dict[str, Any]) -> str:
|
|
172
|
+
expected = finding.get("expected") or {}
|
|
173
|
+
return json.dumps(
|
|
174
|
+
[
|
|
175
|
+
finding.get(key)
|
|
176
|
+
for key in (
|
|
177
|
+
"code",
|
|
178
|
+
"path",
|
|
179
|
+
"line",
|
|
180
|
+
"column",
|
|
181
|
+
"source_module",
|
|
182
|
+
"target_module",
|
|
183
|
+
)
|
|
184
|
+
]
|
|
185
|
+
+ [expected.get("test"), expected.get("operation")],
|
|
186
|
+
sort_keys=True,
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def _compare_reports(before: dict[str, Any], after: dict[str, Any]) -> dict[str, Any]:
|
|
191
|
+
"""Compare identical sources/policies; retain multiplicity and changed payloads."""
|
|
192
|
+
remaining: dict[str, deque[dict[str, Any]]] = defaultdict(deque)
|
|
193
|
+
for finding in before["violations"]:
|
|
194
|
+
remaining[_identity(finding)].append(finding)
|
|
195
|
+
added, updated = [], []
|
|
196
|
+
for finding in after["violations"]:
|
|
197
|
+
previous = remaining[_identity(finding)]
|
|
198
|
+
if not previous:
|
|
199
|
+
added.append(finding)
|
|
200
|
+
continue
|
|
201
|
+
old = previous.popleft()
|
|
202
|
+
if old != finding:
|
|
203
|
+
updated.append({"before": old, "after": finding})
|
|
204
|
+
return {
|
|
205
|
+
"before": dict(
|
|
206
|
+
sorted(Counter(v["code"] for v in before["violations"]).items())
|
|
207
|
+
),
|
|
208
|
+
"after": dict(sorted(Counter(v["code"] for v in after["violations"]).items())),
|
|
209
|
+
"added": added,
|
|
210
|
+
"removed": [finding for key in sorted(remaining) for finding in remaining[key]],
|
|
211
|
+
"updated": updated,
|
|
212
|
+
"interpretation": "Recognition changes under the same input; counts are not confirmed defects or test-effectiveness scores.",
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def _run_process(
|
|
217
|
+
args: list[str], cwd: Path, timeout: float, *, env: dict[str, str] | None = None
|
|
218
|
+
) -> dict[str, Any]:
|
|
219
|
+
if not math.isfinite(timeout) or timeout <= 0:
|
|
220
|
+
raise ValueError("timeout must be finite and positive")
|
|
221
|
+
start = perf_counter()
|
|
222
|
+
result: dict[str, Any] = {"args": args, "cwd": str(cwd), "timeout_seconds": timeout}
|
|
223
|
+
try:
|
|
224
|
+
# Argument vectors come from this explicit evaluation command, without a shell.
|
|
225
|
+
process = subprocess.run( # noqa: S603
|
|
226
|
+
args,
|
|
227
|
+
cwd=cwd,
|
|
228
|
+
env=env,
|
|
229
|
+
capture_output=True,
|
|
230
|
+
text=True,
|
|
231
|
+
encoding="utf-8",
|
|
232
|
+
errors="replace",
|
|
233
|
+
timeout=timeout,
|
|
234
|
+
check=False,
|
|
235
|
+
)
|
|
236
|
+
result.update(
|
|
237
|
+
exit=process.returncode,
|
|
238
|
+
stdout=process.stdout,
|
|
239
|
+
stderr=process.stderr,
|
|
240
|
+
error=None,
|
|
241
|
+
)
|
|
242
|
+
except subprocess.TimeoutExpired as exc:
|
|
243
|
+
result.update(
|
|
244
|
+
exit=None,
|
|
245
|
+
stdout=_decoded(exc.stdout),
|
|
246
|
+
stderr=_decoded(exc.stderr),
|
|
247
|
+
error="process-timeout",
|
|
248
|
+
)
|
|
249
|
+
except OSError as exc:
|
|
250
|
+
result.update(
|
|
251
|
+
exit=None, stdout="", stderr=str(exc), error="process-start-failed"
|
|
252
|
+
)
|
|
253
|
+
result["duration_seconds"] = round(perf_counter() - start, 3)
|
|
254
|
+
return result
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
def _decoded(value: str | bytes | None) -> str:
|
|
258
|
+
return (
|
|
259
|
+
value.decode("utf-8", errors="replace")
|
|
260
|
+
if isinstance(value, bytes)
|
|
261
|
+
else value or ""
|
|
262
|
+
)
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
def _report(result: dict[str, Any]) -> dict[str, Any] | None:
|
|
266
|
+
try:
|
|
267
|
+
value = json.loads(result["stdout"])
|
|
268
|
+
except (ValueError, TypeError):
|
|
269
|
+
result["error"] = result["error"] or "invalid-json"
|
|
270
|
+
return None
|
|
271
|
+
if (
|
|
272
|
+
result["exit"] not in (0, 1)
|
|
273
|
+
or not isinstance(value, dict)
|
|
274
|
+
or not isinstance(value.get("status"), str)
|
|
275
|
+
or value.get("status") not in {"passed", "failed"}
|
|
276
|
+
or not isinstance(value.get("violations"), list)
|
|
277
|
+
):
|
|
278
|
+
result["error"] = result["error"] or "analysis-failed"
|
|
279
|
+
return None
|
|
280
|
+
for finding in value["violations"]:
|
|
281
|
+
if (
|
|
282
|
+
not isinstance(finding, dict)
|
|
283
|
+
or not isinstance(finding.get("code"), str)
|
|
284
|
+
or (
|
|
285
|
+
finding.get("expected") is not None
|
|
286
|
+
and not isinstance(finding["expected"], dict)
|
|
287
|
+
)
|
|
288
|
+
):
|
|
289
|
+
result["error"] = "invalid-report"
|
|
290
|
+
return None
|
|
291
|
+
return value
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"""Reuse explicitly file-local rule decisions without narrowing global analysis."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import TYPE_CHECKING
|
|
6
|
+
|
|
7
|
+
from pydantic import TypeAdapter
|
|
8
|
+
|
|
9
|
+
from smelt.diagnostics.violation import Violation
|
|
10
|
+
|
|
11
|
+
if TYPE_CHECKING:
|
|
12
|
+
from collections.abc import Iterator
|
|
13
|
+
|
|
14
|
+
from smelt.analysis.context import AnalysisContext
|
|
15
|
+
from smelt.analysis.files import SourceFile
|
|
16
|
+
from smelt.rules.base import Rule
|
|
17
|
+
|
|
18
|
+
_VIOLATIONS = TypeAdapter(list[Violation])
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def rule_findings(rule: Rule, ctx: AnalysisContext) -> Iterator[Violation]:
|
|
22
|
+
cache = ctx.cache
|
|
23
|
+
if cache is not None and getattr(rule, "member_local", False):
|
|
24
|
+
yield from _member_findings(rule, ctx)
|
|
25
|
+
return
|
|
26
|
+
if cache is not None and getattr(rule, "test_local", False):
|
|
27
|
+
yield from _test_findings(rule, ctx)
|
|
28
|
+
return
|
|
29
|
+
if cache is None or not getattr(rule, "file_local", False):
|
|
30
|
+
yield from rule.check(ctx)
|
|
31
|
+
return
|
|
32
|
+
policy = cache.key("file-policy", ctx.config.model_dump(mode="json"))
|
|
33
|
+
entries: list[tuple[SourceFile, str, list[Violation] | None]] = []
|
|
34
|
+
for source in ctx.files.sources.values():
|
|
35
|
+
key = cache.key(
|
|
36
|
+
"file-check",
|
|
37
|
+
[
|
|
38
|
+
rule.code,
|
|
39
|
+
policy,
|
|
40
|
+
source.module,
|
|
41
|
+
source.path,
|
|
42
|
+
source.is_package,
|
|
43
|
+
ctx.files.content_id(source.path),
|
|
44
|
+
ctx.python_targets.for_path(source.absolute).to_json(),
|
|
45
|
+
],
|
|
46
|
+
)
|
|
47
|
+
entries.append((source, key, cache.read(key, _VIOLATIONS)))
|
|
48
|
+
ctx.store.prefetch(
|
|
49
|
+
source.absolute for source, _, cached in entries if cached is None
|
|
50
|
+
)
|
|
51
|
+
for source, key, cached in entries:
|
|
52
|
+
findings = cached
|
|
53
|
+
if findings is None:
|
|
54
|
+
findings = list(rule.check(ctx.file_view(source)))
|
|
55
|
+
cache.write(key, findings, _VIOLATIONS)
|
|
56
|
+
yield from findings
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _test_findings(rule: Rule, ctx: AnalysisContext) -> Iterator[Violation]:
|
|
60
|
+
cache = ctx.cache
|
|
61
|
+
if cache is None:
|
|
62
|
+
yield from rule.check(ctx)
|
|
63
|
+
return
|
|
64
|
+
policy = cache.key(
|
|
65
|
+
"test-policy",
|
|
66
|
+
[
|
|
67
|
+
ctx.config.model_dump(mode="json"),
|
|
68
|
+
sorted(ctx.files.sources),
|
|
69
|
+
sorted(ctx.files.packages),
|
|
70
|
+
],
|
|
71
|
+
)
|
|
72
|
+
for path in ctx.test_syntax.modules:
|
|
73
|
+
key = cache.key("test-check", [rule.code, policy, ctx.test_syntax.inputs(path)])
|
|
74
|
+
findings = cache.read(key, _VIOLATIONS)
|
|
75
|
+
if findings is None:
|
|
76
|
+
findings = list(rule.check(ctx.test_view(path)))
|
|
77
|
+
cache.write(key, findings, _VIOLATIONS)
|
|
78
|
+
yield from findings
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def _member_findings(rule: Rule, ctx: AnalysisContext) -> Iterator[Violation]:
|
|
82
|
+
cache = ctx.cache
|
|
83
|
+
if cache is None:
|
|
84
|
+
yield from rule.check(ctx)
|
|
85
|
+
return
|
|
86
|
+
policy = cache.key("member-policy", ctx.config.model_dump(mode="json"))
|
|
87
|
+
index = ctx.member_usage
|
|
88
|
+
for path in index.local_paths():
|
|
89
|
+
key = cache.key("member-check", [rule.code, policy, index.decision_key(path)])
|
|
90
|
+
findings = cache.read(key, _VIOLATIONS)
|
|
91
|
+
if findings is None:
|
|
92
|
+
findings = list(rule.check(ctx.member_view(path)))
|
|
93
|
+
cache.write(key, findings, _VIOLATIONS)
|
|
94
|
+
yield from findings
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"""An immutable, in-memory project view addressed by captured Git object IDs."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import fnmatch
|
|
6
|
+
import os
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from typing import TYPE_CHECKING
|
|
9
|
+
|
|
10
|
+
from smelt.analysis.errors import AnalysisError
|
|
11
|
+
from smelt.config.source import SourceStore
|
|
12
|
+
from smelt.engine.changes import _git, read_blobs
|
|
13
|
+
|
|
14
|
+
if TYPE_CHECKING:
|
|
15
|
+
from collections.abc import Iterable, Iterator
|
|
16
|
+
|
|
17
|
+
from smelt.analysis.cache import FactCache
|
|
18
|
+
from smelt.engine.changes import GitIndex
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class GitSourceStore(SourceStore):
|
|
22
|
+
def __init__(
|
|
23
|
+
self, repository: Path, blobs: dict[str, str], *, cache: FactCache | None = None
|
|
24
|
+
) -> None:
|
|
25
|
+
self.repository = repository
|
|
26
|
+
self.blobs = blobs
|
|
27
|
+
self.cache = cache
|
|
28
|
+
self._contents: dict[str, bytes] = {}
|
|
29
|
+
self._dependencies: dict[str, str] = {}
|
|
30
|
+
self._children: dict[Path, set[Path]] = {repository: set()}
|
|
31
|
+
self._objects = {repository / path: oid for path, oid in blobs.items()}
|
|
32
|
+
self._files = set(self._objects)
|
|
33
|
+
for file_path in self._files:
|
|
34
|
+
path = file_path
|
|
35
|
+
while path != repository:
|
|
36
|
+
self._children.setdefault(path.parent, set()).add(path)
|
|
37
|
+
path = path.parent
|
|
38
|
+
|
|
39
|
+
@classmethod
|
|
40
|
+
def from_index(cls, index: GitIndex) -> GitSourceStore:
|
|
41
|
+
return cls(index.repository, index.blobs)
|
|
42
|
+
|
|
43
|
+
@classmethod
|
|
44
|
+
def from_revision(
|
|
45
|
+
cls, repository: Path, revision: str, *, cache: FactCache | None = None
|
|
46
|
+
) -> GitSourceStore:
|
|
47
|
+
blobs: dict[str, str] = {}
|
|
48
|
+
for record in _git(repository, "ls-tree", "-rz", "--full-tree", revision).split(
|
|
49
|
+
b"\0"
|
|
50
|
+
):
|
|
51
|
+
if not record:
|
|
52
|
+
continue
|
|
53
|
+
metadata, raw_path = record.split(b"\t", 1)
|
|
54
|
+
mode, kind, oid = metadata.split()
|
|
55
|
+
path = Path(os.fsdecode(raw_path))
|
|
56
|
+
if kind != b"blob":
|
|
57
|
+
continue
|
|
58
|
+
if mode == b"120000":
|
|
59
|
+
raise AnalysisError(
|
|
60
|
+
f"changed/staged snapshots do not support symbolic links: {path}"
|
|
61
|
+
)
|
|
62
|
+
if path.drive or path.is_absolute() or ".." in path.parts:
|
|
63
|
+
raise AnalysisError(f"unsafe path in Git snapshot: {path}")
|
|
64
|
+
blobs[path.as_posix()] = oid.decode("ascii")
|
|
65
|
+
return cls(repository, blobs, cache=cache)
|
|
66
|
+
|
|
67
|
+
def resolve(self, path: Path) -> Path:
|
|
68
|
+
# Working-tree symlinks and missing directories cannot change a Git view.
|
|
69
|
+
return Path(os.path.abspath(path)) # noqa: PTH100 - Git paths must not follow working-tree symlinks
|
|
70
|
+
|
|
71
|
+
def is_file(self, path: Path) -> bool:
|
|
72
|
+
return path in self._files or self.resolve(path) in self._files
|
|
73
|
+
|
|
74
|
+
def inventory(self) -> Iterable[Path]:
|
|
75
|
+
return self._files
|
|
76
|
+
|
|
77
|
+
def is_dir(self, path: Path) -> bool:
|
|
78
|
+
return path in self._children or self.resolve(path) in self._children
|
|
79
|
+
|
|
80
|
+
def exists(self, path: Path) -> bool:
|
|
81
|
+
return self.is_file(path) or self.is_dir(path)
|
|
82
|
+
|
|
83
|
+
def iterdir(self, path: Path) -> Iterator[Path]:
|
|
84
|
+
yield from sorted(self._children.get(self.resolve(path), ()))
|
|
85
|
+
|
|
86
|
+
def glob(self, path: Path, pattern: str) -> Iterator[Path]:
|
|
87
|
+
root = self.resolve(path)
|
|
88
|
+
parts = Path(pattern).parts
|
|
89
|
+
|
|
90
|
+
def visit(directory: Path, remaining: tuple[str, ...]) -> Iterator[Path]:
|
|
91
|
+
if not remaining:
|
|
92
|
+
yield directory
|
|
93
|
+
elif remaining[0] == "**":
|
|
94
|
+
yield from visit(directory, remaining[1:])
|
|
95
|
+
for child in self.iterdir(directory):
|
|
96
|
+
if self.is_dir(child):
|
|
97
|
+
yield from visit(child, remaining)
|
|
98
|
+
else:
|
|
99
|
+
for child in self.iterdir(directory):
|
|
100
|
+
if fnmatch.fnmatchcase(child.name, remaining[0]):
|
|
101
|
+
yield from visit(child, remaining[1:])
|
|
102
|
+
|
|
103
|
+
yield from visit(root, parts)
|
|
104
|
+
|
|
105
|
+
def identity(self, path: Path) -> str | None:
|
|
106
|
+
resolved = path if path in self._objects else self.resolve(path)
|
|
107
|
+
oid = self._objects.get(resolved)
|
|
108
|
+
if oid is not None:
|
|
109
|
+
self._dependencies[resolved.relative_to(self.repository).as_posix()] = oid
|
|
110
|
+
return f"git:{oid}" if oid is not None else None
|
|
111
|
+
|
|
112
|
+
def dependencies(self) -> dict[str, str]:
|
|
113
|
+
return self._dependencies
|
|
114
|
+
|
|
115
|
+
def prefetch(self, paths: Iterable[Path]) -> None:
|
|
116
|
+
identifiers = set()
|
|
117
|
+
for original in paths:
|
|
118
|
+
oid = self._objects.get(original) or self._objects.get(
|
|
119
|
+
self.resolve(original)
|
|
120
|
+
)
|
|
121
|
+
if oid is not None:
|
|
122
|
+
identifiers.add(oid)
|
|
123
|
+
missing = identifiers - self._contents.keys()
|
|
124
|
+
if missing and self.cache is not None:
|
|
125
|
+
self._contents.update(self.cache.read_blobs(sorted(missing)))
|
|
126
|
+
missing -= self._contents.keys()
|
|
127
|
+
if missing:
|
|
128
|
+
fetched = read_blobs(self.repository, sorted(missing))
|
|
129
|
+
self._contents.update(fetched)
|
|
130
|
+
if self.cache is not None:
|
|
131
|
+
self.cache.write_blobs(fetched)
|
|
132
|
+
|
|
133
|
+
def read_bytes(self, path: Path) -> bytes:
|
|
134
|
+
identity = self.identity(path)
|
|
135
|
+
if identity is None:
|
|
136
|
+
raise FileNotFoundError(str(path))
|
|
137
|
+
oid = identity.removeprefix("git:")
|
|
138
|
+
if oid not in self._contents:
|
|
139
|
+
self.prefetch([path])
|
|
140
|
+
return self._contents[oid]
|