Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #35161 > unrolled thread

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

Started bysaqib.ali.75@gmail.com
First post2012-12-19 13:40 -0800
Last post2012-12-20 12:46 +0530
Articles 5 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Why does os.stat() tell me that my file-group has no members? saqib.ali.75@gmail.com - 2012-12-19 13:40 -0800
    Re: Why does os.stat() tell me that my file-group has no members? Hans Mulder <hansmu@xs4all.nl> - 2012-12-20 00:17 +0100
      Re: Why does os.stat() tell me that my file-group has no members? saqib.ali.75@gmail.com - 2012-12-19 17:23 -0800
        Re: Why does os.stat() tell me that my file-group has no members? Chris Angelico <rosuav@gmail.com> - 2012-12-20 13:28 +1100
        Re: Why does os.stat() tell me that my file-group has no members? Kushal Kumaran <kushal.kumaran+python@gmail.com> - 2012-12-20 12:46 +0530

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

Fromsaqib.ali.75@gmail.com
Date2012-12-19 13:40 -0800
SubjectWhy does os.stat() tell me that my file-group has no members?
Message-ID<ec7fc1b0-1553-4e17-8d2f-71de44ed2840@googlegroups.com>

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=[])

[toc] | [next] | [standalone]


#35164

FromHans Mulder <hansmu@xs4all.nl>
Date2012-12-20 00:17 +0100
Message-ID<50d24afc$0$6955$e4fe514c@news2.news.xs4all.nl>
In reply to#35161
On 19/12/12 22:40:00, saqib.ali.75@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

[toc] | [prev] | [next] | [standalone]


#35171

Fromsaqib.ali.75@gmail.com
Date2012-12-19 17:23 -0800
Message-ID<c4fa639f-5959-4a65-9d54-33e51d1acb50@googlegroups.com>
In reply to#35164
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.75@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

[toc] | [prev] | [next] | [standalone]


#35174

FromChris Angelico <rosuav@gmail.com>
Date2012-12-20 13:28 +1100
Message-ID<mailman.1085.1355970508.29569.python-list@python.org>
In reply to#35171
On Thu, Dec 20, 2012 at 12:23 PM,  <saqib.ali.75@gmail.com> wrote:
>
> 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

Want to show your appreciation in a lasting way? Write up a patch, or
a new paragraph or so of text for the page, and open a tracker issue
to improve the docs!

http://bugs.python.org/

ChrisA

[toc] | [prev] | [next] | [standalone]


#35183

FromKushal Kumaran <kushal.kumaran+python@gmail.com>
Date2012-12-20 12:46 +0530
Message-ID<mailman.1092.1355987831.29569.python-list@python.org>
In reply to#35171
saqib.ali.75@gmail.com writes:

> 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
>

That's mostly because it represents the group data almost exactly as the
underlying OS, including using the same structure member names as
defined in pwd.h.  The unix-specific modules often assume the user is
familiar with the underlying library calls and data structures, and is
looking for a way to use them from python.

> <snip>

-- 
regards,
kushal

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web