Return an absolute path to a runnable ``cmdop-core`` binary. Raises :class:`FileNotFoundError` if neither the override nor a baked binary is present, or if the host platform/arch has no prebuilt binary.
()
| 67 | |
| 68 | |
| 69 | def locate_binary() -> str: |
| 70 | """Return an absolute path to a runnable ``cmdop-core`` binary. |
| 71 | |
| 72 | Raises :class:`FileNotFoundError` if neither the override nor a baked binary |
| 73 | is present, or if the host platform/arch has no prebuilt binary. |
| 74 | """ |
| 75 | override = os.environ.get("CMDOP_CORE_BINARY") |
| 76 | if override: |
| 77 | if not os.path.exists(override): |
| 78 | raise FileNotFoundError( |
| 79 | f"CMDOP_CORE_BINARY points at a missing file: {override}" |
| 80 | ) |
| 81 | _make_executable(override) |
| 82 | return override |
| 83 | |
| 84 | slug = _host_slug() |
| 85 | if slug is None: |
| 86 | raise FileNotFoundError( |
| 87 | f"cmdop-core has no prebuilt binary for {sys.platform}/" |
| 88 | f"{platform.machine().lower()}. Set CMDOP_CORE_BINARY to a " |
| 89 | "`go build -o /path/cmdop-core ./cmd/cmdop-core` output." |
| 90 | ) |
| 91 | |
| 92 | name = _binary_name() |
| 93 | try: |
| 94 | resource = files("cmdop._bin").joinpath(name) |
| 95 | with as_file(resource) as p: |
| 96 | path = str(p) |
| 97 | if not os.path.exists(path): |
| 98 | raise FileNotFoundError(path) |
| 99 | except (FileNotFoundError, ModuleNotFoundError, OSError) as exc: |
| 100 | raise FileNotFoundError( |
| 101 | f"cmdop-core binary not found ({name}). The 5 baked binaries ship " |
| 102 | "in the wheel under cmdop/_bin/; reinstall cmdop, or set " |
| 103 | "CMDOP_CORE_BINARY to a `go build -o /path/cmdop-core " |
| 104 | "./cmd/cmdop-core` output." |
| 105 | ) from exc |
| 106 | |
| 107 | _make_executable(path) |
| 108 | return path |