Repository navigation
Expand file tree
/
Copy pathworkspace_boundary_check.py
More file actions
178 lines (154 loc) · 5.4 KB
/
Copy pathworkspace_boundary_check.py
File metadata and controls
178 lines (154 loc) · 5.4 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
#!/usr/bin/env python3
"""Validate stable WebCodex Cargo workspace boundaries."""
from __future__ import annotations
import argparse
import json
import os
import re
import sys
from pathlib import Path
from typing import Any
REQUIRED_PACKAGES = frozenset(
{
"webcodex",
"webcodex-admin",
"webcodex-agent-config",
"webcodex-cli",
"webcodex-core",
"webcodex-runner",
"webcodex-sandbox",
"webcodex-workspace",
}
)
FORBIDDEN_DIRECT_DEPENDENCIES = {
"webcodex-core": frozenset({"salvo", "rusqlite", "quinn", "landlock"}),
"webcodex-runner": frozenset({"salvo", "rusqlite"}),
"webcodex-cli": frozenset(
{
"salvo",
"rusqlite",
"quinn",
"landlock",
"webcodex-runner",
"webcodex-sandbox",
"webcodex-workspace",
}
),
}
EXCLUDED_DIRECTORIES = frozenset(
{
".git",
"docs",
"target",
"generated",
"gen",
"dist",
"node_modules",
"coverage",
}
)
PARENT_SOURCE_PATH = re.compile(r'#\s*\[\s*path\s*=\s*"\.\./')
def check_metadata(metadata: dict[str, Any]) -> list[str]:
"""Return workspace membership and direct-dependency violations."""
violations: list[str] = []
packages_by_id = {
package["id"]: package
for package in metadata.get("packages", [])
if isinstance(package, dict) and "id" in package
}
workspace_packages = []
for member_id in metadata.get("workspace_members", []):
package = packages_by_id.get(member_id)
if package is None:
violations.append(
f"workspace member {member_id!r} is absent from cargo metadata packages"
)
continue
workspace_packages.append(package)
packages_by_name = {
package.get("name"): package
for package in workspace_packages
if isinstance(package.get("name"), str)
}
missing = sorted(REQUIRED_PACKAGES.difference(packages_by_name))
if missing:
violations.append(
"workspace is missing required package(s): " + ", ".join(missing)
)
for package_name, forbidden in FORBIDDEN_DIRECT_DEPENDENCIES.items():
package = packages_by_name.get(package_name)
if package is None:
continue
direct_dependencies = {
dependency.get("name")
for dependency in package.get("dependencies", [])
if isinstance(dependency, dict)
and isinstance(dependency.get("name"), str)
}
illegal = sorted(forbidden.intersection(direct_dependencies))
if illegal:
violations.append(
f"package {package_name} directly depends on forbidden package(s): "
+ ", ".join(illegal)
)
return violations
def check_parent_source_paths(root: Path) -> list[str]:
"""Return Rust source locations that share source across a parent directory."""
violations: list[str] = []
for current, directory_names, file_names in os.walk(root):
directory_names[:] = sorted(
name for name in directory_names if name not in EXCLUDED_DIRECTORIES
)
current_path = Path(current)
for file_name in sorted(file_names):
if not file_name.endswith(".rs"):
continue
path = current_path / file_name
try:
lines = path.read_text(encoding="utf-8").splitlines()
except (OSError, UnicodeError) as error:
violations.append(f"could not inspect Rust source {path}: {error}")
continue
relative_path = path.relative_to(root).as_posix()
for line_number, line in enumerate(lines, start=1):
if PARENT_SOURCE_PATH.search(line):
violations.append(
f"{relative_path}:{line_number}: cross-parent #[path] source sharing"
)
return violations
def evaluate(root: Path, metadata: dict[str, Any]) -> list[str]:
root = root.resolve()
return check_metadata(metadata) + check_parent_source_paths(root)
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--root", required=True, type=Path)
parser.add_argument("--metadata", required=True, type=Path)
return parser.parse_args()
def main() -> int:
args = parse_args()
try:
metadata = json.loads(args.metadata.read_text(encoding="utf-8"))
except (OSError, UnicodeError, json.JSONDecodeError) as error:
print(
f"[workspace-boundary][FAIL] could not read cargo metadata: {error}",
file=sys.stderr,
)
return 2
violations = evaluate(args.root, metadata)
if violations:
for violation in violations:
print(f"[workspace-boundary][FAIL] {violation}", file=sys.stderr)
print(
f"[workspace-boundary][FAIL] {len(violations)} violation(s)",
file=sys.stderr,
)
return 1
print("[workspace-boundary][ok] all 8 required workspace packages are present")
print(
"[workspace-boundary][ok] core, runner, and CLI direct dependencies "
"respect their boundaries"
)
print("[workspace-boundary][ok] no cross-parent #[path] source sharing")
return 0
if __name__ == "__main__":
raise SystemExit(main())