Last active
April 8, 2016 12:01
-
-
Save particledecay/24fd13f2aee599fea4c8d6eff1203a07 to your computer and use it in GitHub Desktop.
Create a new virtualenv with git cloning and package requirements
This file contains hidden or 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
| #!/bin/bash | |
| # define a help printout | |
| function help { | |
| echo "usage: `basename $0` [-h] [--project-dir dir] [--requirements reqfile] [--url git-url]" | |
| echo " [--virtualenv-dir venv-dir] <project-name>" | |
| echo "" | |
| echo "Create a new virtualenv with git cloning and package requirements." | |
| echo "" | |
| echo "positional arguments:" | |
| printf " project-name\t\tname of the desired project\n" | |
| echo "" | |
| echo "optional arguments:" | |
| printf " -h, --help\t\tshow this help message and exit\n" | |
| echo " --project-dir DIR, -p DIR" | |
| printf " \t\t\tdirectory where project will be created (default: PROJECT_HOME)\n" | |
| echo " --requirements FILE, -r FILE" | |
| printf " \t\t\trequirements file to install python packages (pre-clone)\n" | |
| echo " --url URL, -u URL" | |
| printf " \t\t\tURL of remote git repository to clone\n" | |
| echo " --virtualenv-dir DIR, -v DIR" | |
| printf " \t\t\tdirectory where virtualenv will be created (default: WORKON_HOME)\n" | |
| if [ -z "$1" ]; then | |
| exit 1 | |
| else | |
| exit $1 | |
| fi | |
| } | |
| # find the virtualenvwrapper file from known locations | |
| WRAPPER_FILE= | |
| for venv_sh in $HOME/.local/bin/virtualenvwrapper.sh \ | |
| /usr/share/virtualenvwrapper/virtualenvwrapper.sh \ | |
| /usr/local/bin/virtualenvwrapper.sh \ | |
| /usr/bin/virtualenvwrapper.sh; do | |
| if [ -f $venv_sh ]; then | |
| WRAPPER_FILE=$venv_sh | |
| break | |
| fi | |
| done | |
| # could not find wrapper script; exit | |
| if [ -z "$WRAPPER_FILE" ]; then | |
| echo "ERROR: Could not locate virtualenvwrapper.sh." | |
| echo "Make sure virtualenvwrapper is installed and try again." | |
| exit 2 | |
| fi | |
| source $WRAPPER_FILE | |
| # parse through all opts with args | |
| REQ_FILE= | |
| REPO_URL= | |
| while [[ $# -gt 1 ]]; do | |
| key=$1 | |
| case $key in | |
| -p|--project-dir) | |
| PROJECT_HOME=$2 | |
| shift | |
| ;; | |
| -r|--requirements) | |
| REQ_FILE=$2 | |
| shift | |
| ;; | |
| -u|--url) | |
| REPO_URL=$2 | |
| shift | |
| ;; | |
| -v|--virtualenv-dir) | |
| WORKON_HOME=$2 | |
| shift | |
| ;; | |
| *) | |
| echo "ERROR: Unknown option ${key}" | |
| help | |
| shift | |
| ;; | |
| esac | |
| shift | |
| done | |
| # parse through opts without args (positional) | |
| if [ ! -z "$1" ]; then | |
| case $1 in | |
| -h|--help) | |
| help 0 | |
| ;; | |
| *) | |
| project_name=$1 | |
| ;; | |
| esac | |
| fi | |
| # whoops, no project name | |
| if [ "$project_name" == "" ]; then | |
| help; | |
| fi | |
| # create the virtualenv and install packages | |
| echo "Creating project \"$project_name\"..." | |
| export PROJECT_HOME | |
| export WORKON_HOME | |
| if [ -z "$REQ_FILE" ]; then | |
| mkproject $project_name | |
| else | |
| # the '-r' allows us to automatically install pip packages | |
| mkproject -r $REQ_FILE $project_name | |
| fi | |
| # create a project directory and optionally load it with a repo | |
| project_dir=$PROJECT_HOME/$project_name | |
| cd $project_dir | |
| if [ ! -z "$REPO_URL" ]; then | |
| git clone $REPO_URL . | |
| fi | |
| # enter the virtualenv | |
| workon $project_name |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment