Find file
Fetching contributors…
Cannot retrieve contributors at this time
73 lines (60 sloc) 2.44 KB
# Copyright (C) 2008-2010 United States Government as represented by
# the Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
import os
import sys
import tempfile
import re
def getEnvironmentFromSourceMe(d='.'):
# pick up environment variables from sourceme
fd, varsFile = tempfile.mkstemp('djangoWsgiSourceMe.txt')
ret = os.system('bash -c "(source %s/ && printenv > %s)"' % (d, varsFile))
if ret != 0:
varsFile = '%s/vars.txt' % d
print >> sys.stderr, ' could not auto-generate environment from, trying to fall back to manually generated file %s' % varsFile
# fallback: user can manually generate vars.txt file by sourcing and running 'printenv > vars.txt'
varsIn = file(varsFile, 'r')
for line in varsIn:
line = line[:-1] # chop final cr
if '=' not in line or '=()' in line:
var, val = line.split('=', 1)
os.environ[var] = val
except OSError:
# set up virtualenv if needed
if 'VIRTUAL_ENV' in os.environ:
activateFile = '%s/bin/' % os.environ['VIRTUAL_ENV']
execfile(activateFile, {'__file__': activateFile})
# add any new entries from PYTHONPATH to Python's sys.path
if 'PYTHONPATH' in os.environ:
envPath = re.sub(':$', '', os.environ['PYTHONPATH'])
sys.path = envPath.split(':') + sys.path
def sendError(start_response, text):
start_response(text, [('Content-type', 'text/html')])
return ["""<html>
""" % (text, text)]
def downForMaintenance(environ, start_response):
import stat
import time
d = os.path.dirname(os.path.realpath(__file__))
downFile = os.path.join(d, 'DOWN_FOR_MAINTENANCE')
downMtime = os.stat(downFile)[stat.ST_MTIME]
downTimeString = time.strftime('%Y-%m-%d %H:%M %Z', time.localtime(downMtime))
return sendError(start_response, '503 Down for maintenance since %s' % downTimeString)
thisDir = os.path.dirname(os.path.realpath(__file__))
if os.path.exists(os.path.join(thisDir, 'DOWN_FOR_MAINTENANCE')):
application = downForMaintenance
from django.core.handlers.wsgi import WSGIHandler
application = WSGIHandler()