66 lines
2.6 KiB
Python
66 lines
2.6 KiB
Python
"""Combine the native OpenDeck bundles for one OS into an installable ZIP."""
|
|
|
|
import argparse
|
|
import json
|
|
import stat
|
|
from pathlib import Path
|
|
from tempfile import TemporaryDirectory
|
|
from zipfile import ZIP_DEFLATED, ZipFile, ZipInfo
|
|
|
|
|
|
BUNDLE = "org.furumi.opendeck.sdPlugin/"
|
|
TARGET_SUFFIX = {
|
|
"windows": "-windows-msvc",
|
|
"macos": "-apple-darwin",
|
|
"linux": "-linux-gnu",
|
|
}
|
|
|
|
|
|
def assemble(platform: str, inputs: Path, output: Path) -> None:
|
|
manifest = json.loads((Path(__file__).parent / "assets/manifest.json").read_bytes())
|
|
programs = {}
|
|
resources = None
|
|
for target, executable in manifest["CodePaths"].items():
|
|
if not target.endswith(TARGET_SUFFIX[platform]):
|
|
continue
|
|
source = inputs / f"furumi-opendeck-{target}.zip"
|
|
with ZipFile(source) as archive:
|
|
files = {
|
|
entry.filename: archive.read(entry)
|
|
for entry in archive.infolist()
|
|
if not entry.is_dir()
|
|
}
|
|
if json.loads(files[BUNDLE + "manifest.json"]) != manifest:
|
|
raise ValueError(f"{source} was built with a different plugin manifest")
|
|
program = BUNDLE + executable
|
|
programs[program] = files.pop(program)
|
|
if not programs[program]:
|
|
raise ValueError(f"Empty executable in {source}: {executable}")
|
|
if resources is None:
|
|
resources = files
|
|
elif files != resources:
|
|
raise ValueError(f"Shared plugin resources differ in {source}")
|
|
|
|
if resources is None:
|
|
raise ValueError(f"No {platform} executables in the plugin manifest")
|
|
output.parent.mkdir(parents=True, exist_ok=True)
|
|
with TemporaryDirectory(dir=output.parent) as staging:
|
|
staged = Path(staging) / output.name
|
|
with ZipFile(staged, "w") as archive:
|
|
for name, data in sorted((resources | programs).items()):
|
|
entry = ZipInfo(name)
|
|
entry.create_system = 3 # Record Unix permissions regardless of the build host.
|
|
mode = 0o755 if name in programs else 0o644
|
|
entry.external_attr = (stat.S_IFREG | mode) << 16
|
|
archive.writestr(entry, data, compress_type=ZIP_DEFLATED)
|
|
staged.replace(output)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("platform", choices=TARGET_SUFFIX)
|
|
parser.add_argument("inputs", type=Path, help="Directory containing native plugin ZIPs")
|
|
parser.add_argument("output", type=Path, help="Path of the resulting platform ZIP")
|
|
args = parser.parse_args()
|
|
assemble(args.platform, args.inputs, args.output)
|