| 1 | #!/usr/bin/env python3 |
| 2 | """Install computer-use support in the existing managed Mac guest and bridge.""" |
| 3 | from importlib import import_module |
| 4 | import json |
| 5 | import os |
| 6 | from pathlib import Path |
| 7 | import tempfile |
| 8 | |
| 9 | vm = import_module("mac-agent-vm") |
| 10 | root = Path(__file__).resolve().parent |
| 11 | identity = vm.tart("exec", vm.NAME, "/bin/hostname", capture_output=True, text=True, timeout=10).stdout.strip() |
| 12 | print("Installing guest tools in " + identity, flush=True) |
| 13 | source = {name: (root / name).read_text() for name in ["mac-computer.py", "mac-agent-gui.swift"]} |
| 14 | vm.tart("exec", "-i", vm.NAME, "/Library/Developer/CommandLineTools/usr/bin/python3", "-c", ''' |
| 15 | import json, pathlib, subprocess, sys, tempfile |
| 16 | files = json.load(sys.stdin) |
| 17 | with tempfile.TemporaryDirectory(prefix="snowglobe-computer-install-") as temporary: |
| 18 | root = pathlib.Path(temporary) |
| 19 | for name, content in files.items(): |
| 20 | (root / name).write_text(content) |
| 21 | subprocess.run(["/usr/bin/swiftc", "-O", str(root / "mac-agent-gui.swift"), "-o", str(root / "agent-gui")], check=True) |
| 22 | subprocess.run(["sudo", "-n", "mkdir", "-p", "/usr/local/libexec"], check=True) |
| 23 | for source, destination in [("mac-computer.py", "/usr/local/libexec/snowglobe-computer.py"), ("agent-gui", "/usr/local/bin/agent-gui")]: |
| 24 | subprocess.run(["sudo", "-n", "install", "-m", "755", str(root / source), destination], check=True) |
| 25 | ''', input=json.dumps(source).encode(), timeout=180) |
| 26 | for name in ["mac-agent-vm.py", "mac-vm-bridge.py"]: |
| 27 | target = vm.ROOT / "runtime/control" / name |
| 28 | with tempfile.NamedTemporaryFile(dir=target.parent, delete=False) as outgoing: |
| 29 | pending = Path(outgoing.name) |
| 30 | outgoing.write((root / name).read_bytes()) |
| 31 | try: |
| 32 | pending.chmod(target.stat().st_mode & 0o777) |
| 33 | os.replace(pending, target) |
| 34 | finally: |
| 35 | pending.unlink(missing_ok=True) |
| 36 | result = vm.tart("exec", "-i", vm.NAME, "/Library/Developer/CommandLineTools/usr/bin/python3", "/usr/local/libexec/snowglobe-computer.py", |
| 37 | input=b'{"action":"status"}', capture_output=True, timeout=15) |
| 38 | value = json.loads(result.stdout) |
| 39 | if not all(value.get("permissions", {}).get(name) for name in ["accessibility", "screenCapture", "postEvent"]): |
| 40 | raise SystemExit("The guest needs computer-use permissions; check its System Settings.") |
| 41 | print(json.dumps(value)) |