blob: 228f4c1013993c4d27d383eefd784cc85267fa70 [file] [log] [blame]
Andrew MacIntyref47d60f2002-02-22 11:06:30 +00001# this module is an OS/2 oriented replacement for the grp standard
2# extension module.
3
4# written by Andrew MacIntyre, April 2001.
5# released into the public domain "as is", with NO WARRANTY
6
7# note that this implementation checks whether ":" or ";" as used as
8# the field separator character.
9
10"""Replacement for grp standard extension module, intended for use on
11OS/2 and similar systems which don't normally have an /etc/group file.
12
13The standard Unix group database is an ASCII text file with 4 fields per
14record (line), separated by a colon:
15 - group name (string)
16 - group password (optional encrypted string)
17 - group id (integer)
18 - group members (comma delimited list of userids, with no spaces)
19
20Note that members are only included in the group file for groups that
21aren't their primary groups.
22(see the section 8.2 of the Python Library Reference)
23
24This implementation differs from the standard Unix implementation by
25allowing use of the platform's native path separator character - ';' on OS/2,
26DOS and MS-Windows - as the field separator in addition to the Unix
27standard ":".
28
29The 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)
34
35Classes
36-------
37
38None
39
40Functions
41---------
42
43getgrgid(gid) - return the record for group-id gid as a 4-tuple
44
45getgrnam(name) - return the record for group 'name' as a 4-tuple
46
47getgrall() - return a list of 4-tuples, each tuple being one record
48 (NOTE: the order is arbitrary)
49
50Attributes
51----------
52
53group_file - the path of the group database file
54
55"""
56
57import os
58
59# try and find the group file
60__group_path = []
61if os.environ.has_key('ETC_GROUP'):
62 __group_path.append(os.environ['ETC_GROUP'])
63if os.environ.has_key('ETC'):
64 __group_path.append('%s/group' % os.environ['ETC'])
65if os.environ.has_key('PYTHONHOME'):
66 __group_path.append('%s/Etc/group' % os.environ['PYTHONHOME'])
67
68group_file = None
69for __i in __group_path:
70 try:
71 __f = open(__i, 'r')
72 __f.close()
73 group_file = __i
74 break
75 except:
76 pass
77
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.
81__field_sep = [':']
82if os.pathsep:
83 if os.pathsep != ':':
84 __field_sep.append(os.pathsep)
85
86# helper routine to identify which separator character is in use
87def __get_field_sep(record):
88 fs = None
89 for c in __field_sep:
90 # there should be 3 delimiter characters (for 4 fields)
91 if record.count(c) == 3:
92 fs = c
93 break
94 if fs:
95 return fs
96 else:
97 raise KeyError, '>> group database fields not delimited <<'
98
99# read the whole file, parsing each entry into tuple form
100# with dictionaries to speed recall by GID or group name
101def __read_group_file():
102 if group_file:
103 group = open(group_file, 'r')
104 else:
105 raise KeyError, '>> no group database <<'
106 gidx = {}
107 namx = {}
108 sep = None
109 while 1:
110 entry = group.readline().strip()
111 if len(entry) > 3:
112 if sep == None:
113 sep = __get_field_sep(entry)
114 fields = entry.split(sep)
115 fields[2] = int(fields[2])
116 record = tuple(fields)
117 if not gidx.has_key(fields[2]):
118 gidx[fields[2]] = record
119 if not namx.has_key(fields[0]):
120 namx[fields[0]] = record
121 elif len(entry) > 0:
122 pass # skip empty or malformed records
123 else:
124 break
125 group.close()
126 if len(gidx) == 0:
127 raise KeyError
128 return (gidx, namx)
129
130# return the group database entry by GID
131def getgrgid(gid):
132 g, n = __read_group_file()
133 return g[gid]
134
135# return the group database entry by group name
136def getgrnam(name):
137 g, n = __read_group_file()
138 return n[name]
139
140# return all the group database entries
141def getgrall():
142 g, n = __read_group_file()
143 return g.values()
144
145# test harness
146if __name__ == '__main__':
147 getgrall()