本文整理匯總了Python中grp.getgrgid方法的典型用法代碼示例。如果您正苦於以下問題:Python grp.getgrgid方法的具體用法?Python grp.getgrgid怎麽用?Python grp.getgrgid使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類grp
的用法示例。
在下文中一共展示了grp.getgrgid方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: setUpClass
# 需要導入模塊: import grp [as 別名]
# 或者: from grp import getgrgid [as 別名]
def setUpClass(cls):
# Tests are depending on two existing and distinct users and groups.
# We chose 'root' with uid/gid 0 and 'nobody', because both exist on
# all relevant platforms. Tests use a mocked request so they run
# under any user.
cls.user = user = pwd.getpwnam('nobody')
cls.group = group = grp.getgrgid(user.pw_gid)
cls.parser = configparser.ConfigParser(
interpolation=configparser.ExtendedInterpolation(),
defaults={
'other_uid': str(user.pw_uid),
'other_username': user.pw_name,
'other_gid': str(group.gr_gid),
'other_groupname': group.gr_name,
}
)
cls.parser.read_string(CONFIG)
示例2: add_user
# 需要導入模塊: import grp [as 別名]
# 或者: from grp import getgrgid [as 別名]
def add_user(self, user):
'''Import a user into /etc/passwd on the image. You can specify a
username, in which case add_user will look up the password entry
via getpwnam(), or you can provide a colon-delimited password
entry, which will be used verbatim.'''
LOG.info('adding user %s', user)
if ':' in user:
self.users.append(user)
else:
pwent = pwd.getpwnam(user)
self.users.append(':'.join(str(x) for x in pwent))
grent = grp.getgrgid(pwent.pw_gid)
self.groups.append(':'.join(str(x) for x in
grent[:3] + (','.join(grent[3]),)
if not isinstance(x, list)))
示例3: check_for_writable_imgdir
# 需要導入模塊: import grp [as 別名]
# 或者: from grp import getgrgid [as 別名]
def check_for_writable_imgdir():
c.debug("Testing write perms by creating tempfile in {}".format(cfg.opts.img_dir))
try:
f = tempfile.TemporaryFile(dir=cfg.opts.img_dir)
f.close()
except:
dirstat = os.stat(cfg.opts.img_dir)
user = pwd.getpwuid(dirstat.st_uid).pw_name
group = grp.getgrgid(dirstat.st_gid).gr_name
print(c.RED("Unable to create new file in image dir '{}' owned by {}:{}".format(cfg.opts.img_dir, user, group)))
if myUser in grp.getgrnam(group).gr_mem:
print("Your user ({}) *is* a member of the appropriate group ({}); however ...\n"
"Your current login session is not running with that group credential\n"
"To fix this, open a new session (ssh/su -) or log out & log back in (GUI)".format(myUser, group))
else:
print("Either fix directory permissions as root or specify alternate dir with '--img-dir' option")
exit(1)
示例4: test_make_archive_owner_group
# 需要導入模塊: import grp [as 別名]
# 或者: from grp import getgrgid [as 別名]
def test_make_archive_owner_group(self):
# testing make_archive with owner and group, with various combinations
# this works even if there's not gid/uid support
if UID_GID_SUPPORT:
group = grp.getgrgid(0)[0]
owner = pwd.getpwuid(0)[0]
else:
group = owner = 'root'
base_dir, root_dir, base_name = self._create_files()
base_name = os.path.join(self.mkdtemp() , 'archive')
res = make_archive(base_name, 'zip', root_dir, base_dir, owner=owner,
group=group)
self.assertTrue(os.path.exists(res))
res = make_archive(base_name, 'zip', root_dir, base_dir)
self.assertTrue(os.path.exists(res))
res = make_archive(base_name, 'tar', root_dir, base_dir,
owner=owner, group=group)
self.assertTrue(os.path.exists(res))
res = make_archive(base_name, 'tar', root_dir, base_dir,
owner='kjhkjhkjg', group='oihohoh')
self.assertTrue(os.path.exists(res))
示例5: test_tarfile_root_owner
# 需要導入模塊: import grp [as 別名]
# 或者: from grp import getgrgid [as 別名]
def test_tarfile_root_owner(self):
tmpdir, tmpdir2, base_name = self._create_files()
old_dir = os.getcwd()
os.chdir(tmpdir)
group = grp.getgrgid(0)[0]
owner = pwd.getpwuid(0)[0]
try:
archive_name = make_tarball(base_name, 'dist', compress=None,
owner=owner, group=group)
finally:
os.chdir(old_dir)
# check if the compressed tarball was created
self.assertTrue(os.path.exists(archive_name))
# now checks the rights
archive = tarfile.open(archive_name)
try:
for member in archive.getmembers():
self.assertEqual(member.uid, 0)
self.assertEqual(member.gid, 0)
finally:
archive.close()
示例6: test_make_archive_owner_group
# 需要導入模塊: import grp [as 別名]
# 或者: from grp import getgrgid [as 別名]
def test_make_archive_owner_group(self):
# testing make_archive with owner and group, with various combinations
# this works even if there's not gid/uid support
if UID_GID_SUPPORT:
group = grp.getgrgid(0)[0]
owner = pwd.getpwuid(0)[0]
else:
group = owner = 'root'
root_dir, base_dir = self._create_files()
base_name = os.path.join(self.mkdtemp(), 'archive')
res = make_archive(base_name, 'zip', root_dir, base_dir, owner=owner,
group=group)
self.assertTrue(os.path.isfile(res))
res = make_archive(base_name, 'zip', root_dir, base_dir)
self.assertTrue(os.path.isfile(res))
res = make_archive(base_name, 'tar', root_dir, base_dir,
owner=owner, group=group)
self.assertTrue(os.path.isfile(res))
res = make_archive(base_name, 'tar', root_dir, base_dir,
owner='kjhkjhkjg', group='oihohoh')
self.assertTrue(os.path.isfile(res))
示例7: test_tarfile_root_owner
# 需要導入模塊: import grp [as 別名]
# 或者: from grp import getgrgid [as 別名]
def test_tarfile_root_owner(self):
root_dir, base_dir = self._create_files()
base_name = os.path.join(self.mkdtemp(), 'archive')
group = grp.getgrgid(0)[0]
owner = pwd.getpwuid(0)[0]
with support.change_cwd(root_dir):
archive_name = make_archive(base_name, 'gztar', root_dir, 'dist',
owner=owner, group=group)
# check if the compressed tarball was created
self.assertTrue(os.path.isfile(archive_name))
# now checks the rights
archive = tarfile.open(archive_name)
try:
for member in archive.getmembers():
self.assertEqual(member.uid, 0)
self.assertEqual(member.gid, 0)
finally:
archive.close()
示例8: test_tarfile_root_owner
# 需要導入模塊: import grp [as 別名]
# 或者: from grp import getgrgid [as 別名]
def test_tarfile_root_owner(self):
tmpdir, tmpdir2, base_name = self._create_files()
old_dir = os.getcwd()
os.chdir(tmpdir)
group = grp.getgrgid(0)[0]
owner = pwd.getpwuid(0)[0]
try:
archive_name = _make_tarball(base_name, 'dist', compress=None,
owner=owner, group=group)
finally:
os.chdir(old_dir)
# check if the compressed tarball was created
self.assertTrue(os.path.exists(archive_name))
# now checks the rights
archive = tarfile.open(archive_name)
try:
for member in archive.getmembers():
self.assertEqual(member.uid, 0)
self.assertEqual(member.gid, 0)
finally:
archive.close()
示例9: _stat_group
# 需要導入模塊: import grp [as 別名]
# 或者: from grp import getgrgid [as 別名]
def _stat_group(self, fp):
"""
Get the filepath's owner's group. If this is not implemented
(say in Windows) return the string "0" since stat-ing a file in
Windows seems to return C{st_gid=0}.
(Reference:
U{http://stackoverflow.com/questions/5275731/os-stat-on-windows})
@param fp: L{twisted.python.filepath.FilePath}
@return: C{str} representing the owner's group
"""
try:
groupID = fp.getGroupID()
except NotImplementedError:
return "0"
else:
if grp is not None:
try:
return grp.getgrgid(groupID)[0]
except KeyError:
pass
return str(groupID)
示例10: extend_app
# 需要導入模塊: import grp [as 別名]
# 或者: from grp import getgrgid [as 別名]
def extend_app(app):
"""
Adds the ``--daemon`` argument to the argument object, and sets the
default ``[daemon]`` config section options.
"""
global CEMENT_DAEMON_APP
CEMENT_DAEMON_APP = app
app.args.add_argument('--daemon', dest='daemon',
action='store_true', help='daemonize the process')
# Add default config
user = pwd.getpwnam(os.getlogin())
group = grp.getgrgid(user.pw_gid)
defaults = dict()
defaults['daemon'] = dict()
defaults['daemon']['user'] = user.pw_name
defaults['daemon']['group'] = group.gr_name
defaults['daemon']['pid_file'] = None
defaults['daemon']['dir'] = '/'
defaults['daemon']['umask'] = 0
app.config.merge(defaults, override=False)
app.extend('daemonize', daemonize)
示例11: __make_workdir
# 需要導入模塊: import grp [as 別名]
# 或者: from grp import getgrgid [as 別名]
def __make_workdir(self):
timestamp = str(time.time()).replace(".", "")
self.workdir = os.path.join(CDIR, f".stimela_workdir-{timestamp}")
while os.path.exists(self.workdir):
timestamp = str(time.time()).replace(".", "")
self.workdir = os.path.join(CDIR, f".stimela_workdir-{timestamp}")
os.mkdir(self.workdir)
# create passwd and group files to be mounted inside the container
template_dir = os.path.join(os.path.dirname(__file__), "cargo/base")
# get current user info
pw = pwd.getpwuid(os.getuid())
gr = grp.getgrgid(pw.pw_gid)
with open(os.path.join(self.workdir, "passwd"), "wt") as file:
file.write(open(os.path.join(template_dir, "passwd.template"), "rt").read())
file.write(f"{pw.pw_name}:x:{pw.pw_uid}:{pw.pw_gid}:{pw.pw_gecos}:/:/bin/bash")
with open(os.path.join(self.workdir, "group"), "wt") as file:
file.write(open(os.path.join(template_dir, "group.template"), "rt").read())
file.write(f"{gr.gr_name}:x:{gr.gr_gid}:")
示例12: test_tarfile_root_owner
# 需要導入模塊: import grp [as 別名]
# 或者: from grp import getgrgid [as 別名]
def test_tarfile_root_owner(self):
tmpdir = self._create_files()
base_name = os.path.join(self.mkdtemp(), 'archive')
old_dir = os.getcwd()
os.chdir(tmpdir)
group = grp.getgrgid(0)[0]
owner = pwd.getpwuid(0)[0]
try:
archive_name = make_tarball(base_name, 'dist', compress=None,
owner=owner, group=group)
finally:
os.chdir(old_dir)
# check if the compressed tarball was created
self.assertTrue(os.path.exists(archive_name))
# now checks the rights
archive = tarfile.open(archive_name)
try:
for member in archive.getmembers():
self.assertEqual(member.uid, 0)
self.assertEqual(member.gid, 0)
finally:
archive.close()
示例13: default_run_group
# 需要導入模塊: import grp [as 別名]
# 或者: from grp import getgrgid [as 別名]
def default_run_group():
if os.name == 'nt':
return '#0'
try:
import pwd
uid = os.getuid()
entry = pwd.getpwuid(uid)
except KeyError:
return '#%d' % uid
try:
import grp
gid = entry.pw_gid
return grp.getgrgid(gid).gr_name
except KeyError:
return '#%d' % gid
示例14: extend_app
# 需要導入模塊: import grp [as 別名]
# 或者: from grp import getgrgid [as 別名]
def extend_app(app):
"""
Adds the ``--daemon`` argument to the argument object, and sets the
default ``[daemon]`` config section options.
"""
global CEMENT_DAEMON_APP
CEMENT_DAEMON_APP = app
app.args.add_argument('--daemon', dest='daemon',
action='store_true', help='daemonize the process')
# Add default config
user = pwd.getpwuid(os.getuid())
group = grp.getgrgid(user.pw_gid)
defaults = dict()
defaults['daemon'] = dict()
defaults['daemon']['user'] = user.pw_name
defaults['daemon']['group'] = group.gr_name
defaults['daemon']['pid_file'] = None
defaults['daemon']['dir'] = '/'
defaults['daemon']['umask'] = 0
app.config.merge(defaults, override=False)
app.extend('daemonize', daemonize)
示例15: group
# 需要導入模塊: import grp [as 別名]
# 或者: from grp import getgrgid [as 別名]
def group(self):
""" return group name of file. """
if iswin32:
raise NotImplementedError("XXX win32")
import grp
entry = py.error.checked_call(grp.getgrgid, self.gid)
return entry[0]