mirror of
https://github.com/yt-dlp/yt-dlp.git
synced 2025-10-04 19:44:33 -04:00
[XAttrMetadata] Refactor and document dependencies
This commit is contained in:
111
yt_dlp/utils.py
111
yt_dlp/utils.py
@@ -4673,87 +4673,56 @@ def decode_png(png_data):
|
||||
|
||||
|
||||
def write_xattr(path, key, value):
|
||||
# This mess below finds the best xattr tool for the job
|
||||
try:
|
||||
# try the pyxattr module...
|
||||
import xattr
|
||||
# Windows: Write xattrs to NTFS Alternate Data Streams:
|
||||
# http://en.wikipedia.org/wiki/NTFS#Alternate_data_streams_.28ADS.29
|
||||
if compat_os_name == 'nt':
|
||||
assert ':' not in key
|
||||
assert os.path.exists(path)
|
||||
|
||||
if hasattr(xattr, 'set'): # pyxattr
|
||||
# Unicode arguments are not supported in python-pyxattr until
|
||||
# version 0.5.0
|
||||
# See https://github.com/ytdl-org/youtube-dl/issues/5498
|
||||
pyxattr_required_version = '0.5.0'
|
||||
if version_tuple(xattr.__version__) < version_tuple(pyxattr_required_version):
|
||||
# TODO: fallback to CLI tools
|
||||
raise XAttrUnavailableError(
|
||||
'python-pyxattr is detected but is too old. '
|
||||
'yt-dlp requires %s or above while your version is %s. '
|
||||
'Falling back to other xattr implementations' % (
|
||||
pyxattr_required_version, xattr.__version__))
|
||||
try:
|
||||
with open(f'{path}:{key}', 'wb') as f:
|
||||
f.write(value)
|
||||
except OSError as e:
|
||||
raise XAttrMetadataError(e.errno, e.strerror)
|
||||
return
|
||||
|
||||
# UNIX Method 1. Use xattrs/pyxattrs modules
|
||||
from .dependencies import xattr
|
||||
|
||||
setxattr = None
|
||||
if getattr(xattr, '_yt_dlp__identifier', None) == 'pyxattr':
|
||||
# Unicode arguments are not supported in pyxattr until version 0.5.0
|
||||
# See https://github.com/ytdl-org/youtube-dl/issues/5498
|
||||
if version_tuple(xattr.__version__) >= (0, 5, 0):
|
||||
setxattr = xattr.set
|
||||
else: # xattr
|
||||
setxattr = xattr.setxattr
|
||||
elif xattr:
|
||||
setxattr = xattr.setxattr
|
||||
|
||||
if setxattr:
|
||||
try:
|
||||
setxattr(path, key, value)
|
||||
except OSError as e:
|
||||
raise XAttrMetadataError(e.errno, e.strerror)
|
||||
return
|
||||
|
||||
except ImportError:
|
||||
if compat_os_name == 'nt':
|
||||
# Write xattrs to NTFS Alternate Data Streams:
|
||||
# http://en.wikipedia.org/wiki/NTFS#Alternate_data_streams_.28ADS.29
|
||||
assert ':' not in key
|
||||
assert os.path.exists(path)
|
||||
# UNIX Method 2. Use setfattr/xattr executables
|
||||
exe = ('setfattr' if check_executable('setfattr', ['--version'])
|
||||
else 'xattr' if check_executable('xattr', ['-h']) else None)
|
||||
if not exe:
|
||||
raise XAttrUnavailableError(
|
||||
'Couldn\'t find a tool to set the xattrs. Install either the python "xattr" or "pyxattr" modules or the '
|
||||
+ ('"xattr" binary' if sys.platform != 'linux' else 'GNU "attr" package (which contains the "setfattr" tool)'))
|
||||
|
||||
ads_fn = path + ':' + key
|
||||
try:
|
||||
with open(ads_fn, 'wb') as f:
|
||||
f.write(value)
|
||||
except OSError as e:
|
||||
raise XAttrMetadataError(e.errno, e.strerror)
|
||||
else:
|
||||
user_has_setfattr = check_executable('setfattr', ['--version'])
|
||||
user_has_xattr = check_executable('xattr', ['-h'])
|
||||
|
||||
if user_has_setfattr or user_has_xattr:
|
||||
|
||||
value = value.decode('utf-8')
|
||||
if user_has_setfattr:
|
||||
executable = 'setfattr'
|
||||
opts = ['-n', key, '-v', value]
|
||||
elif user_has_xattr:
|
||||
executable = 'xattr'
|
||||
opts = ['-w', key, value]
|
||||
|
||||
cmd = ([encodeFilename(executable, True)]
|
||||
+ [encodeArgument(o) for o in opts]
|
||||
+ [encodeFilename(path, True)])
|
||||
|
||||
try:
|
||||
p = Popen(
|
||||
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
|
||||
except OSError as e:
|
||||
raise XAttrMetadataError(e.errno, e.strerror)
|
||||
stdout, stderr = p.communicate_or_kill()
|
||||
stderr = stderr.decode('utf-8', 'replace')
|
||||
if p.returncode != 0:
|
||||
raise XAttrMetadataError(p.returncode, stderr)
|
||||
|
||||
else:
|
||||
# On Unix, and can't find pyxattr, setfattr, or xattr.
|
||||
if sys.platform.startswith('linux'):
|
||||
raise XAttrUnavailableError(
|
||||
"Couldn't find a tool to set the xattrs. "
|
||||
"Install either the python 'pyxattr' or 'xattr' "
|
||||
"modules, or the GNU 'attr' package "
|
||||
"(which contains the 'setfattr' tool).")
|
||||
else:
|
||||
raise XAttrUnavailableError(
|
||||
"Couldn't find a tool to set the xattrs. "
|
||||
"Install either the python 'xattr' module, "
|
||||
"or the 'xattr' binary.")
|
||||
value = value.decode('utf-8')
|
||||
try:
|
||||
p = Popen(
|
||||
[exe, '-w', key, value, path] if exe == 'xattr' else [exe, '-n', key, '-v', value, path],
|
||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
|
||||
except OSError as e:
|
||||
raise XAttrMetadataError(e.errno, e.strerror)
|
||||
stderr = p.communicate_or_kill()[1].decode('utf-8', 'replace')
|
||||
if p.returncode:
|
||||
raise XAttrMetadataError(p.returncode, stderr)
|
||||
|
||||
|
||||
def random_birthday(year_field, month_field, day_field):
|
||||
|
Reference in New Issue
Block a user