-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython
More file actions
executable file
·85 lines (68 loc) · 2.37 KB
/
python
File metadata and controls
executable file
·85 lines (68 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env bash
# -----------------------------------------------------------------------------
# uv python shim -- v1.1.0
# https://github.com/newbery/uv-python-shims
#
# - Mimics the pyenv python shim behavior.
# - Uses `uv python find` for discovery (including .python-version handling).
# - If invoked as python3.X, enforces major.minor = 3.X by re-finding with 3.X.
# - If a suitable python cannot be found, this will fail with an error message.
# -----------------------------------------------------------------------------
set -euo pipefail
FLAGS=(--resolve-links)
# Uncomment to ignore virtual environments
# Note that an 'activated' virtual environment will likely shadow this shim
# so this flag is only useful for ignoring non-activated virtual environments.
# FLAGS+=(--system)
# Uncomment to ignore '.python-version' files
# FLAGS+=(--no-config)
# Uncomment to avoid discovering a project or workspace
# FLAGS+=(--no-project)
# Uncomment to force uv-managed pythons only
# FLAGS+=(--managed-python)
# Pop shim dir out of PATH to avoid recursion
shim_dir="$(cd "$(dirname "$0")" && pwd)"
path=""
IFS=':' read -r -a parts <<< "${PATH:-}"
for p in "${parts[@]}"; do
[[ -z "$p" ]] && continue
[[ "$p" == "$shim_dir" ]] && continue
path="${path:+$path:}$p"
done
uv_find() {
PATH="$path" uv python find "${FLAGS[@]}" "$@"
}
uv_find_version() {
PATH="$path" uv python find --show-version "${FLAGS[@]}"
}
major_minor() {
local v="$1"
IFS='.' read -r a b _rest <<< "$v"
printf '%s.%s\n' "$a" "$b"
}
py=""
# Try to extract major.minor override from shim name or environment variable:
# python3.11 -> 3.11
# python3.11.2 -> 3.11
shim_prefix=""
invoked_as="${UV_PYTHON_SHIM_INVOKED_AS:-$(basename "$0")}"
if [[ "$invoked_as" =~ ^python3\.([0-9]+)(\.([0-9]+))?$ ]]; then
shim_prefix="3.${BASH_REMATCH[1]}"
fi
# If major.minor override is given, test if found version matches
if [[ -n "$shim_prefix" ]]; then
found_prefix=""
found_version="$(uv_find_version 2>/dev/null || true)"
if [[ -n "$found_version" ]]; then
found_prefix="$(major_minor "$found_version")"
fi
# If not a match, let's defer to the result using the shim prefix
if [[ "$found_prefix" != "$shim_prefix" ]]; then
py="$(uv_find "$shim_prefix")"
fi
fi
# Otherwise, the default `uv python find` result is good enough.
if [[ -z "$py" ]]; then
py="$(uv_find)"
fi
exec env PATH="$path" "$py" "$@"