SPB Git

Repository Public

provision.py 4.8 KB 61 lines python

1#!/usr/bin/env python32# Author: Simon-Pierre Boucher — contact@spboucher.ai3# File: Scripts/provision.py4# Description: Registers SP Drive's bundle identifiers (app + File Provider extension, release and development variants), the Associated Domains capability of the app, and their Developer ID provisioning profiles — with the EXISTING App Store Connect API key and Developer ID certificate.5#6# Idempotent: existing resources are reused; no certificate is ever created. Nothing secret is printed.7import base64, hashlib, os, subprocess, sys8sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))9import asc1011OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "Build", "profiles")12# identifier, display name, profile name, file, capabilities13TARGETS = [14    ("ai.spboucher.spdrive", "SP Drive", "SP Drive Developer ID", "SPDrive.provisionprofile", ["ASSOCIATED_DOMAINS"]),15    ("ai.spboucher.spdrive.fileprovider", "SP Drive File Provider", "SP Drive File Provider Developer ID", "SPDriveFileProvider.provisionprofile", []),16    ("ai.spboucher.spdrive.dev", "SP Drive Dev", "SP Drive Dev Developer ID", "SPDriveDev.provisionprofile", []),17    ("ai.spboucher.spdrive.dev.fileprovider", "SP Drive Dev File Provider", "SP Drive Dev File Provider Developer ID", "SPDriveDevFileProvider.provisionprofile", []),18]1920out = subprocess.run(["security", "find-certificate", "-c", "Developer ID Application", "-Z"], capture_output=True, text=True).stdout21local = [l.split(":")[1].strip() for l in out.splitlines() if l.startswith("SHA-1")]22if not local: sys.exit("no Developer ID Application certificate in the keychain")23s, d = asc.call("GET", "/v1/certificates?filter[certificateType]=DEVELOPER_ID_APPLICATION,DEVELOPER_ID_APPLICATION_G2&limit=100")24cert = next((c["id"] for c in d.get("data", []) if hashlib.sha1(base64.b64decode(c["attributes"]["certificateContent"])).hexdigest().upper() in local), None)25if not cert: sys.exit("the keychain's Developer ID certificate is not visible through the API")26s, allp = asc.call("GET", "/v1/profiles?filter[profileType]=MAC_APP_DIRECT&limit=200")27os.makedirs(OUT, exist_ok=True)2829for BID, DISPLAY, NAME, FILE, CAPS in TARGETS:30    s, d = asc.call("GET", f"/v1/bundleIds?filter[identifier]={BID}&limit=20")31    ids = [b for b in d.get("data", []) if b["attributes"]["identifier"] == BID]32    if ids: bid = ids[0]["id"]; print("bundle id exists:", BID)33    else:34        s, d = asc.call("POST", "/v1/bundleIds", {"data": {"type": "bundleIds", "attributes": {"identifier": BID, "name": DISPLAY, "platform": "MAC_OS"}}})35        if s >= 300: sys.exit(f"cannot create bundle id {BID}: HTTP {s}")36        bid = d["data"]["id"]; print("bundle id created:", BID)37    s, d = asc.call("GET", f"/v1/bundleIds/{bid}/bundleIdCapabilities")38    have = [c["attributes"]["capabilityType"] for c in d.get("data", [])]39    changed = False40    for cap in CAPS:41        if cap in have: continue42        s, d = asc.call("POST", "/v1/bundleIdCapabilities", {"data": {"type": "bundleIdCapabilities", "attributes": {"capabilityType": cap}, "relationships": {"bundleId": {"data": {"type": "bundleIds", "id": bid}}}}})43        if s >= 300: sys.exit(f"cannot enable {cap} on {BID}: HTTP {s}")44        print("capability enabled:", cap); changed = True45    if changed: s, allp = asc.call("GET", "/v1/profiles?filter[profileType]=MAC_APP_DIRECT&limit=200")46    mine = [p for p in allp.get("data", []) if p["attributes"]["name"] == NAME]47    active = [p for p in mine if p["attributes"]["profileState"] == "ACTIVE"]48    if active: p = active[0]; print("profile exists:", NAME)49    else:50        # a capability change on the App ID invalidates its profiles: the stale one is replaced51        for stale in mine: asc.call("DELETE", f"/v1/profiles/{stale['id']}"); print("stale profile removed:", stale["attributes"]["profileState"])52        s, d = asc.call("POST", "/v1/profiles", {"data": {"type": "profiles", "attributes": {"name": NAME, "profileType": "MAC_APP_DIRECT"}, "relationships": {"bundleId": {"data": {"type": "bundleIds", "id": bid}}, "certificates": {"data": [{"type": "certificates", "id": cert}]}}}})53        if s >= 300: sys.exit(f"cannot create the profile {NAME}: HTTP {s}")54        p = d["data"]; print("profile created:", NAME)55    path = os.path.join(OUT, FILE)56    open(path, "wb").write(base64.b64decode(p["attributes"]["profileContent"]))57    print("  written:", os.path.relpath(path), "| uuid", p["attributes"]["uuid"], "| expires", p["attributes"]["expirationDate"][:10])58    for dest in ("~/Library/Developer/Xcode/UserData/Provisioning Profiles", "~/Library/MobileDevice/Provisioning Profiles"):59        dd = os.path.expanduser(dest); os.makedirs(dd, exist_ok=True)60        open(os.path.join(dd, p["attributes"]["uuid"] + ".provisionprofile"), "wb").write(base64.b64decode(p["attributes"]["profileContent"]))61
Files main
Menu

Theme