本文整理汇总了Python中pwd.struct_passwd方法的典型用法代码示例。如果您正苦于以下问题:Python pwd.struct_passwd方法的具体用法?Python pwd.struct_passwd怎么用?Python pwd.struct_passwd使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pwd
的用法示例。
在下文中一共展示了pwd.struct_passwd方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_check_environ_getpwuid
# 需要导入模块: import pwd [as 别名]
# 或者: from pwd import struct_passwd [as 别名]
def test_check_environ_getpwuid(self):
util._environ_checked = 0
os.environ.pop('HOME', None)
import pwd
# only set pw_dir field, other fields are not used
def mock_getpwuid(uid):
return pwd.struct_passwd((None, None, None, None, None,
'/home/distutils', None))
with swap_attr(pwd, 'getpwuid', mock_getpwuid):
check_environ()
self.assertEqual(os.environ['HOME'], '/home/distutils')
util._environ_checked = 0
os.environ.pop('HOME', None)
# bpo-10496: Catch pwd.getpwuid() error
def getpwuid_err(uid):
raise KeyError
with swap_attr(pwd, 'getpwuid', getpwuid_err):
check_environ()
self.assertNotIn('HOME', os.environ)
示例2: test_check_environ_getpwuid
# 需要导入模块: import pwd [as 别名]
# 或者: from pwd import struct_passwd [as 别名]
def test_check_environ_getpwuid(self):
util._environ_checked = 0
os.environ.pop('HOME', None)
import pwd
# only set pw_dir field, other fields are not used
result = pwd.struct_passwd((None, None, None, None, None,
'/home/distutils', None))
with mock.patch.object(pwd, 'getpwuid', return_value=result):
check_environ()
self.assertEqual(os.environ['HOME'], '/home/distutils')
util._environ_checked = 0
os.environ.pop('HOME', None)
# bpo-10496: Catch pwd.getpwuid() error
with mock.patch.object(pwd, 'getpwuid', side_effect=KeyError):
check_environ()
self.assertNotIn('HOME', os.environ)
示例3: test_addUser
# 需要导入模块: import pwd [as 别名]
# 或者: from pwd import struct_passwd [as 别名]
def test_addUser(self):
"""
L{UserDatabase.addUser} accepts seven arguments, one for each field of
a L{pwd.struct_passwd}, and makes the new record available via
L{UserDatabase.getpwuid}, L{UserDatabase.getpwnam}, and
L{UserDatabase.getpwall}.
"""
username = 'alice'
password = 'secr3t'
uid = 123
gid = 456
gecos = 'Alice,,,'
home = '/users/alice'
shell = '/usr/bin/foosh'
db = self.database
db.addUser(username, password, uid, gid, gecos, home, shell)
for [entry] in [[db.getpwuid(uid)], [db.getpwnam(username)],
db.getpwall()]:
self.assertEqual(entry.pw_name, username)
self.assertEqual(entry.pw_passwd, password)
self.assertEqual(entry.pw_uid, uid)
self.assertEqual(entry.pw_gid, gid)
self.assertEqual(entry.pw_gecos, gecos)
self.assertEqual(entry.pw_dir, home)
self.assertEqual(entry.pw_shell, shell)
示例4: get_user_group
# 需要导入模块: import pwd [as 别名]
# 或者: from pwd import struct_passwd [as 别名]
def get_user_group(self, user=None, group=None):
"""
Get the user and group information.
Parameters
----------
user : str
User name or user id (default is the `os.getuid()`).
group : str
Group name or group id (default is the group of `user`).
Returns
-------
user : pwd.struct_passwd
User object.
group : grp.struct_group
Group object.
"""
user = user or os.getuid()
# Convert the information we have obtained to a user object
try:
try:
user = pwd.getpwuid(int(user))
except ValueError:
user = pwd.getpwnam(user)
except KeyError as ex: # pragma: no cover
self.logger.fatal("could not resolve user: %s", ex)
raise
# Get the group
group = group or user.pw_gid
try:
try:
group = grp.getgrgid(int(group))
except ValueError:
group = grp.getgrnam(group)
except KeyError as ex: # pragma: no cover
self.logger.fatal("could not resolve group:%s", ex)
raise
return user, group
示例5: fake_getpwuid
# 需要导入模块: import pwd [as 别名]
# 或者: from pwd import struct_passwd [as 别名]
def fake_getpwuid(uid):
return pwd.struct_passwd(
("user", "x", uid, uid, "", "/home/user", "/bin/bash")
)
示例6: get_users
# 需要导入模块: import pwd [as 别名]
# 或者: from pwd import struct_passwd [as 别名]
def get_users(self):
"""Returns a list of all local users on the computer.
Each user is represented as a dict with the keys: C{username},
C{name}, C{uid}, C{enabled}, C{location}, C{work-phone} and
C{home-phone}.
"""
users = []
found_usernames = set()
for user in self.get_user_data():
if not isinstance(user, struct_passwd):
user = struct_passwd(user)
if user.pw_name in found_usernames:
continue
gecos_data = [x or None for x in user.pw_gecos.split(",")[:4]]
while len(gecos_data) < 4:
gecos_data.append(None)
name, location, work_phone, home_phone = tuple(gecos_data)
enabled = user.pw_name not in self.locked_users
users.append({"username": user.pw_name, "name": name,
"uid": user.pw_uid, "enabled": enabled,
"location": location, "work-phone": work_phone,
"home-phone": home_phone,
"primary-gid": user.pw_gid})
found_usernames.add(user.pw_name)
return users
示例7: test_addUser
# 需要导入模块: import pwd [as 别名]
# 或者: from pwd import struct_passwd [as 别名]
def test_addUser(self):
"""
L{UserDatabase.addUser} accepts seven arguments, one for each field of
a L{pwd.struct_passwd}, and makes the new record available via
L{UserDatabase.getpwuid}, L{UserDatabase.getpwnam}, and
L{UserDatabase.getpwall}.
"""
username = 'alice'
password = 'secr3t'
uid = 123
gid = 456
gecos = 'Alice,,,'
home = '/users/alice'
shell = '/usr/bin/foosh'
db = self.database
db.addUser(username, password, uid, gid, gecos, home, shell)
for entry in [db.getpwuid(uid), db.getpwnam(username)]:
self.assertEquals(entry.pw_name, username)
self.assertEquals(entry.pw_passwd, password)
self.assertEquals(entry.pw_uid, uid)
self.assertEquals(entry.pw_gid, gid)
self.assertEquals(entry.pw_gecos, gecos)
self.assertEquals(entry.pw_dir, home)
self.assertEquals(entry.pw_shell, shell)
[entry] = db.getpwall()
self.assertEquals(entry.pw_name, username)
self.assertEquals(entry.pw_passwd, password)
self.assertEquals(entry.pw_uid, uid)
self.assertEquals(entry.pw_gid, gid)
self.assertEquals(entry.pw_gecos, gecos)
self.assertEquals(entry.pw_dir, home)
self.assertEquals(entry.pw_shell, shell)