-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautd3_build_utils.py
221 lines (181 loc) · 6.54 KB
/
autd3_build_utils.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
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
import contextlib
import os
import platform
import re
import shutil
import subprocess
import sys
import tarfile
import urllib.request
from collections.abc import Generator
from pathlib import Path
from typing import Self
def err(msg: str) -> None:
print("\033[91mERR \033[0m: " + msg)
def warn(msg: str) -> None:
print("\033[93mWARN\033[0m: " + msg)
def info(msg: str) -> None:
print("\033[92mINFO\033[0m: " + msg)
@contextlib.contextmanager
def working_dir(path: Path) -> Generator:
cwd = Path.cwd()
os.chdir(path)
try:
yield
finally:
os.chdir(cwd)
@contextlib.contextmanager
def with_env(**kwargs: str) -> Generator:
env = os.environ.copy()
for key, value in kwargs.items():
os.environ[key] = value
try:
yield
finally:
os.environ.clear()
os.environ.update(env)
def run_command(command: list[str], *, shell: bool = False) -> None:
try:
subprocess.run(command, check=False, shell=shell).check_returncode()
except subprocess.CalledProcessError:
err(f"command failed: {' '.join(command)}")
sys.exit(-1)
def fetch_submodule(*, recursive: bool = False) -> None:
if shutil.which("git") is not None:
command = ["git", "submodule", "update", "--init"]
if recursive:
command.append("--recursive")
run_command(command)
else:
err("git is not installed. Skip fetching submodules.")
def _remove(path: Path) -> None:
with contextlib.suppress(PermissionError):
if path.is_file():
path.unlink(missing_ok=True)
elif path.is_dir():
for f in path.iterdir():
_remove(f)
path.rmdir()
def rremove(pattern: str, *, path: Path | str | None = None, exclude: str | None = None) -> None:
path = path or Path.cwd()
path = Path(path)
if not path.exists():
return
if path.is_file():
path.unlink(missing_ok=True)
elif path.is_dir():
paths = set(path.rglob(pattern))
if exclude is not None:
paths -= set(path.rglob(exclude))
for f in paths:
_remove(f)
def remove(path: Path | str) -> None:
path = Path(path)
rremove("*", path=path)
if path.is_dir():
path.rmdir()
def substitute_in_file(
src_file: Path | str,
mapping: list[tuple[str, str]],
*,
target_file: Path | str | None = None,
flags: re.RegexFlag = re.NOFLAG,
) -> None:
src_file = Path(src_file)
target_file = Path(target_file) if target_file is not None else Path(src_file)
content = src_file.read_text(encoding="utf-8")
for key, value in mapping:
content = re.sub(key, value, content, flags=flags)
target_file.write_text(content, encoding="utf-8")
class BaseConfig:
_platform: str
arch: str
release: bool
def __init__(self: Self, args) -> None: # noqa: ANN001
self._platform = platform.system()
if not self.is_windows() and not self.is_macos() and not self.is_linux():
err(f'platform "{platform.system()}" is not supported.')
sys.exit(-1)
self.release = getattr(args, "release", False) or False
arch: str = getattr(args, "arch", None)
machine = platform.machine().lower()
if arch is not None and arch:
machine = arch.lower()
if machine in ["amd64", "x86_64", "x64"]:
self.arch = "x64"
elif machine in ["arm64", "aarch64"]:
self.arch = "aarch64"
elif machine in ["arm32", "armv7l"]:
self.arch = "armv7l"
else:
err(f"Unsupported platform: {machine}")
def is_windows(self: Self) -> bool:
return self._platform == "Windows"
def is_macos(self: Self) -> bool:
return self._platform == "Darwin"
def is_linux(self: Self) -> bool:
return self._platform == "Linux"
def exe_ext(self: Self) -> str:
return ".exe" if self.is_windows() else ""
def is_pcap_available(self: Self) -> bool:
if not self.is_windows():
return True
wpcap_exists = Path("C:\\Windows\\System32\\wpcap.dll").is_file() and Path("C:\\Windows\\System32\\Npcap\\wpcap.dll").is_file()
packet_exists = Path("C:\\Windows\\System32\\Packet.dll").is_file() and Path("C:\\Windows\\System32\\Npcap\\Packet.dll").is_file()
return wpcap_exists and packet_exists
def download_and_extract( # noqa: C901, PLR0912
self: Self,
repo: str,
name: str,
version: str,
dest_dirs: list[str],
*,
ty: str = "shared",
) -> None:
url: str
base_url = f"https://github.com/shinolab/{repo}/releases/download/v{version}/{name}-v{version}"
if self.is_windows():
match self.arch:
case "x64":
url = f"{base_url}-win-x64-{ty}.zip"
case "aarch64":
url = f"{base_url}-win-aarch64-{ty}.zip"
case _:
err(f"Unsupported platform: {platform.machine()}")
elif self.is_macos():
url = f"{base_url}-macos-aarch64-{ty}.tar.gz"
elif self.is_linux():
match self.arch:
case "x64":
url = f"{base_url}-linux-x64-{ty}.tar.gz"
case "aarch64":
url = f"{base_url}-linux-armv7-{ty}.tar.gz"
case "armv7l":
url = f"{base_url}-linux-aarch64-{ty}.tar.gz"
tmp_file = Path("tmp.zip" if url.endswith(".zip") else "tmp.tar.gz")
urllib.request.urlretrieve(url, tmp_file)
if tmp_file.suffix == ".zip":
shutil.unpack_archive(tmp_file, ".")
else:
with tarfile.open(tmp_file, "r:gz") as tar:
tar.extractall(filter="fully_trusted")
tmp_file.unlink()
for dest_dir in dest_dirs:
Path(dest_dir).mkdir(parents=True, exist_ok=True)
for dll in Path("bin").glob("*.dll"):
for dest_dir in dest_dirs:
shutil.copy(dll, dest_dir)
for dylib in Path("bin").glob("*.dylib"):
for dest_dir in dest_dirs:
shutil.copy(dylib, dest_dir)
for so in Path("bin").glob("*.so"):
for dest_dir in dest_dirs:
shutil.copy(so, dest_dir)
for lib in Path("lib").glob("*.lib"):
for dest_dir in dest_dirs:
shutil.copy(lib, dest_dir)
for a in Path("lib").glob("*.a"):
for dest_dir in dest_dirs:
shutil.copy(a, dest_dir)
remove("bin")
remove("lib")