2

I have an apache2 server with active mod-wsgi, but I can't get the environment variable PYTHON_EGG_CACHE.
Here the important lines out of my virtualhost.conf:

    DAV svn
    SVNParentPath /var/svn
    SVNListParentPath Off

    WSGIProcessGroup sites
    WSGIApplicationGroup %{GLOBAL}
    SetEnv PYTHON_EGG_CACHE /var/trac/eggs

    AuthType Basic
    AuthName "Restricted SVN"
    AuthBasicProvider wsgi
    WSGIAuthUserScript /var/trac/cgi-bin/acctmgr_auth.wsgi
    Require valid-user

And here the acctmgr_auth.wsgi:

import os, sys
os.environ['PYTHON_EGG_CACHE'] = PYTHON_EGG_CACHE

from trac.env import open_environment, Environment

acct_mgr = None

def check_password(environ, user, password):
    global acct_mgr

    # Try loading the env from the global cache, addit it if needed
    environ['PYTHON_EGG_CACHE']
    env = open_environment(environ['trac.env_path'], use_cache=True)

    if acct_mgr is None:
        from acct_mgr.api import AccountManager
        acct_mgr = AccountManager

    if acct_mgr(env).check_password(user, password):
        return True
    else:
        return False

def groups_for_user(environ, user):
    return ['']

The single environ['PYTHON_EGG_CACHE'] is just a test, but I get a

KeyError: 'PYTHON_EGG_CACHE'

.

I also tried following, but I get the same error as above:

        RewriteCond ${lowercase:%{REQUEST_URI}} ^/svn/([^/]+)
        RewriteRule . - [E=trac.svn_path:/var/trac/envs/%1]

What is wrong?

Thanks for any advices.

EDIT: After some resarch I think I found the problem. The mod_dav is configured for the location /svn and it processes the request immediately, so the rewritecond and rewriterule will be ignored.
Is there any possibility to set environment variables with mod_dav?

CSchulz
  • 233
  • 3
  • 20

3 Answers3

4

This is how I did it on my django site, where the path is set in the apache file (as MY_PATH). Notice that I copy it into the os.environ variable.

import os, sys, django.core.handlers.wsgi
_application = django.core.handlers.wsgi.WSGIHandler()

def application(environ, start_response):

    path = environ['MY_PATH']
    if path not in sys.path:
        sys.path.append(path)


    os.environ['MY_PATH'] = environ['MY_PATH']
    from django.conf import settings
    settings.DEBUG = True

    return _application(environ, start_response)

Since I copied the environment variable to the os.environ, I can now use it (for example) in the settings file. Note that I have set all the os.environ variables before including the settings. In the settings I get it like so:

import os
PATH = os.environ['MY_PATH']
Marius
  • 353
  • 4
  • 10
3

SetEnv usually only sets process environment variables when using CGI. For mod_wsgi, SetEnv variables go into per request WSGI environment and not the process environment.
Thus they aren't accessible from global scope in WSGI script and even in the application callable, you have to access them from environ dictionary passed as argument.

CSchulz
  • 233
  • 3
  • 20
Graham Dumpleton
  • 5,990
  • 2
  • 20
  • 19
1

I found a solution by using the content of environ['REQUEST_URI'] and check it in the python file.
More details can you find here: http://trac-hacks.org/ticket/8525

CSchulz
  • 233
  • 3
  • 20