Created
December 23, 2009 21:18
-
-
Save tswicegood/262811 to your computer and use it in GitHub Desktop.
fabfile for generating a django app inside virtualenv using pip (requires tswicegood/fabric)
This file contains 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
from fabric.api import env | |
import fabric.api | |
import fabric.contrib.files | |
import os, random, re | |
__all__ = ['local', 'init', ] | |
###### | |
## | |
## Fabric callbacks | |
## | |
env.cmd = fabric.api.local | |
def local(): | |
env.hosts ['localhost',] | |
env.cmd = fabric.api.local | |
def init(): | |
cmd('virtualenv .') | |
cmd('pip install -E . -r requirements.txt') | |
init_django() | |
def cmd(*args): | |
[env.cmd(a) for a in args] | |
# TODO: make database configurable, allow replacing | |
SETTINGS_TEMPLATE = """ | |
import os | |
_CONFIG_PATH = os.path.dirname(__file__) | |
_BASE_PATH = os.path.join(_CONFIG_PATH, "..") | |
ADMINS = ( | |
('%(username)s', '%(username)s@%(hostname)s'), | |
) | |
MANAGERS = ADMINS | |
DATABASE_ENGINE = 'sqlite3' # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. | |
DATABASE_NAME = 'var/project.db' | |
DATABASE_USER = '' # Not used with sqlite3. | |
DATABASE_PASSWORD = '' # Not used with sqlite3. | |
DATABASE_HOST = '' # Set to empty string for localhost. Not used with sqlite3. | |
DATABASE_PORT = '' # Set to empty string for default. Not used with sqlite3. | |
TIME_ZONE = 'America/Chicago' | |
LANGUAGE_CODE = 'en-us' | |
# Absolute path to the directory that holds media. | |
# Example: "/home/media/media.lawrence.com/" | |
MEDIA_ROOT = os.path.join(_BASE_PATH, "media") | |
# URL that handles the media served from MEDIA_ROOT. Make sure to use a | |
# trailing slash if there is a path component (optional in other cases). | |
# Examples: "http://media.lawrence.com", "http://example.com/media/" | |
MEDIA_URL = '/media/' | |
# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a | |
# trailing slash. | |
# Examples: "http://foo.com/media/", "/media/". | |
ADMIN_MEDIA_PREFIX = '/admin_media/' | |
# Don't share this with anybody. | |
SECRET_KEY = '%(secret)s' | |
MIDDLEWARE_CLASSES = ( | |
'django.middleware.common.CommonMiddleware', | |
'django.contrib.sessions.middleware.SessionMiddleware', | |
'django.contrib.auth.middleware.AuthenticationMiddleware', | |
'django.middleware.doc.XViewMiddleware', | |
) | |
ROOT_URLCONF = 'config.urls' | |
INSTALLED_APPS = ( | |
'django.contrib.auth', | |
'django.contrib.contenttypes', | |
'django.contrib.sessions', | |
'django.contrib.admin', | |
) | |
TEMPLATE_LOADERS = ( | |
'django.template.loaders.filesystem.load_template_source', | |
'django.template.loaders.app_directories.load_template_source', | |
) | |
TEMPLATE_DIRS = ( | |
os.path.join(_BASE_PATH, "templates"), | |
) | |
if os.path.exists(os.path.join(_CONFIG_PATH, "local_settings.py")): | |
from .local_settings import * | |
""".strip() | |
DEVELOPMENT_TEMPLATE = """ | |
from config.settings import * | |
DEBUG=True | |
TEMPLATE_DEBUG=DEBUG | |
if os.path.exists(os.path.join(os.path.dirname(__file__), "local_development.py")): | |
from .local_development import * | |
""".strip() | |
PRODUCTION_TEMPLATE = """ | |
from config.settings import * | |
if os.path.exists(os.path.join(os.path.dirname(__file__), "local_production{{{.py")): | |
from .local_production import * | |
""".strip() | |
URLS_TEMPLATE = """ | |
from django.conf.urls.defaults import patterns, include, handler500, url | |
from django.conf import settings | |
from django.contrib import admin | |
admin.autodiscover() | |
handler500 # Pyflakes | |
urlpatterns = patterns( | |
'', | |
url(r'^admin/', include(admin.site.urls)), | |
url(r'^accounts/login/$', 'django.contrib.auth.views.login'), | |
) | |
if settings.DEBUG: | |
urlpatterns += patterns('', | |
(r'^media/(?P<path>.*)$', 'django.views.static.serve', | |
{'document_root': settings.MEDIA_ROOT}), | |
) | |
""".strip() | |
DJANGO_EXECUTABLE = """ | |
#!/usr/bin/env python | |
import django.core.management | |
import optparse | |
import os | |
import sys | |
def main(): | |
sys.path[0:0] = [ | |
os.path.join(os.path.dirname(__file__), ".."), | |
] | |
parser = optparse.OptionParser() | |
parser.add_option( | |
'-s', '--settings', | |
dest='settings', | |
help='settings file to execute using', | |
default='development' | |
) | |
(options, args) = parser.parse_args() | |
# TODO: handle ImportError | |
# Ruthlessly borrowed from the buildout djangorecipe | |
settings_file = 'config.%s' % options.settings | |
settings = __import__(settings_file) | |
for comp in settings_file.split('.')[1:]: | |
settings = getattr(settings, comp) | |
django.core.management.execute_manager(settings) | |
if __name__ == '__main__': | |
main() | |
""".strip() | |
# TODO: Move into fabric.contrib.files | |
def mkdir(directory, cmd=cmd): | |
if not fabric.contrib.files.exists(directory, cmd=cmd): | |
cmd("mkdir %s" % directory) | |
def init_django(): | |
def executable(force=False): | |
mkdir('bin', cmd=env.cmd) | |
if fabric.contrib.files.exists('bin/django', cmd=env.cmd) and not force: | |
return | |
fabric.contrib.files.write('bin/django', DJANGO_EXECUTABLE, cmd=env.cmd) | |
cmd('chmod u+x bin/django') | |
def settings(): | |
# borrowed from djangorecipe | |
def generate_secret(): | |
chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)' | |
return ''.join([random.choice(chars) for i in range(50)]) | |
mkdir('config', cmd=env.cmd) | |
if not fabric.contrib.files.exists('config/__init__.py', cmd=env.cmd): | |
fabric.contrib.files.write('config/__init__.py', '', cmd=env.cmd) | |
if not fabric.contrib.files.exists("config/settings.py", cmd=env.cmd): | |
fabric.contrib.files.write( | |
'config/settings.py', | |
SETTINGS_TEMPLATE % { | |
"secret": generate_secret(), | |
"username": os.environ['USER'], | |
"hostname": os.uname()[1], | |
}, | |
cmd=env.cmd | |
) | |
if not fabric.contrib.files.exists('config/development.py', cmd=env.cmd): | |
fabric.contrib.files.write('config/development.py', DEVELOPMENT_TEMPLATE, cmd=env.cmd) | |
if not fabric.contrib.files.exists('config/development.py', cmd=env.cmd): | |
fabric.contrib.files.write('config/production.py', PRODUCTION_TEMPLATE, cmd=env.cmd) | |
def urls(): | |
if not fabric.contrib.files.exists('config/urls.py', cmd=env.cmd): | |
fabric.contrib.files.write('config/urls.py', URLS_TEMPLATE, cmd=env.cmd) | |
executable() | |
settings() | |
urls() | |
dirs_to_create = ['apps', 'media', 'resources', 'templates', 'var',] | |
[mkdir(dir, cmd=env.cmd) for dir in dirs_to_create] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment