-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
120 lines (104 loc) · 3.48 KB
/
Copy pathmain.py
File metadata and controls
120 lines (104 loc) · 3.48 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
from __future__ import annotations
import argparse
import sys
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parent
SRC_DIR = REPO_ROOT / "src"
if str(SRC_DIR) not in sys.path:
sys.path.insert(0, str(SRC_DIR))
INSTALL_HINT = (
"Install project dependencies first:\n"
" pip install .\n"
"or\n"
" pip install -r requirements.txt"
)
SUPPORT_HINT = (
"If you hit an error or have questions about this workflow, "
"please contact Hoshin Kim ([email protected])."
)
def build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
description="Run SIMPLE in interactive mode or from a TOML config.",
)
mode = parser.add_mutually_exclusive_group(required=True)
mode.add_argument(
"--interactive",
action="store_true",
help="Launch the interactive workflow wizard and then run it.",
)
mode.add_argument(
"--config",
help="Path to a TOML config file for non-interactive execution.",
)
parser.add_argument(
"--write-config",
help="When using --interactive, save the prompted answers to this TOML file.",
)
parser.add_argument(
"--from-stage",
default="prepare",
choices=("prepare", "system", "md"),
help="Stage to start from.",
)
parser.add_argument(
"--to-stage",
default="md",
choices=("prepare", "system", "md"),
help="Stage to stop at.",
)
parser.add_argument(
"--dry-run",
action="store_true",
help="Generate files without executing Amber binaries.",
)
return parser
def main(argv: list[str] | None = None) -> int:
parser = build_parser()
args = parser.parse_args(argv)
if args.config and args.write_config:
parser.error("--write-config can only be used together with --interactive.")
try:
from amber_metallo.config import load_config
from amber_metallo.workflow import run_workflow
if args.interactive:
from amber_metallo.cli import build_wizard_configs, execute_wizard_configs
from amber_metallo.subdirectory_search import (
prompt_for_subdirectory_search,
subdirectory_search_scope,
)
search_subdirectories = prompt_for_subdirectory_search(Path.cwd())
with subdirectory_search_scope(search_subdirectories):
wizard_result = build_wizard_configs(args.write_config)
return execute_wizard_configs(
wizard_result.configs,
from_stage=args.from_stage,
to_stage=args.to_stage,
dry_run=args.dry_run,
failure_hint=SUPPORT_HINT,
)
else:
config = load_config(args.config)
except ModuleNotFoundError as exc:
missing = exc.name or "a required package"
parser.exit(
1,
f"Missing dependency: {missing}\n{INSTALL_HINT}\n",
)
try:
result = run_workflow(
config=config,
from_stage=args.from_stage,
to_stage=args.to_stage,
dry_run=args.dry_run,
)
except Exception:
print(SUPPORT_HINT, file=sys.stderr)
raise
try:
from amber_metallo.reporting import print_workflow_summary
print_workflow_summary(result)
except ModuleNotFoundError:
print(result)
return 0
if __name__ == "__main__":
raise SystemExit(main())