This commit was manufactured by cvs2svn to create tag 'r23b1-mac'.
[python/dscho.git] / Lib / plat-os2emx / pwd.py
blob6e2b008cd8804129a8a704093d7e8de5b660bc08
1 # this module is an OS/2 oriented replacement for the pwd standard
2 # extension module.
4 # written by Andrew MacIntyre, April 2001.
5 # released into the public domain "as is", with NO WARRANTY
7 # note that this implementation checks whether ":" or ";" as used as
8 # the field separator character. Path conversions are are applied when
9 # the database uses ":" as the field separator character.
11 """Replacement for pwd standard extension module, intended for use on
12 OS/2 and similar systems which don't normally have an /etc/passwd file.
14 The standard Unix password database is an ASCII text file with 7 fields
15 per record (line), separated by a colon:
16 - user name (string)
17 - password (encrypted string, or "*" or "")
18 - user id (integer)
19 - group id (integer)
20 - description (usually user's name)
21 - home directory (path to user's home directory)
22 - shell (path to the user's login shell)
24 (see the section 8.1 of the Python Library Reference)
26 This implementation differs from the standard Unix implementation by
27 allowing use of the platform's native path separator character - ';' on OS/2,
28 DOS and MS-Windows - as the field separator in addition to the Unix
29 standard ":". Additionally, when ":" is the separator path conversions
30 are applied to deal with any munging of the drive letter reference.
32 The module looks for the password database at the following locations
33 (in order first to last):
34 - ${ETC_PASSWD} (or %ETC_PASSWD%)
35 - ${ETC}/passwd (or %ETC%/passwd)
36 - ${PYTHONHOME}/Etc/passwd (or %PYTHONHOME%/Etc/passwd)
38 Classes
39 -------
41 None
43 Functions
44 ---------
46 getpwuid(uid) - return the record for user-id uid as a 7-tuple
48 getpwnam(name) - return the record for user 'name' as a 7-tuple
50 getpwall() - return a list of 7-tuples, each tuple being one record
51 (NOTE: the order is arbitrary)
53 Attributes
54 ----------
56 passwd_file - the path of the password database file
58 """
60 import os
62 # try and find the passwd file
63 __passwd_path = []
64 if os.environ.has_key('ETC_PASSWD'):
65 __passwd_path.append(os.environ['ETC_PASSWD'])
66 if os.environ.has_key('ETC'):
67 __passwd_path.append('%s/passwd' % os.environ['ETC'])
68 if os.environ.has_key('PYTHONHOME'):
69 __passwd_path.append('%s/Etc/passwd' % os.environ['PYTHONHOME'])
71 passwd_file = None
72 for __i in __passwd_path:
73 try:
74 __f = open(__i, 'r')
75 __f.close()
76 passwd_file = __i
77 break
78 except:
79 pass
81 # path conversion handlers
82 def __nullpathconv(path):
83 return path.replace(os.altsep, os.sep)
85 def __unixpathconv(path):
86 # two known drive letter variations: "x;" and "$x"
87 if path[0] == '$':
88 conv = path[1] + ':' + path[2:]
89 elif path[1] == ';':
90 conv = path[0] + ':' + path[2:]
91 else:
92 conv = path
93 return conv.replace(os.altsep, os.sep)
95 # decide what field separator we can try to use - Unix standard, with
96 # the platform's path separator as an option. No special field conversion
97 # handler is required when using the platform's path separator as field
98 # separator, but are required for the home directory and shell fields when
99 # using the standard Unix (":") field separator.
100 __field_sep = {':': __unixpathconv}
101 if os.pathsep:
102 if os.pathsep != ':':
103 __field_sep[os.pathsep] = __nullpathconv
105 # helper routine to identify which separator character is in use
106 def __get_field_sep(record):
107 fs = None
108 for c in __field_sep.keys():
109 # there should be 6 delimiter characters (for 7 fields)
110 if record.count(c) == 6:
111 fs = c
112 break
113 if fs:
114 return fs
115 else:
116 raise KeyError, '>> passwd database fields not delimited <<'
118 # read the whole file, parsing each entry into tuple form
119 # with dictionaries to speed recall by UID or passwd name
120 def __read_passwd_file():
121 if passwd_file:
122 passwd = open(passwd_file, 'r')
123 else:
124 raise KeyError, '>> no password database <<'
125 uidx = {}
126 namx = {}
127 sep = None
128 while 1:
129 entry = passwd.readline().strip()
130 if len(entry) > 6:
131 if sep == None:
132 sep = __get_field_sep(entry)
133 fields = entry.split(sep)
134 for i in (2, 3):
135 fields[i] = int(fields[i])
136 for i in (5, 6):
137 fields[i] = __field_sep[sep](fields[i])
138 record = tuple(fields)
139 if not uidx.has_key(fields[2]):
140 uidx[fields[2]] = record
141 if not namx.has_key(fields[0]):
142 namx[fields[0]] = record
143 elif len(entry) > 0:
144 pass # skip empty or malformed records
145 else:
146 break
147 passwd.close()
148 if len(uidx) == 0:
149 raise KeyError
150 return (uidx, namx)
152 # return the passwd database entry by UID
153 def getpwuid(uid):
154 u, n = __read_passwd_file()
155 return u[uid]
157 # return the passwd database entry by passwd name
158 def getpwnam(name):
159 u, n = __read_passwd_file()
160 return n[name]
162 # return all the passwd database entries
163 def getpwall():
164 u, n = __read_passwd_file()
165 return n.values()
167 # test harness
168 if __name__ == '__main__':
169 getpwall()