On Tue, 2009-02-10 at 10:56 +0000, Alan wrote:
>  Hi list,
> 
> 
> My problem is that: since when using apache + mod_wsgi I got a neutral
> env for my server (e.g. PATH=/usr/bin:/bin) but in the end what I need
> is all the envs that is in a particular $HOME/.bashrc file in order to
> when server spawns a program (that may not be in the neutral PATH and
> besides need some extra env parameters) it can run.
>  
> 
> 
> So what I was trying is to find a python module or code that would do
> something like ". $HOME/basrc" inside my python code and before
> executing my binary program in order to pass to it the particular
> user's environment.

The concept of $HOME or "user" doesn't really make sense for Apache.
Well, it does, but the user running the Apache process will typically
not have a home directory (or even be a login-enabled account). That's
one clue that this probably isn't a neat solution.

Another clue is that .bashrc is for Bash shells, not arbitrary scripts.
It might well contain some information that is appropriate for other
process environments, but, if so, factor out that stuff into a separate
file. Don't try to use a shell-specific script for general process
environments -- it will cause trouble down the road.


> My program is called with os.spawnvp(os.P_NOWAIT, program, args) and a
> simple os.system(". $HOME/basrc") before spawning it doesn't work.
> 
> 
> What works is to do os.putenv('PATH', 'blabla'), but then I have to do
> it for every key in my bashrc file (not only silly but
> unmaintainable).

This could be made very easily maintainable. If you're only setting
environment variables of the form "FOO=blah", then put them into a file
in that form and then, in Python:

        for line in open('environ-vars.txt'):
           line = line.strip()
           var, value = line.split('=', 1)
           os.putenv(var, value)
        
Game over.

Alternatively read those values into a dictionary and use spawnvpe() to
pass in the environment specifically (rather than also changing the
local process environment).

In short, I think that trying to use the shell to set up the environment
for something that is not a shell process is a little fragile. I realise
you're trying to get maximum reuse, but the ongoing costs in debugging
hassle and maintenance will outweigh the temporary benefits.

You could even exploit the fact that a file with lines of the form
"FOO=blah"is both valid shell and valid Python, so you could import as a
Python module and source it from your .bashrc to set shell variables
(that's also slightly fragile, though, since you have to keep it as
valid shell and Python. So I'd recommend the first route for reading it.

Regards,
Malcolm


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to