Re: How to capture environment state after running a shell script.

2007-03-14 Thread Gerard Flanagan
On Mar 13, 7:54 pm, [EMAIL PROTECTED] wrote:
 On Mar 13, 5:57 am, Gerard Flanagan [EMAIL PROTECTED] wrote:

  Hello,

  I have a third party shell script which updates multiple environment
  values, and I want to investigate (and ultimately capture to python)
  the environment state after the script has run.

 First close the input so that the (sub) process
 knows to terminate and flush the output.  Then,
 you can read from the output:

 import subprocess
 import popen2

 p = subprocess.Popen([/bin/sh], stdin=subprocess.PIPE,
 stdout=subprocess.PIPE)

 p.stdin.write(env -i FOO=BAR\n)
 p.stdin.close()
 status = p.wait()
 ret = p.stdout.readlines()
 p.stdout.close()


 print ret


Perfect! Works a charm. Thanks for helping me out.

Gerard


-- 
http://mail.python.org/mailman/listinfo/python-list


How to capture environment state after running a shell script.

2007-03-13 Thread Gerard Flanagan
Hello,

I have a third party shell script which updates multiple environment
values, and I want to investigate (and ultimately capture to python)
the environment state after the script has run. But running the script
as a child process only sets values for that process, which are lost
after execution.  So I thought I could simply tack on an 'env' command
line to the script input lines as shown below. However, using
subprocess.Popen gives the error shown (even though the docs say that
any file object may be used for stdin), and using popen2 hangs
indefinitely. I think I'm missing something basic, any advice? Or is
there a better approach?

(This is Python 2.4 and a Korn shell script on AIX Unix)

Thanks in advance.

from StringIO import StringIO
from subprocess import Popen
from popen2 import popen2

fname = '/opt/WebSphere/AppServer/bin/setupCmdLine.sh'

buf = StringIO()

f = open(fname, 'r')

try:
f.readline() #ignore shebang
for line in f:
buf.write(line)
finally:
f.close()

buf.write('\nenv\n')

buf.seek(0)

## first method ##
p = Popen('/bin/sh', stdin=buf)
print p.stdout.readlines()

Traceback (most recent call last):
  File scratch.py, line 36, in ?
p = Popen('/bin/sh', stdin=buf)
  File /usr/local/lib/python2.4/subprocess.py, line 534, in __init__
(p2cread, p2cwrite,
  File /usr/local/lib/python2.4/subprocess.py, line 830, in
_get_handles
p2cread = stdin.fileno()
AttributeError: StringIO instance has no attribute 'fileno'

## second method ##
cmdout, cmdin = popen2('/bin/sh')
for line in buf:
cmdin.write(line)

ret = cmdout.readlines()
cmdout.close()
cmdin.close()

print ret

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to capture environment state after running a shell script.

2007-03-13 Thread attn . steven . kuo
On Mar 13, 5:57 am, Gerard Flanagan [EMAIL PROTECTED] wrote:
 Hello,

 I have a third party shell script which updates multiple environment
 values, and I want to investigate (and ultimately capture to python)
 the environment state after the script has run. But running the script
 as a child process only sets values for that process, which are lost
 after execution.  So I thought I could simply tack on an 'env' command
 line to the script input lines as shown below. However, using
 subprocess.Popen gives the error shown (even though the docs say that
 any file object may be used for stdin), and using popen2 hangs
 indefinitely. I think I'm missing something basic, any advice? Or is
 there a better approach?


(snipped)

 ## first method ##
 p = Popen('/bin/sh', stdin=buf)
 print p.stdout.readlines()

 Traceback (most recent call last):
   File scratch.py, line 36, in ?
 p = Popen('/bin/sh', stdin=buf)
   File /usr/local/lib/python2.4/subprocess.py, line 534, in __init__
 (p2cread, p2cwrite,
   File /usr/local/lib/python2.4/subprocess.py, line 830, in
 _get_handles
 p2cread = stdin.fileno()
 AttributeError: StringIO instance has no attribute 'fileno'

 ## second method ##
 cmdout, cmdin = popen2('/bin/sh')
 for line in buf:
 cmdin.write(line)

 ret = cmdout.readlines()
 cmdout.close()
 cmdin.close()

 print ret



First close the input so that the (sub) process
knows to terminate and flush the output.  Then,
you can read from the output:

import subprocess
import popen2

p = subprocess.Popen([/bin/sh], stdin=subprocess.PIPE,
stdout=subprocess.PIPE)

p.stdin.write(env -i FOO=BAR\n)
p.stdin.close()
status = p.wait()
ret = p.stdout.readlines()
p.stdout.close()

print ret

# Or

cmdout, cmdin = popen2.popen2(/bin/sh)
cmdin.write(env -i FOO=BAR\n)
cmdin.close()
ret = cmdout.readlines()
cmdout.close

print ret

--
Hope this helps,
Steven

-- 
http://mail.python.org/mailman/listinfo/python-list