Skip to content

Instantly share code, notes, and snippets.

@Ciantic
Created July 8, 2010 21:45
Show Gist options
  • Save Ciantic/468691 to your computer and use it in GitHub Desktop.
Save Ciantic/468691 to your computer and use it in GitHub Desktop.
Django FCGI application
#!/usr/bin/python
import sys, os
# Add a custom Python path.
THE_SITE = "mysite"
PYTHONPATH = "/var/www/mysite.example.com/django-fcgi/pythonpath/"
# Following is inferred from above
THE_DIR = PYTHONPATH + THE_SITE
DJANGO_PATH = PYTHONPATH + "django"
DJANGO_SETTINGS_MODULE = "%s.settings" % THE_SITE
PS1 = "[\[\033[32m\]\w]\[\033[0m\]\n\[\033[1;36m\]%s\[\033[1;33m\]-> \[\033[0m\]" % THE_SITE
sys.path.insert(0, PYTHONPATH)
# Switch to the directory of your project. (Optional.)
os.chdir(THE_DIR)
env = {
'PYTHONPATH' : os.environ.get('PYTHONPATH', '') + os.pathsep + PYTHONPATH,
'DJANGO_SETTINGS_MODULE' : DJANGO_SETTINGS_MODULE,
'PS1' : PS1,
'PATH' : os.environ.get('PATH', '') + os.pathsep + DJANGO_PATH + "/bin",
}
os.environ.update(env)
if len(sys.argv) > 1:
import subprocess
def list_split(arglist, splitter="--"):
cache = []
while len(arglist):
arg = arglist.pop(0)
if arg == splitter:
yield cache
cache = []
continue
cache.append(arg)
yield cache
class ArgumentCommands(object):
def shell(self, args=None):
subprocess.call(["/bin/bash", "--norc"], env = env)
def python(self, args=None):
subprocess.call(["python"], env = env)
def manage(self, args=None):
subprocess.call(["python", "manage.py"] + args, env = env)
return []
def admin(self, args=None):
subprocess.call(["django-admin.py"] + args, env = env)
return []
def gitpull(self, args=None):
subprocess.call(["git", "pull"])
def restart(self, args=None):
subprocess.call(["killall", "django.fcgi"])
def settings(self, args=None):
subprocess.call(["pico", "settings.py"])
def urls(self, args=None):
subprocess.call(["pico", "urls.py"])
argcommands = ArgumentCommands()
# Chainable commands, e.g. "django.fcgi settings restart" first go to settings, then restart
args = sys.argv[1:]
for arglist in list_split(args):
while len(arglist):
out_args = getattr(argcommands, arglist.pop(0))(arglist)
if out_args is not None:
arglist = out_args
sys.exit(0)
from django.core.servers.fastcgi import runfastcgi
runfastcgi(method="prefork", maxchildren=3, minspare=0, maxspare=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment