Edgewall Software

Ticket #3249: default_env.patch

File default_env.patch, 3.8 KB (added by coderanger@…, 3 years ago)
  • main.py

    old new  
    225225        environ.setdefault('trac.env_path', options.get('TracEnv')) 
    226226        environ.setdefault('trac.env_parent_dir', 
    227227                           options.get('TracEnvParentDir')) 
     228        environ.setdefault('trac.default_env', 
     229                           options.get('TracDefaultEnv')) 
    228230        environ.setdefault('trac.env_index_template', 
    229231                           options.get('TracEnvIndexTemplate')) 
    230232        environ.setdefault('trac.template_vars', 
     
    246248        environ.setdefault('trac.env_path', os.getenv('TRAC_ENV')) 
    247249        environ.setdefault('trac.env_parent_dir', 
    248250                           os.getenv('TRAC_ENV_PARENT_DIR')) 
     251        environ.setdefault('trac.default_env', 
     252                           os.getenv('TRAC_DEFAULT_ENV')) 
    249253        environ.setdefault('trac.env_index_template', 
    250254                           os.getenv('TRAC_ENV_INDEX_TEMPLATE')) 
    251255        environ.setdefault('trac.template_vars', 
     
    265269    if not env_path: 
    266270        env_parent_dir = environ.get('trac.env_parent_dir') 
    267271        env_paths = environ.get('trac.env_paths') 
     272        default_env = environ.get('trac.default_env') 
    268273        if env_parent_dir or env_paths: 
    269274            # The first component of the path is the base name of the 
    270275            # environment 
    271276            path_info = environ.get('PATH_INFO', '').lstrip('/').split('/') 
    272277            env_name = path_info.pop(0) 
     278            fake_root = False 
    273279 
    274280            if not env_name: 
    275                 # No specific environment requested, so render an environment 
    276                 # index page 
    277                 send_project_index(environ, start_response, env_parent_dir, 
    278                                    env_paths) 
    279                 return [] 
     281                if default_env: 
     282                    env_name = default_env 
     283                    fake_root = True 
     284                else: 
     285                    # No specific environment requested, so render an environment 
     286                    # index page 
     287                    send_project_index(environ, start_response, env_parent_dir, 
     288                                       env_paths) 
     289                    return [] 
    280290 
    281291            # To make the matching patterns of request handlers work, we append 
    282292            # the environment name to the `SCRIPT_NAME` variable, and keep only 
    283             # the remaining path in the `PATH_INFO` variable. 
    284             environ['SCRIPT_NAME'] = Href(environ['SCRIPT_NAME'])(env_name) 
     293            # the remaining path in the `PATH_INFO` variable. Store the originals 
     294            # In case we need to revert them below 
     295            original_script_name = environ['SCRIPT_NAME'] 
     296            original_path_info = environ['PATH_INFO'] 
     297            if not fake_root: 
     298                environ['SCRIPT_NAME'] = Href(environ['SCRIPT_NAME'])(env_name) 
    285299            environ['PATH_INFO'] = '/'.join([''] + path_info) 
    286300 
    287301            if env_parent_dir: 
     
    290304                env_path = get_environments(environ).get(env_name) 
    291305 
    292306            if not env_path or not os.path.isdir(env_path): 
    293                 start_response('404 Not Found', []) 
    294                 return ['Environment not found'] 
     307                if default_env: 
     308                    # Reset these variable so we point to the default env 
     309                    environ['SCRIPT_NAME'] = original_script_name 
     310                    environ['PATH_INFO'] = original_path_info 
     311                    env_path = os.path.join(env_parent_dir, default_env) 
     312                else: 
     313                    start_response('404 Not Found', []) 
     314                    return ['Environment not found'] 
    295315 
    296316    if not env_path: 
    297317        raise EnvironmentError('The environment options "TRAC_ENV" or '