Created
March 29, 2009 23:31
-
-
Save tvon/87566 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
""" | |
This script aims at providing a simple way to bootstrap a self-contained django project. | |
In short, it makes a few trivial decisions for you. | |
To those who use the Django framework this may seem a bit unecessary, but I believe that | |
it could be easier to get started with django, without having to impose minor decisions | |
on new users such as where to place templates and basic media files. | |
$ gig myproject | |
Will create: | |
myproject/ | |
lib/ | |
lib/django/ | |
config/__init__.py | |
config/urls.py | |
config/settings/ | |
config/settings/__init__.py | |
config/settings/base.py | |
templates/ | |
media/ | |
script/ | |
script/manage | |
A bit about this layout. | |
lib/ | |
This is added to PYTHONPATH, so any new apps should go in here, as well as any existing | |
django apps you might want to use | |
config/ | |
The project-wide config files (what `startproject` would typically generate, mostly). Of | |
note are `urls.py` and... | |
config/settings/ | |
This is a module hoding `base.py`, used as the general config, and any host-specific configs. | |
For example, on my home machine I might have `config/settings/home.py` with paths specific to | |
that setup, while at work I might have `config/settings/office.py`. Or development and production | |
configs, of course. Basically the same deal as Rails environments. | |
templates/ | |
App tempaltes. | |
media/ | |
App media (maybe, there is that whole deal of ) | |
script/manage | |
The usual manage.py created from `django-admin startproject`, but tweaked to deal with it's location. | |
""" | |
import sys, os, stat | |
import urllib | |
import tarfile | |
import tempfile | |
django_version="1.0.2" | |
cms_version="r41" | |
#django_url = 'http://media.djangoproject.com/releases/%s/Django-%s-final.tar.gz' % (django_version, django_version) | |
django_url = 'http://localhost/~tvon/Django-%s-final.tar.gz' % django_version | |
cms_url = 'http://django-cms.org/media/releases/cms-%s.tar.gz' % cms_version | |
def log(s): | |
"This will at some point be more useful..." | |
print s | |
def buildTree(): | |
global projectname | |
makeFolder(projectname) | |
makeFolder('%s/lib' % projectname) | |
makeFolder('%s/config' % projectname) | |
makeFolder('%s/config/settings' % projectname) | |
makeFolder('%s/templates' % projectname) | |
makeFolder('%s/media' % projectname) | |
makeFolder('%s/script' % projectname) | |
def makeFolder(path): | |
"TODO: check if folder exists, report accordingly" | |
log("creating: %s" % path) | |
try: | |
os.mkdir(path) | |
except OSError: | |
log("...already exists") | |
def copyToLib(url): | |
f = urllib.urlretrieve(url) | |
return f[0] | |
def installDjagno(): | |
global projectname, django_url | |
log("fetching Django-xxx") | |
filename = copyToLib(django_url) | |
tar = tarfile.open(filename, "r:gz") | |
tempdir = tempfile.mkdtemp() | |
log("extracting Django-xxx") | |
tar.extractall(tempdir) | |
log ("moving django-xxx/django to project/lib/django") | |
os.rename('%s/Django-1.0.2-final/django' % tempdir, '%s/lib/django' % projectname) | |
tar.close() | |
# Erm, tempdir cleanup? | |
def makeManageScript(): | |
global projectname | |
script = """#!/usr/bin/env python | |
# TODO: non-standard pythons installs | |
import sys, os | |
# Add project-specific modules to pythonpath. | |
# We have to insert it in the beginning to avoid an | |
# installed Django getting picked up instead | |
basedir = os.path.normpath ( '%s/..' % os.path.split( os.path.abspath( sys.argv[0]) )[0] ) | |
sys.path[1:1] = ['%s/config' % basedir, '%s/lib' % basedir] | |
from django.core.management import execute_manager | |
try: | |
# TODO: import non-base config, eg 'hostname' or 'development' | |
from settings import base as settings | |
except ImportError: | |
import sys | |
sys.stderr.write("Error: Can't import settings") | |
sys.exit(1) | |
if __name__ == "__main__": | |
execute_manager(settings) | |
""" | |
filename = '%s/script/manage.py' % projectname | |
f = open(filename, 'w') | |
f.write(script) | |
f.close() | |
# Maxe executable | |
statinfo = os.stat(filename) | |
permissions = stat.S_IEXEC | statinfo[0] | |
os.chmod(filename, permissions) | |
def setupProjectFiles(): | |
global projectname | |
from django.core import management | |
management.base.copy_helper(None, 'project', projectname, projectname) | |
os.unlink('%s/%s/manage.py' % (projectname, projectname)) | |
os.unlink('%s/%s/__init__.py' % (projectname, projectname)) | |
os.rename('%s/%s/settings.py' % (projectname, projectname), '%s/config/settings/base.py' % projectname) | |
os.rename('%s/%s/urls.py' % (projectname, projectname), '%s/config/urls.py' % projectname) | |
os.rmdir('%s/%s' % (projectname, projectname)) | |
# Make config/settings/ a module | |
f = open('%s/config/settings/__init__.py' % projectname, 'w') | |
f.write('#\n') | |
f.close() | |
#### The goods #### | |
projectname = sys.argv[1] | |
buildTree() | |
installDjagno() | |
makeManageScript() | |
# Django should be available now... | |
sys.path[1:1] = [os.path.abspath('%s/lib/' % projectname)] | |
import django | |
setupProjectFiles() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment