From 47980db72ba4409439632c8f916eb75ab10ffe78 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Fri, 1 Jan 2021 11:52:31 -0500 Subject: [PATCH] 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 --- x.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/x.py b/x.py index 7973730ef177c..60cc589cd6388 100755 --- a/x.py +++ b/x.py @@ -1,4 +1,18 @@ -#!/usr/bin/env python +# The beginning of this script is both valid shell and valid python, such that +# the script starts with the shell and is reexecuted with the right python. +# This works because shells only execute a line at a time. +# Thanks to `./mach` from servo for the idea! +''':' && { +exists() { command -v "$1" >/dev/null 2>&1; } +if exists python3; then + exec python3 "$0" "$@" +elif exists python; then + exec python "$0" "$@" +else + exec python2 "$0" "$@" +fi +} +''' # This file is only a "symlink" to bootstrap.py, all logic should go there.