Skip to content

Commit 47980db

Browse files
committed
Choose an appropriate python version for x.py at runtime
- Default to python3 -> python -> python2 - Start as shell script and re-execute the program with the right python interpreter
1 parent 18d27b2 commit 47980db

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

x.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
#!/usr/bin/env python
1+
# The beginning of this script is both valid shell and valid python, such that
2+
# the script starts with the shell and is reexecuted with the right python.
3+
# This works because shells only execute a line at a time.
4+
# Thanks to `./mach` from servo for the idea!
5+
''':' && {
6+
exists() { command -v "$1" >/dev/null 2>&1; }
7+
if exists python3; then
8+
exec python3 "$0" "$@"
9+
elif exists python; then
10+
exec python "$0" "$@"
11+
else
12+
exec python2 "$0" "$@"
13+
fi
14+
}
15+
'''
216

317
# This file is only a "symlink" to bootstrap.py, all logic should go there.
418

0 commit comments

Comments
 (0)