Why does os.stat() tell me that my file-group has no members?

2012-12-19 Thread saqib . ali . 75


I'm using python 2.6.4 on Solaris 5-10.

I have a file named "myFile". It is owned by someone else, by I ("myuser") am 
in the file's group ("mygrp"). Below is my python code. Why does it tell me 
that mygrp has no members???


>>> import os, pwd, grp
>>> stat_info = os.stat("myFile")
>>> fileUID = stat_info.st_uid
>>> fileGID = stat_info.st_gid
>>> fileGroup = grp.getgrgid(fileGID)[0]
>>> fileUser = pwd.getpwuid(fileUID)[0]
>>> print "grp.getgrgid(fileGID) = %s" % grp.getgrgid(fileGID)

grp.getgrgid(fileGID) = grp.struct_group(gr_name='mygrp', gr_passwd='', 
gr_gid=100, gr_mem=[])
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why does os.stat() tell me that my file-group has no members?

2012-12-19 Thread saqib . ali . 75

Thanks!! This was very helpful. It worked perfectly.
I had no clue about the intricacies of how python represents the group data 
from the underlying OS.

This page doesn't go into to detailed explanation like you did: 
http://docs.python.org/2/library/grp.html



On Wednesday, December 19, 2012 6:17:16 PM UTC-5, Hans Mulder wrote:
> On 19/12/12 22:40:00, saqib.ali...@gmail.com wrote:
> 
> > 
> 
> > 
> 
> > I'm using python 2.6.4 on Solaris 5-10.
> 
> > 
> 
> > I have a file named "myFile". It is owned by someone else, by
> 
> > I ("myuser") am in the file's group ("mygrp"). Below is my python
> 
> > code. Why does it tell me that mygrp has no members???
> 
> > 
> 
> > 
> 
>  import os, pwd, grp
> 
>  stat_info = os.stat("myFile")
> 
>  fileUID = stat_info.st_uid
> 
>  fileGID = stat_info.st_gid
> 
>  fileGroup = grp.getgrgid(fileGID)[0]
> 
>  fileUser = pwd.getpwuid(fileUID)[0]
> 
>  print "grp.getgrgid(fileGID) = %s" % grp.getgrgid(fileGID)
> 
> > 
> 
> > grp.getgrgid(fileGID) = grp.struct_group(gr_name='mygrp', gr_passwd='', 
> > gr_gid=100, gr_mem=[])
> 
> 
> 
> It doesn't say that your group has no members.
> 
> 
> 
> Every account has a primary group, and some accounts also
> 
> have addtional groups.  The primary group is the one in the
> 
> .pw_gid attribute in the pwd entry.  The additional groups
> 
> are those that mention the account in the .gr_mem attribute
> 
> in their grp entry.
> 
> 
> 
> Your experiment shows that nobody has "mygrp" as an additional
> 
> group.  So if you're a member of mygrp, then it must be your
> 
> primary group, i.e. os.getgid() should return 100 for you.
> 
> 
> 
> You can get a complete list of members of group by adding
> 
> two lists:
> 
> 
> 
> def all_members(gid):
> 
> primary_members = [ user.pw_name
> 
> for user in pwd.getpwall() if user.pw_gid == gid ]
> 
> additional_members = grp.getgrgid(gid).gr_mem
> 
> return primary_members + additional_members
> 
> 
> 
> 
> 
> Hope this helps,
> 
> 
> 
> -- HansM
-- 
http://mail.python.org/mailman/listinfo/python-list


Why is pexpect acting funny with sendline() and expect()?

2012-12-26 Thread saqib . ali . 75
I am running Solaris 5-10, python 2.6.2 and pexpect 2.4

I have the very simple python script below which exercises the functionality of 
sending and receiving text from the shell.

My understanding is that pexepect([pexpect.TIMEOUT, x,y,z], timeout=w) will 
return the index of the match that it found *since the last time pexpect was 
called*, but if it takes longer than w seconds, it will return 0.


Here is my very simple script:


#!/usr/bin/env python

import pexpect
myPrompt = " % "

myShell = pexpect.spawn("/bin/tcsh")
print "Sending 'JUNK-0' to shell"
x = myShell.sendline("JUNK-0")
y = myShell.expect([pexpect.TIMEOUT], timeout=1)   
print "y = %s" % y
print myShell.before 
print "=" * 80
print "\n\n"
 
for i in range(2):
print "i = %d" % (i+1)
print "Sending 'JUNK-%d' to shell" % (i+1)
x = myShell.sendline("JUNK-%d" % (i+1))
y = myShell.expect([pexpect.TIMEOUT, myPrompt], timeout=10)  
print "y = %s" % y
print myShell.before
print "=" * 80
print "\n\n"



FYI, my shell prompt is "myMachine % ", however in this script I have simply 
used " % " to keep it generic.

When I run it, I see the following output: 




Sending 'JUNK-0' to shell
y = 0
JUNK-0
myMachine % JUNK-0
JUNK-0: Command not found.
myMachine % 




i = 1
Sending 'JUNK-1' to shell
y = 1
JUNK-0
myMachine 




i = 2
Sending 'JUNK-2' to shell
y = 1
JUNK-0
JUNK-0: Command not found.
myMachine 





Why do I see "JUNK-0" consistently recurring in the output? It should be 
consumed by the first myShell.expect() statement, but it keeps showing up. Why??
-- 
http://mail.python.org/mailman/listinfo/python-list