本文整理汇总了Python中os.getgroups方法的典型用法代码示例。如果您正苦于以下问题:Python os.getgroups方法的具体用法?Python os.getgroups怎么用?Python os.getgroups使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类os
的用法示例。
在下文中一共展示了os.getgroups方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: drop_privileges
# 需要导入模块: import os [as 别名]
# 或者: from os import getgroups [as 别名]
def drop_privileges():
from certidude import config
import pwd
_, _, uid, gid, gecos, root, shell = pwd.getpwnam("certidude")
restricted_groups = []
restricted_groups.append(gid)
# PAM needs access to /etc/shadow
if config.AUTHENTICATION_BACKENDS == {"pam"}:
import grp
name, passwd, num, mem = grp.getgrnam("shadow")
click.echo("Adding current user to shadow group due to PAM authentication backend")
restricted_groups.append(num)
os.setgroups(restricted_groups)
os.setgid(gid)
os.setuid(uid)
click.echo("Switched %s (pid=%d) to user %s (uid=%d, gid=%d); member of groups %s" %
(getproctitle(), os.getpid(), "certidude", os.getuid(), os.getgid(), ", ".join([str(j) for j in os.getgroups()])))
os.umask(0o007)
示例2: testNoArgFunctions
# 需要导入模块: import os [as 别名]
# 或者: from os import getgroups [as 别名]
def testNoArgFunctions(self):
# test posix functions which take no arguments and have
# no side-effects which we need to cleanup (e.g., fork, wait, abort)
NO_ARG_FUNCTIONS = [ "ctermid", "getcwd", "getcwdu", "uname",
"times", "getloadavg", "tmpnam",
"getegid", "geteuid", "getgid", "getgroups",
"getpid", "getpgrp", "getppid", "getuid",
]
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "", DeprecationWarning)
for name in NO_ARG_FUNCTIONS:
posix_func = getattr(posix, name, None)
if posix_func is not None:
posix_func()
self.assertRaises(TypeError, posix_func, 1)
示例3: test_getgroups
# 需要导入模块: import os [as 别名]
# 或者: from os import getgroups [as 别名]
def test_getgroups(self):
with os.popen('id -G 2>/dev/null') as idg:
groups = idg.read().strip()
ret = idg.close()
if ret != None or not groups:
raise unittest.SkipTest("need working 'id -G'")
# Issues 16698: OS X ABIs prior to 10.6 have limits on getgroups()
if sys.platform == 'darwin':
import sysconfig
dt = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET') or '10.0'
if tuple(int(n) for n in dt.split('.')[0:2]) < (10, 6):
raise unittest.SkipTest("getgroups(2) is broken prior to 10.6")
# 'id -G' and 'os.getgroups()' should return the same
# groups, ignoring order and duplicates.
# #10822 - it is implementation defined whether posix.getgroups()
# includes the effective gid so we include it anyway, since id -G does
self.assertEqual(
set([int(x) for x in groups.split()]),
set(posix.getgroups() + [posix.getegid()]))
示例4: is_writable
# 需要导入模块: import os [as 别名]
# 或者: from os import getgroups [as 别名]
def is_writable(path):
# Ensure that it exists.
if not os.path.exists(path):
return False
# If we're on a posix system, check its permissions.
if hasattr(os, 'getuid'):
statdata = os.stat(path)
perm = stat.S_IMODE(statdata.st_mode)
# is it world-writable?
if (perm & 0o002):
return True
# do we own it?
elif statdata.st_uid == os.getuid() and (perm & 0o200):
return True
# are we in a group that can write to it?
elif (statdata.st_gid in [os.getgid()] + os.getgroups()) and (perm & 0o020):
return True
# otherwise, we can't write to it.
else:
return False
# Otherwise, we'll assume it's writable.
# [xx] should we do other checks on other platforms?
return True
示例5: test_getgroups
# 需要导入模块: import os [as 别名]
# 或者: from os import getgroups [as 别名]
def test_getgroups(self):
with os.popen('id -G') as idg:
groups = idg.read().strip()
ret = idg.close()
if ret != None or not groups:
raise unittest.SkipTest("need working 'id -G'")
# Issues 16698: OS X ABIs prior to 10.6 have limits on getgroups()
if sys.platform == 'darwin':
import sysconfig
dt = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET') or '10.0'
if float(dt) < 10.6:
raise unittest.SkipTest("getgroups(2) is broken prior to 10.6")
# 'id -G' and 'os.getgroups()' should return the same
# groups, ignoring order and duplicates.
# #10822 - it is implementation defined whether posix.getgroups()
# includes the effective gid so we include it anyway, since id -G does
self.assertEqual(
set([int(x) for x in groups.split()]),
set(posix.getgroups() + [posix.getegid()]))
示例6: set_groups
# 需要导入模块: import os [as 别名]
# 或者: from os import getgroups [as 别名]
def set_groups(path, new_uid, new_gid, verbose=True):
'''For sudo case, set GID to non-SuperUser value.'''
if not app_state['sudo_based_usage']:
debug('set_groups: called for non-sudo use')
return False
try:
debug('Changing file owner: file=' + path + ', uid=' + str(new_uid))
new_gid_list = []
new_gid_list = os.getgroups()
if verbose:
debug('os.getgroups: new_gid_list: ' + str(new_gid_list))
os.setgroups([])
if verbose:
debug('calling os.setgroups(' + str(new_gid_list) + ')..')
# os.setgroups(new_gid_list) # XXX macOS: ValueError: too many groups
os.setgroups([new_gid_list[0]]) # XXX macOS: ValueError: too many groups
if verbose:
debug('calling os.setgid(' + str(new_gid) + ')..')
os.setgid(new_gid)
except OSError as e:
critical(e, 'Unable to to update UID on file: ' + path)
sys.exc_info()
log('Exception ' + str(e.errno) + ': ' + str(e))
return False
return True
示例7: test_getgroups
# 需要导入模块: import os [as 别名]
# 或者: from os import getgroups [as 别名]
def test_getgroups(self):
with os.popen('id -G 2>/dev/null') as idg:
groups = idg.read().strip()
ret = idg.close()
if ret is not None or not groups:
raise unittest.SkipTest("need working 'id -G'")
# Issues 16698: OS X ABIs prior to 10.6 have limits on getgroups()
if sys.platform == 'darwin':
import sysconfig
dt = sysconfig.get_config_var('MACOSX_DEPLOYMENT_TARGET') or '10.0'
if tuple(int(n) for n in dt.split('.')[0:2]) < (10, 6):
raise unittest.SkipTest("getgroups(2) is broken prior to 10.6")
# 'id -G' and 'os.getgroups()' should return the same
# groups, ignoring order and duplicates.
# #10822 - it is implementation defined whether posix.getgroups()
# includes the effective gid so we include it anyway, since id -G does
self.assertEqual(
set([int(x) for x in groups.split()]),
set(posix.getgroups() + [posix.getegid()]))
# tests for the posix *at functions follow
示例8: get_current_users_and_groups
# 需要导入模块: import os [as 别名]
# 或者: from os import getgroups [as 别名]
def get_current_users_and_groups(displayer=None):
ruid, euid, suid = getresuid()
rgid, egid, sgid = getresgid()
supplementary_groups = os.getgroups()
_supplementary_groups = []
for _id in supplementary_groups:
_name = get_name_by_gid(_id)
_supplementary_groups.append({'name': _name, 'id': _id})
return {
'users': {
'real user': {'name': get_name_by_uid(ruid), 'id': ruid},
'effective user': {'name': get_name_by_uid(euid), 'id': euid},
'saved user': {'name': get_name_by_uid(suid), 'id': suid},},
'groups': {
'real group': {'name': get_name_by_gid(rgid), 'id': rgid},
'effective group': {'name': get_name_by_gid(rgid), 'id': rgid},
'saved group': {'name': get_name_by_gid(rgid), 'id': rgid},},
'supplementary_groups': _supplementary_groups,
}
示例9: _check_plugdev_group
# 需要导入模块: import os [as 别名]
# 或者: from os import getgroups [as 别名]
def _check_plugdev_group(self):
"""
Check if the user is a member of the plugdev group. For the root
user, this always returns True
:rtype: bool
"""
if getpass.getuser() == 'root':
return True
try:
return grp.getgrnam('plugdev').gr_gid in os.getgroups()
except KeyError:
pass
return False
示例10: is_writable
# 需要导入模块: import os [as 别名]
# 或者: from os import getgroups [as 别名]
def is_writable(path):
# Ensure that it exists.
if not os.path.exists(path):
return False
# If we're on a posix system, check its permissions.
if hasattr(os, 'getuid'):
statdata = os.stat(path)
perm = stat.S_IMODE(statdata.st_mode)
# is it world-writable?
if (perm & 0o002):
return True
# do we own it?
elif statdata.st_uid == os.getuid() and (perm & 0o200):
return True
# are we in a group that can write to it?
elif (statdata.st_gid in [os.getgid()] + os.getgroups()) \
and (perm & 0o020):
return True
# otherwise, we can't write to it.
else:
return False
# Otherwise, we'll assume it's writable.
# [xx] should we do other checks on other platforms?
return True
######################################################################
# NLTK Error reporting
######################################################################
示例11: setUp
# 需要导入模块: import os [as 别名]
# 或者: from os import getgroups [as 别名]
def setUp(self):
if posix.getuid() != 0:
raise unittest.SkipTest("not enough privileges")
if not hasattr(posix, 'getgroups'):
raise unittest.SkipTest("need posix.getgroups")
if sys.platform == 'darwin':
raise unittest.SkipTest("getgroups(2) is broken on OSX")
self.saved_groups = posix.getgroups()
示例12: test_initgroups
# 需要导入模块: import os [as 别名]
# 或者: from os import getgroups [as 别名]
def test_initgroups(self):
# find missing group
g = max(self.saved_groups or [0]) + 1
name = pwd.getpwuid(posix.getuid()).pw_name
posix.initgroups(name, g)
self.assertIn(g, posix.getgroups())
示例13: test_initgroups
# 需要导入模块: import os [as 别名]
# 或者: from os import getgroups [as 别名]
def test_initgroups(self):
# find missing group
g = max(self.saved_groups) + 1
name = pwd.getpwuid(posix.getuid()).pw_name
posix.initgroups(name, g)
self.assertIn(g, posix.getgroups())
示例14: _runAsUser
# 需要导入模块: import os [as 别名]
# 或者: from os import getgroups [as 别名]
def _runAsUser(self, f, *args, **kw):
euid = os.geteuid()
egid = os.getegid()
groups = os.getgroups()
uid, gid = self.getUserGroupId()
os.setegid(0)
os.seteuid(0)
os.setgroups(self.getOtherGroups())
os.setegid(gid)
os.seteuid(uid)
try:
f = iter(f)
except TypeError:
f = [(f, args, kw)]
try:
for i in f:
func = i[0]
args = len(i) > 1 and i[1] or ()
kw = len(i) > 2 and i[2] or {}
r = func(*args, **kw)
finally:
os.setegid(0)
os.seteuid(0)
os.setgroups(groups)
os.setegid(egid)
os.seteuid(euid)
return r
示例15: testNoArgFunctions
# 需要导入模块: import os [as 别名]
# 或者: from os import getgroups [as 别名]
def testNoArgFunctions(self):
# test posix functions which take no arguments and have
# no side-effects which we need to cleanup (e.g., fork, wait, abort)
NO_ARG_FUNCTIONS = [ "ctermid", "getcwd", "getcwdb", "uname",
"times", "getloadavg",
"getegid", "geteuid", "getgid", "getgroups",
"getpid", "getpgrp", "getppid", "getuid", "sync",
]
for name in NO_ARG_FUNCTIONS:
posix_func = getattr(posix, name, None)
if posix_func is not None:
posix_func()
self.assertRaises(TypeError, posix_func, 1)