-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtool_metadata.py
More file actions
319 lines (264 loc) · 10.6 KB
/
tool_metadata.py
File metadata and controls
319 lines (264 loc) · 10.6 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/usr/bin/env python3
"""Shared discovery helpers for Python tools under Tools/."""
from __future__ import annotations
import argparse
import json
import re
from dataclasses import asdict, dataclass
from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple
try:
import tomllib
except ImportError:
try:
import tomli as tomllib # type: ignore[import-not-found,no-redef]
except ImportError as exc: # pragma: no cover - environment guard
raise SystemExit("tomllib (or tomli on Python <3.11) is required") from exc
LIBRARY_SOURCE_PATHS = [
"Libraries/PyKotor/src",
"Libraries/bioware-kaitai-formats/src",
]
LIBRARY_TEST_PATHS = [
"Libraries/PyKotor/tests",
]
VERSION_KEY_PATTERNS = {
'"currentVersion":': "currentVersion",
"CURRENT_VERSION =": "CURRENT_VERSION",
"__version__ =": "__version__",
}
def _strip_py_suffix(path_str: str) -> str:
s = str(path_str)
return s[:-3] if s.endswith(".py") else s
@dataclass(frozen=True)
class ToolMetadata:
directory: str
relative_path: str
project_name: str
build_name: str
package_name: str
entrypoint: str
module_name: str
src_path: str
tests_path: Optional[str]
requirements_path: Optional[str]
requires_qt: bool
qt_api: Optional[str]
console: Optional[bool]
windowed: Optional[bool]
version_file: Optional[str]
version_key: Optional[str]
release_tag_suffix: Optional[str]
script_names: List[str]
min_python: str
validate_py38: bool
@property
def is_cli(self) -> bool:
return not bool(self.windowed) or bool(self.console)
def to_dict(self) -> Dict[str, Any]:
payload = asdict(self)
payload["is_cli"] = self.is_cli
return payload
def read_pyproject(pyproject_path: Path) -> Dict[str, Any]:
if not pyproject_path.exists():
return {}
with pyproject_path.open("rb") as handle:
return tomllib.load(handle)
def _module_target_to_entrypoint(module_target: str) -> str:
module_name = module_target.split(":", 1)[0]
return module_name.replace(".", "/") + ".py"
def _entrypoint_from_scripts(project_table: Dict[str, Any]) -> Tuple[Optional[str], List[str]]:
scripts = project_table.get("scripts", {})
if not scripts:
return None, []
script_names = sorted(str(name) for name in scripts)
first_target = scripts[script_names[0]]
return _module_target_to_entrypoint(str(first_target)), script_names
def _discover_entrypoint(src_dir: Path) -> Optional[str]:
for candidate in sorted(src_dir.rglob("__main__.py")):
return candidate.relative_to(src_dir).as_posix()
return None
def _detect_qt(
project_table: Dict[str, Any], tool_table: Dict[str, Any]
) -> Tuple[bool, Optional[str]]:
qt_api: Optional[str] = None
requires_qt = False
for dependency in project_table.get("dependencies", []):
dep_lower = str(dependency).lower()
if "pyqt5" in dep_lower:
requires_qt = True
qt_api = qt_api or "PyQt5"
elif "pyqt6" in dep_lower:
requires_qt = True
qt_api = qt_api or "PyQt6"
elif "pyside2" in dep_lower:
requires_qt = True
qt_api = qt_api or "PySide2"
elif "pyside6" in dep_lower:
requires_qt = True
qt_api = qt_api or "PySide6"
elif "qtpy" in dep_lower:
requires_qt = True
dep_table = tool_table.get("dependencies", {})
configured_qt = dep_table.get("qt-api")
if configured_qt:
requires_qt = True
qt_api = str(configured_qt)
pyinstaller_table = tool_table.get("pyinstaller", {})
pyinstaller_qt = pyinstaller_table.get("qt-api")
if pyinstaller_qt:
requires_qt = True
qt_api = str(pyinstaller_qt)
return requires_qt, qt_api
def _discover_package_name(entrypoint: str, src_dir: Path) -> str:
entry_path = Path(entrypoint)
if len(entry_path.parts) > 1:
return entry_path.parts[0]
for package_dir in sorted(src_dir.iterdir()):
if package_dir.is_dir() and (package_dir / "__init__.py").exists():
return package_dir.name
raise ValueError(f"Unable to infer package name from src directory: {src_dir}")
def _infer_version_info(
tool_root: Path,
package_name: str,
tool_config: Dict[str, Any],
) -> Tuple[Optional[str], Optional[str], Optional[str]]:
custom = tool_config.get("pykotor-tool", {})
version_file = custom.get("version-file")
version_key = custom.get("version-key")
tag_suffix = custom.get("release-tag-suffix")
if version_file and version_key:
return str(version_file), str(version_key), str(tag_suffix) if tag_suffix else None
candidate_paths = [
tool_root / "src" / package_name / "config" / "config_info.py",
tool_root / "src" / package_name / "config.py",
tool_root / "src" / package_name / "__main__.py",
tool_root / "src" / package_name / "__init__.py",
]
for candidate in candidate_paths:
if not candidate.exists():
continue
text = candidate.read_text(encoding="utf-8")
for pattern, inferred_key in VERSION_KEY_PATTERNS.items():
if pattern in text:
relative = candidate.relative_to(tool_root).as_posix()
return relative, inferred_key, str(tag_suffix) if tag_suffix else None
return None, None, str(tag_suffix) if tag_suffix else None
def _parse_min_python(requires_python: Optional[str]) -> Tuple[int, int]:
if not requires_python:
return (3, 8)
match = re.search(r">=\s*(\d+)\.(\d+)", requires_python)
if match:
return (int(match.group(1)), int(match.group(2)))
return (3, 8)
def _validate_py38(min_python: Tuple[int, int]) -> bool:
return min_python <= (3, 8)
def discover_tools(repo_root: Optional[Path] = None) -> List[ToolMetadata]:
repo_root = repo_root or Path(__file__).resolve().parent
tools_dir = repo_root / "Tools"
if not tools_dir.exists():
return []
discovered: List[ToolMetadata] = []
for tool_root in sorted(path for path in tools_dir.iterdir() if path.is_dir()):
if tool_root.name.startswith("."):
continue
pyproject = read_pyproject(tool_root / "pyproject.toml")
src_dir = tool_root / "src"
if not pyproject or not src_dir.exists():
continue
project_table = pyproject.get("project", {})
tool_table = pyproject.get("tool", {})
pyinstaller_table = tool_table.get("pyinstaller", {})
entrypoint = pyinstaller_table.get("entrypoint")
script_names: List[str] = []
if not entrypoint:
entrypoint, script_names = _entrypoint_from_scripts(project_table)
if not entrypoint:
entrypoint = _discover_entrypoint(src_dir)
if not entrypoint:
continue
package_name = _discover_package_name(str(entrypoint), src_dir)
project_name = str(project_table.get("name") or tool_root.name.lower())
build_name = str(pyinstaller_table.get("name") or project_name)
module_name = _strip_py_suffix(str(entrypoint)).replace("/", ".")
requires_qt, qt_api = _detect_qt(project_table, tool_table)
tests_dir = tool_root / "tests"
requirements_file = tool_root / "requirements.txt"
version_file, version_key, tag_suffix = _infer_version_info(
tool_root, package_name, tool_table
)
requires_python = project_table.get("requires-python")
min_py = _parse_min_python(
str(requires_python) if requires_python is not None else None
)
min_python = f"{min_py[0]}.{min_py[1]}"
validate_py38 = _validate_py38(min_py)
discovered.append(
ToolMetadata(
directory=tool_root.name,
relative_path=tool_root.relative_to(repo_root).as_posix(),
project_name=project_name,
build_name=build_name,
package_name=package_name,
entrypoint=str(entrypoint),
module_name=module_name,
src_path=(tool_root / "src").relative_to(repo_root).as_posix(),
tests_path=tests_dir.relative_to(repo_root).as_posix()
if tests_dir.exists()
else None,
requirements_path=(
requirements_file.relative_to(repo_root).as_posix()
if requirements_file.exists()
else None
),
requires_qt=requires_qt,
qt_api=qt_api,
console=pyinstaller_table.get("console"),
windowed=pyinstaller_table.get("windowed"),
version_file=version_file,
version_key=version_key,
release_tag_suffix=tag_suffix or build_name,
script_names=script_names,
min_python=min_python,
validate_py38=validate_py38,
)
)
return discovered
def resolve_tool(selector: str, repo_root: Optional[Path] = None) -> ToolMetadata:
normalized = selector.replace("\\", "/").rstrip("/").lower()
for tool in discover_tools(repo_root):
candidates = {
tool.directory.lower(),
tool.project_name.lower(),
tool.build_name.lower(),
tool.package_name.lower(),
tool.relative_path.lower(),
}
if normalized in candidates:
return tool
raise KeyError(selector)
def _emit_list(tools: List[ToolMetadata], format_name: str) -> None:
payload = [tool.to_dict() for tool in tools]
if format_name == "json":
print(json.dumps(payload, indent=2))
return
print(f"tools_matrix={json.dumps(payload)}")
def main() -> None:
parser = argparse.ArgumentParser(description="Discover Python tools under Tools/")
parser.add_argument("--tool", help="Return metadata for one tool selector")
parser.add_argument("--format", choices=["json", "github"], default="json")
parser.add_argument("--cli-only", action="store_true", help="Only emit non-windowed tools")
args = parser.parse_args()
repo_root = Path(__file__).resolve().parent
if args.tool:
try:
tool = resolve_tool(args.tool, repo_root)
except KeyError as exc:
raise SystemExit(f"Unknown tool selector: {args.tool}") from exc
print(json.dumps(tool.to_dict(), indent=2))
return
tools = discover_tools(repo_root)
if args.cli_only:
tools = [tool for tool in tools if tool.is_cli]
_emit_list(tools, args.format)
if __name__ == "__main__":
main()