当前位置: 首页>>代码示例>>Python>>正文


Python os.fchown方法代码示例

本文整理汇总了Python中os.fchown方法的典型用法代码示例。如果您正苦于以下问题:Python os.fchown方法的具体用法?Python os.fchown怎么用?Python os.fchown使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在os的用法示例。


在下文中一共展示了os.fchown方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: set_file_owner

# 需要导入模块: import os [as 别名]
# 或者: from os import fchown [as 别名]
def set_file_owner(path, owner, group=None, fd=None):
    if owner:
        uid = pwd.getpwnam(owner).pw_uid
    else:
        uid = os.geteuid()

    if group:
        gid = grp.getgrnam(group).gr_gid
    else:
        gid = os.getegid()

    if fd is not None and hasattr(os, 'fchown'):
        os.fchown(fd, (uid, gid))
    else:
        # Python<2.6
        os.chown(path, (uid, gid)) 
开发者ID:dw,项目名称:mitogen,代码行数:18,代码来源:target.py

示例2: _test_all_chown_common

# 需要导入模块: import os [as 别名]
# 或者: from os import fchown [as 别名]
def _test_all_chown_common(self, chown_func, first_param):
        """Common code for chown, fchown and lchown tests."""
        if os.getuid() == 0:
            try:
                # Many linux distros have a nfsnobody user as MAX_UID-2
                # that makes a good test case for signedness issues.
                #   http://bugs.python.org/issue1747858
                # This part of the test only runs when run as root.
                # Only scary people run their tests as root.
                ent = pwd.getpwnam('nfsnobody')
                chown_func(first_param, ent.pw_uid, ent.pw_gid)
            except KeyError:
                pass
        else:
            # non-root cannot chown to root, raises OSError
            self.assertRaises(OSError, chown_func,
                              first_param, 0, 0)

        # test a successful chown call
        chown_func(first_param, os.getuid(), os.getgid()) 
开发者ID:Acmesec,项目名称:CTFCrackTools-V2,代码行数:22,代码来源:test_posix.py

示例3: test_fchown

# 需要导入模块: import os [as 别名]
# 或者: from os import fchown [as 别名]
def test_fchown(self):
        self.check(os.fchown, -1, -1) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:4,代码来源:test_os.py

示例4: test_fchown

# 需要导入模块: import os [as 别名]
# 或者: from os import fchown [as 别名]
def test_fchown(self):
        os.unlink(test_support.TESTFN)

        # re-create the file
        test_file = open(test_support.TESTFN, 'w')
        try:
            fd = test_file.fileno()
            self._test_all_chown_common(posix.fchown, fd,
                                        getattr(posix, 'fstat', None))
        finally:
            test_file.close() 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:13,代码来源:test_posix.py

示例5: test_fchown

# 需要导入模块: import os [as 别名]
# 或者: from os import fchown [as 别名]
def test_fchown(self):
        if hasattr(os, "fchown"):
            self.check(os.fchown, -1, -1) 
开发者ID:dxwu,项目名称:BinderFilter,代码行数:5,代码来源:test_os.py

示例6: setstat

# 需要导入模块: import os [as 别名]
# 或者: from os import fchown [as 别名]
def setstat(self, filename, attrs, fsetstat=False):
        """setstat and fsetstat requests.

        Filename is an handle in the fstat variant.
        If you're using Python < 3.3,
        you could find useful the futimes file / function.
        """
        if not fsetstat:
            f = os.open(filename, os.O_WRONLY)
            chown = os.chown
            chmod = os.chmod
        else:  # filename is a fd
            f = filename
            chown = os.fchown
            chmod = os.fchmod

        if b'size' in attrs:
            os.ftruncate(f, attrs[b'size'])
        if all(k in attrs for k in (b'uid', b'gid')):
            chown(filename, attrs[b'uid'], attrs[b'gid'])
        if b'perm' in attrs:
            chmod(filename, attrs[b'perm'])

        if all(k in attrs for k in (b'atime', b'mtime')):
            if not fsetstat:
                os.utime(filename, (attrs[b'atime'], attrs[b'mtime']))
            else:
                futimes(filename, (attrs[b'atime'], attrs[b'mtime'])) 
开发者ID:unbit,项目名称:pysftpserver,代码行数:30,代码来源:storage.py

示例7: test_fchown

# 需要导入模块: import os [as 别名]
# 或者: from os import fchown [as 别名]
def test_fchown(self):
        os.unlink(support.TESTFN)

        # re-create the file
        test_file = open(support.TESTFN, 'w')
        try:
            fd = test_file.fileno()
            self._test_all_chown_common(posix.fchown, fd,
                                        getattr(posix, 'fstat', None))
        finally:
            test_file.close() 
开发者ID:Microvellum,项目名称:Fluid-Designer,代码行数:13,代码来源:test_posix.py

示例8: _chown

# 需要导入模块: import os [as 别名]
# 或者: from os import fchown [as 别名]
def _chown(self):
        if self.uid >= 0 or self.gid >= 0:
            os.fchown(self.fh.fileno(), self.uid, self.gid) 
开发者ID:trbs,项目名称:pid,代码行数:5,代码来源:posix.py

示例9: copy

# 需要导入模块: import os [as 别名]
# 或者: from os import fchown [as 别名]
def copy(self, dst, src=None):
        """Atomically copy tickets to destination."""
        if src is None:
            src = self.tkt_path

        dst_dir = os.path.dirname(dst)
        with io.open(src, 'rb') as tkt_src_file:
            # TODO; rewrite as fs.write_safe.
            with tempfile.NamedTemporaryFile(dir=dst_dir,
                                             prefix='.tmp' + self.princ,
                                             delete=False,
                                             mode='wb') as tkt_dst_file:
                try:
                    # Copy binary from source to dest
                    shutil.copyfileobj(tkt_src_file, tkt_dst_file)
                    # Set the owner
                    if self.uid is not None:
                        os.fchown(tkt_dst_file.fileno(), self.uid, -1)
                    # Copy the mode
                    src_stat = os.fstat(tkt_src_file.fileno())
                    os.fchmod(tkt_dst_file.fileno(),
                              stat.S_IMODE(src_stat.st_mode))
                    tkt_dst_file.flush()
                    os.rename(tkt_dst_file.name, dst)

                except (IOError, OSError):
                    _LOGGER.exception('Error copying ticket from %s to %s',
                                      src, dst)
                finally:
                    fs.rm_safe(tkt_dst_file.name) 
开发者ID:Morgan-Stanley,项目名称:treadmill,代码行数:32,代码来源:__init__.py

示例10: test_fchown

# 需要导入模块: import os [as 别名]
# 或者: from os import fchown [as 别名]
def test_fchown(self):
        os.unlink(test_support.TESTFN)

        # re-create the file
        test_file = open(test_support.TESTFN, 'w')
        try:
            fd = test_file.fileno()
            self._test_all_chown_common(posix.fchown, fd)
        finally:
            test_file.close() 
开发者ID:Acmesec,项目名称:CTFCrackTools-V2,代码行数:12,代码来源:test_posix.py

示例11: _test_all_chown_common

# 需要导入模块: import os [as 别名]
# 或者: from os import fchown [as 别名]
def _test_all_chown_common(self, chown_func, first_param, stat_func):
        """Common code for chown, fchown and lchown tests."""
        def check_stat(uid, gid):
            if stat_func is not None:
                stat = stat_func(first_param)
                self.assertEqual(stat.st_uid, uid)
                self.assertEqual(stat.st_gid, gid)
        uid = os.getuid()
        gid = os.getgid()
        # test a successful chown call
        chown_func(first_param, uid, gid)
        check_stat(uid, gid)
        chown_func(first_param, -1, gid)
        check_stat(uid, gid)
        chown_func(first_param, uid, -1)
        check_stat(uid, gid)

        if uid == 0:
            # Try an amusingly large uid/gid to make sure we handle
            # large unsigned values.  (chown lets you use any
            # uid/gid you like, even if they aren't defined.)
            #
            # This problem keeps coming up:
            #   http://bugs.python.org/issue1747858
            #   http://bugs.python.org/issue4591
            #   http://bugs.python.org/issue15301
            # Hopefully the fix in 4591 fixes it for good!
            #
            # This part of the test only runs when run as root.
            # Only scary people run their tests as root.

            big_value = 2**31
            chown_func(first_param, big_value, big_value)
            check_stat(big_value, big_value)
            chown_func(first_param, -1, -1)
            check_stat(big_value, big_value)
            chown_func(first_param, uid, gid)
            check_stat(uid, gid)
        elif platform.system() in ('HP-UX', 'SunOS'):
            # HP-UX and Solaris can allow a non-root user to chown() to root
            # (issue #5113)
            raise unittest.SkipTest("Skipping because of non-standard chown() "
                                    "behavior")
        else:
            # non-root cannot chown to root, raises OSError
            self.assertRaises(OSError, chown_func, first_param, 0, 0)
            check_stat(uid, gid)
            self.assertRaises(OSError, chown_func, first_param, 0, -1)
            check_stat(uid, gid)
            if 0 not in os.getgroups():
                self.assertRaises(OSError, chown_func, first_param, -1, 0)
                check_stat(uid, gid)
        # test illegal types
        for t in str, float:
            self.assertRaises(TypeError, chown_func, first_param, t(uid), gid)
            check_stat(uid, gid)
            self.assertRaises(TypeError, chown_func, first_param, uid, t(gid))
            check_stat(uid, gid) 
开发者ID:IronLanguages,项目名称:ironpython2,代码行数:60,代码来源:test_posix.py


注:本文中的os.fchown方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。