-
Notifications
You must be signed in to change notification settings - Fork 2
/
djangoWsgi.py
72 lines (60 loc) · 2.44 KB
/
djangoWsgi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# __BEGIN_LICENSE__
# Copyright (C) 2008-2010 United States Government as represented by
# the Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
# __END_LICENSE__
import os
import sys
import tempfile
import re
def getEnvironmentFromSourceMe(d='.'):
# pick up environment variables from sourceme
fd, varsFile = tempfile.mkstemp('djangoWsgiSourceMe.txt')
os.close(fd)
ret = os.system('bash -c "(source %s/sourceme.sh && printenv > %s)"' % (d, varsFile))
if ret != 0:
varsFile = '%s/vars.txt' % d
print >> sys.stderr, 'djangoWsgi.py: could not auto-generate environment from sourceme.sh, trying to fall back to manually generated file %s' % varsFile
# fallback: user can manually generate vars.txt file by sourcing sourceme.sh 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:
continue
var, val = line.split('=', 1)
os.environ[var] = val
varsIn.close()
try:
os.unlink(varsFile)
except OSError:
pass
# set up virtualenv if needed
if 'VIRTUAL_ENV' in os.environ:
activateFile = '%s/bin/activate_this.py' % 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>
<head><title>%s</title></head>
<body><h1>%s</h1></body>
</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__))
getEnvironmentFromSourceMe(thisDir)
if os.path.exists(os.path.join(thisDir, 'DOWN_FOR_MAINTENANCE')):
application = downForMaintenance
else:
from django.core.handlers.wsgi import WSGIHandler
application = WSGIHandler()