Ticket #3249: default_env.patch
| File default_env.patch, 3.8 KB (added by coderanger@…, 3 years ago) |
|---|
-
main.py
old new 225 225 environ.setdefault('trac.env_path', options.get('TracEnv')) 226 226 environ.setdefault('trac.env_parent_dir', 227 227 options.get('TracEnvParentDir')) 228 environ.setdefault('trac.default_env', 229 options.get('TracDefaultEnv')) 228 230 environ.setdefault('trac.env_index_template', 229 231 options.get('TracEnvIndexTemplate')) 230 232 environ.setdefault('trac.template_vars', … … 246 248 environ.setdefault('trac.env_path', os.getenv('TRAC_ENV')) 247 249 environ.setdefault('trac.env_parent_dir', 248 250 os.getenv('TRAC_ENV_PARENT_DIR')) 251 environ.setdefault('trac.default_env', 252 os.getenv('TRAC_DEFAULT_ENV')) 249 253 environ.setdefault('trac.env_index_template', 250 254 os.getenv('TRAC_ENV_INDEX_TEMPLATE')) 251 255 environ.setdefault('trac.template_vars', … … 265 269 if not env_path: 266 270 env_parent_dir = environ.get('trac.env_parent_dir') 267 271 env_paths = environ.get('trac.env_paths') 272 default_env = environ.get('trac.default_env') 268 273 if env_parent_dir or env_paths: 269 274 # The first component of the path is the base name of the 270 275 # environment 271 276 path_info = environ.get('PATH_INFO', '').lstrip('/').split('/') 272 277 env_name = path_info.pop(0) 278 fake_root = False 273 279 274 280 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 [] 280 290 281 291 # To make the matching patterns of request handlers work, we append 282 292 # 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) 285 299 environ['PATH_INFO'] = '/'.join([''] + path_info) 286 300 287 301 if env_parent_dir: … … 290 304 env_path = get_environments(environ).get(env_name) 291 305 292 306 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'] 295 315 296 316 if not env_path: 297 317 raise EnvironmentError('The environment options "TRAC_ENV" or '
