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,151 @@
1
+ from __future__ import annotations
2
+
3
+ from dataclasses import replace
4
+ from typing import TYPE_CHECKING
5
+
6
+ from smelt import __version__
7
+ from smelt.analysis.files import FileIndex
8
+ from smelt.cli.support import (
9
+ EXIT_OK,
10
+ EXIT_VIOLATIONS,
11
+ CliError,
12
+ Console,
13
+ load_project_config,
14
+ use_color,
15
+ )
16
+ from smelt.diagnostics.render.machine import render_github, render_json, render_sarif
17
+ from smelt.diagnostics.render.text import render_text
18
+ from smelt.diagnostics.violation import Severity
19
+ from smelt.engine.check import CheckOptions, relative_scope, run_check
20
+ from smelt.engine.paths import project_metadata
21
+ from smelt.engine.staged import staged_project
22
+
23
+ if TYPE_CHECKING:
24
+ import argparse
25
+ from pathlib import Path
26
+
27
+ from smelt.config import LoadedConfig
28
+ from smelt.engine.staged import StagedProject
29
+
30
+
31
+ def check(args: argparse.Namespace, console: Console, cwd: Path) -> int:
32
+ if args.staged:
33
+ if args.fix:
34
+ raise CliError(
35
+ "--staged cannot be combined with --fix; fix and stage the working files first",
36
+ option="--fix",
37
+ value="--staged",
38
+ )
39
+ with staged_project(
40
+ cwd,
41
+ args.config,
42
+ index=getattr(args, "_staged_index", None),
43
+ use_cache=not args.no_cache,
44
+ ) as staged:
45
+ if staged.loaded.source is not None:
46
+ args._staged_inputs = staged.loaded.source.dependencies()
47
+ return _check_loaded(
48
+ args,
49
+ console,
50
+ cwd,
51
+ staged.loaded,
52
+ staged=staged,
53
+ )
54
+ loaded = load_project_config(args.config, cwd)
55
+ return _check_loaded(args, console, cwd, loaded)
56
+
57
+
58
+ def _split_selectors(raw: str | None) -> tuple[str, ...]:
59
+ if not raw:
60
+ return ()
61
+ return tuple(code.strip() for code in raw.split(",") if code.strip())
62
+
63
+
64
+ def _scope(loaded: LoadedConfig, cwd: Path, raw: list[str]) -> tuple[str, ...]:
65
+ paths = tuple(relative_scope(loaded.root, cwd, p, store=loaded.source) for p in raw)
66
+ # A changed config can break any file, so it widens the scope to the project.
67
+ config = relative_scope(loaded.root, cwd, str(loaded.path), store=loaded.source)
68
+ return () if config in paths else paths
69
+
70
+
71
+ def _check_loaded(
72
+ args: argparse.Namespace,
73
+ console: Console,
74
+ cwd: Path,
75
+ loaded: LoadedConfig,
76
+ *,
77
+ staged: StagedProject | None = None,
78
+ ) -> int:
79
+ original = staged.original_config if staged else None
80
+ index_hash = staged.index_hash if staged else None
81
+ origin = replace(loaded, path=original) if original else loaded
82
+ options = CheckOptions(
83
+ strict_coverage=args.strict_coverage,
84
+ show_coverage=args.show_coverage,
85
+ paths=_scope(origin, cwd, args.paths),
86
+ changed=args.changed or args.staged or args.base is not None,
87
+ base=args.base,
88
+ select=_split_selectors(args.select),
89
+ ignore=_split_selectors(args.ignore),
90
+ fail_on=Severity(args.fail_on),
91
+ use_debt=not args.no_debt,
92
+ use_cache=not args.no_cache,
93
+ cache_dir=origin.root / ".smelt_cache",
94
+ comparison_root=origin.root if args.staged else None,
95
+ staged=args.staged,
96
+ snapshot_id=index_hash,
97
+ repository_root=staged.repository if staged else None,
98
+ comparison=getattr(args, "_comparison", None),
99
+ )
100
+ outcome = run_check(loaded, options)
101
+ if args.fix:
102
+ from smelt.engine.ordering_fixes import apply_ordering_fixes # noqa: PLC0415
103
+
104
+ fixes = apply_ordering_fixes(outcome)
105
+ if fixes.files:
106
+ outcome = run_check(loaded, options)
107
+ outcome.report.fixes = {"ordering": fixes.to_json()}
108
+ if args.format == "text":
109
+ console.print(
110
+ f"Ordering fixes: {len(fixes.files)} file(s), {fixes.sections} section(s); {len(fixes.skipped)} skipped."
111
+ )
112
+ report = outcome.report
113
+ if args.staged:
114
+ report.project = project_metadata(origin)
115
+ if report.comparison is not None:
116
+ report.comparison = {**report.comparison, "index_hash": index_hash}
117
+ files = outcome.context.__dict__.get("files") or FileIndex(
118
+ outcome.context.root, outcome.context.config, store=outcome.context.store
119
+ )
120
+
121
+ def read_line(path: str, line: int) -> str:
122
+ try:
123
+ return files.line(path, line)
124
+ except OSError:
125
+ return ""
126
+
127
+ match args.format:
128
+ case "json":
129
+ console.print(render_json(report), end="")
130
+ case "sarif":
131
+ console.print(render_sarif(report, __version__), end="")
132
+ case "github":
133
+ console.print(render_github(report), end="")
134
+ case _:
135
+ color = use_color(console.out, disabled=args.no_color)
136
+ text = render_text(
137
+ report,
138
+ read_line,
139
+ color=color,
140
+ # Hints on the files just changed are few and relevant.
141
+ show_hints=args.show_hints or options.changed,
142
+ statistics=args.statistics,
143
+ )
144
+ console.print(text, end="")
145
+ if args.show_coverage and report.coverage:
146
+ console.print("Coverage inventory:")
147
+ for entry in report.coverage.get("inventory", []):
148
+ console.print(
149
+ f" {entry['status']}: {entry['path']} — {entry['reason']}"
150
+ )
151
+ return EXIT_VIOLATIONS if report.failed else EXIT_OK
@@ -0,0 +1,51 @@
1
+ from __future__ import annotations
2
+
3
+ import json
4
+ from typing import TYPE_CHECKING
5
+
6
+ from smelt.analysis.context import AnalysisContext
7
+ from smelt.analysis.parsing import AnalysisError
8
+ from smelt.cli.support import EXIT_OK, CliError, Console, load_project_config
9
+ from smelt.config import ConfigError
10
+ from smelt.engine.briefing import (
11
+ TargetError,
12
+ build_briefing,
13
+ render_briefing,
14
+ resolve_target,
15
+ )
16
+ from smelt.engine.check import CheckOptions, relative_scope, run_check
17
+ from smelt.engine.paths import missing_paths
18
+
19
+ if TYPE_CHECKING:
20
+ import argparse
21
+ from pathlib import Path
22
+
23
+
24
+ def context(args: argparse.Namespace, console: Console, cwd: Path) -> int:
25
+ loaded = load_project_config(args.config, cwd)
26
+ ctx = AnalysisContext(loaded.root, loaded.config)
27
+ issues = missing_paths(ctx)
28
+ if issues:
29
+ raise ConfigError(issues, loaded.path.name)
30
+ raw = args.target
31
+ if (
32
+ raw is not None
33
+ and raw not in ctx.model.features
34
+ and ctx.files.path_for_module(raw) is None
35
+ ):
36
+ raw = relative_scope(loaded.root, cwd, raw)
37
+ try:
38
+ target = resolve_target(ctx, raw)
39
+ except TargetError as exc:
40
+ raise CliError(str(exc), option="target", value=args.target) from exc
41
+ try:
42
+ outcome = run_check(loaded, CheckOptions(), context=ctx)
43
+ except (AnalysisError, ConfigError) as exc:
44
+ briefing = build_briefing(ctx, target, None, analysis_error=str(exc))
45
+ else:
46
+ briefing = build_briefing(ctx, target, outcome.report)
47
+ if args.format == "json":
48
+ console.print(json.dumps(briefing.to_json(), indent=2))
49
+ else:
50
+ console.print(render_briefing(briefing, ctx), end="")
51
+ return EXIT_OK
@@ -0,0 +1,112 @@
1
+ from __future__ import annotations
2
+
3
+ import json
4
+ from typing import TYPE_CHECKING
5
+
6
+ import yaml
7
+
8
+ from smelt.cli.support import EXIT_OK, CliError, Console, load_project_config
9
+ from smelt.config.errors import did_you_mean
10
+ from smelt.engine.check import rule_meta
11
+ from smelt.engine.schema import config_json_schema
12
+ from smelt.rules.registry import build_rule_set
13
+
14
+ if TYPE_CHECKING:
15
+ import argparse
16
+ from pathlib import Path
17
+
18
+ from smelt.rules.base import RuleSet
19
+
20
+
21
+ def explain(args: argparse.Namespace, console: Console, cwd: Path) -> int:
22
+ rules = build_rule_set()
23
+ rule = rules.lookup(args.rule)
24
+ if rule is None:
25
+ msg = f'unknown rule "{args.rule}"{_suggestion(args.rule, rules)}'
26
+ raise CliError(
27
+ f"{msg}; `smelt rules` lists them all", option="rule", value=args.rule
28
+ )
29
+ doc = rule.explain()
30
+ meta = rule_meta(rule)
31
+ if args.format == "json":
32
+ data = {
33
+ **meta.to_json(),
34
+ "rationale": doc.rationale,
35
+ "bad": doc.bad,
36
+ "good": doc.good,
37
+ "fix": doc.fix,
38
+ "config": list(doc.config),
39
+ "example_language": doc.example_language,
40
+ }
41
+ console.print(json.dumps(data, indent=2))
42
+ return EXIT_OK
43
+ lines = [
44
+ f"{rule.code} {rule.name} [{rule.category.value}, activation: {meta.activation['mode']}, severity: {meta.default_severity.value}]",
45
+ meta.activation["description"],
46
+ "",
47
+ doc.summary,
48
+ "",
49
+ "Why:",
50
+ _indent(doc.rationale),
51
+ "",
52
+ "Bad:",
53
+ _indent(doc.bad),
54
+ "",
55
+ "Good:",
56
+ _indent(doc.good),
57
+ "",
58
+ "How to fix:",
59
+ _indent(doc.fix),
60
+ ]
61
+ if doc.config:
62
+ lines.extend(["", "Config:", *(f" {key}" for key in doc.config)])
63
+ lines.extend(["", f"Docs: {meta.docs_url}"])
64
+ console.print("\n".join(lines))
65
+ return EXIT_OK
66
+
67
+
68
+ def rules(args: argparse.Namespace, console: Console, cwd: Path) -> int:
69
+ rule_set = build_rule_set()
70
+ metas = [rule_meta(rule) for rule in rule_set.rules]
71
+ if args.format == "json":
72
+ console.print(json.dumps([m.to_json() for m in metas], indent=2))
73
+ return EXIT_OK
74
+ for meta in metas:
75
+ console.print(
76
+ f"{meta.code} {meta.name:<28} {meta.category.value:<13} {meta.activation['mode']:<8} {meta.default_severity.value}"
77
+ )
78
+ return EXIT_OK
79
+
80
+
81
+ def config_show(args: argparse.Namespace, console: Console, cwd: Path) -> int:
82
+ loaded = load_project_config(args.config, cwd)
83
+ data = loaded.config.model_dump(mode="json", by_alias=True)
84
+ if args.format == "json":
85
+ console.print(json.dumps(data, indent=2))
86
+ else:
87
+ console.print(f"# resolved from {loaded.path.as_posix()}")
88
+ console.print(yaml.safe_dump(data, sort_keys=False, allow_unicode=True), end="")
89
+ return EXIT_OK
90
+
91
+
92
+ def config_schema(args: argparse.Namespace, console: Console, cwd: Path) -> int:
93
+ console.print(json.dumps(config_json_schema(), indent=2))
94
+ return EXIT_OK
95
+
96
+
97
+ def _suggestion(raw: str, rules: RuleSet) -> str:
98
+ """A close rule name, or a code that differs in one character only."""
99
+ wanted = raw.strip().upper()
100
+ if not wanted[:1].isalpha() or not wanted[-1:].isdigit():
101
+ return did_you_mean(raw.strip(), [r.name for r in rules.rules])
102
+ close = [
103
+ r.code
104
+ for r in rules.rules
105
+ if len(r.code) == len(wanted)
106
+ and sum(a != b for a, b in zip(r.code, wanted, strict=True)) == 1
107
+ ]
108
+ return f' (did you mean "{close[0]}"?)' if len(close) == 1 else ""
109
+
110
+
111
+ def _indent(text: str) -> str:
112
+ return "\n".join(f" {line}" if line else "" for line in text.splitlines())
smelt/cli/session.py ADDED
@@ -0,0 +1,163 @@
1
+ """Bounded local workers for the native CLI; each request captures fresh inputs."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import contextlib
6
+ import io
7
+ import json
8
+ import os
9
+ import secrets
10
+ import socket
11
+ import sys
12
+ import threading
13
+ from pathlib import Path
14
+ from typing import Any
15
+
16
+ from smelt.analysis.identity import analyzer_identity
17
+ from smelt.analysis.retained import RetainedAnalysis, retain
18
+ from smelt.cli.app import main
19
+ from smelt.cli.session_slots import worker_slot
20
+
21
+ _LIMIT = 1024 * 1024
22
+ _PROTOCOL = 2
23
+ _REQUEST_SECONDS = 5
24
+
25
+
26
+ class Capture(io.StringIO):
27
+ def __init__(self, *, tty: bool) -> None:
28
+ super().__init__()
29
+ self._tty = tty
30
+
31
+ def isatty(self) -> bool:
32
+ return self._tty
33
+
34
+
35
+ def _dispatch(
36
+ message: dict[str, Any],
37
+ root: str,
38
+ identity: str,
39
+ *,
40
+ state: RetainedAnalysis | None = None,
41
+ ) -> dict[str, Any]:
42
+ analyzer_identity.cache_clear()
43
+ if analyzer_identity() != identity:
44
+ return {"code": 2, "out": "", "err": "", "restart": True}
45
+ arguments = message.get("arguments")
46
+ if (
47
+ message.get("cwd") != root
48
+ or not isinstance(arguments, list)
49
+ or not arguments
50
+ or arguments[0] != "check"
51
+ or any(not isinstance(value, str) for value in arguments)
52
+ or any(
53
+ value.startswith("--fi") or value in {"--f", "--help", "-h"}
54
+ for value in arguments
55
+ )
56
+ ):
57
+ raise ValueError("worker accepts read-only checks for its original directory")
58
+ out = Capture(tty=message.get("tty") is True)
59
+ err = Capture(tty=False)
60
+ with (
61
+ retain(state),
62
+ contextlib.chdir(root),
63
+ contextlib.redirect_stdout(out),
64
+ contextlib.redirect_stderr(err),
65
+ ):
66
+ code = main(arguments)
67
+ return {"code": code, "out": out.getvalue(), "err": err.getvalue()}
68
+
69
+
70
+ def _serve_internal(
71
+ endpoint: Path, *, root: str | None = None, idle_seconds: float = 120
72
+ ) -> None:
73
+ with worker_slot(endpoint.parent) as available:
74
+ if available:
75
+ _serve(endpoint, root=root, idle_seconds=idle_seconds)
76
+
77
+
78
+ def _serve(endpoint: Path, *, root: str | None, idle_seconds: float) -> None:
79
+ identity = analyzer_identity()
80
+ state = RetainedAnalysis()
81
+ root = root or os.getcwd() # noqa: PTH109 - match the native client's lexical cwd
82
+ # Neither the Python worker nor its Windows environment redirector may keep
83
+ # an idle project's directory open and prevent worktree deletion or renames.
84
+ os.chdir(endpoint.parent)
85
+ token = secrets.token_hex(32)
86
+ with socket.create_server(("127.0.0.1", 0), backlog=32) as listener:
87
+ listener.settimeout(idle_seconds)
88
+ metadata = {
89
+ "port": listener.getsockname()[1],
90
+ "token": token,
91
+ "pid": os.getpid(),
92
+ "protocol": _PROTOCOL,
93
+ }
94
+ pending = endpoint.with_suffix(f".{os.getpid()}.pending")
95
+ descriptor = os.open(pending, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
96
+ with os.fdopen(descriptor, "w", encoding="utf-8") as output:
97
+ json.dump(metadata, output)
98
+ pending.replace(endpoint)
99
+ try:
100
+ while True:
101
+ try:
102
+ stream, _ = listener.accept()
103
+ except TimeoutError:
104
+ break
105
+ with stream:
106
+ stream.settimeout(0.25)
107
+ stream.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
108
+ # This process owns the timeout. No PID from a stale endpoint
109
+ # is killed by a client, and OS locks/SQLite recover on exit.
110
+ watchdog = threading.Timer(_REQUEST_SECONDS, os._exit, args=(2,))
111
+ watchdog.daemon = True
112
+ watchdog.start()
113
+ try:
114
+ try:
115
+ response = _respond(stream, token, root, identity, state)
116
+ except (ValueError, KeyError, TypeError, OSError):
117
+ # Transport failure is never the check's verdict.
118
+ response = {
119
+ "code": 2,
120
+ "out": "",
121
+ "err": "",
122
+ "restart": True,
123
+ }
124
+ response["pid"] = os.getpid()
125
+ with contextlib.suppress(OSError):
126
+ stream.sendall(json.dumps(response).encode() + b"\n")
127
+ finally:
128
+ watchdog.cancel()
129
+ watchdog.join()
130
+ if response.get("restart"):
131
+ break
132
+ finally:
133
+ with contextlib.suppress(OSError, ValueError):
134
+ if json.loads(endpoint.read_text(encoding="utf-8"))["token"] == token:
135
+ endpoint.unlink()
136
+
137
+
138
+ def _respond(
139
+ stream: socket.socket,
140
+ token: str,
141
+ root: str,
142
+ identity: str,
143
+ state: RetainedAnalysis,
144
+ ) -> dict[str, Any]:
145
+ with stream.makefile("rb") as incoming:
146
+ line = incoming.readline(_LIMIT)
147
+ message = json.loads(line)
148
+ if not isinstance(message, dict) or message.get("token") != token:
149
+ raise ValueError("invalid worker token")
150
+ stream.settimeout(_REQUEST_SECONDS)
151
+ if message.get("ack") is True:
152
+ stream.sendall(
153
+ json.dumps({"accepted": True, "pid": os.getpid()}).encode() + b"\n"
154
+ )
155
+ if message.get("arguments") == ["shutdown"]:
156
+ return {"code": 0, "out": "", "err": "", "restart": True}
157
+ return _dispatch(message, root, identity, state=state)
158
+
159
+
160
+ if __name__ == "__main__":
161
+ if client := sys.argv[3:]:
162
+ os.environ["SMELT_NATIVE_CLIENT"] = client[0]
163
+ _serve_internal(Path(sys.argv[1]), root=sys.argv[2])
@@ -0,0 +1,43 @@
1
+ """OS-owned worker slots, released even when a worker crashes or is terminated."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import contextlib
6
+ import os
7
+ import sys
8
+ from typing import TYPE_CHECKING
9
+
10
+ if TYPE_CHECKING:
11
+ from collections.abc import Iterator
12
+ from pathlib import Path
13
+
14
+ _MAX_WORKERS = 4
15
+
16
+
17
+ @contextlib.contextmanager
18
+ def worker_slot(directory: Path) -> Iterator[bool]:
19
+ slots = directory / "worker-slots"
20
+ slots.mkdir(exist_ok=True)
21
+ with contextlib.ExitStack() as cleanup:
22
+ for number in range(_MAX_WORKERS):
23
+ stream = (slots / str(number)).open("a+b")
24
+ try:
25
+ if os.fstat(stream.fileno()).st_size == 0:
26
+ stream.write(b"\0")
27
+ stream.flush()
28
+ stream.seek(0)
29
+ if sys.platform == "win32":
30
+ import msvcrt # noqa: PLC0415 - platform-specific OS lock
31
+
32
+ msvcrt.locking(stream.fileno(), msvcrt.LK_NBLCK, 1)
33
+ else:
34
+ import fcntl # noqa: PLC0415 - platform-specific OS lock
35
+
36
+ fcntl.flock(stream.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
37
+ except OSError:
38
+ stream.close()
39
+ continue
40
+ cleanup.callback(stream.close)
41
+ yield True
42
+ return
43
+ yield False
smelt/cli/support.py ADDED
@@ -0,0 +1,90 @@
1
+ from __future__ import annotations
2
+
3
+ import contextlib
4
+ import os
5
+ import sys
6
+ from dataclasses import dataclass
7
+ from pathlib import Path
8
+ from typing import TYPE_CHECKING, TextIO
9
+
10
+ from smelt.config.errors import ConfigError, ConfigIssue
11
+
12
+ if TYPE_CHECKING:
13
+ from smelt.config.loader import LoadedConfig
14
+
15
+ EXIT_OK = 0
16
+ EXIT_VIOLATIONS = 1
17
+ EXIT_ERROR = 2
18
+
19
+
20
+ class CliError(Exception):
21
+ """A usage or environment problem; reported with exit code 2.
22
+
23
+ ``kind``, ``option`` and ``value`` end up in the JSON error document;
24
+ ``option`` and ``value`` name the argument the user got wrong, if there is one.
25
+ """
26
+
27
+ def __init__(
28
+ self,
29
+ message: str,
30
+ *,
31
+ kind: str = "usage",
32
+ option: str | None = None,
33
+ value: str | None = None,
34
+ ) -> None:
35
+ super().__init__(message)
36
+ self.kind = kind
37
+ self.option = option
38
+ self.value = value
39
+
40
+
41
+ @dataclass
42
+ class Console:
43
+ out: TextIO
44
+ err: TextIO
45
+
46
+ def print(self, text: str = "", *, end: str = "\n") -> None:
47
+ self.out.write(text + end)
48
+
49
+ def error(self, text: str) -> None:
50
+ self.err.write(f"error: {text}\n")
51
+
52
+ def warn(self, text: str) -> None:
53
+ self.err.write(f"warning: {text}\n")
54
+
55
+
56
+ def use_color(stream: TextIO, *, disabled: bool) -> bool:
57
+ if disabled or os.environ.get("NO_COLOR"):
58
+ return False
59
+ isatty = getattr(stream, "isatty", None)
60
+ return bool(isatty and isatty())
61
+
62
+
63
+ def load_project_config(config_path: str | None, cwd: Path) -> LoadedConfig:
64
+ from smelt.config.loader import ( # noqa: PLC0415 - keep schemas out of CLI startup
65
+ discover_config,
66
+ load_config,
67
+ )
68
+
69
+ if config_path:
70
+ path = Path(config_path)
71
+ if not path.is_absolute():
72
+ path = cwd / path
73
+ if not path.is_file():
74
+ raise ConfigError(
75
+ [ConfigIssue("", f"config file not found: {config_path}")]
76
+ )
77
+ return load_config(path)
78
+ found = discover_config(cwd)
79
+ if found is None:
80
+ msg = "no smelt.yaml found in this directory or its parents (run `smelt init`)"
81
+ raise CliError(msg, kind="config")
82
+ return load_config(found)
83
+
84
+
85
+ def configure_streams() -> None:
86
+ for stream in (sys.stdout, sys.stderr):
87
+ reconfigure = getattr(stream, "reconfigure", None)
88
+ if reconfigure is not None:
89
+ with contextlib.suppress(ValueError, OSError):
90
+ reconfigure(encoding="utf-8", errors="replace")
@@ -0,0 +1,36 @@
1
+ from importlib import import_module
2
+ from typing import TYPE_CHECKING, Any
3
+
4
+ from smelt.config.errors import ConfigError, ConfigIssue
5
+
6
+ if TYPE_CHECKING:
7
+ from smelt.config.loader import (
8
+ CONFIG_FILENAME,
9
+ CONFIG_FILENAMES,
10
+ LoadedConfig,
11
+ discover_config,
12
+ load_config,
13
+ parse_config,
14
+ )
15
+ from smelt.config.models import SmeltConfig
16
+
17
+
18
+ __all__ = [
19
+ "CONFIG_FILENAME",
20
+ "CONFIG_FILENAMES",
21
+ "ConfigError",
22
+ "ConfigIssue",
23
+ "LoadedConfig",
24
+ "SmeltConfig",
25
+ "discover_config",
26
+ "load_config",
27
+ "parse_config",
28
+ ]
29
+
30
+
31
+ def __getattr__(name: str) -> Any:
32
+ if name == "SmeltConfig":
33
+ return getattr(import_module("smelt.config.models"), name)
34
+ if name in __all__:
35
+ return getattr(import_module("smelt.config.loader"), name)
36
+ raise AttributeError(name)