Last active
December 19, 2018 16:46
-
-
Save Erotemic/4e02fd93f85a6a3dd872ae9a01cf9b6b to your computer and use it in GitHub Desktop.
The setup_venv3 bash command sets up a python3 virtual environment on a fresh ubuntu install. The file contains quite a few helper functions that make the actual implementation of setup_venv3 rather short.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
__heredoc__(){ NOOP=; } | |
has_pymodule(){ | |
__heredoc__ ''' | |
Check if a python module is installed. Echos "True" or "False" to the | |
command line depending on the result. | |
Example: | |
source $HOME/local/init/utils.sh | |
has_pymodule sys | |
has_pymodule fake_module | |
''' | |
if [ "$2" ]; then | |
PYEXE="$1" | |
PYMOD="$2" | |
else | |
PYEXE=python | |
PYMOD="$1" | |
fi | |
pyblock "$PYEXE" " | |
try: | |
import $PYMOD | |
print(True) | |
except ImportError: | |
print(False) | |
" | |
} | |
pyblock(){ | |
__heredoc__ ''' | |
Executes python code and handles nice indentation. Need to be slightly | |
careful about the type of quotes used. Typically stick to doublequotes | |
around the code and singlequotes inside python code. Sometimes it will be | |
necessary to escape some characters. | |
Usage: | |
pyblock [PYEXE] TEXT | |
Args: | |
PYEXE : positional arg to specify the python executable. | |
if not specified, it defaults to "python" | |
TEXT : arbitrary python code to execute | |
Notes: | |
Capture results from stdout either using using | |
(1) dollar-parens: $() | |
(2) backticks: `` | |
Example: | |
source $HOME/local/init/utils.sh | |
OUTPUT=$(pyblock " | |
import sys | |
print(sys.executable) | |
") | |
echo "OUTPUT = $OUTPUT" | |
''' | |
if [ "$2" ]; then | |
PYEXE="$1" | |
TEXT="$2" | |
else | |
PYEXE=python | |
TEXT="$1" | |
fi | |
$PYEXE -c "$(codeblock "$TEXT")" | |
} | |
codeblock() | |
{ | |
if [ "-h" == "$1" ] || [ "--help" == "$1" ]; then | |
# Use codeblock to show the usage of codeblock, so you can use | |
# codeblock while you codeblock. | |
echo "$(codeblock ' | |
Unindents code before its executed so you can maintain a pretty | |
indentation in your code file. Multiline strings simply begin | |
with | |
"$(codeblock " | |
and end with | |
")" | |
Example: | |
echo "$(codeblock " | |
a long | |
multiline string. | |
this is the last line that will be considered. | |
")" | |
')" | |
else | |
# Prevents python indentation errors in bash | |
#python -c "from textwrap import dedent; print(dedent('''$1''').strip('\n'))" | |
echo "$1" | python -c "import sys; from textwrap import dedent; print(dedent(sys.stdin.read()).strip('\n'))" | |
fi | |
} | |
have_sudo(){ | |
__heredoc__ ''' | |
Tests if we have the ability to use sudo. | |
Returns the string "True" if we do. | |
Example: | |
HAVE_SUDO=$(have_sudo) | |
if [ "$HAVE_SUDO" == "True" ]; then | |
sudo do stuff | |
else | |
we dont have sudo | |
fi | |
''' | |
python -c "$(codeblock " | |
import grp, pwd | |
user = '$(whoami)' | |
groups = [g.gr_name for g in grp.getgrall() if user in g.gr_mem] | |
gid = pwd.getpwnam(user).pw_gid | |
groups.append(grp.getgrgid(gid).gr_name) | |
print('sudo' in groups) | |
")" | |
} | |
ensure_curl(){ | |
HAVE_SUDO=$(have_sudo) | |
if [ "$(which curl)" == "" ]; then | |
echo "Need to install curl" | |
if [ "$HAVE_SUDO" == "True" ]; then | |
sudo apt install curl -y | |
else | |
echo "Cannot install curl without sudo" | |
fi | |
fi | |
} | |
setup_venv3(){ | |
__heredoc__ """ | |
CommandLine: | |
source ~/local/init/freshstart_ubuntu.sh && setup_venv3 | |
""" | |
# Ensure PIP, setuptools, and virtual are on the SYSTEM | |
if [ "$(has_pymodule python3 pip)" == "False" ]; then | |
#if [ "$(which pip3)" == "" ]; then | |
ensure_curl | |
mkdir -p ~/tmp | |
curl https://bootstrap.pypa.io/get-pip.py > ~/tmp/get-pip.py | |
# Test if we have distutils; if not, install it. | |
python3 -c "from distutils import sysconfig as distutils_sysconfig" | |
if [ "$(echo $?)" != "0" ]; then | |
sudo apt install python3-distutils -y | |
fi | |
python3 ~/tmp/get-pip.py --user | |
fi | |
python3 -m pip install pip setuptools virtualenv -U --user | |
#python3 -c "import sys; print(sys.version)" | |
#PYVERSUFF=$(python3 -c "import sys; print('.'.join(map(str, sys.version_info[0:3])))") | |
#PYVERSUFF=$(python3 -c "import sysconfig; print(sysconfig.get_config_var('LDVERSION'))") | |
PYEXE=$(python3 -c "import sys; print(sys.executable)") | |
PYVERSUFF=$(python3 -c "import sysconfig; print(sysconfig.get_config_var('VERSION'))") | |
PYTHON3_VERSION_VENV="$HOME/venv$PYVERSUFF" | |
mkdir -p $PYTHON3_VERSION_VENV | |
python3 -m virtualenv -p $PYEXE $PYTHON3_VERSION_VENV | |
python3 -m virtualenv --relocatable $PYTHON3_VERSION_VENV | |
PYTHON3_VENV="$HOME/venv3" | |
# symlink to the real env | |
ln -s $PYTHON3_VERSION_VENV $PYTHON3_VENV | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment