-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhelper.py
More file actions
257 lines (212 loc) · 7.9 KB
/
helper.py
File metadata and controls
257 lines (212 loc) · 7.9 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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
from __future__ import annotations
import os
import re
import shlex
import shutil
import stat
import subprocess
import tomllib
from pathlib import Path
from typing import Any
import colorama
import structlog
colorama.init(autoreset=True)
def get_logger() -> structlog.BoundLogger:
return structlog.get_logger(__name__)
def configure_logging() -> None:
import logging
logging.basicConfig(format="%(message)s", level=logging.INFO)
structlog.configure(
processors=[
structlog.stdlib.add_log_level,
structlog.processors.TimeStamper(fmt="iso"),
structlog.processors.StackInfoRenderer(),
structlog.processors.format_exc_info,
structlog.dev.ConsoleRenderer(colors=True),
],
wrapper_class=structlog.make_filtering_bound_logger(logging.INFO),
logger_factory=structlog.stdlib.LoggerFactory(),
cache_logger_on_first_use=True,
)
class BuildError(Exception):
def __init__(self, message: str, details: dict[str, Any] | None = None) -> None:
super().__init__(message)
self.details = details or {}
VAR_PATTERN = re.compile(r"%([^%]+)%")
def load_user_config(path: Path) -> dict[str, str]:
if not path.exists():
raise BuildError("user_config.cmd must exist", {"path": str(path)})
values: dict[str, str] = {}
for raw_line in path.read_text(encoding="utf-8", errors="ignore").splitlines():
line = raw_line.strip()
if (
not line
or line.lower().startswith("@rem")
or line.lower().startswith("rem")
):
continue
if line.lower().startswith("@set "):
line = line[5:]
elif line.lower().startswith("set "):
line = line[4:]
if "=" not in line:
continue
name, value = line.split("=", 1)
values[name.strip()] = value.strip()
return values
def load_config(path: Path) -> dict[str, str]:
if not path.exists():
raise BuildError("config.toml must exist", {"path": str(path)})
raw = tomllib.loads(path.read_text(encoding="utf-8", errors="ignore"))
if not isinstance(raw, dict):
raise BuildError("config file must contain a table", {"path": str(path)})
values: dict[str, str] = {}
def flatten(item: Any) -> None:
if isinstance(item, dict):
for key, value in item.items():
if isinstance(value, dict):
flatten(value)
else:
values[key] = str(value)
else:
raise BuildError("config file contains invalid entry", {"path": str(path)})
flatten(raw)
return values
def expand_value(value: str, env: dict[str, str]) -> str:
if not value:
return value
for _ in range(10):
candidate = VAR_PATTERN.sub(
lambda match: env.get(match.group(1), match.group(0)), value
)
if candidate == value:
break
value = candidate
return value
def run(
args: list[str],
cwd: Path | None = None,
env: dict[str, str] | None = None,
check: bool = True,
capture_output: bool = False,
stdout=None,
stderr=None,
shell: bool = False,
) -> subprocess.CompletedProcess[str]:
command_str = (
args
if isinstance(args, str)
else " ".join(shlex.quote(str(arg)) for arg in args)
)
print()
print(
colorama.Fore.YELLOW + ">>> executing command: ",
colorama.Style.BRIGHT + colorama.Fore.CYAN + command_str,
colorama.Fore.MAGENTA + f" (cwd: {cwd})",
)
result = subprocess.run(
args,
cwd=str(cwd) if cwd else None,
env=env,
check=False,
capture_output=capture_output,
stdout=stdout,
stderr=stderr,
text=True,
shell=shell,
)
if result.returncode != 0:
print(colorama.Fore.RED + f"<<< FAIL: code = {result.returncode}")
print()
else:
print(colorama.Fore.GREEN + f"<<< OK")
print()
if check and result.returncode != 0:
raise BuildError(
"process failed",
{
"args": args,
"returncode": result.returncode,
"stdout": result.stdout,
"stderr": result.stderr,
},
)
return result
def read_version_file(path: Path) -> int:
if not path.exists():
raise BuildError("version file not found", {"path": str(path)})
text = path.read_text(encoding="utf-8", errors="ignore").strip()
if not text:
raise BuildError("version file is empty", {"path": str(path)})
try:
return int(text.splitlines()[0].strip())
except ValueError as exc:
raise BuildError(
"version file contains invalid integer", {"path": str(path), "text": text}
) from exc
def write_version_file(path: Path, version: int) -> None:
path.write_text(f"{version}\n", encoding="utf-8")
def ensure_directory(path: Path) -> None:
path.mkdir(parents=True, exist_ok=True)
def copy_tree(source: Path, destination: Path) -> None:
if not source.exists():
raise BuildError("source tree missing", {"path": str(source)})
if not source.is_dir():
raise BuildError("source tree must be a directory", {"path": str(source)})
for root, dirs, files in os.walk(source):
rel_root = Path(root).relative_to(source)
for folder in dirs:
(destination / rel_root / folder).mkdir(parents=True, exist_ok=True)
for filename in files:
src = Path(root) / filename
dst = destination / rel_root / filename
dst.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(src, dst)
# get_logger().debug("copy.tree.file", source=str(src), destination=str(dst))
def set_readonly(path: Path, readonly: bool) -> None:
if not path.exists():
return
current_mode = path.stat().st_mode
if readonly:
current_mode &= ~stat.S_IWUSR
current_mode |= stat.S_IREAD
else:
current_mode |= stat.S_IWUSR | stat.S_IREAD
path.chmod(current_mode)
def create_junction(target: Path, link_name: Path) -> None:
if link_name.exists() or link_name.is_symlink():
if link_name.is_dir() and not link_name.is_symlink():
link_name.rmdir()
else:
link_name.unlink(missing_ok=True)
run(["cmd", "/c", "mklink", "/J", str(link_name), str(target)], shell=False)
def remove_path(path: Path) -> None:
if not path.exists() and not path.is_symlink():
return
if path.is_dir() and not path.is_symlink():
shutil.rmtree(path, ignore_errors=True)
else:
path.unlink(missing_ok=True)
def copy_file(source: Path, destination: Path, optional: bool = False) -> None:
if not source.exists():
if optional:
get_logger().debug("copy.optional.missing", source=str(source))
return
raise BuildError("required file missing", {"path": str(source)})
destination.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(source, destination)
level = "optional" if optional else "required"
get_logger().info(f"copy.{level}", source=str(source), destination=str(destination))
def flash_console() -> None:
try:
import ctypes
hwnd = ctypes.windll.kernel32.GetConsoleWindow()
if hwnd:
ctypes.windll.user32.FlashWindow(hwnd, True)
except Exception as exc:
pass
def open_shell(path: Path) -> None:
shell = shutil.which("bash.exe") or shutil.which("cmd.exe")
if not shell:
raise BuildError("no shell available to open", {})
run([shell], cwd=path, env=os.environ.copy(), check=False)