-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprepare_release.py
More file actions
executable file
·400 lines (321 loc) · 11.9 KB
/
prepare_release.py
File metadata and controls
executable file
·400 lines (321 loc) · 11.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#!/usr/bin/env python3
# ruff: noqa: T201
import json
import re
import shutil
import subprocess
import tomllib
from collections.abc import Iterator
from functools import cache
from io import BytesIO
from os import path
from pathlib import Path
from zipfile import ZIP_DEFLATED, ZipFile
from pick_release_name import pick_release_name
THIS_FOLDER = Path(__file__).parent
# The various locations we extract to within the zip file
ZIP_MODS_FOLDER = Path("sdk_mods")
ZIP_STUBS_FOLDER = ZIP_MODS_FOLDER / ".stubs"
ZIP_SETTINGS_FOLDER = ZIP_MODS_FOLDER / "settings"
ZIP_EXECUTABLE_FOLDER = Path("Binaries")
ZIP_PLUGINS_FOLDER = ZIP_EXECUTABLE_FOLDER / "Plugins"
# The base CMake directories - these need the preset added after
BUILD_DIR_BASE = THIS_FOLDER / "out" / "build"
INSTALL_DIR_BASE = THIS_FOLDER / "out" / "install"
# Within the install folder, the folder we use for files that actually go into the exe's folder
INSTALL_EXECUTABLE_FOLDER_NAME = ".exe_folder"
# All non-gitignored folders which include any file matching one of these file suffixes becomes its
# own nested zip/.sdkmod.
# The list of suffixes is also useful to make sure we don't match dotfiles from submodules.
MODS_FOLDER = THIS_FOLDER / "src"
VALID_MOD_FILE_SUFFIXES = {".py", ".pyi", ".pyd", ".md"}
# And there are a few extra files which we want which aren't matched by the above
INIT_SCRIPT = MODS_FOLDER / "__main__.py"
SETTINGS_GITIGNORE = MODS_FOLDER / "settings" / ".gitignore"
STUBS_DIR = THIS_FOLDER / "libs" / "pyunrealsdk" / "stubs"
STUBS_LICENSE = THIS_FOLDER / "libs" / "pyunrealsdk" / "LICENSE"
# The default license we use for any folders which don't already have one (due to being a submodule)
LICENSE = THIS_FOLDER / "LICENSE"
# Only used to extract the version number
MANAGER_PYPROJECT = THIS_FOLDER / "manager_pyproject.toml"
# Regex to extract presets from a `cmake --list-presets` command
LIST_PRESETS_RE = re.compile(' "(.+)"')
@cache
def cmake_get_presets() -> list[str]:
"""
Gets the presets which may be used.
Returns:
A list of presets.
"""
proc = subprocess.run(
["cmake", "--list-presets"],
check=True,
stdout=subprocess.PIPE,
encoding="utf8",
)
return LIST_PRESETS_RE.findall(proc.stdout)
def cmake_configure(preset: str, extra_args: list[str]) -> None:
"""
Configures the given CMake preset.
Args:
preset: The preset to configure.
extra_args: Extra CMake args to append to the configure command.
"""
subprocess.check_call(["cmake", ".", "--preset", preset, *extra_args])
def cmake_install(build_dir: Path) -> None:
"""
Builds and installs a cmake configuration.
Args:
build_dir: The preset's build dir.
"""
subprocess.check_call(["cmake", "--build", build_dir, "--target", "install"])
@cache
def get_git_commit_hash(identifier: str | None = None) -> str:
"""
Gets the full commit hash of the current git repo.
Args:
identifier: The identifier of the commit to get, or None to get the latest.
Returns:
The commit hash.
"""
args = ["git", "show", "-s", "--format=%H"]
if identifier is not None:
args.append(identifier)
return subprocess.run(
args,
cwd=Path(__file__).parent,
check=True,
stdout=subprocess.PIPE,
encoding="utf8",
).stdout.strip()
@cache
def check_git_is_dirty() -> bool:
"""
Checks if the git repo is dirty.
Returns:
True if the repo is dirty.
"""
# This command returns the list of modified files, so any output means dirty
return any(
subprocess.run(
["git", "status", "--porcelain"],
cwd=Path(__file__).parent,
check=True,
stdout=subprocess.PIPE,
).stdout,
)
@cache
def get_git_repo_version() -> str:
"""
Gets a version string representing the current state of the git repo.
Returns:
The version string.
"""
return get_git_commit_hash()[:8] + (", dirty" if check_git_is_dirty() else "")
def iter_non_gitignored_mod_folders() -> Iterator[Path]:
"""
Iterates through all folders which are *not* gitignored within the mods folder.
Includes untracked folders.
Yields:
Paths for each matched folder.
"""
for folder in MODS_FOLDER.iterdir():
if not folder.is_dir():
continue
# Check if this folder has any valid contents
stdout = (
subprocess.run(
["git", "ls-files", folder],
check=True,
stdout=subprocess.PIPE,
encoding="utf8",
).stdout
+ subprocess.run(
["git", "ls-files", "-o", "--exclude-standard", folder],
check=True,
stdout=subprocess.PIPE,
encoding="utf8",
).stdout
)
if not stdout:
continue
yield folder
def zip_dlls(zip_file: ZipFile, install_dir: Path) -> None:
"""
Adds all the dlls/related files to the zip.
Args:
zip_file: The zip file to add the dlls to.
install_dir: The CMake install dir with the built files.
"""
exe_folder = install_dir / INSTALL_EXECUTABLE_FOLDER_NAME
for file in install_dir.glob("**/*"):
if not file.is_file():
continue
dest: Path
if file.is_relative_to(exe_folder):
dest = ZIP_EXECUTABLE_FOLDER / file.relative_to(exe_folder)
else:
dest = ZIP_PLUGINS_FOLDER / file.relative_to(install_dir)
zip_file.write(file, dest)
# Also add a '._pth' file. This is equivalent to the default settings, so for more people this
# is redundant. However, if someone has a global 'PYTHONPATH'/'PYTHONHOME' env var, having this
# file means we ignore them.
py_stem = next(install_dir.glob("python*.zip")).stem
zip_file.writestr(
str(ZIP_PLUGINS_FOLDER / (py_stem + "._pth")),
(
f"{py_stem}.zip\n" # dummy comment to force multiline
"DLLs\n"
),
)
def zip_config_file(zip_file: ZipFile) -> None:
"""
Adds the config file to the zip.
Args:
zip_file: The zip file to add the config file to.
"""
# Path.relative_to doesn't work when where's no common base, need to use os.path
# These paths are relative to the plugins folder
init_script_path = path.relpath(ZIP_MODS_FOLDER / INIT_SCRIPT.name, ZIP_PLUGINS_FOLDER)
pyexec_root = path.relpath(ZIP_MODS_FOLDER, ZIP_PLUGINS_FOLDER)
version_number = tomllib.loads(MANAGER_PYPROJECT.read_text())["project"]["version"]
git_version = get_git_repo_version()
display_version = f"{version_number} ({git_version})"
release_name = pick_release_name(get_git_commit_hash())
# Tomllib doesn't support dumping yet, so we have to create it as a string
# Using `json.dumps` to escape strings, since it should be mostly compatible
config = (
f"[unrealsdk]\n"
f"locking_function_calls = true\n"
f"\n"
f"[pyunrealsdk]\n"
f"init_script = {json.dumps(init_script_path)}\n"
f"pyexec_root = {json.dumps(pyexec_root)}\n"
f"\n"
f"[mod_manager]\n"
f"display_version = {json.dumps(display_version)}\n"
f"\n"
f"[willow1_mod_menu]\n"
f"display_version = {json.dumps(release_name)}\n"
)
zip_file.writestr(str(ZIP_PLUGINS_FOLDER / "unrealsdk.toml"), config)
def iter_mod_files(mod_folder: Path, debug: bool) -> Iterator[Path]:
"""
Iterates through all files in the given mod folder which are valid to export.
Args:
mod_folder: Path to the mod folder to iterate through.
debug: True if creating a debug zip.
Yields:
Valid files to export.
"""
for file in mod_folder.glob("**/*"):
if not file.is_file():
continue
if file.parent.name == "__pycache__":
continue
if file.suffix not in VALID_MOD_FILE_SUFFIXES:
continue
if file.suffix == ".pyd" and file.stem.endswith("_d") != debug:
continue
yield file
def zip_dot_sdkmod(zip_file: ZipFile, mod: Path, mod_files: list[Path]) -> None:
"""
Adds a .sdkmod to the zip.
Args:
zip_file: The zip file to add the mod to.
mod: The mod folder.
mod_files: The list of valid files to zip up.
"""
buffer = BytesIO()
with ZipFile(buffer, "w", ZIP_DEFLATED, compresslevel=9) as sdkmod_zip:
for file in mod_files:
sdkmod_zip.write(
file,
mod.name / file.relative_to(mod),
)
# Add the license
license_file = (
existing_license if (existing_license := mod / "LICENSE").exists() else LICENSE
)
sdkmod_zip.write(license_file, Path(mod.name) / LICENSE.name)
buffer.seek(0)
zip_file.writestr(
str(ZIP_MODS_FOLDER / (mod.name + ".sdkmod")),
buffer.read(),
)
def zip_mod_folder(zip_file: ZipFile, mod: Path, mod_files: list[Path]) -> None:
"""
Adds a mod folder to the zip.
Args:
zip_file: The zip file to add the mod to.
mod: The mod folder.
mod_files: The list of valid files to zip up.
"""
# We have to add it as a raw folder
for file in mod_files:
zip_file.write(
file,
ZIP_MODS_FOLDER / mod.name / file.relative_to(mod),
)
# Add the license
license_file = existing_license if (existing_license := mod / "LICENSE").exists() else LICENSE
zip_file.write(license_file, ZIP_MODS_FOLDER / mod.name / LICENSE.name)
if __name__ == "__main__":
from argparse import ArgumentParser
parser = ArgumentParser(description="Prepares a release zip.")
parser.add_argument(
"preset",
choices=cmake_get_presets(),
help="The CMake preset to use.",
)
parser.add_argument(
"--configure",
action="store_true",
help="Configure CMake before building.",
)
parser.add_argument(
"-C",
"--configure-extra-args",
action="append",
default=[],
metavar="...",
help="Extra args to append to the CMake configure call.",
)
parser.add_argument(
"--skip-install",
action="store_true",
help="If specified, skips performing a CMake install. The directory must already exist.",
)
args = parser.parse_args()
if check_git_is_dirty():
print("WARNING: git repo is dirty")
install_dir = INSTALL_DIR_BASE / str(args.preset)
if args.configure:
cmake_configure(args.preset, args.configure_extra_args)
else:
assert (BUILD_DIR_BASE / args.preset).exists(), "configure dir doesn't exist"
if not args.skip_install:
shutil.rmtree(install_dir, ignore_errors=True)
cmake_install(BUILD_DIR_BASE / args.preset)
assert install_dir.exists() and install_dir.is_dir(), "install dir doesn't exist"
zip_name = f"willow1-sdk-{args.preset}.zip"
print(f"Zipping {zip_name} ...")
with ZipFile(zip_name, "w", ZIP_DEFLATED, compresslevel=9) as zip_file:
zip_dlls(zip_file, install_dir)
zip_config_file(zip_file)
for folder in iter_non_gitignored_mod_folders():
mod_files = list(iter_mod_files(folder, "debug" in args.preset))
if not any(mod_files):
continue
if any(file.suffix == ".pyd" for file in mod_files):
zip_mod_folder(zip_file, folder, mod_files)
else:
zip_dot_sdkmod(zip_file, folder, mod_files)
zip_file.write(INIT_SCRIPT, ZIP_MODS_FOLDER / INIT_SCRIPT.name)
zip_file.write(SETTINGS_GITIGNORE, ZIP_SETTINGS_FOLDER / SETTINGS_GITIGNORE.name)
for file in STUBS_DIR.glob("**/*.pyi"):
zip_file.write(
file,
ZIP_STUBS_FOLDER / file.relative_to(STUBS_DIR),
)
zip_file.write(STUBS_LICENSE, ZIP_STUBS_FOLDER / STUBS_LICENSE.name)