當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。