adre-cli 0.1.0__py3-none-any.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.
- adre/__init__.py +5 -0
- adre/analyzers/__init__.py +1 -0
- adre/analyzers/architecture.py +253 -0
- adre/analyzers/findings.py +457 -0
- adre/analyzers/inference.py +163 -0
- adre/cli.py +923 -0
- adre/core/__init__.py +1 -0
- adre/core/accepted.py +225 -0
- adre/core/adaptive.py +221 -0
- adre/core/baseline.py +1165 -0
- adre/core/config.py +60 -0
- adre/core/decision_engine.py +361 -0
- adre/core/declared.py +313 -0
- adre/core/evolution_intelligence.py +390 -0
- adre/core/explainer.py +413 -0
- adre/core/git_intelligence.py +392 -0
- adre/core/github_check.py +212 -0
- adre/core/incremental.py +288 -0
- adre/core/layers.py +306 -0
- adre/core/pr_intelligence.py +261 -0
- adre/core/pr_review.py +454 -0
- adre/core/prioritization.py +196 -0
- adre/core/project.py +41 -0
- adre/core/refactor_intelligence.py +201 -0
- adre/core/repositories.py +312 -0
- adre/core/review.py +96 -0
- adre/core/rules_shared.py +75 -0
- adre/core/sarif.py +141 -0
- adre/core/scope.py +141 -0
- adre/core/snapshots.py +242 -0
- adre/core/stability_intelligence.py +361 -0
- adre/graph/__init__.py +0 -0
- adre/graph/dependencies.py +379 -0
- adre/parsers/__init__.py +87 -0
- adre/parsers/go.py +140 -0
- adre/parsers/javascript.py +364 -0
- adre/parsers/jvm.py +127 -0
- adre/parsers/python.py +153 -0
- adre/reports/__init__.py +1 -0
- adre/reports/architecture.py +349 -0
- adre/rules.py +12 -0
- adre/web.py +3731 -0
- adre_cli-0.1.0.dist-info/METADATA +343 -0
- adre_cli-0.1.0.dist-info/RECORD +48 -0
- adre_cli-0.1.0.dist-info/WHEEL +5 -0
- adre_cli-0.1.0.dist-info/entry_points.txt +2 -0
- adre_cli-0.1.0.dist-info/licenses/LICENSE +202 -0
- adre_cli-0.1.0.dist-info/top_level.txt +1 -0
adre/core/incremental.py
ADDED
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
import hashlib
|
|
2
|
+
import json
|
|
3
|
+
import os
|
|
4
|
+
from collections import defaultdict
|
|
5
|
+
|
|
6
|
+
from adre.core.config import (
|
|
7
|
+
ANALYZER_VERSION,
|
|
8
|
+
BASELINE_DIRNAME,
|
|
9
|
+
MAX_FILE_BYTES,
|
|
10
|
+
PARSE_CACHE_FILENAME,
|
|
11
|
+
PARSE_CACHE_SCHEMA_VERSION,
|
|
12
|
+
)
|
|
13
|
+
from adre.core.layers import get_layer, label_file
|
|
14
|
+
from adre.parsers import collect_source_files, parse_source_file
|
|
15
|
+
from adre.parsers.go import GO_EXTENSIONS, nearest_go_module
|
|
16
|
+
from adre.parsers.javascript import JS_EXTENSIONS, nearest_config
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def default_parse_cache_path(project_root):
|
|
20
|
+
return os.path.join(os.path.realpath(project_root), BASELINE_DIRNAME, PARSE_CACHE_FILENAME)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def hash_source_file(absolute_path, project_root=None):
|
|
24
|
+
if os.path.islink(absolute_path):
|
|
25
|
+
return None
|
|
26
|
+
real = os.path.realpath(absolute_path)
|
|
27
|
+
if project_root:
|
|
28
|
+
root = os.path.realpath(project_root)
|
|
29
|
+
if real != root and not real.startswith(root + os.sep):
|
|
30
|
+
return None
|
|
31
|
+
digest = hashlib.sha256()
|
|
32
|
+
try:
|
|
33
|
+
with open(real, "rb") as handle:
|
|
34
|
+
while True:
|
|
35
|
+
chunk = handle.read(65536)
|
|
36
|
+
if not chunk:
|
|
37
|
+
break
|
|
38
|
+
digest.update(chunk)
|
|
39
|
+
except OSError:
|
|
40
|
+
return None
|
|
41
|
+
return digest.hexdigest()
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def empty_incremental_stats():
|
|
45
|
+
return {
|
|
46
|
+
"discovered": 0,
|
|
47
|
+
"parsed": 0,
|
|
48
|
+
"reused": 0,
|
|
49
|
+
"changed": 0,
|
|
50
|
+
"added": 0,
|
|
51
|
+
"deleted": 0,
|
|
52
|
+
"invalidated": 0,
|
|
53
|
+
"cache_used": False,
|
|
54
|
+
"cache_reason": "no cache",
|
|
55
|
+
"parsed_files": [],
|
|
56
|
+
"reused_files": [],
|
|
57
|
+
"changed_files": [],
|
|
58
|
+
"added_files": [],
|
|
59
|
+
"deleted_files": [],
|
|
60
|
+
"invalidated_files": [],
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _valid_cached_record(record):
|
|
65
|
+
if not isinstance(record, dict):
|
|
66
|
+
return False
|
|
67
|
+
return isinstance(record.get("imports"), list) and "cluster" in record and "role" in record
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def load_parse_cache(project_root):
|
|
71
|
+
path = default_parse_cache_path(project_root)
|
|
72
|
+
if not os.path.isfile(path):
|
|
73
|
+
return None, "no cache"
|
|
74
|
+
if os.path.islink(path):
|
|
75
|
+
return None, "cache path is a symlink"
|
|
76
|
+
real = os.path.realpath(path)
|
|
77
|
+
root = os.path.realpath(project_root)
|
|
78
|
+
expected_dir = os.path.join(root, BASELINE_DIRNAME)
|
|
79
|
+
if not real.startswith(expected_dir + os.sep) and os.path.dirname(real) != expected_dir:
|
|
80
|
+
return None, "cache path escaped the project"
|
|
81
|
+
try:
|
|
82
|
+
with open(real, "r", encoding="utf-8") as handle:
|
|
83
|
+
data = json.load(handle)
|
|
84
|
+
except (OSError, ValueError, json.JSONDecodeError):
|
|
85
|
+
return None, "cache unreadable"
|
|
86
|
+
if not isinstance(data, dict) or not isinstance(data.get("files"), dict):
|
|
87
|
+
return None, "cache is not a parse index"
|
|
88
|
+
schema = data.get("schema_version") or 0
|
|
89
|
+
if schema > PARSE_CACHE_SCHEMA_VERSION:
|
|
90
|
+
return None, "cache schema is newer than this analyzer"
|
|
91
|
+
if data.get("analyzer_version") != ANALYZER_VERSION:
|
|
92
|
+
return None, "cache analyzer version mismatch"
|
|
93
|
+
cached_root = data.get("project_root")
|
|
94
|
+
if cached_root and os.path.realpath(cached_root) != root:
|
|
95
|
+
return None, "cache belongs to a different project path"
|
|
96
|
+
return data, "ok"
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def save_parse_cache(project_root, files, incremental=None):
|
|
100
|
+
root = os.path.realpath(project_root)
|
|
101
|
+
path = default_parse_cache_path(root)
|
|
102
|
+
directory = os.path.dirname(path)
|
|
103
|
+
if directory != os.path.join(root, BASELINE_DIRNAME):
|
|
104
|
+
raise ValueError("Parse cache destination must stay inside the project.")
|
|
105
|
+
os.makedirs(directory, exist_ok=True)
|
|
106
|
+
payload = {
|
|
107
|
+
"schema_version": PARSE_CACHE_SCHEMA_VERSION,
|
|
108
|
+
"analyzer_version": ANALYZER_VERSION,
|
|
109
|
+
"project_root": root,
|
|
110
|
+
"files": files,
|
|
111
|
+
}
|
|
112
|
+
if incremental:
|
|
113
|
+
payload["incremental"] = {
|
|
114
|
+
"discovered": incremental.get("discovered", 0),
|
|
115
|
+
"parsed": incremental.get("parsed", 0),
|
|
116
|
+
"reused": incremental.get("reused", 0),
|
|
117
|
+
}
|
|
118
|
+
with open(path, "w", encoding="utf-8") as handle:
|
|
119
|
+
json.dump(payload, handle, indent=2, sort_keys=True)
|
|
120
|
+
return path
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def _index_record(rel_path, parsed, digest):
|
|
124
|
+
labels = label_file(rel_path)
|
|
125
|
+
parsed = parsed or {}
|
|
126
|
+
return {
|
|
127
|
+
"layer": labels["cluster"],
|
|
128
|
+
"cluster": labels["cluster"],
|
|
129
|
+
"role": labels["role"],
|
|
130
|
+
"language": parsed.get("language"),
|
|
131
|
+
"imports": list(parsed.get("imports") or []),
|
|
132
|
+
"import_details": list(parsed.get("import_details") or []),
|
|
133
|
+
"symbols": list(parsed.get("symbols") or []),
|
|
134
|
+
"parse_error": parsed.get("parse_error"),
|
|
135
|
+
"package": parsed.get("package") or "",
|
|
136
|
+
"hash": digest,
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def _cache_payload(record, depends_on=None):
|
|
141
|
+
return {
|
|
142
|
+
"hash": record.get("hash"),
|
|
143
|
+
"record": {
|
|
144
|
+
"layer": record.get("layer"),
|
|
145
|
+
"cluster": record.get("cluster"),
|
|
146
|
+
"role": record.get("role"),
|
|
147
|
+
"language": record.get("language"),
|
|
148
|
+
"imports": list(record.get("imports") or []),
|
|
149
|
+
"import_details": list(record.get("import_details") or []),
|
|
150
|
+
"symbols": list(record.get("symbols") or []),
|
|
151
|
+
"parse_error": record.get("parse_error"),
|
|
152
|
+
"package": record.get("package") or "",
|
|
153
|
+
},
|
|
154
|
+
"depends_on": list(depends_on or []),
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def build_incremental_index(project_root, rules=None, persist_cache=True, reuse_cache=True):
|
|
159
|
+
del rules
|
|
160
|
+
files, truncated, unsupported = collect_source_files(project_root)
|
|
161
|
+
cache, cache_reason = load_parse_cache(project_root) if reuse_cache else (None, "cache disabled")
|
|
162
|
+
cached_files = (cache or {}).get("files") or {}
|
|
163
|
+
dependency_graph = {}
|
|
164
|
+
parsed_files = []
|
|
165
|
+
reused_files = []
|
|
166
|
+
changed_files = []
|
|
167
|
+
added_files = []
|
|
168
|
+
current_paths = []
|
|
169
|
+
|
|
170
|
+
for absolute, rel_path in files:
|
|
171
|
+
digest = hash_source_file(absolute, project_root=project_root)
|
|
172
|
+
current_paths.append(rel_path)
|
|
173
|
+
cached = cached_files.get(rel_path)
|
|
174
|
+
can_reuse = (
|
|
175
|
+
cache is not None
|
|
176
|
+
and digest
|
|
177
|
+
and cached
|
|
178
|
+
and cached.get("hash") == digest
|
|
179
|
+
and _valid_cached_record(cached.get("record"))
|
|
180
|
+
)
|
|
181
|
+
if can_reuse:
|
|
182
|
+
record = dict(cached["record"])
|
|
183
|
+
labels = label_file(rel_path)
|
|
184
|
+
record["cluster"] = labels["cluster"]
|
|
185
|
+
record["role"] = labels["role"]
|
|
186
|
+
record["layer"] = get_layer(rel_path) or labels["cluster"]
|
|
187
|
+
record["hash"] = digest
|
|
188
|
+
record["reused"] = True
|
|
189
|
+
dependency_graph[rel_path] = record
|
|
190
|
+
reused_files.append(rel_path)
|
|
191
|
+
continue
|
|
192
|
+
|
|
193
|
+
parsed = parse_source_file(absolute, rel_path, max_bytes=MAX_FILE_BYTES) or {}
|
|
194
|
+
record = _index_record(rel_path, parsed, digest)
|
|
195
|
+
record["layer"] = get_layer(rel_path) or record["cluster"]
|
|
196
|
+
record["reused"] = False
|
|
197
|
+
dependency_graph[rel_path] = record
|
|
198
|
+
parsed_files.append(rel_path)
|
|
199
|
+
if rel_path in cached_files:
|
|
200
|
+
changed_files.append(rel_path)
|
|
201
|
+
else:
|
|
202
|
+
added_files.append(rel_path)
|
|
203
|
+
|
|
204
|
+
config_cache = {}
|
|
205
|
+
go_cache = {}
|
|
206
|
+
for rel_path, record in dependency_graph.items():
|
|
207
|
+
extension = os.path.splitext(rel_path)[1].lower()
|
|
208
|
+
if extension in JS_EXTENSIONS:
|
|
209
|
+
resolution = nearest_config(rel_path, project_root, config_cache)
|
|
210
|
+
if resolution:
|
|
211
|
+
record["module_resolution"] = resolution
|
|
212
|
+
elif extension in GO_EXTENSIONS:
|
|
213
|
+
module_info = nearest_go_module(rel_path, project_root, go_cache)
|
|
214
|
+
if module_info:
|
|
215
|
+
record["go_module"] = module_info
|
|
216
|
+
|
|
217
|
+
deleted_files = sorted(set(cached_files) - set(current_paths))
|
|
218
|
+
incremental = empty_incremental_stats()
|
|
219
|
+
incremental.update({
|
|
220
|
+
"discovered": len(current_paths),
|
|
221
|
+
"parsed": len(parsed_files),
|
|
222
|
+
"reused": len(reused_files),
|
|
223
|
+
"changed": len(changed_files),
|
|
224
|
+
"added": len(added_files),
|
|
225
|
+
"deleted": len(deleted_files),
|
|
226
|
+
"cache_used": cache is not None,
|
|
227
|
+
"cache_reason": cache_reason,
|
|
228
|
+
"parsed_files": sorted(parsed_files),
|
|
229
|
+
"reused_files": sorted(reused_files),
|
|
230
|
+
"changed_files": sorted(changed_files),
|
|
231
|
+
"added_files": sorted(added_files),
|
|
232
|
+
"deleted_files": deleted_files,
|
|
233
|
+
"previous_depends_on": {
|
|
234
|
+
path: list(entry.get("depends_on") or [])
|
|
235
|
+
for path, entry in cached_files.items()
|
|
236
|
+
if isinstance(entry, dict)
|
|
237
|
+
},
|
|
238
|
+
})
|
|
239
|
+
return dependency_graph, {
|
|
240
|
+
"truncated": truncated,
|
|
241
|
+
"unsupported_files": unsupported,
|
|
242
|
+
"incremental": incremental,
|
|
243
|
+
"persist_cache": persist_cache,
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
def _affected_files(changed, added, deleted, resolved_graph, previous_depends):
|
|
248
|
+
reverse_prev = defaultdict(set)
|
|
249
|
+
for source, deps in (previous_depends or {}).items():
|
|
250
|
+
for dep in deps:
|
|
251
|
+
reverse_prev[dep].add(source)
|
|
252
|
+
reverse_cur = defaultdict(set)
|
|
253
|
+
for source, deps in (resolved_graph or {}).items():
|
|
254
|
+
for dep in deps:
|
|
255
|
+
reverse_cur[dep].add(source)
|
|
256
|
+
|
|
257
|
+
seeds = set(changed or []) | set(added or []) | set(deleted or [])
|
|
258
|
+
affected = set(seeds)
|
|
259
|
+
for path in list(seeds):
|
|
260
|
+
affected.update(reverse_prev.get(path) or [])
|
|
261
|
+
affected.update(reverse_cur.get(path) or [])
|
|
262
|
+
affected.update((previous_depends or {}).get(path) or [])
|
|
263
|
+
affected.update(resolved_graph.get(path) or [])
|
|
264
|
+
return sorted(affected)
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
def finalize_incremental_index(project_root, dependency_graph, resolved_graph, incremental, persist_cache=True):
|
|
268
|
+
incremental = dict(incremental or empty_incremental_stats())
|
|
269
|
+
affected = _affected_files(
|
|
270
|
+
incremental.get("changed_files") or [],
|
|
271
|
+
incremental.get("added_files") or [],
|
|
272
|
+
incremental.get("deleted_files") or [],
|
|
273
|
+
resolved_graph,
|
|
274
|
+
incremental.get("previous_depends_on") or {},
|
|
275
|
+
)
|
|
276
|
+
incremental["invalidated_files"] = affected
|
|
277
|
+
incremental["invalidated"] = len(affected)
|
|
278
|
+
incremental.pop("previous_depends_on", None)
|
|
279
|
+
|
|
280
|
+
if persist_cache and dependency_graph:
|
|
281
|
+
cache_files = {}
|
|
282
|
+
for rel_path, record in dependency_graph.items():
|
|
283
|
+
cache_files[rel_path] = _cache_payload(
|
|
284
|
+
record,
|
|
285
|
+
depends_on=sorted(resolved_graph.get(rel_path) or []),
|
|
286
|
+
)
|
|
287
|
+
save_parse_cache(project_root, cache_files, incremental=incremental)
|
|
288
|
+
return incremental
|
adre/core/layers.py
ADDED
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import re
|
|
3
|
+
from collections import Counter
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
ROLE_ALIASES = {
|
|
7
|
+
"presentation": {
|
|
8
|
+
"routes", "routers", "api", "endpoints", "views", "controllers", "controller",
|
|
9
|
+
"handler", "rest",
|
|
10
|
+
"pages", "screens", "components", "interface", "interfaces",
|
|
11
|
+
"presentation", "presenters", "handlers",
|
|
12
|
+
},
|
|
13
|
+
"application": {
|
|
14
|
+
"services", "service", "business", "use_cases", "usecases",
|
|
15
|
+
"application", "commands", "queries",
|
|
16
|
+
},
|
|
17
|
+
"domain": {
|
|
18
|
+
"domain", "entities", "entity", "value_objects", "models", "model", "schemas",
|
|
19
|
+
},
|
|
20
|
+
"data": {
|
|
21
|
+
"repositories", "repository", "persistence", "infrastructure", "infra",
|
|
22
|
+
"database", "db", "dao", "dal", "gateways", "gateway",
|
|
23
|
+
},
|
|
24
|
+
"shared": {
|
|
25
|
+
"shared", "utils", "dependencies",
|
|
26
|
+
},
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
# Folder names whose meaning depends on where they live. "queries" is a
|
|
30
|
+
# CQRS application folder at the top level but SQL inside db/; "models" is
|
|
31
|
+
# the domain at the top level but ORM tables inside persistence/.
|
|
32
|
+
AMBIGUOUS_ROLE_TOKENS = {
|
|
33
|
+
"queries", "commands", "models", "model", "schemas", "entities", "entity",
|
|
34
|
+
"handlers", "interfaces", "interface",
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
GENERIC_CLUSTER_NAMES = {
|
|
38
|
+
"app", "src", "lib", "core", "pkg", "root", "source", "python", "js", "ts",
|
|
39
|
+
"backend", "frontend", "main", "java", "kotlin", "internal", "cmd", "com", "org",
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
FILENAME_ROLE_SUFFIXES = {
|
|
43
|
+
"controller": "presentation",
|
|
44
|
+
"controllers": "presentation",
|
|
45
|
+
"route": "presentation",
|
|
46
|
+
"routes": "presentation",
|
|
47
|
+
"router": "presentation",
|
|
48
|
+
"handler": "presentation",
|
|
49
|
+
"view": "presentation",
|
|
50
|
+
"service": "application",
|
|
51
|
+
"services": "application",
|
|
52
|
+
"usecase": "application",
|
|
53
|
+
"use_case": "application",
|
|
54
|
+
"repository": "data",
|
|
55
|
+
"repo": "data",
|
|
56
|
+
"dao": "data",
|
|
57
|
+
"gateway": "data",
|
|
58
|
+
"client": "data",
|
|
59
|
+
"model": "domain",
|
|
60
|
+
"entity": "domain",
|
|
61
|
+
"schema": "domain",
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
FORBIDDEN_ROLE_EDGES = {
|
|
65
|
+
("data", "presentation"),
|
|
66
|
+
("data", "application"),
|
|
67
|
+
("domain", "presentation"),
|
|
68
|
+
("domain", "application"),
|
|
69
|
+
("domain", "data"),
|
|
70
|
+
("application", "presentation"),
|
|
71
|
+
("presentation", "data"),
|
|
72
|
+
("shared", "presentation"),
|
|
73
|
+
("shared", "application"),
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
PERSISTENCE_IMPORTS = {
|
|
77
|
+
"sqlite3", "sqlalchemy", "psycopg2", "psycopg", "asyncpg", "pymongo",
|
|
78
|
+
"mongoengine", "peewee", "django.db", "sqlmodel",
|
|
79
|
+
"pg", "mysql", "mysql2", "mongodb", "mongoose", "prisma", "typeorm",
|
|
80
|
+
"sequelize", "knex", "better-sqlite3", "@prisma/client",
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
# Database packages identified by a dotted (JVM, Django) or slashed (Go) prefix.
|
|
84
|
+
PERSISTENCE_PREFIXES = (
|
|
85
|
+
"django.db",
|
|
86
|
+
"java.sql", "javax.sql", "javax.persistence", "jakarta.persistence",
|
|
87
|
+
"org.hibernate", "org.springframework.jdbc", "org.springframework.data.jpa",
|
|
88
|
+
"org.springframework.data.mongodb", "org.jooq", "org.mybatis", "com.mongodb",
|
|
89
|
+
"androidx.room", "org.jetbrains.exposed",
|
|
90
|
+
"database/sql", "gorm.io/gorm", "github.com/jmoiron/sqlx", "github.com/jackc/pgx",
|
|
91
|
+
"github.com/lib/pq", "github.com/go-sql-driver/mysql", "go.mongodb.org/mongo-driver",
|
|
92
|
+
"github.com/mattn/go-sqlite3", "entgo.io/ent",
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def persistence_import_hits(record):
|
|
97
|
+
"""Imports of a database driver or ORM package.
|
|
98
|
+
|
|
99
|
+
Relative imports and imports that resolve to a project file are local
|
|
100
|
+
modules (e.g. a project's own `pg.py`), not database drivers.
|
|
101
|
+
"""
|
|
102
|
+
hits = []
|
|
103
|
+
for detail in (record or {}).get("import_details") or []:
|
|
104
|
+
module = (detail.get("module") or "").strip()
|
|
105
|
+
if not module or module.startswith("."):
|
|
106
|
+
continue
|
|
107
|
+
if detail.get("level"):
|
|
108
|
+
continue
|
|
109
|
+
if detail.get("resolved"):
|
|
110
|
+
continue
|
|
111
|
+
root = module.split("/")[0].split(".")[0]
|
|
112
|
+
if (
|
|
113
|
+
module in PERSISTENCE_IMPORTS
|
|
114
|
+
or root in PERSISTENCE_IMPORTS
|
|
115
|
+
or any(module == prefix or module.startswith(prefix + ".") or module.startswith(prefix + "/")
|
|
116
|
+
for prefix in PERSISTENCE_PREFIXES)
|
|
117
|
+
):
|
|
118
|
+
hits.append(detail)
|
|
119
|
+
return hits
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def normalize_token(value):
|
|
123
|
+
return (value or "").lower().replace("-", "_")
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def path_parts(rel_path):
|
|
127
|
+
normalized = rel_path.replace("\\", "/")
|
|
128
|
+
return [part for part in normalized.split("/") if part and part != "."]
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def role_for_token(token):
|
|
132
|
+
token = normalize_token(token)
|
|
133
|
+
for role, aliases in ROLE_ALIASES.items():
|
|
134
|
+
if token in aliases:
|
|
135
|
+
return role
|
|
136
|
+
return None
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def role_from_filename(filename):
|
|
140
|
+
stem = os.path.splitext(filename)[0]
|
|
141
|
+
split_camel = re.sub(r"([a-z0-9])([A-Z])", r"\1_\2", stem)
|
|
142
|
+
parts = [normalize_token(part) for part in re.split(r"[_\.\-]+", split_camel) if part]
|
|
143
|
+
if not parts:
|
|
144
|
+
return None
|
|
145
|
+
return FILENAME_ROLE_SUFFIXES.get(parts[-1])
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def label_file(rel_path):
|
|
149
|
+
parts = path_parts(rel_path)
|
|
150
|
+
filename = parts[-1] if parts else rel_path
|
|
151
|
+
folders = parts[:-1]
|
|
152
|
+
|
|
153
|
+
role_folders = [
|
|
154
|
+
(normalize_token(folder), role_for_token(folder))
|
|
155
|
+
for folder in folders
|
|
156
|
+
if role_for_token(folder)
|
|
157
|
+
]
|
|
158
|
+
if role_folders:
|
|
159
|
+
cluster, role = role_folders[-1]
|
|
160
|
+
if cluster in AMBIGUOUS_ROLE_TOKENS:
|
|
161
|
+
# app/db/queries is database code and app/api/schemas is API code:
|
|
162
|
+
# an ambiguous inner folder takes the role of the nearest
|
|
163
|
+
# unambiguous layer folder that contains it.
|
|
164
|
+
for outer_cluster, outer_role in reversed(role_folders[:-1]):
|
|
165
|
+
if outer_cluster not in AMBIGUOUS_ROLE_TOKENS:
|
|
166
|
+
if outer_role != role:
|
|
167
|
+
cluster, role = outer_cluster, outer_role
|
|
168
|
+
break
|
|
169
|
+
return {
|
|
170
|
+
"cluster": cluster,
|
|
171
|
+
"role": role,
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
filename_role = role_from_filename(filename)
|
|
175
|
+
if filename_role:
|
|
176
|
+
stem = os.path.splitext(filename)[0]
|
|
177
|
+
split_camel = re.sub(r"([a-z0-9])([A-Z])", r"\1_\2", stem)
|
|
178
|
+
parts_from_name = [normalize_token(part) for part in re.split(r"[_\.\-]+", split_camel) if part]
|
|
179
|
+
cluster = parts_from_name[-1] if parts_from_name else filename_role
|
|
180
|
+
return {
|
|
181
|
+
"cluster": cluster,
|
|
182
|
+
"role": filename_role,
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if folders:
|
|
186
|
+
meaningful = [
|
|
187
|
+
normalize_token(part)
|
|
188
|
+
for part in folders
|
|
189
|
+
if normalize_token(part) not in GENERIC_CLUSTER_NAMES
|
|
190
|
+
]
|
|
191
|
+
cluster = meaningful[-1] if meaningful else normalize_token(folders[-1])
|
|
192
|
+
return {
|
|
193
|
+
"cluster": cluster,
|
|
194
|
+
"role": "unknown",
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return {
|
|
198
|
+
"cluster": "root",
|
|
199
|
+
"role": "unknown",
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def filename_role_token(filename):
|
|
204
|
+
stem = os.path.splitext(filename)[0]
|
|
205
|
+
split_camel = re.sub(r"([a-z0-9])([A-Z])", r"\1_\2", stem)
|
|
206
|
+
parts = [normalize_token(part) for part in re.split(r"[_\.\-]+", split_camel) if part]
|
|
207
|
+
if not parts:
|
|
208
|
+
return None
|
|
209
|
+
token = parts[-1]
|
|
210
|
+
if token in FILENAME_ROLE_SUFFIXES:
|
|
211
|
+
return token
|
|
212
|
+
return None
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def refine_relationship_clusters(dependency_graph, resolved_graph):
|
|
216
|
+
"""Split a physical cluster when filename roles differ AND imports connect them.
|
|
217
|
+
|
|
218
|
+
Folder names stay the physical cluster. Architectural roles are assigned from
|
|
219
|
+
proven internal relationships (service → client/repository), not from a lone
|
|
220
|
+
service.py filename. Returns the number of files reassigned.
|
|
221
|
+
"""
|
|
222
|
+
files_by_cluster = {}
|
|
223
|
+
for file_path, record in dependency_graph.items():
|
|
224
|
+
cluster = record.get("cluster") or "root"
|
|
225
|
+
files_by_cluster.setdefault(cluster, []).append(file_path)
|
|
226
|
+
|
|
227
|
+
reassigned = 0
|
|
228
|
+
for cluster, files in files_by_cluster.items():
|
|
229
|
+
if len(files) < 2:
|
|
230
|
+
continue
|
|
231
|
+
if "/" in cluster:
|
|
232
|
+
continue
|
|
233
|
+
groups = {"application": [], "data": [], "other": []}
|
|
234
|
+
tokens = {"application": [], "data": []}
|
|
235
|
+
for file_path in files:
|
|
236
|
+
token = filename_role_token(os.path.basename(file_path))
|
|
237
|
+
role = FILENAME_ROLE_SUFFIXES.get(token) if token else None
|
|
238
|
+
if role in {"application", "data"}:
|
|
239
|
+
groups[role].append(file_path)
|
|
240
|
+
tokens[role].append(token)
|
|
241
|
+
else:
|
|
242
|
+
groups["other"].append(file_path)
|
|
243
|
+
if not groups["application"] or not groups["data"]:
|
|
244
|
+
continue
|
|
245
|
+
|
|
246
|
+
application_files = set(groups["application"])
|
|
247
|
+
data_files = set(groups["data"])
|
|
248
|
+
physical = set(files)
|
|
249
|
+
internal_edges = 0
|
|
250
|
+
for source in groups["application"]:
|
|
251
|
+
for target in resolved_graph.get(source) or []:
|
|
252
|
+
if target in data_files:
|
|
253
|
+
internal_edges += 1
|
|
254
|
+
if internal_edges < 1:
|
|
255
|
+
continue
|
|
256
|
+
|
|
257
|
+
inbound = 0
|
|
258
|
+
for source, targets in resolved_graph.items():
|
|
259
|
+
if source in physical:
|
|
260
|
+
continue
|
|
261
|
+
for target in targets:
|
|
262
|
+
if target in application_files:
|
|
263
|
+
inbound += 1
|
|
264
|
+
break
|
|
265
|
+
if inbound < 1 and len(groups["application"]) < 2:
|
|
266
|
+
continue
|
|
267
|
+
|
|
268
|
+
application_token = Counter(tokens["application"]).most_common(1)[0][0]
|
|
269
|
+
data_token = Counter(tokens["data"]).most_common(1)[0][0]
|
|
270
|
+
for file_path in groups["application"]:
|
|
271
|
+
record = dependency_graph[file_path]
|
|
272
|
+
record["physical_cluster"] = cluster
|
|
273
|
+
record["cluster"] = "{0}/{1}".format(cluster, application_token)
|
|
274
|
+
record["role"] = "application"
|
|
275
|
+
record["layer"] = record["cluster"]
|
|
276
|
+
record["relationship_split"] = True
|
|
277
|
+
reassigned += 1
|
|
278
|
+
for file_path in groups["data"]:
|
|
279
|
+
record = dependency_graph[file_path]
|
|
280
|
+
record["physical_cluster"] = cluster
|
|
281
|
+
record["cluster"] = "{0}/{1}".format(cluster, data_token)
|
|
282
|
+
record["role"] = "data"
|
|
283
|
+
record["layer"] = record["cluster"]
|
|
284
|
+
record["relationship_split"] = True
|
|
285
|
+
reassigned += 1
|
|
286
|
+
return reassigned
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
def get_layer(rel_path, rules=None):
|
|
290
|
+
if rules:
|
|
291
|
+
aliases = rules.get("layer_aliases") or {}
|
|
292
|
+
parts = {normalize_token(part) for part in path_parts(rel_path)}
|
|
293
|
+
for layer, layer_aliases in aliases.items():
|
|
294
|
+
if parts & set(layer_aliases):
|
|
295
|
+
return layer
|
|
296
|
+
return label_file(rel_path)["cluster"]
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
def established_pattern_from_roles(present_roles):
|
|
300
|
+
sequence = []
|
|
301
|
+
for role in ("presentation", "application", "domain", "data"):
|
|
302
|
+
if role in present_roles:
|
|
303
|
+
sequence.append(role)
|
|
304
|
+
if len(sequence) >= 2:
|
|
305
|
+
return " -> ".join(sequence)
|
|
306
|
+
return ""
|