Skip to content

Instantly share code, notes, and snippets.

@rkulla
Last active August 29, 2015 14:03
Show Gist options
  • Save rkulla/fbefc51c896bd7781c8d to your computer and use it in GitHub Desktop.
Save rkulla/fbefc51c896bd7781c8d to your computer and use it in GitHub Desktop.
Django Skeleton Script
#!/bin/bash
# django-skeleton by Ryan Kulla <[email protected]>
# Sets up my prefered Django project layout. Creates a virtualenv with
# django-toolbelt django-debug-toolbar and django-coverage. And creates
# an initial local Git repository.
default_packages=(django-toolbelt django-debug-toolbar django-coverage)
root_folder="$1"
project_name="$2"
root_basename=$(basename "$root_folder")
logfile=/tmp/django-skel.log
usage() {
printf "Usage: django-skeleton [root folder] [project name]\n\n"
printf "Example: django-skeleton /tmp/mysite.com mysite\n"
printf "will create a new project 'mysite' in /tmp/mysite.com.\n\n"
exit
}
validate_environment() {
if [[ -d "$root_folder" ]]; then
printf "%s already exists.\n" "$root_folder"
exit 0
fi
}
main() {
printf "Creating project structure: %s/%s\n" "$root_folder" "$project_name"
mkdir -p "$root_folder" && cd $_
printf "Creating virtualenv: %s\n" "$root_basename"
source $(which virtualenvwrapper.sh)
mkvirtualenv "$root_basename" > $logfile
if [[ $(basename "$VIRTUAL_ENV") != "$root_basename" ]]; then
printf "Could not switch to new virtualenv. Try workon?\n"
exit 0
fi
printf "Installing %s\n" "${default_packages[@]}"
pip install "${default_packages[@]}" >> $logfile
mkdir requirements
pip freeze > requirements/common.txt
echo "-r common.txt" > requirements/dev.txt
echo "-r common.txt" > requirements/prod.txt
echo "-r requirements/prod.txt" > requirements.txt
django-admin.py startproject "$project_name" .
cd "$project_name"
mkdir apps libs
touch {apps,libs}/__init__.py
cd "$root_folder"
git init
git add .
git commit -qam "Initial project setup"
printf "Committed what we have so far to git\n\n"
printf "Consult %s if anything went wrong\n\n" "$logfile"
./manage.py runserver 0.0.0.0:8000
}
if (( $# < 2 )); then
usage
else
validate_environment
main
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment