-
Notifications
You must be signed in to change notification settings - Fork 4
/
build.py
executable file
·185 lines (152 loc) · 5.25 KB
/
build.py
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
#!/usr/bin/env python3
import os
import platform
import shutil
import subprocess
import logging
from pathlib import Path
import argparse
import sys
system = platform.system().lower()
home = Path.home()
def set_log_level(log_level):
numeric_level = getattr(logging, log_level.upper(), None)
if not isinstance(numeric_level, int):
raise ValueError(f"Invalid log level: {log_level}")
logging.basicConfig(
level=numeric_level, format="%(asctime)s - %(levelname)s - %(message)s"
)
def bin_dir() -> Path:
if system in ["linux", "darwin"]:
return home / ".local" / "bin"
elif system == "windows":
return Path(os.getenv("USERHOME"))
else:
raise OSError(f"Unsupported OS {system}")
def data_dir() -> Path:
if system == "linux":
return home / ".local" / "share" / "extism-py"
elif system == "darwin":
return home / "Library" / "Application Support" / "extism-py"
elif system == "windows":
return Path(os.getenv("APPDATA")) / "extism-py"
else:
raise OSError(f"Unsupported OS {system}")
def exe_file() -> str:
if system == "windows":
return "extism-py.exe"
else:
return "extism-py"
def run_subprocess(command, cwd=None, quiet=False):
try:
logging.info(f"Running command: {' '.join(command)} in {cwd or '.'}")
stdout = subprocess.DEVNULL if quiet else None
stderr = subprocess.DEVNULL if quiet else None
subprocess.run(command, cwd=cwd, check=True, stdout=stdout, stderr=stderr)
except subprocess.CalledProcessError as e:
logging.error(f"Command '{' '.join(command)}' failed with error: {e}")
raise
def check_rust_installed():
try:
subprocess.run(
["rustc", "--version"],
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
subprocess.run(
["cargo", "--version"],
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
except subprocess.CalledProcessError:
logging.error(
"Rust and Cargo are required but not found. Please install Rust: https://www.rust-lang.org/tools/install"
)
sys.exit(1)
def do_build(args):
check_rust_installed()
run_subprocess(["cargo", "build", "--release"], cwd="./lib", quiet=args.quiet)
run_subprocess(["cargo", "build", "--release"], cwd="./bin", quiet=args.quiet)
shutil.copy2(Path("./bin/target/release") / exe_file(), Path(".") / exe_file())
logging.info("Build completed successfully.")
def do_install(args):
do_build(args)
bin_dir = Path(args.bin_dir)
data_dir = Path(args.data_dir)
bin_dir.mkdir(parents=True, exist_ok=True)
data_dir.mkdir(parents=True, exist_ok=True)
dest_path = bin_dir / exe_file()
logging.info(f"Copying binary to {dest_path}")
shutil.copy2(Path("./bin/target/release") / exe_file(), dest_path)
logging.info(f"Copying data files to {data_dir}")
shutil.copytree(
Path("./lib/target/wasm32-wasi/wasi-deps"), data_dir, dirs_exist_ok=True
)
logging.info(f"{exe_file()} installed to {bin_dir}")
logging.info(f"Data files installed to {data_dir}")
logging.info("Installation completed successfully.")
def do_clean(args):
logging.info("Cleaning build artifacts...")
shutil.rmtree("./lib/target", ignore_errors=True)
shutil.rmtree("./bin/target", ignore_errors=True)
if (Path(".") / exe_file()).exists():
(Path(".") / exe_file()).unlink()
logging.info("Clean completed successfully.")
def get_version(path):
try:
result = subprocess.run(
[path / exe_file(), "--version"], capture_output=True, text=True, check=True
)
return result.stdout.strip()
except subprocess.CalledProcessError:
return "Unknown"
def main():
parser = argparse.ArgumentParser(
prog="build.py", description="Extism Python PDK builder"
)
parser.add_argument(
"command",
choices=["build", "install", "clean"],
default="build",
nargs="?",
help="Command to run",
)
parser.add_argument(
"--bin-dir",
default=bin_dir(),
dest="bin_dir",
help="Directory to install binaries",
)
parser.add_argument(
"--data-dir",
default=data_dir(),
dest="data_dir",
help="Directory to install data files",
)
parser.add_argument(
"--log-level",
default="INFO",
choices=["debug", "info", "warning", "error", "critical"],
help="Set the logging level",
)
parser.add_argument("--quiet", "-q", action="store_true", help="Suppress output")
args = parser.parse_args()
if not args.quiet:
set_log_level(args.log_level)
try:
if args.command == "build":
do_build(args)
elif args.command == "install":
do_install(args)
elif args.command == "clean":
do_clean(args)
if args.command in ["install"]:
version = get_version(Path(args.bin_dir))
logging.info(f"extism-py version: {version}")
except Exception as e:
logging.error(f"An error occurred: {e}")
sys.exit(1)
if __name__ == "__main__":
main()