File: pwd.py

package info (click to toggle)
jython 2.7.2%2Brepack1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 62,676 kB
  • sloc: python: 640,908; java: 306,458; xml: 1,984; sh: 522; ansic: 126; makefile: 76
file content (89 lines) | stat: -rw-r--r-- 2,724 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""
This module provides access to the Unix password database.

Password database entries are reported as 7-tuples containing the
following items from the password database (see `<pwd.h>'), in order:
pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.  The
uid and gid items are integers, all others are strings. An exception
is raised if the entry asked for cannot be found.
"""

__all__ = ['getpwuid', 'getpwnam', 'getpwall']

try:
    from os import _name, _posix_impl
    from org.python.core.Py import newStringOrUnicode
except:
    raise ImportError
import sys

if _name == 'nt':
    raise ImportError, 'pwd module not supported on Windows'

class struct_passwd(tuple):
    """
    pwd.struct_passwd: Results from getpw*() routines.

    This object may be accessed either as a tuple of
      (pw_name,pw_passwd,pw_uid,pw_gid,pw_gecos,pw_dir,pw_shell)
    or via the object attributes as named in the above tuple.
    """

    attrs = ['pw_name', 'pw_passwd', 'pw_uid', 'pw_gid', 'pw_gecos',
             'pw_dir', 'pw_shell']

    def __new__(cls, pwd):
        pwd = (newStringOrUnicode(pwd.loginName), newStringOrUnicode(pwd.password), int(pwd.UID),
               int(pwd.GID), newStringOrUnicode(pwd.GECOS), newStringOrUnicode(pwd.home),
               newStringOrUnicode(pwd.shell))
        return tuple.__new__(cls, pwd)

    def __getattr__(self, attr):
        try:
            return self[self.attrs.index(attr)]
        except ValueError:
            raise AttributeError


def getpwuid(uid):
    """
    getpwuid(uid) -> (pw_name,pw_passwd,pw_uid,
                      pw_gid,pw_gecos,pw_dir,pw_shell)
    Return the password database entry for the given numeric user ID.
    See pwd.__doc__ for more on password database entries.
    """
    if uid > sys.maxint or uid < 0:
        raise KeyError(uid)
    entry = _posix_impl.getpwuid(uid)
    if not entry:
        raise KeyError(uid)
    return struct_passwd(entry)


def getpwnam(name):
    """
    getpwnam(name) -> (pw_name,pw_passwd,pw_uid,
                        pw_gid,pw_gecos,pw_dir,pw_shell)
    Return the password database entry for the given user name.
    See pwd.__doc__ for more on password database entries.
    """
    entry = _posix_impl.getpwnam(name)
    if not entry:
        raise KeyError(name)
    return struct_passwd(entry)


def getpwall():
    """
    getpwall() -> list_of_entries
    Return a list of all available password database entries,
    in arbitrary order.
    See pwd.__doc__ for more on password database entries.
    """
    entries = []
    while True:
        entry = _posix_impl.getpwent()
        if not entry:
            break
        entries.append(struct_passwd(entry))
    return entries