ยปCore Development>Code coverage>Lib/plat-os2emx/pwd.py

Python code coverage for Lib/plat-os2emx/pwd.py

#countcontent
1n/a# this module is an OS/2 oriented replacement for the pwd standard
2n/a# extension module.
3n/a
4n/a# written by Andrew MacIntyre, April 2001.
5n/a# updated July 2003, adding field accessor support
6n/a
7n/a# note that this implementation checks whether ":" or ";" as used as
8n/a# the field separator character. Path conversions are are applied when
9n/a# the database uses ":" as the field separator character.
10n/a
11n/a"""Replacement for pwd standard extension module, intended for use on
12n/aOS/2 and similar systems which don't normally have an /etc/passwd file.
13n/a
14n/aThe standard Unix password database is an ASCII text file with 7 fields
15n/aper record (line), separated by a colon:
16n/a - user name (string)
17n/a - password (encrypted string, or "*" or "")
18n/a - user id (integer)
19n/a - group id (integer)
20n/a - description (usually user's name)
21n/a - home directory (path to user's home directory)
22n/a - shell (path to the user's login shell)
23n/a
24n/a(see the section 8.1 of the Python Library Reference)
25n/a
26n/aThis implementation differs from the standard Unix implementation by
27n/aallowing use of the platform's native path separator character - ';' on OS/2,
28n/aDOS and MS-Windows - as the field separator in addition to the Unix
29n/astandard ":". Additionally, when ":" is the separator path conversions
30n/aare applied to deal with any munging of the drive letter reference.
31n/a
32n/aThe module looks for the password database at the following locations
33n/a(in order first to last):
34n/a - ${ETC_PASSWD} (or %ETC_PASSWD%)
35n/a - ${ETC}/passwd (or %ETC%/passwd)
36n/a - ${PYTHONHOME}/Etc/passwd (or %PYTHONHOME%/Etc/passwd)
37n/a
38n/aClasses
39n/a-------
40n/a
41n/aNone
42n/a
43n/aFunctions
44n/a---------
45n/a
46n/agetpwuid(uid) - return the record for user-id uid as a 7-tuple
47n/a
48n/agetpwnam(name) - return the record for user 'name' as a 7-tuple
49n/a
50n/agetpwall() - return a list of 7-tuples, each tuple being one record
51n/a (NOTE: the order is arbitrary)
52n/a
53n/aAttributes
54n/a----------
55n/a
56n/apasswd_file - the path of the password database file
57n/a
58n/a"""
59n/a
60n/aimport os
61n/a
62n/a# try and find the passwd file
63n/a__passwd_path = []
64n/aif 'ETC_PASSWD' in os.environ:
65n/a __passwd_path.append(os.environ['ETC_PASSWD'])
66n/aif 'ETC' in os.environ:
67n/a __passwd_path.append('%s/passwd' % os.environ['ETC'])
68n/aif 'PYTHONHOME' in os.environ:
69n/a __passwd_path.append('%s/Etc/passwd' % os.environ['PYTHONHOME'])
70n/a
71n/apasswd_file = None
72n/afor __i in __passwd_path:
73n/a try:
74n/a __f = open(__i, 'r')
75n/a __f.close()
76n/a passwd_file = __i
77n/a break
78n/a except:
79n/a pass
80n/a
81n/a# path conversion handlers
82n/adef __nullpathconv(path):
83n/a return path.replace(os.altsep, os.sep)
84n/a
85n/adef __unixpathconv(path):
86n/a # two known drive letter variations: "x;" and "$x"
87n/a if path[0] == '$':
88n/a conv = path[1] + ':' + path[2:]
89n/a elif path[1] == ';':
90n/a conv = path[0] + ':' + path[2:]
91n/a else:
92n/a conv = path
93n/a return conv.replace(os.altsep, os.sep)
94n/a
95n/a# decide what field separator we can try to use - Unix standard, with
96n/a# the platform's path separator as an option. No special field conversion
97n/a# handler is required when using the platform's path separator as field
98n/a# separator, but are required for the home directory and shell fields when
99n/a# using the standard Unix (":") field separator.
100n/a__field_sep = {':': __unixpathconv}
101n/aif os.pathsep:
102n/a if os.pathsep != ':':
103n/a __field_sep[os.pathsep] = __nullpathconv
104n/a
105n/a# helper routine to identify which separator character is in use
106n/adef __get_field_sep(record):
107n/a fs = None
108n/a for c in __field_sep.keys():
109n/a # there should be 6 delimiter characters (for 7 fields)
110n/a if record.count(c) == 6:
111n/a fs = c
112n/a break
113n/a if fs:
114n/a return fs
115n/a else:
116n/a raise KeyError('>> passwd database fields not delimited <<')
117n/a
118n/a# class to match the new record field name accessors.
119n/a# the resulting object is intended to behave like a read-only tuple,
120n/a# with each member also accessible by a field name.
121n/aclass Passwd:
122n/a def __init__(self, name, passwd, uid, gid, gecos, dir, shell):
123n/a self.__dict__['pw_name'] = name
124n/a self.__dict__['pw_passwd'] = passwd
125n/a self.__dict__['pw_uid'] = uid
126n/a self.__dict__['pw_gid'] = gid
127n/a self.__dict__['pw_gecos'] = gecos
128n/a self.__dict__['pw_dir'] = dir
129n/a self.__dict__['pw_shell'] = shell
130n/a self.__dict__['_record'] = (self.pw_name, self.pw_passwd,
131n/a self.pw_uid, self.pw_gid,
132n/a self.pw_gecos, self.pw_dir,
133n/a self.pw_shell)
134n/a
135n/a def __len__(self):
136n/a return 7
137n/a
138n/a def __getitem__(self, key):
139n/a return self._record[key]
140n/a
141n/a def __setattr__(self, name, value):
142n/a raise AttributeError('attribute read-only: %s' % name)
143n/a
144n/a def __repr__(self):
145n/a return str(self._record)
146n/a
147n/a def __cmp__(self, other):
148n/a this = str(self._record)
149n/a if this == other:
150n/a return 0
151n/a elif this < other:
152n/a return -1
153n/a else:
154n/a return 1
155n/a
156n/a
157n/a# read the whole file, parsing each entry into tuple form
158n/a# with dictionaries to speed recall by UID or passwd name
159n/adef __read_passwd_file():
160n/a if passwd_file:
161n/a passwd = open(passwd_file, 'r')
162n/a else:
163n/a raise KeyError('>> no password database <<')
164n/a uidx = {}
165n/a namx = {}
166n/a sep = None
167n/a while True:
168n/a entry = passwd.readline().strip()
169n/a if len(entry) > 6:
170n/a if sep is None:
171n/a sep = __get_field_sep(entry)
172n/a fields = entry.split(sep)
173n/a for i in (2, 3):
174n/a fields[i] = int(fields[i])
175n/a for i in (5, 6):
176n/a fields[i] = __field_sep[sep](fields[i])
177n/a record = Passwd(*fields)
178n/a if fields[2] not in uidx:
179n/a uidx[fields[2]] = record
180n/a if fields[0] not in namx:
181n/a namx[fields[0]] = record
182n/a elif len(entry) > 0:
183n/a pass # skip empty or malformed records
184n/a else:
185n/a break
186n/a passwd.close()
187n/a if len(uidx) == 0:
188n/a raise KeyError
189n/a return (uidx, namx)
190n/a
191n/a# return the passwd database entry by UID
192n/adef getpwuid(uid):
193n/a u, n = __read_passwd_file()
194n/a return u[uid]
195n/a
196n/a# return the passwd database entry by passwd name
197n/adef getpwnam(name):
198n/a u, n = __read_passwd_file()
199n/a return n[name]
200n/a
201n/a# return all the passwd database entries
202n/adef getpwall():
203n/a u, n = __read_passwd_file()
204n/a return n.values()
205n/a
206n/a# test harness
207n/aif __name__ == '__main__':
208n/a getpwall()