46 lines
1.5 KiB
Python
46 lines
1.5 KiB
Python
"""Build a 7-Zip SFX wrapper around a prepared .7z release archive."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
import os
|
|
import shutil
|
|
|
|
|
|
def build_sfx(
|
|
stub: Path,
|
|
archive: Path,
|
|
output: Path,
|
|
) -> Path:
|
|
"""Use the official 7z.sfx extractor (not the temporary installer stub).
|
|
|
|
Its native dialog selects a persistent destination. No installer config,
|
|
registry modifications, temporary extraction or automatic launch is needed.
|
|
"""
|
|
stub = Path(stub)
|
|
archive = Path(archive)
|
|
output = Path(output)
|
|
if not stub.is_file() or not archive.is_file():
|
|
raise FileNotFoundError("SFX stub and archive must both exist")
|
|
if output.exists():
|
|
raise FileExistsError(output)
|
|
with stub.open('rb') as stream:
|
|
if stream.read(2) != b'MZ':
|
|
raise ValueError('Expected Windows SFX executable')
|
|
with archive.open('rb') as stream:
|
|
if stream.read(6) != b'7z\xbc\xaf\x27\x1c':
|
|
raise ValueError('Expected 7z archive')
|
|
output.parent.mkdir(parents=True, exist_ok=True)
|
|
temporary = output.with_name(f".{output.name}.{os.getpid()}.tmp")
|
|
try:
|
|
with temporary.open("xb") as target:
|
|
for source in (stub, archive):
|
|
with source.open('rb') as stream:
|
|
shutil.copyfileobj(stream, target, 1024 * 1024)
|
|
target.flush()
|
|
os.fsync(target.fileno())
|
|
os.replace(temporary, output)
|
|
finally:
|
|
temporary.unlink(missing_ok=True)
|
|
return output
|