[xattr] Write bytestrings, not characters
[youtube-dl] / youtube_dl / postprocessor / xattrpp.py
1 import os
2 import subprocess
3 import sys
4
5 from .common import PostProcessor
6 from ..utils import (
7     hyphenate_date,
8     preferredencoding,
9 )
10
11
12 class XAttrMetadataPP(PostProcessor):
13
14     #
15     # More info about extended attributes for media:
16     #   http://freedesktop.org/wiki/CommonExtendedAttributes/
17     #   http://www.freedesktop.org/wiki/PhreedomDraft/
18     #   http://dublincore.org/documents/usageguide/elements.shtml
19     #
20     # TODO:
21     #  * capture youtube keywords and put them in 'user.dublincore.subject' (comma-separated)
22     #  * figure out which xattrs can be used for 'duration', 'thumbnail', 'resolution'
23     #
24
25     def run(self, info):
26         """ Set extended attributes on downloaded file (if xattr support is found). """
27
28         # This mess below finds the best xattr tool for the job and creates a
29         # "write_xattr" function.
30         try:
31             # try the pyxattr module...
32             import xattr
33             def write_xattr(path, key, value):
34                 return xattr.setxattr(path, key, value)
35
36         except ImportError:
37
38             if os.name == 'posix':
39                 def which(bin):
40                     for dir in os.environ["PATH"].split(":"):
41                         path = os.path.join(dir, bin)
42                         if os.path.exists(path):
43                             return path
44
45                 user_has_setfattr = which("setfattr")
46                 user_has_xattr    = which("xattr")
47
48                 if user_has_setfattr or user_has_xattr:
49
50                     def write_xattr(path, key, value):
51                         import errno
52                         potential_errors = {
53                             # setfattr: /tmp/blah: Operation not supported
54                             "Operation not supported": errno.EOPNOTSUPP,
55                             # setfattr: ~/blah: No such file or directory
56                             # xattr: No such file: ~/blah
57                             "No such file": errno.ENOENT,
58                         }
59
60                         if user_has_setfattr:
61                             cmd = ['setfattr', '-n', key, '-v', value, path]
62                         elif user_has_xattr:
63                             cmd = ['xattr', '-w', key, value, path]
64
65                         try:
66                             subprocess.check_output(cmd, stderr=subprocess.STDOUT)
67                         except subprocess.CalledProcessError as e:
68                             errorstr = e.output.strip().decode()
69                             for potential_errorstr, potential_errno in potential_errors.items():
70                                 if errorstr.find(potential_errorstr) > -1:
71                                     e = OSError(potential_errno, potential_errorstr)
72                                     e.__cause__ = None
73                                     raise e
74                             raise  # Reraise unhandled error
75
76                 else:
77                     # On Unix, and can't find pyxattr, setfattr, or xattr.
78                     if sys.platform.startswith('linux'):
79                         self._downloader.report_error("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).")
80                     elif sys.platform == 'darwin':
81                         self._downloader.report_error("Couldn't find a tool to set the xattrs. Install either the python 'xattr' module, or the 'xattr' binary.")
82             else:
83                 # Write xattrs to NTFS Alternate Data Streams: http://en.wikipedia.org/wiki/NTFS#Alternate_data_streams_.28ADS.29
84                 def write_xattr(path, key, value):
85                     assert(key.find(":") < 0)
86                     assert(path.find(":") < 0)
87                     assert(os.path.exists(path))
88
89                     ads_fn = path + ":" + key
90                     with open(ads_fn, "w") as f:
91                         f.write(value)
92
93         # Write the metadata to the file's xattrs
94         self._downloader.to_screen('[metadata] Writing metadata to file\'s xattrs...')
95
96         filename = info['filepath']
97
98         try:
99             xattr_mapping = {
100                 'user.xdg.referrer.url': 'webpage_url',
101                 # 'user.xdg.comment':            'description',
102                 'user.dublincore.title': 'title',
103                 'user.dublincore.date': 'upload_date',
104                 'user.dublincore.description': 'description',
105                 'user.dublincore.contributor': 'uploader',
106                 'user.dublincore.format': 'format',
107             }
108
109             for xattrname, infoname in xattr_mapping.items():
110
111                 value = info.get(infoname)
112
113                 if value:
114                     if infoname == "upload_date":
115                         value = hyphenate_date(value)
116
117                     byte_value = value.encode(preferredencoding())
118                     write_xattr(filename, xattrname, byte_value)
119
120             return True, info
121
122         except OSError:
123             self._downloader.report_error("This filesystem doesn't support extended attributes. (You may have to enable them in your /etc/fstab)")
124             return False, info
125