How to use only a sub shell to execute many commands in python

2008-01-18 Thread raocheng
Please see the following code.
Suppose I have many shell commands to be executed. And I don't want to
fork a sub shell for each command(eg: status,output =
commands.getstatusoutput(cmd)) because it is too expensive. I want to
use only one sub shell to execute all these commands and want to get
each command's output. How can I accomplish this task ? Thanks in
advance.

===
#!/usr/bin/env python
import os
fi, fo = os.popen2(
'''
while read line
do
  eval $line
done
''',   't')

#Suppose I have many commands to execute, but I don't want to fork a
sub shell for each command
cmds = ['date','uptime','pwd','ls -rltF','who']

for cmd in cmds:
#pseudocode
fi.executeCmd(cmd)
output = fo.readResult()

print output
===
-- 
http://mail.python.org/mailman/listinfo/python-list


How to get the user/group name from uid/gid in python ?

2008-01-19 Thread raocheng
We know that we can get process or file's uid/gid in python. For
example:
$ python
>>> import os
>>> os.getuid()
1516
>>> os.getgid()
102
>>> os.geteuid()
1516
>>> os.getegid()
102
>>> os.getgroups()
[102, 600]
>>>


$ python
>>> import os,stat
>>> uid = os.stat("./file1")[stat.ST_UID]
>>> gid = os.stat("./file1")[stat.ST_GID]
>>> print uid, gid
1516 102
>>>

How can we get the user/group name from the uid/gid ?
Generally, we can get this information from /etc/passwd. But it is too
inconvenient and when we are using integrated password management, we
can not depend on /etc/passwd.

Thanks in advance.
-- 
http://mail.python.org/mailman/listinfo/python-list