forked from sarbian/ModuleManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodulemanager_copy_build.py
More file actions
82 lines (62 loc) · 2.68 KB
/
Copy pathmodulemanager_copy_build.py
File metadata and controls
82 lines (62 loc) · 2.68 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
#!/usr/bin/env python3
"""Cross-platform post-build copy script for ModuleManager."""
from __future__ import annotations
import argparse
import os
import shutil
import subprocess
import sys
from pathlib import Path
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Copy build artifacts to KSP GameData")
parser.add_argument("--target-path", default=os.environ.get("TARGET_PATH", ""))
parser.add_argument("--target-dir", default=os.environ.get("TARGET_DIR", ""))
parser.add_argument("--target-name", default=os.environ.get("TARGET_NAME", ""))
parser.add_argument("--kspdir", default=os.environ.get("KSPDIR", ""))
parser.add_argument("--pdb2mdb", default=os.environ.get("PDB2MDB", ""))
return parser.parse_args()
def copy_if_exists(src: Path, dest: Path) -> None:
if src.exists():
shutil.copy2(src, dest)
def main() -> int:
args = parse_args()
if not args.target_path:
raise RuntimeError("Expected target path but it is not defined")
target_path = Path(args.target_path)
if not target_path.is_file():
raise RuntimeError(f"Expected target path to be a file but it is not: {target_path}")
if not args.target_dir:
raise RuntimeError("Expected target directory but it is not defined")
target_dir = Path(args.target_dir)
if not target_dir.is_dir():
raise RuntimeError(f"Expected target directory to exist but it is not: {target_dir}")
if not args.target_name:
raise RuntimeError("Expected target name but it is not defined")
if args.pdb2mdb:
print(f"Running '{args.pdb2mdb}'")
subprocess.run([args.pdb2mdb, str(target_path)], check=False)
else:
print("$PDB2MDB not found")
if not args.kspdir:
print("$KSPDIR not found")
return 0
kspdir = Path(args.kspdir)
if not kspdir.is_dir():
raise RuntimeError(f"Expected KSPDIR to point to a directory but it is not: {kspdir}")
game_data = kspdir / "GameData"
if not game_data.is_dir():
raise RuntimeError(f"Expected KSPDIR to contain a GameData directory but it does not: {kspdir}")
print(f"Copying to '{kspdir}'")
shutil.copy2(target_path, game_data)
copy_if_exists(target_dir / f"{args.target_name}.pdb", game_data)
copy_if_exists(target_dir / f"{args.target_name}.dll.mdb", game_data)
config_cache = game_data / "ModuleManager.ConfigCache"
if config_cache.exists():
config_cache.unlink()
return 0
if __name__ == "__main__":
try:
raise SystemExit(main())
except Exception as ex: # pragma: no cover - top-level script error handling
print(str(ex), file=sys.stderr)
raise SystemExit(1)