Python os.supports_fd() Examples

The following are code examples for showing how to use os.supports_fd(). They are extracted from open source Python projects. You can vote up the examples you like or vote down the exmaples you don't like. You can also save this page to your account.

Example 1
Project: fusecry   Author: nul-one   File: io.py    (license) View Source Project 6 votes vote down vote up
def touch(self, path, mode=0o644, dir_fd=None, **kwargs):
        """Create an empty file with selected permissions.

        Args:
            path (str): File path.
            mode (:obj:`int`, optional): File access mode. Defaults to 0o644.
            dir_fd (optional): If set, it should be a file descriptor open to a
                directory and path should then be relative to that directory.
                Defaults to None.
            **kwargs: Arbitrary keyword arguments.
        """
        flags = os.O_CREAT | os.O_APPEND
        with os.fdopen(
                os.open(path, flags=flags, mode=mode, dir_fd=dir_fd) ) as f:
            os.utime(
                f.fileno() if os.utime in os.supports_fd else path,
                dir_fd=None if os.supports_fd else dir_fd,
                **kwargs ) 
Example 2
Project: web_ctp   Author: molebot   File: test_shutil.py    (license) View Source Project 6 votes vote down vote up
def test_rmtree_uses_safe_fd_version_if_available(self):
        _use_fd_functions = ({os.open, os.stat, os.unlink, os.rmdir} <=
                             os.supports_dir_fd and
                             os.listdir in os.supports_fd and
                             os.stat in os.supports_follow_symlinks)
        if _use_fd_functions:
            self.assertTrue(shutil._use_fd_functions)
            self.assertTrue(shutil.rmtree.avoids_symlink_attacks)
            tmp_dir = self.mkdtemp()
            d = os.path.join(tmp_dir, 'a')
            os.mkdir(d)
            try:
                real_rmtree = shutil._rmtree_safe_fd
                class Called(Exception): pass
                def _raiser(*args, **kwargs):
                    raise Called
                shutil._rmtree_safe_fd = _raiser
                self.assertRaises(Called, shutil.rmtree, d)
            finally:
                shutil._rmtree_safe_fd = real_rmtree
        else:
            self.assertFalse(shutil._use_fd_functions)
            self.assertFalse(shutil.rmtree.avoids_symlink_attacks) 
Example 3
Project: ouroboros   Author: pybee   File: test_shutil.py    (license) View Source Project 6 votes vote down vote up
def test_rmtree_uses_safe_fd_version_if_available(self):
        _use_fd_functions = ({os.open, os.stat, os.unlink, os.rmdir} <=
                             os.supports_dir_fd and
                             os.listdir in os.supports_fd and
                             os.stat in os.supports_follow_symlinks)
        if _use_fd_functions:
            self.assertTrue(shutil._use_fd_functions)
            self.assertTrue(shutil.rmtree.avoids_symlink_attacks)
            tmp_dir = self.mkdtemp()
            d = os.path.join(tmp_dir, 'a')
            os.mkdir(d)
            try:
                real_rmtree = shutil._rmtree_safe_fd
                class Called(Exception): pass
                def _raiser(*args, **kwargs):
                    raise Called
                shutil._rmtree_safe_fd = _raiser
                self.assertRaises(Called, shutil.rmtree, d)
            finally:
                shutil._rmtree_safe_fd = real_rmtree
        else:
            self.assertFalse(shutil._use_fd_functions)
            self.assertFalse(shutil.rmtree.avoids_symlink_attacks) 
Example 4
Project: kbe_server   Author: xiaohaoppy   File: test_shutil.py    (license) View Source Project 6 votes vote down vote up
def test_rmtree_uses_safe_fd_version_if_available(self):
        _use_fd_functions = ({os.open, os.stat, os.unlink, os.rmdir} <=
                             os.supports_dir_fd and
                             os.listdir in os.supports_fd and
                             os.stat in os.supports_follow_symlinks)
        if _use_fd_functions:
            self.assertTrue(shutil._use_fd_functions)
            self.assertTrue(shutil.rmtree.avoids_symlink_attacks)
            tmp_dir = self.mkdtemp()
            d = os.path.join(tmp_dir, 'a')
            os.mkdir(d)
            try:
                real_rmtree = shutil._rmtree_safe_fd
                class Called(Exception): pass
                def _raiser(*args, **kwargs):
                    raise Called
                shutil._rmtree_safe_fd = _raiser
                self.assertRaises(Called, shutil.rmtree, d)
            finally:
                shutil._rmtree_safe_fd = real_rmtree
        else:
            self.assertFalse(shutil._use_fd_functions)
            self.assertFalse(shutil.rmtree.avoids_symlink_attacks) 
Example 5
Project: plaid2text   Author: madhat2r   File: config_manager.py    (license) View Source Project 5 votes vote down vote up
def touch(fname, mode=0o666, dir_fd=None, **kwargs):
    """
    Implementation of coreutils touch
    http://stackoverflow.com/a/1160227
    """
    flags = os.O_CREAT | os.O_APPEND
    with os.fdopen(os.open(fname, flags=flags, mode=mode, dir_fd=dir_fd)) as f:
        os.utime(f.fileno() if os.utime in os.supports_fd else fname,
            dir_fd=None if os.supports_fd else dir_fd, **kwargs) 
Example 6
Project: gprime   Author: GenealogyCollective   File: generic.py    (license) View Source Project 5 votes vote down vote up
def touch(fname, mode=0o666, dir_fd=None, **kwargs):
    ## After http://stackoverflow.com/questions/1158076/implement-touch-using-python
    if sys.version_info < (3, 3, 0):
        with open(fname, 'a'):
            os.utime(fname, None) # set to now
    else:
        flags = os.O_CREAT | os.O_APPEND
        with os.fdopen(os.open(fname, flags=flags, mode=mode, dir_fd=dir_fd)) as f:
            os.utime(f.fileno() if os.utime in os.supports_fd else fname,
                     dir_fd=None if os.supports_fd else dir_fd, **kwargs) 
Example 7
Project: loris-redux   Author: jpstroop   File: file_system_resolver_tests.py    (license) View Source Project 5 votes vote down vote up
def touch(fname, time=None):
    # not a real equivalent to `touch`, but close enough
    flags = os.O_CREAT | os.O_APPEND
    time = datetime.now().timestamp() if time is None else time
    with os.fdopen(os.open(fname, flags=flags, mode=0o666)) as f:
        path = f.fileno() if os.utime in os.supports_fd else fname
        os.utime(path, times=(time, time))
        return fname 
Example 8
Project: AlarmBot   Author: guysoft   File: alarm.py    (license) View Source Project 5 votes vote down vote up
def touch(fname, mode=0o666, dir_fd=None, **kwargs):
    flags = os.O_CREAT | os.O_APPEND
    with os.fdopen(os.open(fname, flags=flags, mode=mode, dir_fd=dir_fd)) as f:
        os.utime(f.fileno() if os.utime in os.supports_fd else fname,
                 dir_fd=None if os.supports_fd else dir_fd, **kwargs) 
Example 9
Project: OverrideAudit   Author: OdatNurd   File: core.py    (license) View Source Project 5 votes vote down vote up
def _touch_override(self, view, zFile, pkg_name, override):
        new_mtime = None
        now = time()
        fname = os.path.join(sublime.packages_path(), pkg_name, override)

        try:
            entry = find_zip_entry(zFile, override)
            zTime = datetime(*entry.date_time).timestamp()

            if zTime > now:
                log("Warning: The packaged '%s/%s' file is from the future" ,
                     pkg_name, override)
                new_mtime = (now, zTime + 1)

            with os.fdopen(os.open(fname, os.O_RDONLY)) as f:
                os.utime(f.fileno() if os.utime in os.supports_fd else fname,
                         new_mtime)

            # TODO: This command could take a list of overrides in the package
            # and handle them all at once.
            view.run_command("override_audit_modify_mark", {
                "package": pkg_name,
                "override": override
            })

            return True
        except:
            return False 
Example 10
Project: python-sense-emu   Author: RPi-Distro   File: screen.py    (license) View Source Project 5 votes vote down vote up
def _touch_run(self):
        while not self._touch_stop.wait(1):
            # "touch" the screen's frame-buffer once a second. This ensures
            # that the screen always updates at least once a second and works
            # around the issue that screen updates can be lost due to lack of
            # resolution of the file modification timestamps. Unfortunately,
            # futimes(3) is not universally supported, and only available in
            # Python 3.3+ so this gets a bit convoluted...
            try:
                if os.utime in os.supports_fd:
                    os.utime(self._fd.fileno())
                else:
                    raise NotImplementedError
            except (AttributeError, NotImplementedError) as e:
                os.utime(self._fd.name, None)