Graphley / app / installer / package.py
| 1 | """Package the signed Graphley app in the shared Finder installer layout. |
| 2 | |
| 3 | python installer/package.py <Graphley.app> <output.dmg> |
| 4 | |
| 5 | The window background is the artwork inside the signed app (Contents/Resources/installer-background.tiff), |
| 6 | the same way Jotley and Inkspring do it, so the disk image shows only Graphley and Applications. |
| 7 | Needs dmgbuild, ds-store and mac-alias (installer/requirements.txt). Signing and notarizing |
| 8 | the disk image are done by scripts/release-mac.sh. |
| 9 | """ |
| 10 | |
| 11 | import argparse |
| 12 | import os |
| 13 | import subprocess |
| 14 | import tempfile |
| 15 | from pathlib import Path |
| 16 | |
| 17 | from dmgbuild import build_dmg |
| 18 | from ds_store import DSStore |
| 19 | from mac_alias import Alias |
| 20 | |
| 21 | ARTWORK = Path("Contents/Resources/installer-background.tiff") |
| 22 | WINDOW = ((160, 160), (720, 520)) |
| 23 | ICON_SIZE = 104 |
| 24 | LOCATIONS = {"Graphley.app": (195, 230), "Applications": (525, 230)} |
| 25 | |
| 26 | |
| 27 | def package(app: Path, output: Path) -> None: |
| 28 | mount: Path |
| 29 | |
| 30 | def remember_mount(mount_point: str, _options: dict) -> None: |
| 31 | nonlocal mount |
| 32 | mount = Path(mount_point) |
| 33 | |
| 34 | def write_background_alias(event: dict) -> None: |
| 35 | if event.get("type") != "operation::finished" or event.get("operation") != "dsstore::create": |
| 36 | return |
| 37 | with DSStore.open(str(mount / ".DS_Store"), "r+") as store: |
| 38 | icons = store["."]["icvp"] |
| 39 | icons["backgroundType"] = 2 |
| 40 | icons["backgroundImageAlias"] = Alias.for_file(str(mount / app.name / ARTWORK)).to_bytes() |
| 41 | store["."]["icvp"] = icons |
| 42 | |
| 43 | build_dmg(str(output), app.stem, settings={ |
| 44 | "format": "UDZO", |
| 45 | "filesystem": "HFS+", |
| 46 | "files": [str(app)], |
| 47 | "symlinks": {"Applications": "/Applications"}, |
| 48 | "window_rect": WINDOW, |
| 49 | "default_view": "icon-view", |
| 50 | "show_toolbar": False, |
| 51 | "show_sidebar": False, |
| 52 | "show_status_bar": False, |
| 53 | "show_pathbar": False, |
| 54 | "show_tab_view": False, |
| 55 | "arrange_by": None, |
| 56 | "scroll_position": (0, 0), |
| 57 | "icon_size": ICON_SIZE, |
| 58 | "text_size": 15, |
| 59 | "label_pos": "bottom", |
| 60 | "icon_locations": LOCATIONS, |
| 61 | "create_hook": remember_mount, |
| 62 | }, callback=write_background_alias) |
| 63 | |
| 64 | |
| 65 | def verify(image: Path) -> None: |
| 66 | """Check the layout in the finished image: two items, their positions, and the artwork reference.""" |
| 67 | # Finder metadata reads reliably only from a local mount point, even when builds run on an external drive. |
| 68 | with tempfile.TemporaryDirectory(prefix="graphley-dmg-check-", dir="/private/tmp") as temporary: |
| 69 | mount = Path(temporary) / "volume" |
| 70 | mount.mkdir() |
| 71 | subprocess.run(["hdiutil", "attach", "-readonly", "-nobrowse", "-noautoopen", "-noverify", |
| 72 | "-mountpoint", str(mount), str(image)], check=True, stdout=subprocess.DEVNULL) |
| 73 | try: |
| 74 | names = {path.name for path in mount.iterdir()} |
| 75 | assert names - {".DS_Store", ".fseventsd", ".Trashes"} == set(LOCATIONS), names |
| 76 | assert os.readlink(mount / "Applications") == "/Applications" |
| 77 | with DSStore.open(str(mount / ".DS_Store"), "r") as store: |
| 78 | window, icons = store["."]["bwsp"], store["."]["icvp"] |
| 79 | locations = {name: tuple(store[name]["Iloc"])[:2] for name in LOCATIONS} |
| 80 | assert window["WindowBounds"] == "{{160, 160}, {720, 520}}", window["WindowBounds"] |
| 81 | assert icons["iconSize"] == ICON_SIZE and icons["backgroundType"] == 2, icons |
| 82 | assert locations == LOCATIONS, locations |
| 83 | assert Alias.from_bytes(icons["backgroundImageAlias"]).target.filename == ARTWORK.name |
| 84 | assert (mount / "Graphley.app" / ARTWORK).is_file() |
| 85 | finally: |
| 86 | subprocess.run(["hdiutil", "detach", str(mount)], check=True, stdout=subprocess.DEVNULL) |
| 87 | |
| 88 | |
| 89 | if __name__ == "__main__": |
| 90 | parser = argparse.ArgumentParser(description=__doc__) |
| 91 | parser.add_argument("app", type=Path) |
| 92 | parser.add_argument("output", type=Path) |
| 93 | arguments = parser.parse_args() |
| 94 | if arguments.app.name != "Graphley.app" or not (arguments.app / ARTWORK).is_file(): |
| 95 | parser.error("Use a Graphley.app that contains the installer artwork (a Mac build of the current project).") |
| 96 | if arguments.output.exists(): |
| 97 | parser.error("The output path already exists.") |
| 98 | package(arguments.app, arguments.output) |
| 99 | verify(arguments.output) |
| 100 | print("Installer layout verified.") |