Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

TurboGearsOnDreamHost: tg_fastcgi.fcgi.2.v0.9a4

File tg_fastcgi.fcgi.2.v0.9a4, 4.5 KB (added by tic, 6 years ago)

Fixed VirtualPathFilter?-supporting version for 0.9a4

Line 
1#!/home/<user>/bin/python/bin/python
2#
3# File name: tg_fastcgi.fcgi
4#
5# This module provides the glue for running TurboGears applications behind
6# FastCGI-enabled web servers. The code in this module depends on the fastcgi
7# module downloadable from here:
8#
9# http://www.saddi.com/software/py-lib/py-lib/fcgi.py
10#
11# NOTE: The fcgi.py file needs to be placed in a location that is on the
12# system path, such as the same the directory as the tg_fastcgi.py file
13# or in the base directory of the TG app code.
14#
15# To configure this module, please edit the three variables in the "USER EDIT
16# SECTION" before starting the TG application.  Also remember to edit the
17# top of this file with the correct Python installation information.
18#
19# 2006-04-17
20# ----------
21# * Updated for 0.9a4 by Mikael Jansson <mail@mikael.jansson.be> for changes in
22#   config files.
23# * Removed hard TABs
24#
25
26import cherrypy
27import sys
28import os
29from os.path import *
30import pkg_resources
31import turbogears
32
33pkg_resources.require("TurboGears")
34
35# -- START USER EDIT SECTION
36# -- Users must edit this section --
37code_dir = ''                               # (Required) The base directory of the TG app code.
38root_class_name = ''                        # (Required) The fully qualified Root class name.
39project_module_name = 'PROJECTNAME.config'  # (Required) The config module name. Replace
40                                            # PROJECTNAME with your project's name (e.g. wiki20).
41log_dir = ''                                # (Optional) The log directory. Default = code_dir.
42# -- END USER EDIT SECTION
43
44class VirtualPathFilter(object):
45    def on_start_resource(self):
46        if not cherrypy.config.get('virtual_path_filter.on', False):
47        return
48    prefix = cherrypy.config.get('virtual_path_filter.prefix', '')
49    if not prefix:
50        return
51
52    path = cherrypy.request.object_path
53    if path == prefix:
54        path = '/'
55    elif path.startswith(prefix):
56        path = path[len(prefix):]
57    else:
58        raise cherrypy.NotFound(path)
59    cherrypy.request.object_path = path
60
61
62def tg_init():
63    """ Checks for the required data and initializes the application. """
64
65    global code_dir
66    global root_class_name
67    global log_dir
68    global project_module_name
69    last_mark = 0
70
71    # Input checks
72    if not code_dir or not isdir(code_dir):
73        raise ValueError("""The code directory setting is missing.
74                            The fastcgi code will be unable to find
75                            the TG code without this setting.""")
76
77    if not root_class_name:
78        raise ValueError("""The fully qualified root class name must
79                            be provided.""")
80
81    last_mark = root_class_name.rfind('.')
82   
83    if (last_mark < 1) or (last_mark + 1) == len(root_class_name):
84        raise ValueError("""The user-defined class name is invalid.
85                            Please make sure to include a fully
86                            qualified class name for the root_class
87                            value (e.g. wiki20.controllers.Root).""")   
88
89    sys.path.append(code_dir)
90
91    # Change the directory so the TG log file will not be written to the
92    # web app root.
93    if log_dir and isdir(log_dir):
94        os.chdir(log_dir)
95    else:
96        os.chdir(code_dir)
97        log_dir = code_dir
98
99    sys.stdout = open(join(log_dir, 'stdout.log'),'a')
100    sys.stderr = open(join(log_dir, 'stderr.log'),'a')       
101
102    if exists(join(code_dir, "setup.py")):
103        turbogears.update_config(configfile=join(code_dir, "devcfg.py"),modulename=project_module_name)
104    else:
105        turbogears.update_config(configfile=join(code_dir, "prodcfg.py"),modulename=project_module_name)
106
107    # Set environment to production to disable auto-reload and
108    # add virutal path information.
109    cherrypy.config.update({
110        'global': {'server.environment': 'production'},
111    '/' : { 'virtual_path_filter.on' : True,
112            'virtual_path_filter.prefix' : '/tg_fastcgi.fcgi' }
113        })
114
115    # Parse out the root class information for Cherrypy Root class.
116    package_name = root_class_name[:last_mark]
117    class_name = root_class_name[last_mark+1:]
118    exec('from %s import %s as Root' % (package_name, class_name))
119    Root._cp_filters = [VirtualPathFilter()]
120    cherrypy.root = Root()
121
122# Main section -
123# Initialize the application, then start the server.
124tg_init()
125
126from fcgi import WSGIServer
127cherrypy.server.start(initOnly=True, serverClass=None)
128
129from cherrypy._cpwsgi import wsgiApp
130WSGIServer(application=wsgiApp).run()