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.
Files changed (133) hide show
  1. smelt/__init__.py +6 -0
  2. smelt/__main__.py +5 -0
  3. smelt/analysis/__init__.py +45 -0
  4. smelt/analysis/annotations.py +105 -0
  5. smelt/analysis/cache.py +168 -0
  6. smelt/analysis/context.py +147 -0
  7. smelt/analysis/contracts.py +210 -0
  8. smelt/analysis/coverage.py +377 -0
  9. smelt/analysis/definition_blocks.py +569 -0
  10. smelt/analysis/enum_values.py +217 -0
  11. smelt/analysis/errors.py +11 -0
  12. smelt/analysis/exports.py +166 -0
  13. smelt/analysis/files.py +279 -0
  14. smelt/analysis/function_usage.py +123 -0
  15. smelt/analysis/graphs.py +77 -0
  16. smelt/analysis/identity.py +64 -0
  17. smelt/analysis/import_cache.py +98 -0
  18. smelt/analysis/imports.py +452 -0
  19. smelt/analysis/literals.py +17 -0
  20. smelt/analysis/member_state.py +108 -0
  21. smelt/analysis/members.py +975 -0
  22. smelt/analysis/ordering.py +399 -0
  23. smelt/analysis/ordering_rewrite.py +232 -0
  24. smelt/analysis/package_exports.py +58 -0
  25. smelt/analysis/parsing.py +126 -0
  26. smelt/analysis/python_version.py +164 -0
  27. smelt/analysis/retained.py +84 -0
  28. smelt/analysis/syntax.py +109 -0
  29. smelt/analysis/test_awaits.py +91 -0
  30. smelt/analysis/test_cases.py +84 -0
  31. smelt/analysis/test_deadlines.py +90 -0
  32. smelt/analysis/test_outcomes.py +71 -0
  33. smelt/analysis/test_subprocesses.py +107 -0
  34. smelt/analysis/test_syntax.py +1425 -0
  35. smelt/analysis/test_timing.py +76 -0
  36. smelt/analysis/type_dispatch.py +193 -0
  37. smelt/analysis/workspace.py +146 -0
  38. smelt/cli/__init__.py +3 -0
  39. smelt/cli/app.py +300 -0
  40. smelt/cli/commands/__init__.py +62 -0
  41. smelt/cli/commands/adopt.py +274 -0
  42. smelt/cli/commands/check.py +151 -0
  43. smelt/cli/commands/discover.py +51 -0
  44. smelt/cli/commands/info.py +112 -0
  45. smelt/cli/session.py +163 -0
  46. smelt/cli/session_slots.py +43 -0
  47. smelt/cli/support.py +90 -0
  48. smelt/config/__init__.py +36 -0
  49. smelt/config/discovery.py +93 -0
  50. smelt/config/errors.py +51 -0
  51. smelt/config/loader.py +282 -0
  52. smelt/config/models.py +717 -0
  53. smelt/config/patterns.py +47 -0
  54. smelt/config/schema.py +23 -0
  55. smelt/config/source.py +56 -0
  56. smelt/config/validation.py +180 -0
  57. smelt/diagnostics/__init__.py +0 -0
  58. smelt/diagnostics/debt.py +226 -0
  59. smelt/diagnostics/dedupe.py +41 -0
  60. smelt/diagnostics/groups.py +109 -0
  61. smelt/diagnostics/render/__init__.py +0 -0
  62. smelt/diagnostics/render/machine.py +237 -0
  63. smelt/diagnostics/render/text.py +321 -0
  64. smelt/diagnostics/report.py +104 -0
  65. smelt/diagnostics/suppressions.py +80 -0
  66. smelt/diagnostics/violation.py +120 -0
  67. smelt/docs.py +98 -0
  68. smelt/engine/__init__.py +0 -0
  69. smelt/engine/briefing.py +630 -0
  70. smelt/engine/cache.py +65 -0
  71. smelt/engine/changes.py +260 -0
  72. smelt/engine/check.py +724 -0
  73. smelt/engine/coverage.py +151 -0
  74. smelt/engine/evaluation.py +291 -0
  75. smelt/engine/file_checks.py +94 -0
  76. smelt/engine/git_source.py +140 -0
  77. smelt/engine/inference.py +854 -0
  78. smelt/engine/mirror_inference.py +95 -0
  79. smelt/engine/native_git.py +190 -0
  80. smelt/engine/ordering_fixes.py +132 -0
  81. smelt/engine/paths.py +163 -0
  82. smelt/engine/result_cache.py +208 -0
  83. smelt/engine/rule_settings.py +164 -0
  84. smelt/engine/schema.py +58 -0
  85. smelt/engine/staged.py +75 -0
  86. smelt/engine/workspace.py +5 -0
  87. smelt/model/__init__.py +9 -0
  88. smelt/model/architecture.py +234 -0
  89. smelt/py.typed +0 -0
  90. smelt/rules/__init__.py +0 -0
  91. smelt/rules/activation.py +129 -0
  92. smelt/rules/base.py +131 -0
  93. smelt/rules/catalog.json +600 -0
  94. smelt/rules/common.py +164 -0
  95. smelt/rules/dependencies/__init__.py +0 -0
  96. smelt/rules/dependencies/composition_root.py +86 -0
  97. smelt/rules/dependencies/cycles.py +329 -0
  98. smelt/rules/dependencies/fanout.py +81 -0
  99. smelt/rules/dependencies/features.py +165 -0
  100. smelt/rules/dependencies/interfaces.py +84 -0
  101. smelt/rules/dependencies/layers.py +138 -0
  102. smelt/rules/dependencies/third_party.py +132 -0
  103. smelt/rules/member_names.py +13 -0
  104. smelt/rules/meta.py +74 -0
  105. smelt/rules/registry.py +132 -0
  106. smelt/rules/structure/__init__.py +0 -0
  107. smelt/rules/structure/assignments.py +114 -0
  108. smelt/rules/structure/contracts.py +71 -0
  109. smelt/rules/structure/coverage.py +100 -0
  110. smelt/rules/structure/enum_values.py +50 -0
  111. smelt/rules/structure/headers.py +143 -0
  112. smelt/rules/structure/import_style.py +145 -0
  113. smelt/rules/structure/layout.py +202 -0
  114. smelt/rules/structure/members.py +116 -0
  115. smelt/rules/structure/naming.py +132 -0
  116. smelt/rules/structure/ordering.py +83 -0
  117. smelt/rules/structure/package_imports.py +117 -0
  118. smelt/rules/structure/type_dispatch.py +79 -0
  119. smelt/rules/testing/__init__.py +0 -0
  120. smelt/rules/testing/awaits.py +46 -0
  121. smelt/rules/testing/cases.py +47 -0
  122. smelt/rules/testing/location.py +537 -0
  123. smelt/rules/testing/mocking.py +140 -0
  124. smelt/rules/testing/outcomes.py +119 -0
  125. smelt/rules/testing/private_access.py +93 -0
  126. smelt/rules/testing/public_attributes.py +62 -0
  127. smelt/rules/testing/public_functions.py +52 -0
  128. smelt/rules/testing/timing.py +62 -0
  129. smelt_cli-0.2.0.data/scripts/smelt.exe +0 -0
  130. smelt_cli-0.2.0.dist-info/METADATA +103 -0
  131. smelt_cli-0.2.0.dist-info/RECORD +133 -0
  132. smelt_cli-0.2.0.dist-info/WHEEL +4 -0
  133. smelt_cli-0.2.0.dist-info/sboms/smelt-cli.cyclonedx.json +5462 -0
@@ -0,0 +1,260 @@
1
+ import hashlib
2
+ import io
3
+ import os
4
+ import subprocess
5
+ from dataclasses import dataclass
6
+ from pathlib import Path
7
+ from typing import Any
8
+
9
+ from smelt.analysis.errors import AnalysisError
10
+ from smelt.engine import native_git
11
+
12
+
13
+ def _git(root: Path, *args: str, input_data: bytes | None = None) -> bytes:
14
+ if args[:-1] == ("ls-tree", "-rz", "--full-tree"):
15
+ native = native_git.tree_records(root, args[3])
16
+ if native is not None:
17
+ return native
18
+ try:
19
+ result = subprocess.run( # noqa: S603
20
+ ["git", *args], # noqa: S607
21
+ cwd=root,
22
+ capture_output=True,
23
+ input=input_data,
24
+ check=False,
25
+ )
26
+ except OSError as exc:
27
+ msg = f"changed/staged checks need git: {exc}"
28
+ raise AnalysisError(msg) from exc
29
+ if result.returncode != 0:
30
+ stderr = result.stderr.decode("utf-8", errors="replace").strip()
31
+ msg = f"git {' '.join(args)} failed: {stderr}"
32
+ raise AnalysisError(msg)
33
+ return result.stdout
34
+
35
+
36
+ def _head(root: Path) -> str | None:
37
+ result = subprocess.run(
38
+ ["git", "rev-parse", "--verify", "--quiet", "HEAD"], # noqa: S607
39
+ cwd=root,
40
+ capture_output=True,
41
+ check=False,
42
+ )
43
+ if result.returncode == 0:
44
+ return result.stdout.decode("ascii").strip()
45
+ if result.returncode == 1:
46
+ return None
47
+ raise AnalysisError(result.stderr.decode("utf-8", errors="replace").strip())
48
+
49
+
50
+ def lexical_path(path: Path) -> Path:
51
+ return Path(os.path.abspath(path)) # noqa: PTH100 - captured Git paths must not follow unstaged symlinks
52
+
53
+
54
+ def git_repository(cwd: Path, requested: Path | None = None) -> Path:
55
+ """Prefer the caller's repository, including staged directories deleted on disk."""
56
+ try:
57
+ repository = Path(
58
+ os.fsdecode(_git(cwd, "rev-parse", "--show-toplevel")).strip()
59
+ ).resolve()
60
+ except AnalysisError:
61
+ if requested is None:
62
+ raise
63
+ else:
64
+ if requested is None or requested.is_relative_to(repository):
65
+ return repository
66
+ if requested is None:
67
+ raise AnalysisError("changed/staged checks need a Git repository")
68
+ anchor = next(
69
+ (path for path in (requested.parent, *requested.parents) if path.is_dir()), cwd
70
+ )
71
+ return Path(
72
+ os.fsdecode(_git(anchor, "rev-parse", "--show-toplevel")).strip()
73
+ ).resolve()
74
+
75
+
76
+ @dataclass(frozen=True)
77
+ class GitRepository:
78
+ path: Path
79
+ head: str | None
80
+
81
+
82
+ def capture_repository(cwd: Path, requested: Path | None = None) -> GitRepository:
83
+ """Read repository location and HEAD with one Git process, including unborn HEAD."""
84
+
85
+ def inspect(anchor: Path) -> GitRepository:
86
+ native = native_git.capture(anchor)
87
+ if native is not None:
88
+ return GitRepository(*native)
89
+ result = subprocess.run(
90
+ ["git", "rev-parse", "--show-toplevel", "--verify", "--quiet", "HEAD"], # noqa: S607
91
+ cwd=anchor,
92
+ capture_output=True,
93
+ check=False,
94
+ )
95
+ if result.returncode not in {0, 1} or not result.stdout:
96
+ raise AnalysisError(result.stderr.decode("utf-8", errors="replace").strip())
97
+ output = result.stdout.rstrip(b"\n")
98
+ if result.returncode == 1:
99
+ return GitRepository(Path(os.fsdecode(output)).resolve(), None)
100
+ root, head = output.rsplit(b"\n", 1)
101
+ return GitRepository(Path(os.fsdecode(root)).resolve(), head.decode("ascii"))
102
+
103
+ try:
104
+ captured = inspect(cwd)
105
+ except AnalysisError:
106
+ if requested is None:
107
+ raise
108
+ else:
109
+ if requested is None or requested.is_relative_to(captured.path):
110
+ return captured
111
+ if requested is None:
112
+ raise AnalysisError("changed/staged checks need a Git repository")
113
+ anchor = next(
114
+ (path for path in (requested.parent, *requested.parents) if path.is_dir()), cwd
115
+ )
116
+ return inspect(anchor)
117
+
118
+
119
+ @dataclass(frozen=True, slots=True)
120
+ class Comparison:
121
+ """What ``--changed`` compared the working tree with."""
122
+
123
+ base: str | None # --base as given
124
+ revision: str | None # the commit compared with; None before the first commit
125
+ head: str | None
126
+ staged: bool = False
127
+
128
+ def to_json(self) -> dict[str, Any]:
129
+ return {
130
+ "mode": "staged" if self.staged else "changed",
131
+ "base": self.base,
132
+ "merge_base": self.revision if self.base is not None else None,
133
+ "compared_revision": self.revision,
134
+ "head": self.head,
135
+ "working_tree": not self.staged,
136
+ }
137
+
138
+
139
+ @dataclass(frozen=True)
140
+ class GitIndex:
141
+ repository: Path
142
+ identity: str
143
+ blobs: dict[str, str]
144
+
145
+
146
+ def capture_index(repository: Path) -> GitIndex:
147
+ """Capture one immutable manifest, including stage/mode validation."""
148
+ records = native_git.index_records(repository)
149
+ if records is None:
150
+ records = _git(repository, "ls-files", "--stage", "-z")
151
+ blobs: dict[str, str] = {}
152
+ for record in records.split(b"\0"):
153
+ if not record:
154
+ continue
155
+ metadata, raw_path = record.split(b"\t", 1)
156
+ mode, object_id, stage = metadata.split()
157
+ path = Path(os.fsdecode(raw_path))
158
+ _snapshot_path(repository, path)
159
+ if stage != b"0":
160
+ raise AnalysisError(f"--staged cannot check an unmerged index: {path}")
161
+ if mode == b"120000":
162
+ raise AnalysisError(f"--staged does not support symbolic links: {path}")
163
+ if mode != b"160000":
164
+ blobs[path.as_posix()] = object_id.decode("ascii")
165
+ return GitIndex(repository, hashlib.sha256(records).hexdigest(), blobs)
166
+
167
+
168
+ def resolve_comparison(
169
+ root: Path,
170
+ base: str | None,
171
+ *,
172
+ repository: Path | None = None,
173
+ captured: GitRepository | None = None,
174
+ ) -> tuple[Path, Comparison]:
175
+ toplevel = (captured.path if captured else repository) or Path(
176
+ os.fsdecode(_git(root, "rev-parse", "--show-toplevel")).strip()
177
+ )
178
+ head = captured.head if captured is not None else _head(root)
179
+ if base is not None:
180
+ ref = _git(root, "merge-base", head or "HEAD", base).decode().strip()
181
+ elif head is not None:
182
+ ref = head
183
+ else:
184
+ return toplevel, Comparison(base, None, None)
185
+ return toplevel, Comparison(base, ref, head)
186
+
187
+
188
+ def export_base(
189
+ root: Path, toplevel: Path, comparison: Comparison, dest: Path
190
+ ) -> Path | None:
191
+ ref = comparison.revision
192
+ if ref is None:
193
+ return None
194
+ prefix = root.resolve().relative_to(toplevel.resolve())
195
+ selected: list[tuple[str, Path]] = []
196
+ # Read the tree itself: git archive can silently omit export-ignore sources
197
+ # or substitute export-subst contents. Sibling workspace roots matter too.
198
+ for record in _git(toplevel, "ls-tree", "-rz", "--full-tree", ref).split(b"\0"):
199
+ if not record:
200
+ continue
201
+ metadata, raw_path = record.split(b"\t", 1)
202
+ mode, kind, object_id = metadata.split()
203
+ path = Path(os.fsdecode(raw_path))
204
+ if kind != b"blob":
205
+ continue
206
+ if mode == b"120000":
207
+ raise AnalysisError(
208
+ f"changed/staged snapshots do not support symbolic links: {path}"
209
+ )
210
+ target = _snapshot_path(dest, path)
211
+ target.parent.mkdir(parents=True, exist_ok=True)
212
+ selected.append((object_id.decode("ascii"), target))
213
+ _copy_blobs(toplevel, selected)
214
+ project = dest / prefix
215
+ return project if project.is_dir() else None
216
+
217
+
218
+ def _snapshot_path(dest: Path, path: Path) -> Path:
219
+ # Snapshots start empty and never create symlinks. Validate the relative
220
+ # components without a filesystem round trip for every repository file.
221
+ if path.drive or path.is_absolute() or ".." in path.parts:
222
+ raise AnalysisError(f"unsafe path in Git snapshot: {path}")
223
+ return dest / path
224
+
225
+
226
+ def _copy_blobs(repository: Path, files: list[tuple[str, Path]]) -> None:
227
+ if not files:
228
+ return
229
+ contents = read_blobs(repository, list(dict.fromkeys(oid for oid, _ in files)))
230
+ for object_id, path in files:
231
+ path.write_bytes(contents[object_id])
232
+
233
+
234
+ def read_blobs(repository: Path, identifiers: list[str]) -> dict[str, bytes]:
235
+ if not identifiers:
236
+ return {}
237
+ native = native_git.read_blobs(repository, identifiers)
238
+ if native is not None:
239
+ return native
240
+ contents = io.BytesIO(
241
+ _git(
242
+ repository,
243
+ "cat-file",
244
+ "--batch",
245
+ input_data="".join(f"{object_id}\n" for object_id in identifiers).encode(
246
+ "ascii"
247
+ ),
248
+ )
249
+ )
250
+ result: dict[str, bytes] = {}
251
+ for object_id in identifiers:
252
+ header = contents.readline().split()
253
+ if len(header) != 3 or header[:2] != [object_id.encode("ascii"), b"blob"]: # noqa: PLR2004 - Git's SHA/type/size header
254
+ raise AnalysisError("cannot read staged Git blob")
255
+ size = int(header[2])
256
+ data = contents.read(size)
257
+ if len(data) != size or contents.read(1) != b"\n":
258
+ raise AnalysisError("incomplete staged Git blob")
259
+ result[object_id] = data
260
+ return result