pwd.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # indirectly based on ctypes implementation: Victor Stinner, 2008-05-08
  2. """
  3. This module provides access to the Unix password database.
  4. It is available on all Unix versions.
  5. Password database entries are reported as 7-tuples containing the following
  6. items from the password database (see `<pwd.h>'), in order:
  7. pw_name, pw_passwd, pw_uid, pw_gid, pw_gecos, pw_dir, pw_shell.
  8. The uid and gid items are integers, all others are strings. An
  9. exception is raised if the entry asked for cannot be found.
  10. """
  11. from _pwdgrp_cffi import ffi, lib
  12. import _structseq
  13. try: from __pypy__ import builtinify
  14. except ImportError: builtinify = lambda f: f
  15. class struct_passwd(metaclass=_structseq.structseqtype):
  16. """
  17. pwd.struct_passwd: Results from getpw*() routines.
  18. This object may be accessed either as a tuple of
  19. (pw_name,pw_passwd,pw_uid,pw_gid,pw_gecos,pw_dir,pw_shell)
  20. or via the object attributes as named in the above tuple.
  21. """
  22. name = "pwd.struct_passwd"
  23. pw_name = _structseq.structseqfield(0)
  24. pw_passwd = _structseq.structseqfield(1)
  25. pw_uid = _structseq.structseqfield(2)
  26. pw_gid = _structseq.structseqfield(3)
  27. pw_gecos = _structseq.structseqfield(4)
  28. pw_dir = _structseq.structseqfield(5)
  29. pw_shell = _structseq.structseqfield(6)
  30. def _mkpwent(pw):
  31. return struct_passwd([
  32. ffi.string(pw.pw_name),
  33. ffi.string(pw.pw_passwd),
  34. pw.pw_uid,
  35. pw.pw_gid,
  36. ffi.string(pw.pw_gecos),
  37. ffi.string(pw.pw_dir),
  38. ffi.string(pw.pw_shell)])
  39. @builtinify
  40. def getpwuid(uid):
  41. """
  42. getpwuid(uid) -> (pw_name,pw_passwd,pw_uid,
  43. pw_gid,pw_gecos,pw_dir,pw_shell)
  44. Return the password database entry for the given numeric user ID.
  45. See pwd.__doc__ for more on password database entries.
  46. """
  47. pw = lib.getpwuid(uid)
  48. if not pw:
  49. raise KeyError("getpwuid(): uid not found: %s" % uid)
  50. return _mkpwent(pw)
  51. @builtinify
  52. def getpwnam(name):
  53. """
  54. getpwnam(name) -> (pw_name,pw_passwd,pw_uid,
  55. pw_gid,pw_gecos,pw_dir,pw_shell)
  56. Return the password database entry for the given user name.
  57. See pwd.__doc__ for more on password database entries.
  58. """
  59. if not isinstance(name, basestring):
  60. raise TypeError("expected string")
  61. name = str(name)
  62. pw = lib.getpwnam(name)
  63. if not pw:
  64. raise KeyError("getpwname(): name not found: %s" % name)
  65. return _mkpwent(pw)
  66. @builtinify
  67. def getpwall():
  68. """
  69. getpwall() -> list_of_entries
  70. Return a list of all available password database entries, in arbitrary order.
  71. See pwd.__doc__ for more on password database entries.
  72. """
  73. users = []
  74. lib.setpwent()
  75. while True:
  76. pw = lib.getpwent()
  77. if not pw:
  78. break
  79. users.append(_mkpwent(pw))
  80. lib.endpwent()
  81. return users
  82. __all__ = ('struct_passwd', 'getpwuid', 'getpwnam', 'getpwall')
  83. if __name__ == "__main__":
  84. # Uncomment next line to test CPython implementation
  85. # from pwd import getpwuid, getpwnam, getpwall
  86. from os import getuid
  87. uid = getuid()
  88. pw = getpwuid(uid)
  89. print("uid %s: %s" % (pw.pw_uid, pw))
  90. name = pw.pw_name
  91. print("name %r: %s" % (name, getpwnam(name)))
  92. print("All:")
  93. for pw in getpwall():
  94. print(pw)