Skip to content

Instantly share code, notes, and snippets.

@glenfant
Last active December 19, 2015 03:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glenfant/5890964 to your computer and use it in GitHub Desktop.
Save glenfant/5890964 to your computer and use it in GitHub Desktop.
Make a wsgi script with zc.buildout (buildouthelpers.py)
WSGISCRIPT_TEMPLATE = """\
# -*- coding: utf-8 -*-
import sys
import os
sys.path[0:0] = [
{0}
]
_application = None
def application(environ, start_response):
global _application
# Potential app environment setup
if _application is None:
from {1} import {2} as _application
return _application(environ, start_response)
"""
def make_wsgi_script(recipe, buildout):
"""Build the script for Apache/mod_wsgi
"""
# Late import: zc.recipe.egg may not be installed when executing 1st
# function
from zc.recipe.egg.egg import Eggs
app_egg = recipe['egg']
wsgi_filepath = recipe['script']
app_mod, app_obj = recipe['app'].rsplit('.', 1) # 'a.b.c.d' -> 'a.b.c', 'd'
reqs, ws = Eggs(buildout, app_egg, recipe).working_set()
egg_paths = [pkg.location for pkg in ws]
src_egg_paths = ',\n'.join([" '{0}'".format(path) for path in egg_paths])
with open(wsgi_filepath, 'w') as fh:
fh.write(WSGISCRIPT_TEMPLATE.format(src_egg_paths, app_mod, app_obj))
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment