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 )
python类supports_fd()的实例源码
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)
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)
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)
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)
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)
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
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)
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
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)