Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/build/darwin/patch-dmg.py
5235 views
1
#!/usr/bin/env python3
2
import subprocess
3
import shutil
4
import tempfile
5
import plistlib
6
import os
7
8
def patch_dmg_icon(dmg_path, new_icon_path):
9
"""Replace the volume icon in an existing DMG."""
10
11
# 1. Convert to read-write format
12
temp_rw = tempfile.NamedTemporaryFile(suffix=".dmg", delete=False)
13
temp_rw.close()
14
15
subprocess.run([
16
"hdiutil", "convert", dmg_path,
17
"-format", "UDRW", # Read-write
18
"-o", temp_rw.name,
19
"-ov" # Overwrite
20
], check=True)
21
22
# 2. Attach the writable DMG
23
result = subprocess.run(
24
["hdiutil", "attach", "-nobrowse", "-plist", temp_rw.name],
25
capture_output=True, check=True
26
)
27
plist = plistlib.loads(result.stdout)
28
29
mount_point = None
30
device = None
31
for entity in plist["system-entities"]:
32
if "mount-point" in entity:
33
mount_point = entity["mount-point"]
34
device = entity["dev-entry"]
35
break
36
37
try:
38
# 3. Copy custom icon
39
icon_target = os.path.join(mount_point, ".VolumeIcon.icns")
40
shutil.copyfile(new_icon_path, icon_target)
41
42
# 4. Set the custom icon attribute on the volume
43
subprocess.run(["/usr/bin/SetFile", "-a", "C", mount_point], check=True)
44
45
# Sync before detach
46
subprocess.run(["sync", "--file-system", mount_point], check=True)
47
48
finally:
49
# 5. Detach
50
subprocess.run(["hdiutil", "detach", device], check=True)
51
52
# 6. Convert back to compressed format (ULMO = lzma)
53
subprocess.run([
54
"hdiutil", "convert", temp_rw.name,
55
"-format", "ULMO",
56
"-o", dmg_path,
57
"-ov"
58
], check=True)
59
60
# Cleanup temp file
61
os.unlink(temp_rw.name)
62
print(f"Successfully patched {dmg_path} with new icon")
63
64
65
if __name__ == "__main__":
66
import sys
67
if len(sys.argv) != 3:
68
print(f"Usage: {sys.argv[0]} <dmg_path> <icon.icns>")
69
sys.exit(1)
70
71
patch_dmg_icon(sys.argv[1], sys.argv[2])
72
73