11#! /usr/bin/env bash
22# name=tppr
3- # version=0.1 .0
3+ # version=0.2 .0
44# description=Tiny Portable Python Runner for hassle-free venv and dep handling
55# url=https://github.com/Jelmerro/tppr
66# author=Jelmer van Arnhem
77# license=MIT
88
99# parse args
1010show_help=0
11- arg_as_cmd=0
1211show_msg=0
12+ systempython=" python"
13+ arg_as_cmd=0
1314show_install=0
1415force_reinstall=0
1516show_version=0
@@ -20,6 +21,9 @@ while [ -n "$arg" ];do
2021 break
2122 elif [[ " $arg " == " -m" || " $arg " == " --show-msg" ]]; then
2223 show_msg=1
24+ elif [[ " $arg " == " -p" || " $arg " == " --python" ]]; then
25+ shift
26+ systempython=$1
2327 elif [[ " $arg " == " -c" || " $arg " == " --cmd" ]]; then
2428 arg_as_cmd=1
2529 elif [[ " $arg " == " -i" || " $arg " == " --show-install" ]]; then
@@ -28,7 +32,6 @@ while [ -n "$arg" ];do
2832 force_reinstall=1
2933 elif [[ " $arg " == " -v" || " $arg " == " --version" ]]; then
3034 show_version=1
31- break
3235 elif [[ " $arg " == " -" * ]]; then
3336 if [[ " $arg " == " --" * ]]; then
3437 echo " x Invalid arg '$arg ', see -h or --help for info"
@@ -40,17 +43,16 @@ while [ -n "$arg" ];do
4043 elif [[ " $char " == " h" ]]; then
4144 show_help=1
4245 break 2
43- elif [[ " $char " == " c" ]]; then
44- arg_as_cmd=1
4546 elif [[ " $char " == " m" ]]; then
4647 show_msg=1
48+ elif [[ " $char " == " c" ]]; then
49+ arg_as_cmd=1
4750 elif [[ " $char " == " i" ]]; then
4851 show_install=1
4952 elif [[ " $char " == " r" ]]; then
5053 force_reinstall=1
5154 elif [[ " $char " == " v" ]]; then
5255 show_version=1
53- break 2
5456 else
5557 echo " x Invalid short arg '$char ', see -h or --help for info"
5658 exit 1
@@ -64,6 +66,21 @@ while [ -n "$arg" ];do
6466done
6567shift
6668
69+ # get version from python path, command or system python, otherwise exit
70+ if ! command -v " $systempython " > /dev/null 2>&1 ; then
71+ systempython=$( realpath -q " $systempython " )
72+ if [ ! -f " $systempython " ]; then
73+ echo " x Invalid python path '$systempython ', file not found"
74+ exit 1
75+ fi
76+ fi
77+ full_version=$( " $systempython " -c " import platform;print(platform.python_version())" 2> /dev/null)
78+ if [ -z " $full_version " ] || [ " $full_version " == " " ]; then
79+ echo " x Invalid python path '$systempython ', not a valid python binary"
80+ exit 1
81+ fi
82+ version=${full_version% .* }
83+
6784# echo helper to only show messages if enabled
6885msg () {
6986 if [ " $show_msg " == 1 ]; then
@@ -73,7 +90,8 @@ msg() {
7390
7491# early exit paths
7592if [ " $show_version " == 1 ]; then
76- echo " This is tppr version 0.1.0"
93+ echo " This is tppr version 0.2.0"
94+ echo " Python version currently used is $full_version "
7795 echo " The Tiny Portable Python Runner for hassle-free venv and dep handling"
7896 echo " Please see https://github.com/Jelmerro/tppr for updates and info"
7997 exit
@@ -88,17 +106,18 @@ if [ "$show_help" == 1 ];then
88106 echo " ... Any arguments meant for the python script go here"
89107 echo
90108 echo " options:"
91- echo " -h, --help Show this help message and exit"
92- echo " -c, --cmd File arg is passed to Python's -c as cmd instead"
93- echo " -m, --show-msg Print tppr info messages while preparing to run"
94- echo " -i, --show-install Print pip requirements installation output"
95- echo " -r, --reinstall Reinstall regardless of a correctly versioned venv"
96- echo " -v, --version Show program version info and exit"
109+ echo " -h, --help Show this help message and exit"
110+ echo " -c, --cmd File arg is passed to Python's -c as cmd instead"
111+ echo " -m, --show-msg Print tppr info messages while preparing to run"
112+ echo " -i, --show-install Print pip requirements installation output"
113+ echo " -r, --reinstall Reinstall regardless of a correctly versioned venv"
114+ echo " -p <p>, --python <p> Set the python binary location to <p>"
115+ echo " -v, --version Show program version info and exit"
97116 exit
98117fi
99118if [ -z " $arg " ] || [ " $arg " == " " ]; then
100119 msg " > no file provided, starting interactive system python"
101- python
120+ " $systempython "
102121 exit
103122fi
104123file=$( realpath " $arg " )
@@ -116,17 +135,15 @@ if [[ "$dir" == "/" ]];then
116135 msg " > project dir not found, running with system python"
117136 if [ " $arg_as_cmd " == 1 ]; then
118137 msg " > pass argument as cmd instead of filename"
119- python -c " $arg " " $@ "
138+ " $systempython " -c " $arg " " $@ "
120139 else
121- python " $file " " $@ "
140+ " $systempython " " $file " " $@ "
122141 fi
123142 exit
124143fi
125144msg " > project dir detected as $dir "
126145
127- # find latest python version and latest executable paths
128- full_version=$( python -c " import platform;print(platform.python_version())" )
129- version=${full_version% .* }
146+ # minimal required executable paths inside the venv
130147python=" $dir /venv/bin/python$version "
131148pip=" $dir /venv/bin/pip$version "
132149activate=" $dir /venv/bin/activate"
145162# create new venv and install deps, then source it
146163if [ ! -f " $python " ] || [ ! -f " $pip " ] || [ ! -f " $activate " ]; then
147164 msg " > creating venv"
148- " python $version " -m venv " $dir /venv"
165+ " $systempython " -m venv " $dir /venv"
149166 source " $activate "
150167 stdout=" /dev/stdout"
151168 stderr=" /dev/stderr"
0 commit comments