~ knotless Search/
↑↓ move↵ open⇧↵ all results

geesawra.industries/hydraulophone

An unofficial YouTube Music app for iPhone, iPad, and Mac.

0
0
main
62 lines · 1.8 KB Raw History
#!/usr/bin/env python3
"""Read the unique Apple Development Team ID from local certificate metadata."""

from __future__ import annotations

import re
import subprocess
import sys


def fail(message: str) -> None:
    print(message, file=sys.stderr)
    raise SystemExit(1)


def main() -> None:
    try:
        certificates = subprocess.run(
            ["security", "find-certificate", "-a", "-c", "Apple Development", "-p"],
            capture_output=True,
            text=True,
            check=False,
        )
    except OSError as error:
        fail(f"Could not inspect local Apple Development certificates: {error}")

    pem_certificates = re.findall(
        r"-----BEGIN CERTIFICATE-----.*?-----END CERTIFICATE-----",
        certificates.stdout,
        re.DOTALL,
    )
    team_ids: set[str] = set()
    for certificate in pem_certificates:
        try:
            subject = subprocess.run(
                ["openssl", "x509", "-noout", "-subject", "-nameopt", "RFC2253"],
                input=certificate,
                capture_output=True,
                text=True,
                check=False,
            )
        except OSError as error:
            fail(f"Could not read certificate subject metadata: {error}")
        if subject.returncode == 0:
            team_ids.update(re.findall(r"OU=([A-Z0-9]{10})", subject.stdout))

    if len(team_ids) == 1:
        print(next(iter(team_ids)))
        return
    if not team_ids:
        fail(
            "No Apple Development Team ID was found in local certificates. "
            "Set HYDRAULOPHONE_DEVELOPMENT_TEAM explicitly."
        )
    fail(
        "Multiple Apple Development Team IDs were found. Set "
        "HYDRAULOPHONE_DEVELOPMENT_TEAM to choose one."
    )


if __name__ == "__main__":
    main()