|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import sysconfig |
| 6 | + |
| 7 | + |
| 8 | +class TyNotFound(FileNotFoundError): ... |
| 9 | + |
| 10 | + |
| 11 | +def find_ty_bin() -> str: |
| 12 | + """Return the ty binary path.""" |
| 13 | + |
| 14 | + ty_exe = "ty" + sysconfig.get_config_var("EXE") |
| 15 | + |
| 16 | + targets = [ |
| 17 | + # The scripts directory for the current Python |
| 18 | + sysconfig.get_path("scripts"), |
| 19 | + # The scripts directory for the base prefix |
| 20 | + sysconfig.get_path("scripts", vars={"base": sys.base_prefix}), |
| 21 | + # Above the package root, e.g., from `pip install --prefix` or `uv run --with` |
| 22 | + ( |
| 23 | + # On Windows, with module path `<prefix>/Lib/site-packages/ty` |
| 24 | + _join(_matching_parents(_module_path(), "Lib/site-packages/ty"), "Scripts") |
| 25 | + if sys.platform == "win32" |
| 26 | + # On Unix, with module path `<prefix>/lib/python3.13/site-packages/ty` |
| 27 | + else _join( |
| 28 | + _matching_parents(_module_path(), "lib/python*/site-packages/ty"), |
| 29 | + "bin", |
| 30 | + ) |
| 31 | + ), |
| 32 | + # Adjacent to the package root, e.g., from `pip install --target` |
| 33 | + # with module path `<target>/ty` |
| 34 | + _join(_matching_parents(_module_path(), "ty"), "bin"), |
| 35 | + # The user scheme scripts directory, e.g., `~/.local/bin` |
| 36 | + sysconfig.get_path("scripts", scheme=_user_scheme()), |
| 37 | + ] |
| 38 | + |
| 39 | + seen = [] |
| 40 | + for target in targets: |
| 41 | + if not target: |
| 42 | + continue |
| 43 | + if target in seen: |
| 44 | + continue |
| 45 | + seen.append(target) |
| 46 | + path = os.path.join(target, ty_exe) |
| 47 | + if os.path.isfile(path): |
| 48 | + return path |
| 49 | + |
| 50 | + locations = "\n".join(f" - {target}" for target in seen) |
| 51 | + raise TyNotFound( |
| 52 | + f"Could not find the ty binary in any of the following locations:\n{locations}\n" |
| 53 | + ) |
| 54 | + |
| 55 | + |
| 56 | +def _module_path() -> str | None: |
| 57 | + path = os.path.dirname(__file__) |
| 58 | + return path |
| 59 | + |
| 60 | + |
| 61 | +def _matching_parents(path: str | None, match: str) -> str | None: |
| 62 | + """ |
| 63 | + Return the parent directory of `path` after trimming a `match` from the end. |
| 64 | + The match is expected to contain `/` as a path separator, while the `path` |
| 65 | + is expected to use the platform's path separator (e.g., `os.sep`). The path |
| 66 | + components are compared case-insensitively and a `*` wildcard can be used |
| 67 | + in the `match`. |
| 68 | + """ |
| 69 | + from fnmatch import fnmatch |
| 70 | + |
| 71 | + if not path: |
| 72 | + return None |
| 73 | + parts = path.split(os.sep) |
| 74 | + match_parts = match.split("/") |
| 75 | + if len(parts) < len(match_parts): |
| 76 | + return None |
| 77 | + |
| 78 | + if not all( |
| 79 | + fnmatch(part, match_part) |
| 80 | + for part, match_part in zip(reversed(parts), reversed(match_parts)) |
| 81 | + ): |
| 82 | + return None |
| 83 | + |
| 84 | + return os.sep.join(parts[: -len(match_parts)]) |
| 85 | + |
| 86 | + |
| 87 | +def _join(path: str | None, *parts: str) -> str | None: |
| 88 | + if not path: |
| 89 | + return None |
| 90 | + return os.path.join(path, *parts) |
| 91 | + |
| 92 | + |
| 93 | +def _user_scheme() -> str: |
| 94 | + if sys.version_info >= (3, 10): |
| 95 | + user_scheme = sysconfig.get_preferred_scheme("user") |
| 96 | + elif os.name == "nt": |
| 97 | + user_scheme = "nt_user" |
| 98 | + elif sys.platform == "darwin" and sys._framework: # ty: ignore[unresolved-attribute] |
| 99 | + user_scheme = "osx_framework_user" |
| 100 | + else: |
| 101 | + user_scheme = "posix_user" |
| 102 | + return user_scheme |
0 commit comments