-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_cli_runtime.py
More file actions
189 lines (156 loc) · 5.1 KB
/
_cli_runtime.py
File metadata and controls
189 lines (156 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# SPDX-License-Identifier: MIT
# Copyright (c) 2026 Den Rozhnovskiy
from __future__ import annotations
import sys
from pathlib import Path
from typing import Protocol
from . import ui_messages as ui
from .cache import CacheStatus
from .contracts import ExitCode
__all__ = [
"configure_metrics_mode",
"metrics_computed",
"print_failed_files",
"resolve_cache_path",
"resolve_cache_status",
"validate_numeric_args",
]
class _RuntimeArgs(Protocol):
cache_path: str | None
max_baseline_size_mb: int
max_cache_size_mb: int
fail_threshold: int
fail_complexity: int
fail_coupling: int
fail_cohesion: int
fail_health: int
fail_on_new_metrics: bool
update_metrics_baseline: bool
skip_metrics: bool
fail_cycles: bool
fail_dead_code: bool
skip_dead_code: bool
skip_dependencies: bool
class _PrinterLike(Protocol):
def print(self, *objects: object, **kwargs: object) -> None: ...
class _CacheLike(Protocol):
@property
def load_status(self) -> CacheStatus | str | None: ...
@property
def load_warning(self) -> str | None: ...
@property
def cache_schema_version(self) -> str | None: ...
def validate_numeric_args(args: _RuntimeArgs) -> bool:
return bool(
not (
args.max_baseline_size_mb < 0
or args.max_cache_size_mb < 0
or args.fail_threshold < -1
or args.fail_complexity < -1
or args.fail_coupling < -1
or args.fail_cohesion < -1
or args.fail_health < -1
)
)
def _metrics_flags_requested(args: _RuntimeArgs) -> bool:
return bool(
args.fail_complexity >= 0
or args.fail_coupling >= 0
or args.fail_cohesion >= 0
or args.fail_cycles
or args.fail_dead_code
or args.fail_health >= 0
or args.fail_on_new_metrics
or args.update_metrics_baseline
)
def configure_metrics_mode(
*,
args: _RuntimeArgs,
metrics_baseline_exists: bool,
console: _PrinterLike,
) -> None:
metrics_flags_requested = _metrics_flags_requested(args)
if args.skip_metrics and metrics_flags_requested:
console.print(
ui.fmt_contract_error(
"--skip-metrics cannot be used together with metrics gating/update "
"flags."
)
)
sys.exit(ExitCode.CONTRACT_ERROR)
if (
not args.skip_metrics
and not metrics_flags_requested
and not metrics_baseline_exists
):
args.skip_metrics = True
if args.skip_metrics:
args.skip_dead_code = True
args.skip_dependencies = True
return
if args.fail_dead_code:
args.skip_dead_code = False
if args.fail_cycles:
args.skip_dependencies = False
def resolve_cache_path(
*,
root_path: Path,
args: _RuntimeArgs,
from_args: bool,
legacy_cache_path: Path,
console: _PrinterLike,
) -> Path:
if from_args and args.cache_path:
return Path(args.cache_path).expanduser()
cache_path = root_path / ".cache" / "codeclone" / "cache.json"
if legacy_cache_path.exists():
try:
legacy_resolved = legacy_cache_path.resolve()
except OSError:
legacy_resolved = legacy_cache_path
if legacy_resolved != cache_path:
console.print(
ui.fmt_legacy_cache_warning(
legacy_path=legacy_resolved,
new_path=cache_path,
)
)
return cache_path
def metrics_computed(args: _RuntimeArgs) -> tuple[str, ...]:
if args.skip_metrics:
return ()
computed = ["complexity", "coupling", "cohesion", "health"]
if not args.skip_dependencies:
computed.append("dependencies")
if not args.skip_dead_code:
computed.append("dead_code")
return tuple(computed)
def resolve_cache_status(cache: _CacheLike) -> tuple[CacheStatus, str | None]:
raw_cache_status = getattr(cache, "load_status", None)
load_warning = getattr(cache, "load_warning", None)
if isinstance(raw_cache_status, CacheStatus):
cache_status = raw_cache_status
elif isinstance(raw_cache_status, str):
try:
cache_status = CacheStatus(raw_cache_status)
except ValueError:
cache_status = (
CacheStatus.OK if load_warning is None else CacheStatus.INVALID_TYPE
)
else:
cache_status = (
CacheStatus.OK if load_warning is None else CacheStatus.INVALID_TYPE
)
raw_cache_schema_version = getattr(cache, "cache_schema_version", None)
cache_schema_version = (
raw_cache_schema_version if isinstance(raw_cache_schema_version, str) else None
)
return cache_status, cache_schema_version
def print_failed_files(*, failed_files: tuple[str, ...], console: _PrinterLike) -> None:
if not failed_files:
return
console.print(ui.fmt_failed_files_header(len(failed_files)))
for failure in failed_files[:10]:
console.print(f" • {failure}")
if len(failed_files) > 10:
console.print(f" ... and {len(failed_files) - 10} more")