Branchwell

Jobley / app · jobley.ai / installer / package.py

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