Jotley / app · jotley.ai / scripts / check-foundation-models-linkage.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Verify model linkage and the exported iOS summary background configuration.""" |
| 3 | import argparse |
| 4 | from pathlib import Path |
| 5 | import plistlib |
| 6 | import subprocess |
| 7 | import tempfile |
| 8 | import zipfile |
| 9 | |
| 10 | |
| 11 | def check(binary): |
| 12 | symbols = subprocess.check_output(["xcrun", "nm", "-u", str(binary)], text=True) |
| 13 | missing_api = [line.strip() for line in symbols.splitlines() |
| 14 | if "FoundationModels19SystemLanguageModelC7VariantV" in line |
| 15 | or "FoundationModels19SystemLanguageModelC7variant" in line] |
| 16 | if missing_api: |
| 17 | raise SystemExit("Incompatible Foundation Models variant imports: " + "; ".join(missing_api)) |
| 18 | print("Verified: no incompatible Foundation Models model-variant imports.") |
| 19 | |
| 20 | |
| 21 | def main(): |
| 22 | parser = argparse.ArgumentParser(description=__doc__) |
| 23 | parser.add_argument("artifact", type=Path, help="App executable or exported IPA") |
| 24 | artifact = parser.parse_args().artifact |
| 25 | if artifact.suffix.lower() != ".ipa": |
| 26 | check(artifact) |
| 27 | return |
| 28 | with zipfile.ZipFile(artifact) as archive, tempfile.TemporaryDirectory() as directory: |
| 29 | infos = [name for name in archive.namelist() |
| 30 | if name.startswith("Payload/") and name.endswith(".app/Info.plist") and name.count("/") == 2] |
| 31 | if len(infos) != 1: |
| 32 | raise SystemExit("Expected one main app in the IPA.") |
| 33 | info = plistlib.loads(archive.read(infos[0])) |
| 34 | identifiers = info.get("BGTaskSchedulerPermittedIdentifiers", []) |
| 35 | if any(".summary." in item for item in identifiers): |
| 36 | expected = info["CFBundleIdentifier"] + ".summary.*" |
| 37 | if expected not in identifiers or "processing" not in info.get("UIBackgroundModes", []): |
| 38 | raise SystemExit("Exported summary background processing registration or mode is missing.") |
| 39 | print("Verified: exported summary background identifier and processing mode.") |
| 40 | executable = info["CFBundleExecutable"] |
| 41 | if Path(executable).name != executable: |
| 42 | raise SystemExit("Invalid app executable name.") |
| 43 | binary = Path(directory) / executable |
| 44 | binary.write_bytes(archive.read(infos[0].rsplit("/", 1)[0] + "/" + executable)) |
| 45 | check(binary) |
| 46 | |
| 47 | |
| 48 | if __name__ == "__main__": |
| 49 | main() |