[mod_python] Getting the script name with publisher

Graham Dumpleton grahamd at dscpl.com.au
Wed Mar 1 17:06:28 EST 2006


Graham Dumpleton wrote ..
> Bart Whiteley wrote ..
> > Thanks.  I didn't know about __file__.  I how have this which seems to
> > work in all cases: 
> > 
> > def _baseScript(req):
> >     drlen = len(req.subprocess_env['DOCUMENT_ROOT'])
> >     if os.path.basename(__file__) == 'index.py':
> >         return os.path.dirname(__file__)[drlen:]
> >     else:
> >         return __file__[drlen:-3]
> 
> Will not work when files are in a user account served up by mod_userdir
> or where Alias directive is used to map in files outside of the directories
> under document root.
> 
> Trying to do what you want is actually quite hard and there are a lot of
> gotchas. When I get some time I'll try and respond properly.

While on this bit of code, this also will not work if Apache was run as
a user which had write access to the directory the files are in. This is
because in that case, Python will be able to write ".pyc" files to the
directory and thus the "__file__" attribute may actually have a ".pyc"
suffix and not ".py". Thus appropriate use of os.path.splitext() may be
in order rather than assuming that ".py" extension will always be used.

Graham


More information about the Mod_python mailing list