Repository navigation
Expand file tree
/
Copy pathsr-cli
More file actions
executable file
·105 lines (92 loc) · 3.03 KB
/
Copy pathsr-cli
File metadata and controls
executable file
·105 lines (92 loc) · 3.03 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env sh
# =============================================================================
# sr — launcher
#
# Finds a suitable Python 3 (>= 3.10) and runs the sr package. Works in three
# layouts, in this order:
#
# 1. zipapp this file *is* sr.pyz; python runs it directly
# 2. source a sibling `sr/` package (development checkout)
# 3. install the package is importable from site-packages
#
# Set SR_PYTHON to pin the interpreter, or SR_PYTHON_MIN to require a newer
# minimum version. POSIX sh only: no bash-isms, so it also runs on macOS.
# =============================================================================
set -eu
SELF=${0%/*}
if [ "$SELF" = "$0" ]; then
SELF=.
fi
SELF=$(cd "$SELF" 2>/dev/null && pwd -P)/$(basename "$0")
HERE=$(dirname "$SELF")
SR_PYTHON_MIN=${SR_PYTHON_MIN:-3.10}
_suitable() {
# $1 = interpreter candidate; prints the path when it is new enough.
command -v "$1" >/dev/null 2>&1 || return 1
"$1" -c '
import sys
minimum = tuple(int(part) for part in sys.argv[1].split("."))
sys.exit(0 if sys.version_info[:2] >= minimum else 1)
' "$SR_PYTHON_MIN" >/dev/null 2>&1 && printf '%s\n' "$1"
}
find_python() {
if [ -n "${SR_PYTHON:-}" ]; then
found=$(_suitable "$SR_PYTHON" || true)
if [ -n "$found" ]; then
command -v "$found"
return 0
fi
echo "sr: SR_PYTHON=$SR_PYTHON is not a Python >= $SR_PYTHON_MIN interpreter" >&2
return 1
fi
for candidate in python3 python3.14 python3.13 python3.12 python3.11 python3.10 python; do
found=$(_suitable "$candidate" || true)
if [ -n "$found" ]; then
command -v "$found"
return 0
fi
done
return 1
}
if ! PYTHON=$(find_python); then
cat >&2 <<EOF
sr: no Python >= $SR_PYTHON_MIN interpreter found.
sr is a single-file Python application with no third-party dependencies.
Install Python 3.10 or newer, then re-run, or point sr at an interpreter:
SR_PYTHON=/opt/python3.11/bin/python3 sr --version
See https://github.com/paulmann/sr-search-replace#installation
EOF
exit 127
fi
# 1) zipapp: this very file is executable Python.
case "$SELF" in
*.pyz) exec "$PYTHON" "$SELF" "$@" ;;
esac
# 2) source checkout: a sibling `sr/` package with __main__.py.
if [ -f "$HERE/sr/__main__.py" ]; then
PYTHONPATH="$HERE${PYTHONPATH:+:$PYTHONPATH}"
export PYTHONPATH
exec "$PYTHON" -m sr "$@"
fi
if [ -f "$HERE/../sr/__main__.py" ]; then
ROOT=$(cd "$HERE/.." && pwd -P)
PYTHONPATH="$ROOT${PYTHONPATH:+:$PYTHONPATH}"
export PYTHONPATH
exec "$PYTHON" -m sr "$@"
fi
# 3) installed package (pip / pipx / system).
if "$PYTHON" -c 'import sr' >/dev/null 2>&1; then
exec "$PYTHON" -m sr "$@"
fi
cat >&2 <<EOF
sr: cannot find the sr package.
Looked for:
1. a zipapp at $SELF
2. a source tree at $HERE/sr/
3. an installed 'sr' module for $PYTHON
Reinstall sr:
curl -fsSL https://raw.githubusercontent.com/paulmann/sr-search-replace/main/scripts/install.sh | bash
# or
pipx install sr-search-replace
EOF
exit 127