Created
August 12, 2012 00:42
-
-
Save philipn/3328228 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# This is an example .wsgi file for serving up localwiki inside of a | |
# custom virtualenv. | |
# | |
############################################################# | |
# CHANGE THIS LINE to the absolute path of the virtualenv: | |
############################################################# | |
VIRTUAL_ENV_PATH = '/home/philip/projects/py_envs/localwiki' | |
import os | |
import sys | |
# WSGI has undefined behavior for stdout. This fixes issues with some external | |
# libraries printing to sys.stdout. | |
sys.stdout = sys.stderr | |
activate_this = os.path.join(VIRTUAL_ENV_PATH, 'bin/activate_this.py') | |
execfile(activate_this, dict(__file__=activate_this)) | |
# Add virtualenv path. | |
from sapling import manage | |
# Add local apps path. | |
apps_path = os.path.abspath(os.path.join(manage.__file__, '..')) | |
if apps_path not in sys.path: | |
sys.path.append(apps_path) | |
def make_tls_property(default=None): | |
"""Creates a class-wide instance property with a thread-specific value.""" | |
class TLSProperty(object): | |
def __init__(self): | |
from threading import local | |
self.local = local() | |
def __get__(self, instance, cls): | |
if not instance: | |
return self | |
return self.value | |
def __set__(self, instance, value): | |
self.value = value | |
def _get_value(self): | |
return getattr(self.local, 'value', default) | |
def _set_value(self, value): | |
self.local.value = value | |
value = property(_get_value, _set_value) | |
return TLSProperty() | |
hostmap = {} | |
class Site(object): | |
def __init__(self, env=None, hosts=None): | |
global hostmap | |
self.env = env | |
self.settings_path = os.path.join(self.env, | |
'share', 'localwiki', 'conf', 'localsettings.py') | |
for host in hosts: | |
hostmap[host] = self | |
self.init_settings() | |
def init_settings(self): | |
locals_dict = {} | |
# By running execfile() we can dump the value of the settings into | |
# locals_dict | |
execfile(self.settings_path, globals(), locals_dict) | |
self.localsettings = locals_dict | |
# The main site's localsettings *must* contain all of the variables we | |
# want to change in all sites' settings. | |
main = Site( | |
env='/home/philip/projects/py_envs/localwiki/', | |
hosts=['localhost:8000'] | |
) | |
one = Site( | |
env='/home/philip/localwiki/deploy/one/', | |
hosts=['127.0.0.1:8000'] | |
) | |
from django.core.handlers.wsgi import WSGIHandler | |
os.environ['DJANGO_SETTINGS_MODULE'] = 'sapling.settings' | |
from django.conf import settings | |
settings.SITE_ID # force evaluation of lazy settings | |
original_settings = {} | |
for k in main.localsettings: | |
try: | |
original_settings[k] = getattr(settings, k) | |
setattr(settings.__dict__['_wrapped'].__class__, k, make_tls_property()) | |
except AttributeError: | |
pass | |
class Application(WSGIHandler): | |
def __call__(self, environ, start_response): | |
if environ['HTTP_HOST'] in hostmap: | |
this_site = hostmap[environ['HTTP_HOST']] | |
from django.conf import settings | |
settings.SITE_ID | |
#print 'before', settings.DATABASES | |
#setattr(settings, 'GLOBAL_LICENSE_NOTE', environ['HTTP_HOST']) | |
# We only set values that are found in main's settings | |
for k in main.localsettings: | |
v = this_site.localsettings.get(k, None) | |
if v is None: | |
print "NOT FOUND", k | |
v = original_settings[k] | |
#print "setting", k, v | |
#import pdb | |
#pdb.set_trace() | |
#o = make_tls_property() | |
#o = v | |
#setattr(settings.__dict__['_wrapped'].__class__, k, o) | |
#setattr(settings.__dict__['_wrapped'].__class__, k, v) | |
print "setting", k, v | |
setattr(settings, k, v) | |
#print 'after', settings.DATABASES | |
return super(Application, self).__call__(environ, start_response) | |
app = Application() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment