Skip to content

Instantly share code, notes, and snippets.

@urschrei
Created May 12, 2012 14:46
Show Gist options
  • Save urschrei/2666927 to your computer and use it in GitHub Desktop.
Save urschrei/2666927 to your computer and use it in GitHub Desktop.
Bootstrap a Flask project
#!/bin/bash -e
# opinionated Flask bootstrap script
# assumes you'll be using MySQL/PG, Fabric, Webassets, WTForms and Blueprints
# creates a virtualenv and an Alembic migrations system
# The script will halt on any error, and remove the project dir, if it created one
# accepts a single argument: a new directory name under which to create the project
# check that Python is installed
type python >/dev/null 2>&1 || { echo >&2 "Python doesn't appear to be installed. Aborting."; exit 1; }
# check that there's a system-wide virtualenv package installed
type virtualenv >/dev/null 2>&1 || { echo >&2 "This script requires a system-wide install of Virtualenv. Aborting."; exit 1; }
if [ ! -d $1 ]; then
# error handler removes the created project dir if there's a problem
dir=`pwd`
trap 'echo "Cleaning up"; cd $dir; rm -rf $1' INT TERM EXIT
mkdir -p $1/$1/config
mkdir -p $1/$1/templates/errors
mkdir -p $1/$1/static/{css,js,img,gen}
mkdir -p $1/apps
mkdir -p $1/fabfile
touch $1/requirements.txt
virtualenv $1/venv
source $1/venv/bin/activate
if type pg_config >/dev/null 2>&1; then
pip install psycopg2
else
echo "Skipping psycopg2 installation: couldn't find pg_config"
fi
for package in flask fabric yolk flask-sqlalchemy flask-assets pyyaml yuicompressor flask-wtf mysql-python alembic ipython
do
pip install $package
done
pip freeze >> $1/requirements.txt
$1/venv/bin/easy_install readline
cd $1
alembic init db
cd ..
touch $1/run.py
touch $1/$1/__init__.py
touch $1/apps/__init__.py
touch $1/fabfile/__init__.py
touch $1/$1/config/common.py
touch $1/$1/config/dev.py
trap 'echo "Finished creating project skeleton"' INT TERM EXIT
deactivate
else
{ echo >&2 "That directory already exists. Aborting."; exit 1; }
fi
@bradwright
Copy link

lolmysql

@urschrei
Copy link
Author

urschrei commented May 13, 2012 via email

@bradwright
Copy link

https://gist.github.com/2689895

I changed it to use requirements.txt instead of pip install, as you'll likely be using that again later.

@urschrei
Copy link
Author

Oh nice. I could just check that type pg_config succeeds (same as for Python and Virtualenv), because that seems to be the biggest stumbling block for PG installation from pip.

@khoffrath
Copy link

Line 21 contains a redundant blank. The line should be: "mkdir -p $1/app/static/{css,js,img,gen}".

@urschrei
Copy link
Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment