Skip to content

Instantly share code, notes, and snippets.

@chroto
Created November 25, 2011 21:19
Show Gist options
  • Save chroto/1394434 to your computer and use it in GitHub Desktop.
Save chroto/1394434 to your computer and use it in GitHub Desktop.
Getting django projects started without fuss.
#! /usr/bin/env python
import os
import subprocess
import pprint
def use_virtualenv(virtualenv_path):
command = ['bash', '-c', 'source '+os.path.join(virtualenv_path,'bin','activate')+' && env']
proc = subprocess.Popen(command, stdout = subprocess.PIPE)
for line in proc.stdout:
(key, _, value) = line.partition("=")
os.environ[key] = value.rstrip('\n')
proc.communicate()
printc('Starting virtualenv with this environment:')
pprint.pprint(dict(os.environ))
def printc(txt, color='green'):
my_colors = {
'red': "[31m",
'green': "[32m",
'yellow': "[33m"
}
if color in my_colors:
print "\033[33m[takeoff.py]\033[0m \033%s%s\033[0m" % (my_colors[color], txt)
printc('Preparing to blast off!')
with open('abbv.txt') as f:
abbv = f.readline().strip('\n')
printc('Got project abbrieviation from abbv.txt as "' + abbv + '"')
pwd = os.getcwd()
workon_home = os.environ.get('WORKON_HOME')
if workon_home:
printc('virtualenv appears to be installed. I\'m going for it.')
subprocess.call('virtualenv --no-site-packages '+os.path.join(workon_home,abbv), shell=True)
use_virtualenv(os.path.join(workon_home,abbv))
printc('virtualenv successfully started.')
printc('Checking for package updates')
subprocess.call('pip install -r requirements.txt', shell=True)
project_dir = os.path.join(pwd,pwd.split(os.sep)[-1].strip('.com'))
if os.path.isdir(project_dir):
printc('Project directory found at ' + project_dir)
printc('Checking for local settings')
local_settings = os.path.join(project_dir, 'settings_local.py')
if os.path.isfile(local_settings):
printc('Found local settings file.')
printc('Continuing to migrations.')
else:
printc('No local settings found.','red')
printc('I am going to create one with basic settings', 'red')
with open(local_settings, 'w') as f:
f.write('''
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'sqlite_database', # Or path to database file if using sqlite3.
'USER': '', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
''')
else:
printc('The project directory could not be found. I tried ' + project_dir, 'red')
create_superuser = False
if not os.path.isfile('sqlite_database'):
printc('No sqlite database found. I am going to create one.', 'red')
subprocess.call('python manage.py syncdb', shell=True)
create_superuser = True
printc('Running migrations')
subprocess.call('python manage.py migrate', shell=True)
if create_superuser:
printc('Prompting user to create a superuser', 'red')
printc('If you already did this, Ctrl+C to skip')
try:
subprocess.call('python manage.py createsuperuser', shell=True)
except KeyboardInterrupt:
pass
printc('Everything looks good to me.')
printc('Starting the server. For great good!')
try:
subprocess.call('python manage.py runserver', shell=True)
except KeyboardInterrupt:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment