-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava_inventory.py
More file actions
executable file
·146 lines (109 loc) · 3.93 KB
/
Copy pathjava_inventory.py
File metadata and controls
executable file
·146 lines (109 loc) · 3.93 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
#!/usr/bin/env python3
"""Master CLI for the Java inventory utilities."""
# pylint: disable=missing-function-docstring
import argparse
import importlib.util
import sys
import textwrap
import tomllib
from pathlib import Path
from common.inventory_common import error, require_command, set_debug
ROOT = Path(__file__).resolve().parent
# Make the repository root importable so modules can share common utilities.
if str(ROOT) not in sys.path:
sys.path.insert(0, str(ROOT))
def project_metadata() -> dict:
"""Load project metadata from pyproject.toml."""
with (ROOT / "pyproject.toml").open("rb") as file:
return tomllib.load(file)["project"]
def load_module(name: str, path: Path):
"""Load a Python module from a path without requiring package layout."""
if not path.is_file():
raise FileNotFoundError(f"inventory module not found: {path}")
spec = importlib.util.spec_from_file_location(name, path)
if spec is None or spec.loader is None:
raise ImportError(f"unable to load inventory module: {path}")
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
def subcommand_setup(_args, _root: Path):
require_command("git")
require_command("mvn")
def subcommand_version(_args):
metadata = project_metadata()
print(f"{metadata['name']} {metadata['version']}")
def subcommand_about(_args):
metadata = project_metadata()
name = metadata["name"]
version = metadata["version"]
description = metadata["description"]
authors = metadata.get("authors", [])
license_info = metadata.get("license", {})
urls = metadata.get("urls", {})
print(f"{name} {version}")
print()
print(textwrap.fill(description, width=80))
print()
if authors:
print(f"Author: {authors[0]['name']}")
if license_info:
print(f"License: {license_info}")
if "Repository" in urls:
print(f"Repository: {urls['Repository']}")
if "Issues" in urls:
print(f"Issues: {urls['Issues']}")
print()
print(f"Run '{name} --help' for help.")
def build_parser():
parser = argparse.ArgumentParser(
prog="java_inventory.py",
description="Java inventory and development tools",
)
parser.add_argument("--debug", action="store_true", help="show debug logs")
subparsers = parser.add_subparsers(dest="command", required=True)
setup_handlers = [lambda args: subcommand_setup(args, ROOT)]
checkstyle = load_module(
"java_inventory_checkstyle",
ROOT / "checkstyle" / "src" / "main" / "python" / "checkstyle_inventory.py",
)
maven = load_module(
"java_inventory_maven",
ROOT / "maven" / "src" / "main" / "python" / "maven_inventory.py",
)
setup = subparsers.add_parser(
"setup",
help="set up all inventory components",
)
info = subparsers.add_parser(
"about",
help="show tool information and available commands",
)
version = subparsers.add_parser(
"version",
help="show the tool version",
)
checkstyle.register_commands(subparsers, ROOT, setup_handlers)
maven.register_commands(subparsers, ROOT, setup_handlers)
setup.set_defaults(setup_handlers=setup_handlers)
info.set_defaults(handler=subcommand_about)
version.set_defaults(handler=subcommand_version)
return parser
def main() -> int:
try:
parser = build_parser()
args = parser.parse_args()
set_debug(args.debug)
if args.command == "setup":
for handler in args.setup_handlers:
handler(args)
return 0
if args.command == "info":
subcommand_about(args)
return 0
args.handler(args)
return 0
except Exception as exc: # pylint: disable=broad-exception-caught # noqa: BLE001
error(str(exc), force=True)
return 1
if __name__ == "__main__":
sys.exit(main())