Setup virtualenv for a Google App Engine project
#!/bin/sh | |
# Script for setting up virtualenv to work correctly with Google App Engine projects | |
# @author Karol Kuczmarski "Xion" | |
DEFAULT_APPENGINE_SDK_PATH="/opt/google_appengine" | |
DEFAULT_PROJECT_LIB_PATH="./lib" | |
# Utility functions | |
error() { | |
echo "$@" >&2; | |
} | |
fatal() { | |
error "fatal: $@" | |
exit | |
} | |
list_subdirectories() { | |
local path=$(readlink -f "$1") # make relative path absolute | |
ls "$path/*" --directory --almost-all --format=single-column | |
} | |
# Handling virtualenv | |
ensure_mkvirtualenv() { | |
which mkvirtualenv >/dev/null \ | |
|| fatal "mkvirtualenv not found -- cannot create virtualenv!" | |
} | |
ensure_virtualenv() { | |
if [ -z "$VIRTUAL_ENV" ]; then | |
echo "virtualenv not detected!" | |
ensure_mkvirtualenv | |
printf "Enter the name to create one using virtualenvwrapper: " | |
read virtualenv_name | |
if [ -z "$virtualenv_name" ]; then | |
fatal "Empty name -- aborting" | |
fi | |
mkvirtualenv "$virtualenv_name" --no-site-packages \ | |
|| fatal "Failed creating virtualenv!" | |
echo "virtualenv created." | |
else | |
echo "Using current virtualenv ($VIRTUAL_ENV)." | |
fi | |
} | |
get_venv_site_packages_path() { | |
local python_version=$(python -V 2>&1 | cut -d' ' -f 2) | |
local short_version="${python_version%.*}" # minor.major, like "2.7" | |
echo "$VIRTUAL_ENV/lib/python$short_version/site-packages" | |
} | |
# Getting paths from the user | |
get_appengine_path() { | |
local appengine_path | |
read -p "Enter the path to Google App Engine Python SDK [$DEFAULT_APPENGINE_SDK_PATH]: " appengine_path | |
if [ -z "$appengine_path" ]; then | |
appengine_path=$DEFAULT_APPENGINE_SDK_PATH | |
fi | |
if [ ! -d "$appengine_path" ]; then | |
fatal "App Engine SDK directory not found -- aborting" | |
fi | |
echo $appengine_path | |
} | |
get_project_lib_path() { | |
local lib_path | |
read -p "Enter the path to project's third party libraries [$DEFAULT_PROJECT_LIB_PATH]: " lib_path | |
if [ -z "$lib_path" ]; then | |
lib_path=$DEFAULT_PROJECT_LIB_PATH | |
fi | |
if [ ! -d "$lib_path" ]; then | |
fatal "Project's libraries not found -- aborting" | |
fi | |
echo $lib_path | |
} | |
# Main script | |
prepare_gae_site_packages() { | |
printf "Adding App Engine libs to site-packages..." | |
local appengine_path=$1 | |
local gae_pth=$(echo $(get_venv_site_packages_path)/gae.pth) | |
echo "$appengine_path" >"$gae_pth" | |
list_subdirectories "$appengine_path"/lib >>"$gae_pth" | |
echo "done." | |
} | |
prepare_lib_site_packages() { | |
printf "Adding project libraries to site-packages..." | |
local lib_path=$1 | |
local project_pth=$(echo $(get_venv_site_packages_path)/project.pth) | |
# check for root __init__.py to see if libraries are self-contained | |
# within subdirectories, or they are part of one Python package | |
if [ -f "$lib_path"/__init__.py ]; then | |
echo $lib_path >"$project_pth" | |
else | |
list_subdirectories "$lib_path" >"$project_pth" | |
fi | |
echo "done." | |
} | |
create_gae_tools_symlinks() { | |
printf "Symlinking App Engine utilities..." | |
local appengine_path=$1 | |
# dev_appserver.py breaks if its working dir is not root of GAE SDK | |
dev_appserver_script=$VIRTUAL_ENV/bin/dev_appserver | |
cat <<EOF >$dev_appserver_script | |
#!/bin/sh | |
$appengine_path/dev_appserver.py \$* | |
EOF | |
chmod a+x $dev_appserver_script | |
ln -s "$appengine_path"/appcfg.py $VIRTUAL_ENV/bin/appcfg | |
echo "done." | |
} | |
main() { | |
ensure_virtualenv | |
local appengine_path=$(get_appengine_path) | |
local project_lib_path=$(get_project_lib_path) | |
prepare_gae_site_packages "$appengine_path" | |
prepare_lib_site_packages "$project_lib_path" | |
create_gae_tools_symlinks "$appengine_path" | |
echo "Setup completed." | |
} | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment