本文整理匯總了Python中stat.S_IWRITE屬性的典型用法代碼示例。如果您正苦於以下問題:Python stat.S_IWRITE屬性的具體用法?Python stat.S_IWRITE怎麽用?Python stat.S_IWRITE使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類stat
的用法示例。
在下文中一共展示了stat.S_IWRITE屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: copymode
# 需要導入模塊: import stat [as 別名]
# 或者: from stat import S_IWRITE [as 別名]
def copymode(src, dst, follow_symlinks=True, **kwargs):
"""
Copy the permission bits from src to dst. The file contents, owner, and group are unaffected. Due to the
limitations of Windows, this function only sets/unsets the dst's FILE_ATTRIBUTE_READ_ONLY flag based on what src's
attribute is set to.
If follow_symlinks is 'False', and both src and dst are symbolic links, copymode() will attempt to modify the mode
of dst itself (rather than the file it points to).
This function supports src and dst being either a local or UNC path. A relative path will be resolved based on the
current working directory.
:param src: The src file or directory to copy the read only flag from.
:param dst: The dst file or directory to copy the read only flag to.
:param follow_symlinks: Whether to copy the read only flag on the symlink or the target of the symlink.
:param kwargs: Common arguments used to build the SMB Session for any UNC paths.
"""
src_mode = stat.S_IMODE(_get_file_stat(src, follow_symlinks, **kwargs).st_mode)
norm_dst = ntpath.normpath(dst)
if norm_dst.startswith('\\\\'):
read_only = not (src_mode & stat.S_IWRITE == stat.S_IWRITE and src_mode & stat.S_IREAD == stat.S_IREAD)
_set_file_basic_info(dst, follow_symlinks, read_only=read_only, **kwargs)
else:
_local_chmod(dst, src_mode, follow_symlinks)
示例2: test_copymode_remote_to_local
# 需要導入模塊: import stat [as 別名]
# 或者: from stat import S_IWRITE [as 別名]
def test_copymode_remote_to_local(smb_share, tmpdir):
test_dir = tmpdir.mkdir("test")
src_filename = "%s\\source.txt" % smb_share
dst_filename = "%s\\target.txt" % test_dir
with open_file(src_filename, mode='w', file_attributes=FileAttributes.FILE_ATTRIBUTE_READONLY) as fd:
fd.write(u"content")
with open(dst_filename, mode='w') as fd:
fd.write(u"content")
copymode(src_filename, dst_filename)
actual = os.stat(dst_filename).st_mode
assert stat.S_IMODE(actual) & stat.S_IWRITE == 0
remove(src_filename)
with open_file(src_filename, mode='w') as fd:
fd.write(u"content")
copymode(src_filename, dst_filename)
actual = os.stat(dst_filename).st_mode
assert stat.S_IMODE(actual) & stat.S_IWRITE == stat.S_IWRITE
示例3: test_copystat_remote_to_local
# 需要導入模塊: import stat [as 別名]
# 或者: from stat import S_IWRITE [as 別名]
def test_copystat_remote_to_local(smb_share, tmpdir):
test_dir = tmpdir.mkdir("test")
src_filename = "%s\\source.txt" % smb_share
dst_filename = "%s\\target.txt" % test_dir
with open_file(src_filename, mode='w', file_attributes=FileAttributes.FILE_ATTRIBUTE_READONLY) as fd:
fd.write(u"content")
utime(src_filename, times=(1024, 1024))
with open(dst_filename, mode='w') as fd:
fd.write(u"content")
copystat(src_filename, dst_filename)
actual = os.stat(dst_filename)
assert actual.st_atime == 1024
assert actual.st_mtime == 1024
assert stat.S_IMODE(actual.st_mode) & stat.S_IWRITE == 0
示例4: test_copystat_local_to_local
# 需要導入模塊: import stat [as 別名]
# 或者: from stat import S_IWRITE [as 別名]
def test_copystat_local_to_local(tmpdir):
test_dir = tmpdir.mkdir("test")
src_filename = "%s\\source.txt" % test_dir
dst_filename = "%s\\target.txt" % test_dir
with open(src_filename, mode='w') as fd:
fd.write(u"content")
os.chmod(src_filename, stat.S_IREAD)
os.utime(src_filename, (1024, 1024))
with open(dst_filename, mode='w') as fd:
fd.write(u"content")
copystat(src_filename, dst_filename)
actual = os.stat(dst_filename)
assert actual.st_atime == 1024
assert actual.st_mtime == 1024
assert stat.S_IMODE(actual.st_mode) & stat.S_IWRITE == 0
示例5: ___test_folder_not_writeable
# 需要導入模塊: import stat [as 別名]
# 或者: from stat import S_IWRITE [as 別名]
def ___test_folder_not_writeable(self):
# make sure we get permissions error and not 'database is locked'
s = ts.SetupDbAndCredentials()
s.test_setup("test_folder_not_writeable", trash_files=True, trash_db=True)
try:
if os.name == "nt":
os.chmod(str(s.root), stat.S_IREAD)
else:
s.root.chmod(0o444)
with self.assertRaises(PermissionError):
s.gp.main([str(s.root), "--skip-shared-albums"])
finally:
if os.name == "nt":
os.chmod(str(s.root), stat.S_IWRITE | stat.S_IREAD)
else:
os.chmod(str(s.root), 0o777)
shutil.rmtree(str(s.root))
示例6: open
# 需要導入模塊: import stat [as 別名]
# 或者: from stat import S_IWRITE [as 別名]
def open(self):
"""Reads file.
:return: byte string.
"""
if self.path.endswith('.mp3'):
try:
stream = open(self.path, 'r+b')
except PermissionError:
os.chmod(self.path, S_IWRITE)
stream = open(self.path, 'r+b')
else:
raise MP3OpenFileError('File must be MP3 format')
data = stream.read()
stream.close()
return data
示例7: rmtree_errorhandler
# 需要導入模塊: import stat [as 別名]
# 或者: from stat import S_IWRITE [as 別名]
def rmtree_errorhandler(func, path, exc_info):
"""
On Windows, some files are read-only (e.g. in in .svn dirs), so when
rmtree() tries to remove them, an exception is thrown.
We catch that here, remove the read-only attribute, and hopefully
continue without problems.
"""
exctype, value = exc_info[:2]
# looking for a windows error
if exctype is not WindowsError or 'Access is denied' not in str(value):
raise
# file type should currently be read only
if ((os.stat(path).st_mode & stat.S_IREAD) != stat.S_IREAD):
raise
# convert to read/write
os.chmod(path, stat.S_IWRITE)
# use the original function to repeat the operation
func(path)
示例8: copyFunc
# 需要導入模塊: import stat [as 別名]
# 或者: from stat import S_IWRITE [as 別名]
def copyFunc(dest, source, env):
"""Install a source file or directory into a destination by copying,
(including copying permission/mode bits)."""
if os.path.isdir(source):
if os.path.exists(dest):
if not os.path.isdir(dest):
raise SCons.Errors.UserError("cannot overwrite non-directory `%s' with a directory `%s'" % (str(dest), str(source)))
else:
parent = os.path.split(dest)[0]
if not os.path.exists(parent):
os.makedirs(parent)
scons_copytree(source, dest)
else:
shutil.copy2(source, dest)
st = os.stat(source)
os.chmod(dest, stat.S_IMODE(st[stat.ST_MODE]) | stat.S_IWRITE)
return 0
#
# Functions doing the actual work of the InstallVersionedLib Builder.
#
示例9: copyFuncVersionedLib
# 需要導入模塊: import stat [as 別名]
# 或者: from stat import S_IWRITE [as 別名]
def copyFuncVersionedLib(dest, source, env):
"""Install a versioned library into a destination by copying,
(including copying permission/mode bits) and then creating
required symlinks."""
if os.path.isdir(source):
raise SCons.Errors.UserError("cannot install directory `%s' as a version library" % str(source) )
else:
# remove the link if it is already there
try:
os.remove(dest)
except:
pass
shutil.copy2(source, dest)
st = os.stat(source)
os.chmod(dest, stat.S_IMODE(st[stat.ST_MODE]) | stat.S_IWRITE)
installShlibLinks(dest, source, env)
return 0
示例10: CacheRetrieveFunc
# 需要導入模塊: import stat [as 別名]
# 或者: from stat import S_IWRITE [as 別名]
def CacheRetrieveFunc(target, source, env):
t = target[0]
fs = t.fs
cd = env.get_CacheDir()
cachedir, cachefile = cd.cachepath(t)
if not fs.exists(cachefile):
cd.CacheDebug('CacheRetrieve(%s): %s not in cache\n', t, cachefile)
return 1
cd.CacheDebug('CacheRetrieve(%s): retrieving from %s\n', t, cachefile)
if SCons.Action.execute_actions:
if fs.islink(cachefile):
fs.symlink(fs.readlink(cachefile), t.get_internal_path())
else:
env.copy_from_cache(cachefile, t.get_internal_path())
try:
os.utime(cachefile, None)
except OSError:
pass
st = fs.stat(cachefile)
fs.chmod(t.get_internal_path(), stat.S_IMODE(st[stat.ST_MODE]) | stat.S_IWRITE)
return 0
示例11: removePath
# 需要導入模塊: import stat [as 別名]
# 或者: from stat import S_IWRITE [as 別名]
def removePath(path):
if sys.platform == "win32":
def onerror(func, path, exc):
os.chmod(path, stat.S_IWRITE)
os.unlink(path)
else:
onerror = None
try:
if os.path.lexists(path):
if os.path.isdir(path) and not os.path.islink(path):
shutil.rmtree(path, onerror=onerror)
else:
os.unlink(path)
except OSError as e:
raise BuildError("Error removing '"+path+"': " + str(e))
示例12: test_remove_negative
# 需要導入模塊: import stat [as 別名]
# 或者: from stat import S_IWRITE [as 別名]
def test_remove_negative(self):
import stat
self.assertRaisesNumber(WindowsError, errno.ENOENT, lambda : nt.remove('some_file_that_does_not_exist'))
try:
file('some_test_file.txt', 'w').close()
nt.chmod('some_test_file.txt', stat.S_IREAD)
self.assertRaisesNumber(WindowsError, errno.EACCES, lambda : nt.remove('some_test_file.txt'))
nt.chmod('some_test_file.txt', stat.S_IWRITE)
f = file('some_test_file.txt', 'w+')
self.assertRaisesNumber(WindowsError, errno.EACCES, lambda : nt.remove('some_test_file.txt'))
f.close()
finally:
nt.chmod('some_test_file.txt', stat.S_IWRITE)
nt.unlink('some_test_file.txt')
# rename tests
示例13: test_access
# 需要導入模塊: import stat [as 別名]
# 或者: from stat import S_IWRITE [as 別名]
def test_access(self):
f = file('new_file_name', 'w')
f.close()
self.assertEqual(nt.access('new_file_name', nt.F_OK), True)
self.assertEqual(nt.access('new_file_name', nt.R_OK), True)
self.assertEqual(nt.access('does_not_exist.py', nt.F_OK), False)
self.assertEqual(nt.access('does_not_exist.py', nt.R_OK), False)
nt.chmod('new_file_name', 0x100) # S_IREAD
self.assertEqual(nt.access('new_file_name', nt.W_OK), False)
nt.chmod('new_file_name', 0x80) # S_IWRITE
nt.unlink('new_file_name')
nt.mkdir('new_dir_name')
self.assertEqual(nt.access('new_dir_name', nt.R_OK), True)
nt.rmdir('new_dir_name')
self.assertRaises(TypeError, nt.access, None, 1)
示例14: CacheRetrieveFunc
# 需要導入模塊: import stat [as 別名]
# 或者: from stat import S_IWRITE [as 別名]
def CacheRetrieveFunc(target, source, env):
t = target[0]
fs = t.fs
cd = env.get_CacheDir()
cachedir, cachefile = cd.cachepath(t)
if not fs.exists(cachefile):
cd.CacheDebug('CacheRetrieve(%s): %s not in cache\n', t, cachefile)
return 1
cd.CacheDebug('CacheRetrieve(%s): retrieving from %s\n', t, cachefile)
if SCons.Action.execute_actions:
if fs.islink(cachefile):
fs.symlink(fs.readlink(cachefile), t.get_internal_path())
else:
env.copy_from_cache(cachefile, t.get_internal_path())
st = fs.stat(cachefile)
fs.chmod(t.get_internal_path(), stat.S_IMODE(st[stat.ST_MODE]) | stat.S_IWRITE)
return 0
示例15: handle_rm_readonly_files
# 需要導入模塊: import stat [as 別名]
# 或者: from stat import S_IWRITE [as 別名]
def handle_rm_readonly_files(_func, path, exc):
"""Handle read-only files on Windows. Adapted from https://stackoverflow.com/a/21263493.
Args:
_func (function): Function which raised the exception
path (str): Path name passed to function
exc (exception): Exception information returned by sys.exc_info()
Raises:
OSError: Raised if the read-only files are unable to be handled
"""
assert platform.system() == "Windows"
path = Path(path)
if exc[1].errno == errno.EACCES:
Path.chmod(path, stat.S_IWRITE)
assert path.is_file()
path.unlink()
else:
raise OSError("Unable to handle read-only files.")