Re: [Tutor] Fwd: RE: How to source a shell script through python to set the environment variables.
On 26/02/16 04:49, Aneeque Khan wrote: > The problem is that virtually any method of running a shell script > will involve starting a separate process and setting the variables in > that process' environment. So far as I can tell the only way to do > what you want would be to run your shell script first then launch your > python script from the shell script. That would mean breaking your > current python script in two, and if you do a lot of work prior to > launching the shell, that's probably not practical. Is it possible to adopt the approach described above? Can you split your Python processing into two(or more) parts, one before and one after the shell script? That will be the easiest way I suspect. > I have recently started with python, can you direct me how we can achieve > this by parsing the shell as a text file. While parsing a shell script is not impossible, especially since you are only looking to trap the environment variables it's far from trivial. Especially for a beginner. I'd treat that as a last resort. Also if the shell script does more than just set the variables its not going to work since you still need to run the script. Do you have any control of the shell script content? Or is that generated outside of your influence? If you can control it you might be able to get the script to dump it's environment vars to a temp file and read that. The next question is what do you plan on doing with these once you have them? They won't be of any value to the shell script or any other script unless you set them again locally? Or are the just path locators? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fwd: RE: How to source a shell script through python to set the environment variables.
Hi guys, any suggestions on this. -Original Message- From: Tutor [mailto:tutor-bounces+aneeque.khan=ericsson@python.org] On Behalf Of Alan Gauld Sent: Tuesday, February 23, 2016 5:39 PM To: tutor Subject: [Tutor] Fwd: RE: How to source a shell script through python to set the environment variables. Forwarding to list. Please always use ReplyAll (or replyList) when responding to tutor. Forwarded Message Subject:RE: [Tutor] How to source a shell script through python to set the environment variables. Date: Tue, 23 Feb 2016 11:14:02 + From: Aneeque Khan To: Alan Gauld Please see my comments with [AK] -Original Message- From: Tutor [mailto:tutor-bounces+aneeque.khan=ericsson@python.org] On Behalf Of Alan Gauld Sent: Tuesday, February 23, 2016 4:16 PM To: tutor@python.org Subject: Re: [Tutor] How to source a shell script through python to set the environment variables. On 23/02/16 07:29, Aneeque Khan wrote: > env_test.sh looks like this :- > #!/bin/bash > export OS_AK="Aneeque" > export OS_KHAN="KHAN" > > echo "Please enter your Stack Password: " > read -sr OS_PWD_INPUT > export OS_PWD=$OS_PWD_INPUT > > Now I want to execute the above script through python file named > "ak_test.py", to set these variables and access them further in the code. Can I first ask why you are trying to do it via a shell script rather than using Python directly? Is this part of a bigger workflow? [AK] :- this is the part of bigger workflow, where I get the shell script as a output of some processing. The problem is that virtually any method of running a shell script will involve starting a separate process and setting the variables in that process' environment. So far as I can tell the only way to do what you want would be to run your shell script first then launch your python script from the shell script. That would mean breaking your current python script in two, and if you do a lot of work prior to launching the shell, that's probably not practical. [AK] :- I can`t set the variables from outside the python script as our workflow requires to process no. of such shell scripts. We have to process each shell script to set the environment and work on further. You can assume that overall workflow have large no. of users each having its own values for certain environment variables. While performing any task for that particular user we require to set the environment first. > def update_env(script_path): >if "--child" in sys.argv: # executed in the child environment > pprint(dict(os.environ)) >else: > python, script = quote(sys.executable), quote(sys.argv[0]) > os.execl("/bin/bash", "/bin/bash", "-c", "source %s; %s %s > --child" % (script_path, python, script)) > > with this approach some variables set while others not. That surprises me since calling execl() overwrites the calling process with the called process, so I would not expect you to see anything in your calling script. How are you testing the results? Are you checking os.environ? Or are you using os.getenv()? [AK] :- I am checking like this :- os.getenv("OS_AK") os.getenv("OS_PWD") and getting None as the result. > Another approach used :- > def update_env2(script): > #pipe1 = subprocess.Popen(". %s; env -0" % script, > stdout=subprocess.PIPE, shell=True, executable="/bin/bash") > pipe1 = subprocess.Popen(". %s; env" % script, > stdout=subprocess.PIPE, shell=True, executable="/bin/bash") > > output = pipe1.communicate()[0] > #env = dict((line.split("=", 1) for line in output.splitlines())) > env = dict((line.split("=", 1) for line in output.split('\0'))) > > os.environ.update(env) While this could work you are not really reading the environment variables you are simply reading the stdout. You could achieve the same by parsing the script as a text file. So can you explain why you need to load these values from a shell script rather than setting os.environ directly? Maybe we can solve the bigger issue. [AK] :- In workflow neither variable names nor their values are known in advance. We only came to know about these at run time through the generated shell scripts. I have recently started with python, can you direct me how we can achieve this by parsing the shell as a text file. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http
[Tutor] Fwd: RE: How to source a shell script through python to set the environment variables.
Forwarding to list. Please always use ReplyAll (or replyList) when responding to tutor. Forwarded Message Subject:RE: [Tutor] How to source a shell script through python to set the environment variables. Date: Tue, 23 Feb 2016 11:14:02 + From: Aneeque Khan To: Alan Gauld Please see my comments with [AK] -Original Message- From: Tutor [mailto:tutor-bounces+aneeque.khan=ericsson@python.org] On Behalf Of Alan Gauld Sent: Tuesday, February 23, 2016 4:16 PM To: tutor@python.org Subject: Re: [Tutor] How to source a shell script through python to set the environment variables. On 23/02/16 07:29, Aneeque Khan wrote: > env_test.sh looks like this :- > #!/bin/bash > export OS_AK="Aneeque" > export OS_KHAN="KHAN" > > echo "Please enter your Stack Password: " > read -sr OS_PWD_INPUT > export OS_PWD=$OS_PWD_INPUT > > Now I want to execute the above script through python file named > "ak_test.py", to set these variables and access them further in the code. Can I first ask why you are trying to do it via a shell script rather than using Python directly? Is this part of a bigger workflow? [AK] :- this is the part of bigger workflow, where I get the shell script as a output of some processing. The problem is that virtually any method of running a shell script will involve starting a separate process and setting the variables in that process' environment. So far as I can tell the only way to do what you want would be to run your shell script first then launch your python script from the shell script. That would mean breaking your current python script in two, and if you do a lot of work prior to launching the shell, that's probably not practical. [AK] :- I can`t set the variables from outside the python script as our workflow requires to process no. of such shell scripts. We have to process each shell script to set the environment and work on further. You can assume that overall workflow have large no. of users each having its own values for certain environment variables. While performing any task for that particular user we require to set the environment first. > def update_env(script_path): >if "--child" in sys.argv: # executed in the child environment > pprint(dict(os.environ)) >else: > python, script = quote(sys.executable), quote(sys.argv[0]) > os.execl("/bin/bash", "/bin/bash", "-c", "source %s; %s %s > --child" % (script_path, python, script)) > > with this approach some variables set while others not. That surprises me since calling execl() overwrites the calling process with the called process, so I would not expect you to see anything in your calling script. How are you testing the results? Are you checking os.environ? Or are you using os.getenv()? [AK] :- I am checking like this :- os.getenv("OS_AK") os.getenv("OS_PWD") and getting None as the result. > Another approach used :- > def update_env2(script): > #pipe1 = subprocess.Popen(". %s; env -0" % script, > stdout=subprocess.PIPE, shell=True, executable="/bin/bash") > pipe1 = subprocess.Popen(". %s; env" % script, > stdout=subprocess.PIPE, shell=True, executable="/bin/bash") > > output = pipe1.communicate()[0] > #env = dict((line.split("=", 1) for line in output.splitlines())) > env = dict((line.split("=", 1) for line in output.split('\0'))) > > os.environ.update(env) While this could work you are not really reading the environment variables you are simply reading the stdout. You could achieve the same by parsing the script as a text file. So can you explain why you need to load these values from a shell script rather than setting os.environ directly? Maybe we can solve the bigger issue. [AK] :- In workflow neither variable names nor their values are known in advance. We only came to know about these at run time through the generated shell scripts. I have recently started with python, can you direct me how we can achieve this by parsing the shell as a text file. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor