Edgewall Software

Changeset 2015

Show
Ignore:
Timestamp:
07/23/2005 08:33:22 AM (3 years ago)
Author:
mgood
Message:

Add environment settings for CGI and FastCGI equivalent to the mod_python multi-project settings:

  • TracEnvParentDir -- TRAC_ENV_PARENT_DIR
  • TracEnvIndexTemplate -- TRAC_ENV_INDEX_TEMPLATE
  • TracTemplateVars -- TRAC_TEMPLATE_VARS
Location:
trunk/trac/web
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • trunk/trac/web/cgi_frontend.py

    r1754 r2015  
    33# Copyright (C) 2005 Edgewall Software 
    44# Copyright (C) 2005 Christopher Lenz <cmlenz@gmx.de> 
     5# Copyright (C) 2005 Matthew Good <trac@matt-good.net> 
    56# 
    67# Trac is free software; you can redistribute it and/or 
     
    1920# 
    2021# Author: Christopher Lenz <cmlenz@gmx.de> 
     22# Author: Matthew Good <trac@matt-good.net> 
    2123 
    22 from trac.env import open_environment 
    23 from trac.web.main import Request, dispatch_request, send_pretty_error 
     24from trac.web.main import Request, dispatch_request, send_pretty_error, \ 
     25                          get_environment 
    2426 
    2527import cgi 
     
    5254 
    5355        self.cgi_location = self.__environ.get('SCRIPT_NAME') 
     56        self.idx_location = self.cgi_location 
     57 
     58        self.path_info = self.__environ.get('PATH_INFO', '') 
     59 
     60        if 'TRAC_ENV_PARENT_DIR' in os.environ and self.path_info: 
     61            env_path = '/' + self.path_info.split('/', 2)[1] 
     62            self.path_info = self.path_info[len(env_path):] 
     63            self.cgi_location += env_path 
     64 
    5465 
    5566 
     
    97108    locale.setlocale(locale.LC_ALL, '') 
    98109 
    99     env = open_environment() 
    100110    req = CGIRequest() 
     111    env = get_environment(req, os.environ, threaded=False) 
     112 
     113    if not env: 
     114        return 
    101115 
    102116    try: 
    103         dispatch_request(os.getenv('PATH_INFO', ''), req, env) 
     117        dispatch_request(req.path_info, req, env) 
    104118    except Exception, e: 
    105119        send_pretty_error(e, env, req) 
  • trunk/trac/web/fcgi_frontend.py

    r1754 r2015  
    33# Copyright (C) 2005 Edgewall Software 
    44# Copyright (C) 2005 Christopher Lenz <cmlenz@gmx.de> 
     5# Copyright (C) 2005 Matthew Good <trac@matt-good.net> 
    56# 
    67# Trac is free software; you can redistribute it and/or 
     
    1920# 
    2021# Author: Christopher Lenz <cmlenz@gmx.de> 
     22# Author: Matthew Good <trac@matt-good.net> 
    2123 
    2224from trac.web.cgi_frontend import * 
     25from trac.web.main import RequestDone, get_environment 
     26from trac.util import TracError, enum, href_join 
     27 
    2328import _thfcgi, locale, sys 
    2429 
     
    3742 
    3843def _handler(_req, _env, _fieldStorage): 
    39       env = open_environment() 
    4044      req = FCGIRequest(_env, _req.stdin, _req.out, _fieldStorage) 
     45      env = get_environment(req, os.environ) 
     46 
     47      if not env: 
     48          return 
    4149 
    4250      try:   
    43           dispatch_request(_env.get('PATH_INFO', ''), req, env) 
     51          dispatch_request(req.path_info, req, env) 
    4452      except Exception, e: 
    4553          send_pretty_error(e, env, req) 
  • trunk/trac/web/main.py

    r2013 r2015  
    33# Copyright (C) 2005 Edgewall Software 
    44# Copyright (C) 2005 Christopher Lenz <cmlenz@gmx.de> 
     5# Copyright (C) 2005 Matthew Good <trac@matt-good.net> 
    56# 
    67# Trac is free software; you can redistribute it and/or 
     
    1920# 
    2021# Author: Christopher Lenz <cmlenz@gmx.de> 
     22# Author: Matthew Good <trac@matt-good.net> 
    2123 
    2224import mimetypes 
     
    2527 
    2628from trac.core import * 
     29from trac.env import open_environment 
    2730from trac.perm import PermissionCache, PermissionError 
    28 from trac.util import escape, http_date, TRUE 
     31from trac.util import escape, http_date, TRUE, enum, href_join 
    2932from trac.web.href import Href 
    3033from trac.web.session import Session 
     34 
     35try: 
     36    import threading 
     37except ImportError: 
     38    import dummy_threading as threading 
    3139 
    3240 
     
    490498        req.write('\n') 
    491499        req.write(tb.getvalue()) 
     500 
     501def send_project_index(req, dir, options): 
     502    from trac.web.clearsilver import HDFWrapper 
     503 
     504    if 'TRAC_ENV_INDEX_TEMPLATE' in options: 
     505        tmpl_path, template = os.path.split(options['TRAC_ENV_INDEX_TEMPLATE']) 
     506 
     507        from trac.config import default_dir 
     508        req.hdf = HDFWrapper(loadpaths=[default_dir('templates'), tmpl_path]) 
     509 
     510        tmpl_vars = {} 
     511        if 'TRAC_TEMPLATE_VARS' in options: 
     512            for pair in options['TRAC_TEMPLATE_VARS'].split(','): 
     513                key, val = pair.split('=') 
     514                req.hdf[key] = val 
     515    else: 
     516        req.hdf = HDFWrapper() 
     517        template = req.hdf.parse('''<html> 
     518<head><title>Available Projects</title></head> 
     519<body><h1>Available Projects</h1><ul><?cs 
     520 each:project = projects ?><li><a href="<?cs 
     521  var:project.href ?>"><?cs var:project.name ?></a></li><?cs 
     522 /each ?></ul></body> 
     523</html>''') 
     524 
     525    try: 
     526        projects = [] 
     527        for ids, project in enum(os.listdir(dir)): 
     528            env_path = os.path.join(dir, project) 
     529            if not os.path.isdir(env_path): 
     530                continue 
     531            try: 
     532                env = open_environment(env_path) 
     533                projects.append({ 
     534                    'name': env.config.get('project', 'name'), 
     535                    'description': env.config.get('project', 'descr'), 
     536                    'href': href_join(req.idx_location, project) 
     537                }) 
     538            except TracError, e: 
     539                raise 
     540# FIXME how should this be done in a cross-frontend way? 
     541                #req.log_error('Error opening environment at %s: %s' 
     542                              #% (env_path, e)) 
     543        projects.sort(lambda x, y: cmp(x['name'], y['name'])) 
     544        req.hdf['projects'] = projects 
     545 
     546        req.display(template, response=200) 
     547    except RequestDone: 
     548        pass 
     549 
     550 
     551env_cache = {} 
     552env_cache_lock = threading.Lock() 
     553 
     554def get_environment(req, options, threaded=True): 
     555    global env_cache, env_cache_lock 
     556 
     557    if 'TRAC_ENV' in options: 
     558        env_path = options['TRAC_ENV'] 
     559    elif 'TRAC_ENV_PARENT_DIR' in options: 
     560        env_parent_dir = options['TRAC_ENV_PARENT_DIR'] 
     561        env_name = req.cgi_location.split('/')[-1] 
     562        env_path = os.path.join(env_parent_dir, env_name) 
     563        if not len(env_name) or not os.path.exists(env_path): 
     564            send_project_index(req, env_parent_dir, options) 
     565            return None 
     566    else: 
     567        raise TracError, \ 
     568              'Missing PythonOption "TracEnv" or "TracEnvParentDir". Trac ' \ 
     569              'requires one of these options to locate the Trac environment(s).' \ 
     570              + str(options) 
     571 
     572    if not threaded: 
     573        return open_environment(env_path) 
     574 
     575    env = None 
     576    env_cache_lock.acquire() 
     577    try: 
     578        if not env_path in env_cache: 
     579            env_cache[env_path] = open_environment(env_path) 
     580        env = env_cache[env_path] 
     581    finally: 
     582        env_cache_lock.release() 
     583    return env 
     584 
     585 
  • trunk/trac/web/modpython_frontend.py

    r2012 r2015  
    33# Copyright (C) 2004, 2005 Edgewall Software 
    44# Copyright (C) 2004, 2005 Christopher Lenz <cmlenz@gmx.de> 
     5# Copyright (C) 2005 Matthew Good <trac@matt-good.net> 
    56# 
    67# Trac is free software; you can redistribute it and/or 
     
    1920# 
    2021# Author: Christopher Lenz <cmlenz@gmx.de> 
     22# Author: Matthew Good <trac@matt-good.net> 
    2123 
    2224import locale 
     
    2426import os 
    2527import re 
    26 try: 
    27     import threading 
    28 except ImportError: 
    29     import dummy_threading as threading 
    3028 
    3129try: 
     
    3634from mod_python import apache, util 
    3735 
    38 from trac.env import open_environment 
    39 from trac.util import TracError, enum, href_join, http_date, rstrip 
     36from trac.util import http_date, rstrip 
    4037from trac.web.main import Request, RequestDone, dispatch_request, \ 
    41                           send_pretty_error 
     38                          send_pretty_error, get_environment 
    4239 
    4340 
     
    172169                             {}, None, {})) 
    173170 
    174  
    175 def send_project_index(req, mpr, dir, options): 
    176     from trac.web.clearsilver import HDFWrapper 
    177  
    178     if 'TracEnvIndexTemplate' in options: 
    179         # Custom project listing template configured 
    180         tmpl_path, template = os.path.split(options['TracEnvIndexTemplate']) 
    181  
    182         from trac.config import default_dir 
    183         mpr.hdf = HDFWrapper(loadpaths=[default_dir('templates'), tmpl_path]) 
    184  
    185         tmpl_vars = {} 
    186         if 'TracTemplateVars' in options: 
    187             pairs = options['TracTemplateVars'].split(',') 
    188             for pair in pairs: 
    189                 key,val = pair.split('=') 
    190                 mpr.hdf[key] = val 
    191  
    192     else: 
    193         # Use the default project listing template 
    194         mpr.hdf = HDFWrapper() 
    195         template = mpr.hdf.parse("""<html> 
    196 <head><title>Available Projects</title></head> 
    197 <body><h1>Available Projects</h1><ul><?cs 
    198  each:project = projects ?><li><a href="<?cs 
    199   var:project.href ?>"><?cs var:project.name ?></a></li><?cs 
    200  /each ?></ul></body> 
    201 </html>""") 
    202  
    203     try: 
    204         projects = [] 
    205         for idx, project in enum(os.listdir(dir)): 
    206             env_path = os.path.join(dir, project) 
    207             if not os.path.isdir(env_path): 
    208                 continue 
    209             try: 
    210                 env = open_environment(env_path) 
    211                 projects.append({ 
    212                     'name': env.config.get('project', 'name'), 
    213                     'description': env.config.get('project', 'descr'), 
    214                     'href': href_join(mpr.idx_location, project) 
    215                 }) 
    216             except TracError, e: 
    217                 req.log_error('Error opening environment at %s: %s' 
    218                               % (env_path, e)) 
    219         projects.sort(lambda x, y: cmp(x['name'], y['name'])) 
    220         mpr.hdf['projects'] = projects 
    221         mpr.display(template, response=200) 
    222     except RequestDone: 
    223         pass 
    224  
    225 env_cache = {} 
    226 env_cache_lock = threading.Lock() 
    227  
    228 def get_environment(req, mpr, options): 
    229     global env_cache, env_cache_lock 
    230  
    231     if options.has_key('TracEnv'): 
    232         env_path = options['TracEnv'] 
    233     elif options.has_key('TracEnvParentDir'): 
    234         env_parent_dir = options['TracEnvParentDir'] 
    235         env_name = mpr.cgi_location.split('/')[-1] 
    236         env_path = os.path.join(env_parent_dir, env_name) 
    237         if len(env_name) == 0 or not os.path.exists(env_path): 
    238             send_project_index(req, mpr, env_parent_dir, options) 
    239             return None 
    240     else: 
    241         raise TracError, \ 
    242               'Missing PythonOption "TracEnv" or "TracEnvParentDir". Trac ' \ 
    243               'requires one of these options to locate the Trac environment(s).' 
    244  
    245     env = None 
    246     env_cache_lock.acquire() 
    247     try: 
    248         if not env_path in env_cache: 
    249             env_cache[env_path] = open_environment(env_path) 
    250         env = env_cache[env_path] 
    251     finally: 
    252         env_cache_lock.release() 
    253     return env 
     171def dict_translate(orig, *mappings): 
     172    result = {} 
     173    for src, dest in mappings: 
     174        if src in orig: 
     175            result[dest] = orig[src] 
     176    return result 
    254177 
    255178def handler(req): 
     
    265188 
    266189    mpr = ModPythonRequest(req, options) 
    267     env = get_environment(req, mpr, options) 
     190    env = get_environment(mpr, dict_translate(options, 
     191                ('TracEnv', 'TRAC_ENV'), 
     192                ('TracEnvParentDir', 'TRAC_ENV_PARENT_DIR'), 
     193                ('TracEnvIndexTemplate', 'TRAC_ENV_INDEX_TEMPLATE'), 
     194                ('TracTemplateVars', 'TRAC_TEMPLATE_VARS'))) 
    268195    if not env: 
    269196        return apache.OK