本文整理汇总了Python中xattr.setxattr函数的典型用法代码示例。如果您正苦于以下问题:Python setxattr函数的具体用法?Python setxattr怎么用?Python setxattr使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了setxattr函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: write_metadata
def write_metadata(fd, metadata, xattr_size=65536, md_key=None):
"""
Helper function to write pickled metadata for an object file.
:param fd: file descriptor or filename to write the metadata
:param md_key: metadata key to be write to object file
:param metadata: metadata to write
"""
meta_key = SWIFT_METADATA_KEY
metastr = pickle.dumps(metadata, PICKLE_PROTOCOL)
key = 0
while metastr:
try:
xattr.setxattr(fd, '%s%s' % (meta_key, key or ''),
metastr[:xattr_size])
metastr = metastr[xattr_size:]
key += 1
except IOError as e:
for err in 'ENOTSUP', 'EOPNOTSUPP':
if hasattr(errno, err) and e.errno == getattr(errno, err):
msg = "Filesystem at %s does not support xattr" % \
_get_filename(fd)
logging.exception(msg)
raise DiskFileXattrNotSupported(e)
if e.errno in (errno.ENOSPC, errno.EDQUOT):
msg = "No space left on device for %s" % _get_filename(fd)
logging.exception(msg)
raise DiskFileNoSpace()
raise
示例2: _process_set_flags
def _process_set_flags(self, account, mailbox, uid, flags):
xattr.setxattr('%s/%s/%s/%d' % (self. root, account, mailbox, uid),
self.xattrname, " ".join(flags))
ret = {"response":"setflags", "account": account,
"mailbox":mailbox, "uid": uid, "status": "OK",
"flags": flags}
return ret
示例3: execute
def execute (self):
op = self._build_operation ('download file', {'channel': self.channel, 'file': self.filename})
f = self._get_url_handler (op)
# Customize the HTTP handler
def download_f_custom_read (self_f, *args):
tmp = self_f.read_orig (*args)
download_f_custom_read.received += len(tmp)
if self.callback_step:
self.callback_step (download_f_custom_read.received)
return tmp
f.read_orig = f.read
f.read = types.MethodType (download_f_custom_read, f)
download_f_custom_read.received = 0
# Decrypt
out_dir = os.path.join (self.download_dir, self.channel)
if not os.path.exists (out_dir):
os.makedirs (out_dir, 0700)
out_fullpath = os.path.join (out_dir, self.filename)
self.keys.decrypt_file_to_path (f, out_fullpath)
# Set file attributes
xattr.setxattr (out_fullpath, 'md5_time', str(time.time()))
xattr.setxattr (out_fullpath, 'md5', utils.md5_file(out_fullpath))
示例4: set_attr
def set_attr(self, attrname, value):
""" Set the value for a raw attribute.
Value should be a string or bytes.
Returns the value on success.
Possibly raises AttrError.
"""
if isinstance(value, bytes):
encodedvalue = value
elif isinstance(value, str):
encodedvalue = value.encode(self.encoding)
else:
valtype = type(value)
raise TypeError(
'Expecting str or bytes. Got: {} ({})'.format(
getattr(valtype, '__name__', valtype),
value))
try:
xattr.setxattr(
self.filepath,
attrname,
encodedvalue,
symlink=self.follow_symlinks)
except EnvironmentError as ex:
raise self.AttrError(
'Unable to set \'{}\' for: {}\n{}'.format(
attrname,
self.filepath,
ex))
return value
示例5: _test_xattr_on_file_with_contents
def _test_xattr_on_file_with_contents(filename, file_contents, xattrs=[], xar_create_flags=[], xar_extract_flags=[]):
try:
# Write file out
with open(filename, "w") as f:
f.write(file_contents)
for (key, value) in xattrs:
xattr.setxattr(f, key, value)
# Store it into a xarchive
archive_name = "{f}.xar".format(f=filename)
with util.archive_created(archive_name, filename, *xar_create_flags) as path:
# Extract xarchive
with util.directory_created("extracted") as directory:
# Validate resulting xattrs
subprocess.check_call(["xar", "-x", "-C", directory, "-f", path] + xar_extract_flags)
for (key, value) in xattrs:
try:
assert xattr.getxattr(os.path.join(directory, filename), key) == value, "extended attribute \"{n}\" has incorrect contents after extraction".format(n=key)
except KeyError:
raise MissingExtendedAttributeError("extended attribute \"{n}\" missing after extraction".format(n=key))
# Validate file contents
with open(os.path.join(directory, filename), "r") as f:
if f.read() != file_contents:
raise MissingExtendedAttributeError("archived file \"{f}\" has has incorrect contents after extraction".format(f=filename))
finally:
os.unlink(filename)
示例6: _checkDeprecated
def _checkDeprecated(self, item, symlink=False):
"""check deprecated list, set, get operations against an item"""
self.assertEqual(self._ignore(xattr.listxattr(item, symlink)),
[])
self.assertRaises(EnvironmentError, xattr.setxattr, item,
self.USER_ATTR, self.USER_VAL,
XATTR_REPLACE)
try:
xattr.setxattr(item, self.USER_ATTR, self.USER_VAL, 0, symlink)
except IOError:
err = sys.exc_info()[1]
if err.errno == errno.EPERM and symlink:
# symlinks may fail, in which case we abort the rest
# of the test for this case
return
raise
self.assertRaises(EnvironmentError, xattr.setxattr, item,
self.USER_ATTR, self.USER_VAL, XATTR_CREATE)
self.assertEqual(self._ignore(xattr.listxattr(item, symlink)),
[self.USER_ATTR])
self.assertEqual(xattr.getxattr(item, self.USER_ATTR, symlink),
self.USER_VAL)
self.assertEqual(self._ignore_tuples(xattr.get_all(item,
nofollow=symlink)),
[(self.USER_ATTR, self.USER_VAL)])
xattr.removexattr(item, self.USER_ATTR)
self.assertEqual(self._ignore(xattr.listxattr(item, symlink)),
[])
self.assertEqual(self._ignore_tuples(xattr.get_all(item,
nofollow=symlink)),
[])
self.assertRaises(EnvironmentError, xattr.removexattr,
item, self.USER_ATTR)
示例7: write_metadata
def write_metadata(path_or_fd, metadata):
"""
Helper function to write pickled metadata for a File/Directory.
:param path_or_fd: File/Directory path or fd to write the metadata
:param metadata: dictionary of metadata write
"""
assert isinstance(metadata, dict)
metastr = pickle.dumps(metadata, PICKLE_PROTOCOL)
key = 0
while metastr:
try:
xattr.setxattr(path_or_fd,
'%s%s' % (METADATA_KEY, key or ''),
metastr[:MAX_XATTR_SIZE])
except IOError as err:
if err.errno in (errno.ENOSPC, errno.EDQUOT):
if isinstance(path_or_fd, int):
filename = get_filename_from_fd(path_or_fd)
do_log_rl("write_metadata(%d, metadata) failed: %s : %s",
path_or_fd, err, filename)
else:
do_log_rl("write_metadata(%s, metadata) failed: %s",
path_or_fd, err)
raise DiskFileNoSpace()
else:
raise GlusterFileSystemIOError(
err.errno,
'xattr.setxattr("%s", %s, metastr)' % (path_or_fd, key))
metastr = metastr[MAX_XATTR_SIZE:]
key += 1
示例8: test_xattr_bad_content_container
def test_xattr_bad_content_container(self):
self.init_content()
xattr.setxattr(
self.chunk_path, 'user.grid.content.container',
self.bad_container_id)
self.assertRaises(exc.OrphanChunk, self.auditor.chunk_audit,
self.chunk_path)
示例9: download
def download(file_path):
'''Download the file by name of "file_path"'''
if (user.is_user_signed_in()):
client = c.get_client()
filename = os.path.basename(file_path)
file_path = sanitize_path(file_path)
if (os.path.isfile(filename) or os.path.isdir(filename)):
logger.die('File or dir with name "' + filename + '" in current directory')
else:
existance = check_existance(file_path, client)
if (existance[0]):
try:
client.download_file("mf:" + file_path , '.')
file_info = existance[1]
xattr.setxattr(filename, 'hash', binascii.a2b_qp(file_info['hash']))
logger.log('File "' + filename + '" downloaded successfully.')
except NotAFolderError:
logger.die('Path "' + remote_path + '" not found on MediaFire')
except ResourceNotFoundError:
logger.die('Path "' + remote_path + '" not found on MediaFire.')
except requests.exceptions.RequestException:
logger.die('Network error, please check network status and try again')
else:
logger.log('File path and name "' + file_path + '" does not exist.')
else:
user.get_auth()
示例10: set_xattr
def set_xattr(path, key, value):
"""Set the value of a specified xattr.
If xattrs aren't supported by the file-system, we skip setting the value.
"""
namespaced_key = _make_namespaced_xattr_key(key)
xattr.setxattr(path, namespaced_key, str(value))
示例11: set_xattr
def set_xattr(self, filepath, key, value):
logger.info("set_xattr - %s, %s=%s" % (filepath, key, value))
with self._get_lock():
ascii_path = filepath.encode('ascii', 'ignore')
localfs_path = self._make_localfs_path(ascii_path)
xattr.setxattr(localfs_path, key, value)
示例12: is_partition_supported
def is_partition_supported(self, folder):
if folder is None:
return False
result = False
to_delete = not os.path.exists(folder)
try:
if to_delete:
os.mkdir(folder)
if not os.access(folder, os.W_OK):
import stat
os.chmod(folder, stat.S_IXUSR | stat.S_IRGRP | stat.S_IXGRP |
stat.S_IRUSR | stat.S_IWGRP | stat.S_IWUSR)
import xattr
attr = "drive-test"
xattr.setxattr(folder, attr, attr)
if xattr.getxattr(folder, attr) == attr:
result = True
xattr.removexattr(folder, attr)
finally:
try:
if to_delete:
os.rmdir(folder)
except:
pass
return result
示例13: test_finder_in_use
def test_finder_in_use(self):
if not AbstractOSIntegration.is_mac():
raise SkipTest('Not relevant on Linux or Windows')
self.engine_1.start()
self.wait_sync(wait_for_async=True)
self.local_client_1.make_file('/', u'File.txt',
content=u'Some Content 1'.encode('utf-8'))
# Emulate the Finder in use flag
OSX_FINDER_INFO_ENTRY_SIZE = 32
key = (OSX_FINDER_INFO_ENTRY_SIZE)*[0]
key[0] = 0x62
key[1] = 0x72
key[2] = 0x6F
key[3] = 0x6B
key[4] = 0x4D
key[5] = 0x41
key[6] = 0x43
key[7] = 0x53
import xattr
xattr.setxattr(self.local_client_1._abspath(u'/File.txt'), xattr.XATTR_FINDERINFO_NAME, bytes(bytearray(key)))
# The file should not be synced and there have no remote id
self.wait_sync(wait_for_async=True, fail_if_timeout=False, timeout=10)
info = self.local_client_1.get_remote_id(u'/File.txt')
self.assertIsNone(info)
# Remove the Finder flag
self.local_client_1.remove_remote_id(u'/File.txt', xattr.XATTR_FINDERINFO_NAME)
# The sync process should now handle the file and sync it
self.wait_sync(wait_for_async=True, fail_if_timeout=False, timeout=10)
info = self.local_client_1.get_remote_id(u'/File.txt')
self.assertIsNotNone(info)
示例14: createChildren
def createChildren(parent, subStructure):
for childName, childStructure in subStructure.iteritems():
if childName.startswith("@"):
continue
childPath = os.path.join(parent, childName)
if "@contents" in childStructure:
# This is a file
with open(childPath, "w") as child:
child.write(childStructure["@contents"])
else:
# This is a directory
os.mkdir(childPath)
createChildren(childPath, childStructure)
if "@xattrs" in childStructure:
xattrs = childStructure["@xattrs"]
for attr, value in xattrs.iteritems():
try:
xattr.setxattr(childPath, attr, value)
except IOError:
pass
# Set access and modified times
if "@timestamp" in childStructure:
timestamp = childStructure["@timestamp"]
os.utime(childPath, (timestamp, timestamp))
示例15: upgradeCalendarHome
def upgradeCalendarHome(homePath, directory):
errorOccurred = False
log.debug("Upgrading calendar home: %s" % (homePath,))
try:
for cal in os.listdir(homePath):
calPath = os.path.join(homePath, cal)
if not os.path.isdir(calPath):
# Skip non-directories; these might have been uploaded by a
# random DAV client, they can't be calendar collections.
continue
if cal == 'notifications':
# Delete the old, now obsolete, notifications directory.
rmdir(calPath)
continue
log.debug("Upgrading calendar: %s" % (calPath,))
if not upgradeCalendarCollection(calPath, directory):
errorOccurred = True
# Change the calendar-free-busy-set xattrs of the inbox to the
# __uids__/<guid> form
if cal == "inbox":
for attr, value in xattr.xattr(calPath).iteritems():
if attr == "WebDAV:{urn:ietf:params:xml:ns:caldav}calendar-free-busy-set":
value = updateFreeBusySet(value, directory)
if value is not None:
# Need to write the xattr back to disk
xattr.setxattr(calPath, attr, value)
except Exception, e:
log.error("Failed to upgrade calendar home %s: %s" % (homePath, e))
raise