Files
dxdesigner-part-replacement/tools/build_self_extracting_release.py
阿白叔 1340f6c233
Core regression / Python core and release configuration (push) Successful in 18s
Core regression / JavaScript syntax (push) Successful in 3s
build: stage immutable release files without duplicate copies
2026-11-04 15:47:21 +08:00

131 lines
5.2 KiB
Python

"""Create the ZIP and 7-Zip self-extracting artifacts for a release."""
from __future__ import annotations
import argparse
import hashlib
import json
import os
import re
from pathlib import Path
import shutil
import subprocess
import zipfile
from self_extracting_release import build_sfx
def _sha256(path: Path) -> str:
digest = hashlib.sha256()
with path.open("rb") as stream:
for chunk in iter(lambda: stream.read(1024 * 1024), b""):
digest.update(chunk)
return digest.hexdigest()
def _zip_tree(source: Path, output: Path) -> int:
files = 0
with zipfile.ZipFile(output, "x", compression=zipfile.ZIP_DEFLATED, compresslevel=6, allowZip64=True) as archive:
for path in sorted(source.rglob("*")):
if path.is_file():
archive.write(path, path.relative_to(source.parent).as_posix())
files += 1
return files
def _stage_file(source, destination):
# Release input is immutable during packaging. Same-volume links avoid a
# second copy of tens of thousands of runtime files; archives are independent.
try:
os.link(source, destination)
except OSError:
shutil.copyfile(source, destination)
return destination
def build_release(source_dir: Path, output_dir: Path, *, version: str, seven_zip: Path, sfx_stub: Path,
license_path: Path | None = None) -> dict:
source_dir = Path(source_dir).resolve(strict=True)
output_dir = Path(output_dir).resolve()
if not source_dir.is_dir():
raise ValueError("source_dir must be a directory")
if output_dir.is_relative_to(source_dir) or source_dir.is_relative_to(output_dir):
raise ValueError('Output must not be inside or contain the source directory')
if (source_dir / 'data').exists():
raise ValueError('Release input must not include user data')
if not re.fullmatch(r'[0-9]+\.[0-9]+\.[0-9]+(?:-[A-Za-z0-9.]+)?', version):
raise ValueError('Invalid release version')
seven_zip = Path(seven_zip).resolve(strict=True)
sfx_stub = Path(sfx_stub).resolve(strict=True)
output_dir.mkdir(parents=True, exist_ok=False)
staging = output_dir / "staging" / "DxPartReplace"
shutil.copytree(source_dir, staging, copy_function=_stage_file)
if license_path is not None:
license_target = staging / "licenses" / "7-Zip-LICENSE.txt"
license_target.parent.mkdir(parents=True, exist_ok=True)
# Break a possible staging hardlink before replacing a pre-existing notice.
license_target.unlink(missing_ok=True)
shutil.copyfile(Path(license_path).resolve(strict=True), license_target)
archive_7z = output_dir / f"DxPartReplace-{version}.7z"
subprocess.run([
str(seven_zip), "a", "-t7z", "-mx=3", "-m0=LZMA2", "-mmt=4", "-ms=64m",
str(archive_7z), "DxPartReplace"
], cwd=staging.parent, check=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True,
timeout=3600)
zip_path = output_dir / f"DxPartReplace-{version}.zip"
file_count = _zip_tree(staging, zip_path)
sfx_path = output_dir / f"DxPartReplace-{version}.exe"
build_sfx(sfx_stub, archive_7z, sfx_path)
archive_7z.unlink()
manifest = {
"version": version,
"file_count": file_count,
"artifacts": {
zip_path.name: {"bytes": zip_path.stat().st_size, "sha256": _sha256(zip_path)},
sfx_path.name: {"bytes": sfx_path.stat().st_size, "sha256": _sha256(sfx_path)},
},
"extractor": {
"kind": "7zip-sfx",
"stub": sfx_stub.name,
"stub_sha256": _sha256(sfx_stub),
"compressor": seven_zip.name,
"compressor_sha256": _sha256(seven_zip),
"version_output": subprocess.check_output([str(seven_zip)], text=True, errors='replace')[:500],
"settings": "LZMA2 level 3, 4 threads, 64 MiB solid blocks",
},
"files": {path.relative_to(staging.parent).as_posix(): _sha256(path)
for path in sorted(staging.rglob('*')) if path.is_file()},
}
(output_dir / f"release-manifest-{version}.json").write_text(
json.dumps(manifest, ensure_ascii=False, indent=2) + "\n", encoding="utf-8"
)
(output_dir / f"SHA256SUMS-{version}.txt").write_text(
"".join(f"{item['sha256']} {name}\n" for name, item in manifest["artifacts"].items()),
encoding="ascii",
)
return manifest
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("--source-dir", type=Path, required=True)
parser.add_argument("--output-dir", type=Path, required=True)
parser.add_argument("--version", required=True)
parser.add_argument("--seven-zip", type=Path, required=True)
parser.add_argument("--sfx-stub", type=Path, required=True)
parser.add_argument("--license", type=Path)
args = parser.parse_args()
manifest = build_release(args.source_dir, args.output_dir, version=args.version,
seven_zip=args.seven_zip, sfx_stub=args.sfx_stub,
license_path=args.license)
print(json.dumps(manifest, ensure_ascii=False, indent=2))
return 0
if __name__ == "__main__":
raise SystemExit(main())