Skip to content

Instantly share code, notes, and snippets.

@oweidner
Created October 27, 2011 22:01
Show Gist options
  • Save oweidner/1321016 to your computer and use it in GitHub Desktop.
Save oweidner/1321016 to your computer and use it in GitHub Desktop.
A Python Environment Bootstrapper
#!/bin/bash
## Description:
##
## pystrap.sh is a shell script to bootstrap a Python environment
## in user space that includes pip and virtualenv. It can be used
## to bootstrap a Python environment remotely, e.g., on a cluster
## where setuptools, virtualenv, etc. are usually not installed.
##
## Example: pystrap via Globus:
##
## globus-job-run tuscany.med.harvard.edu:2119/jobmanager-fork "/bin/sh" -c \
## "curl -fsSLk https://raw.github.com/gist/1321016 > pystrap.sh \
## && /bin/sh pystrap.sh -luuid,threadpool"
##
## The bootstrapped environment can then be accessed via the
## standard virtualenv way: source /tmp/mypy_u`id -u`/bin/activate
##
## Author: Ole Christian Weidner
## Email: [email protected]
## Website: https://gist.github.com/1321016
##
## Copyright 2011, Ole Christian Weidner
## Licensed under the MIT license.
E_BADARGS=65
E_SUCCESS=0
E_BADOPT=85
# defaults
ARG_INSTALL_DIR=`pwd`
ARG_PYTHON_EXE=python
ARG_PACKAGES=""
ARG_OVERWRITE=true
usage () {
echo "Usage: `basename $0` [options...] "
echo "Options: "
echo " -h Display (this) help screeen and exit. "
echo " -n Do not overwrite any existing, previously "
echo " bootstrapped environments. "
echo " -d <dir> Use non-default base directory <dir> to "
echo " bootstrap the Python environment in. "
echo " (default: current working directory) "
echo " -l <list> Comma-separated list of extra packages to "
echo " install in the Python environment. "
echo " (default: none) "
echo " -p <python> Use <python> as an alternative Python "
echo " binary. "
echo " (default: first 'python' found in \$PATH) "
echo " "
}
while getopts ":hnd:l:p:" Option
do
case $Option in
h ) usage
exit $E_SUCCESS
;;
n ) ARG_OVERWRITE=false
;;
d ) ARG_INSTALL_DIR=$OPTARG
;;
p ) ARG_PYTHON_EXE=$OPTARG
;;
l ) ARG_PACKAGES=$OPTARG
;;
: )
echo "Option '-$OPTARG' requires an argument." >&2
exit $E_BADOPT
;;
* ) echo "Option '-$OPTARG' doesn't exist.";# Default.
esac
done
shift $(($OPTIND - 1))
MYPY_DIR=$ARG_INSTALL_DIR/mypy_u`id -u`
PYTHON=$ARG_PYTHON_EXE
PYVER=`$PYTHON -c 'import platform; print platform.python_version()[0:3]'`
if [ -d "$MYPY_DIR" ]; then
if ! $ARG_OVERWRITE ;
then
if [ -f "$MYPY_DIR/.done" ]; then
# complete installation found. exiting
echo "Found a previously bootstrapped environemnt in $MYPY_DIR. Exiting."
exit $E_SUCCESS
else
echo "Found a previously bootstrapped environemnt in $MYPY_DIR. But it seems to be currupted. Reinstalling."
fi
fi
rm -r $MYPY_DIR
fi
MYPY_DIR_FULL=$MYPY_DIR/lib/python$PYVER/site-packages
mkdir -p $MYPY_DIR_FULL
date > $MYPY_DIR/.lock
# Install setup tools
curl -k "http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz" | tar xz
cd setuptools-0.6c11
PYTHONPATH=$PYTHONPATH:$MYPY_DIR_FULL $PYTHON setup.py install --prefix=$MYPY_DIR
PYTHONPATH=$PYTHONPATH:$MYPY_DIR_FULL $MYPY_DIR/bin/easy_install --prefix=$MYPY_DIR pip
PYTHONPATH=$PYTHONPATH:$MYPY_DIR_FULL $MYPY_DIR/bin/easy_install --prefix=$MYPY_DIR virtualenv
#cleanup
cd ..
rm -rf setuptools-0.6c11
# Create & test virtual Python environment
PYTHONPATH=$PYTHONPATH:$MYPY_DIR_FULL $MYPY_DIR/bin/virtualenv $MYPY_DIR
. $MYPY_DIR/bin/activate
$PYTHON -c 'import pip'
# Install additional packages
for i in $(echo $ARG_PACKAGES | tr "," "\n")
do
pip install $i
done
echo date > $MYPY_DIR/.done
rm $MYPY_DIR/.lock
echo " "
echo "Yay!!! Python environment bootstrapped in $MYPY_DIR for $PYTHON ($PYVER)"
echo " You can activate it like this: source $MYPY_DIR/bin/activate "
echo " "
exit $E_SUCCESS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment