我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用grp.getgrgid()。
def add_group(group_name, system_group=False, gid=None): """Add a group to the system Will log but otherwise succeed if the group already exists. :param str group_name: group to create :param bool system_group: Create system group :param int gid: GID for user being created :returns: The password database entry struct, as returned by `grp.getgrnam` """ try: group_info = grp.getgrnam(group_name) log('group {0} already exists!'.format(group_name)) if gid: group_info = grp.getgrgid(gid) log('group with gid {0} already exists!'.format(gid)) except KeyError: log('creating group {0}'.format(group_name)) add_new_group(group_name, system_group, gid) group_info = grp.getgrnam(group_name) return group_info
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))
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()
def chown(self, cpioinfo, cpiogetpath): """Set owner of cpiogetpath according to cpioinfo. """ if PWD and hasattr(os, "geteuid") and os.geteuid() == 0: # We have to be root to do so. try: g = GRP.getgrgid(cpioinfo.gid)[2] except KeyError: g = os.getgid() try: u = PWD.getpwuid(cpioinfo.uid)[2] except KeyError: u = os.getuid() try: if cpioinfo.issym() and hasattr(os, "lchown"): os.lchown(cpiogetpath, u, g) else: if sys.platform != "os2emx": os.chown(cpiogetpath, u, g) except EnvironmentError: raise ExtractError("could not change owner")
def ironic_prep(): """Prepare ironic user for SSH authentication""" ironic = pwd.getpwnam("ironic") print "creating ssh keys" ironic_pubkey = create_pubkey(ironic) print "setting .ssh/config" ssh_config = "Host *\n\tStrictHostKeyChecking no\n" ssh_config_path = os.path.join(ironic.pw_dir, ".ssh", "config") with open(ssh_config_path, "w") as fh: fh.write(ssh_config) os.chmod(ssh_config_path, 0600) print "populating authorized_keys" path = os.path.join(ironic.pw_dir, ".ssh", "authorized_keys") with open(path, "w") as fh: fh.write(ironic_pubkey) owner = "%s:%s" % (ironic.pw_name, grp.getgrgid(ironic.pw_gid).gr_name) check_call(["chown", "-R", owner, os.path.join(ironic.pw_dir, ".ssh", "authorized_keys")]) # Configure the Ironic bare-metal service
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()
def test_real_group_data(self): """ Assert that L{UserProvider.get_group}'s functionality reflects what is accessible from the Python standard C{grp} module. """ provider = UserProvider() group_0 = grp.getgrgid(0) groups = provider.get_groups() for group in groups: if group["name"] == group_0.gr_name: self.assertEqual(group["gid"], 0) self.assertEqual(group["members"], group_0.gr_mem) break else: self.fail("The group %s (gid=0) was not found in the get_data " "result." % (group_0.gr_name,))
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))
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()