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.v9

File tg_fastcgi.fcgi.v9, 3.7 KB (added by sdelatorre, 6 years ago)

tg_fastcgi.fcgi script for TG v. 9

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
19import cherrypy
20import sys
21import os
22from os.path import *
23import pkg_resources
24import turbogears
25
26pkg_resources.require("TurboGears")
27
28# -- START USER EDIT SECTION
29# -- Users must edit this section --
30code_dir = ''                               # (Required) The base directory of the TG app code.
31root_class_name = ''                        # (Required) The fully qualified Root class name.
32project_module_name = 'PROJECTNAME.config'  # (Required) The config module name. Replace
33                                            # PROJECTNAME with your project's name (e.g. wiki20).
34log_dir = ''                                # (Optional) The log directory. Default = code_dir.
35# -- END USER EDIT SECTION
36
37
38def tg_init():
39    """ Checks for the required data and initializes the application. """
40
41    global code_dir
42    global root_class_name
43    global log_dir
44    global project_module_name
45    last_mark = 0
46
47    # Input checks
48    if not code_dir or not isdir(code_dir):
49        raise ValueError("""The code directory setting is missing.
50                            The fastcgi code will be unable to find
51                            the TG code without this setting.""")
52
53    if not root_class_name:
54        raise ValueError("""The fully qualified root class name must
55                            be provided.""")
56
57    last_mark = root_class_name.rfind('.')
58   
59    if (last_mark < 1) or (last_mark + 1) == len(root_class_name):
60        raise ValueError("""The user-defined class name is invalid.
61                            Please make sure to include a fully
62                            qualified class name for the root_class
63                            value (e.g. wiki20.controllers.Root).""")   
64
65    sys.path.append(code_dir)
66
67    # Change the directory so the TG log file will not be written to the
68    # web app root.
69    if log_dir and isdir(log_dir):
70        os.chdir(log_dir)
71    else:
72        os.chdir(code_dir)
73        log_dir = code_dir
74
75    sys.stdout = open(join(log_dir, 'stdout.log'),'a')
76    sys.stderr = open(join(log_dir, 'stderr.log'),'a')       
77
78    if exists(join(code_dir, "setup.py")):
79        turbogears.update_config(configfile=join(code_dir, "devcfg.py"),modulename=project_module_name)
80    else:
81        turbogears.update_config(configfile=join(code_dir, "prodcfg.py"),modulename=project_module_name)
82
83    # Set environment to production to disable auto-reload and
84    # add virutal path information.
85    cherrypy.config.update({
86        'global': {'server.environment': 'production'}})
87
88    # Parse out the root class information for Cherrypy Root class.
89    package_name = root_class_name[:last_mark]
90    class_name = root_class_name[last_mark+1:]
91    exec('from %s import %s as Root' % (package_name, class_name))
92    cherrypy.root = Root()
93
94# Main section -
95# Initialize the application, then start the server.
96tg_init()
97
98from fcgi import WSGIServer
99cherrypy.server.start(initOnly=True, serverClass=None)
100
101from cherrypy._cpwsgi import wsgiApp
102WSGIServer(application=wsgiApp).run()