1 # this module is an OS/2 oriented replacement for the grp standard
4 # written by Andrew MacIntyre, April 2001.
5 # updated July 2003, adding field accessor support
7 # note that this implementation checks whether ":" or ";" as used as
8 # the field separator character.
10 """Replacement for grp standard extension module, intended for use on
11 OS/2 and similar systems which don't normally have an /etc/group file.
13 The standard Unix group database is an ASCII text file with 4 fields per
14 record (line), separated by a colon:
16 - group password (optional encrypted string)
18 - group members (comma delimited list of userids, with no spaces)
20 Note that members are only included in the group file for groups that
21 aren't their primary groups.
22 (see the section 8.2 of the Python Library Reference)
24 This implementation differs from the standard Unix implementation by
25 allowing use of the platform's native path separator character - ';' on OS/2,
26 DOS and MS-Windows - as the field separator in addition to the Unix
29 The module looks for the group database at the following locations
30 (in order first to last):
31 - ${ETC_GROUP} (or %ETC_GROUP%)
32 - ${ETC}/group (or %ETC%/group)
33 - ${PYTHONHOME}/Etc/group (or %PYTHONHOME%/Etc/group)
43 getgrgid(gid) - return the record for group-id gid as a 4-tuple
45 getgrnam(name) - return the record for group 'name' as a 4-tuple
47 getgrall() - return a list of 4-tuples, each tuple being one record
48 (NOTE: the order is arbitrary)
53 group_file - the path of the group database file
59 # try and find the group file
61 if os
.environ
.has_key('ETC_GROUP'):
62 __group_path
.append(os
.environ
['ETC_GROUP'])
63 if os
.environ
.has_key('ETC'):
64 __group_path
.append('%s/group' % os
.environ
['ETC'])
65 if os
.environ
.has_key('PYTHONHOME'):
66 __group_path
.append('%s/Etc/group' % os
.environ
['PYTHONHOME'])
69 for __i
in __group_path
:
78 # decide what field separator we can try to use - Unix standard, with
79 # the platform's path separator as an option. No special field conversion
80 # handlers are required for the group file.
84 __field_sep
.append(os
.pathsep
)
86 # helper routine to identify which separator character is in use
87 def __get_field_sep(record
):
90 # there should be 3 delimiter characters (for 4 fields)
91 if record
.count(c
) == 3:
97 raise KeyError, '>> group database fields not delimited <<'
99 # class to match the new record field name accessors.
100 # the resulting object is intended to behave like a read-only tuple,
101 # with each member also accessible by a field name.
103 def __init__(self
, name
, passwd
, gid
, mem
):
104 self
.__dict
__['gr_name'] = name
105 self
.__dict
__['gr_passwd'] = passwd
106 self
.__dict
__['gr_gid'] = gid
107 self
.__dict
__['gr_mem'] = mem
108 self
.__dict
__['_record'] = (self
.gr_name
, self
.gr_passwd
,
109 self
.gr_gid
, self
.gr_mem
)
114 def __getitem__(self
, key
):
115 return self
._record
[key
]
117 def __setattr__(self
, name
, value
):
118 raise AttributeError('attribute read-only: %s' % name
)
121 return str(self
._record
)
123 def __cmp__(self
, other
):
124 this
= str(self
._record
)
133 # read the whole file, parsing each entry into tuple form
134 # with dictionaries to speed recall by GID or group name
135 def __read_group_file():
137 group
= open(group_file
, 'r')
139 raise KeyError, '>> no group database <<'
144 entry
= group
.readline().strip()
147 sep
= __get_field_sep(entry
)
148 fields
= entry
.split(sep
)
149 fields
[2] = int(fields
[2])
150 fields
[3] = [f
.strip() for f
in fields
[3].split(',')]
151 record
= Group(*fields
)
152 if not gidx
.has_key(fields
[2]):
153 gidx
[fields
[2]] = record
154 if not namx
.has_key(fields
[0]):
155 namx
[fields
[0]] = record
157 pass # skip empty or malformed records
165 # return the group database entry by GID
167 g
, n
= __read_group_file()
170 # return the group database entry by group name
172 g
, n
= __read_group_file()
175 # return all the group database entries
177 g
, n
= __read_group_file()
181 if __name__
== '__main__':