[xattr] Always output a warning message on errors
[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(
80                             "Couldn't find a tool to set the xattrs. "
81                             "Install either the python 'pyxattr' or 'xattr' "
82                             "modules, or the GNU 'attr' package "
83                             "(which contains the 'setfattr' tool).")
84                     else:
85                         self._downloader.report_error(
86                             "Couldn't find a tool to set the xattrs. "
87                             "Install either the python 'xattr' module, "
88                             "or the 'xattr' binary.")
89             else:
90                 # Write xattrs to NTFS Alternate Data Streams: http://en.wikipedia.org/wiki/NTFS#Alternate_data_streams_.28ADS.29
91                 def write_xattr(path, key, value):
92                     assert(key.find(":") < 0)
93                     assert(path.find(":") < 0)
94                     assert(os.path.exists(path))
95
96                     ads_fn = path + ":" + key
97                     with open(ads_fn, "w") as f:
98                         f.write(value)
99
100         # Write the metadata to the file's xattrs
101         self._downloader.to_screen('[metadata] Writing metadata to file\'s xattrs...')
102
103         filename = info['filepath']
104
105         try:
106             xattr_mapping = {
107                 'user.xdg.referrer.url': 'webpage_url',
108                 # 'user.xdg.comment':            'description',
109                 'user.dublincore.title': 'title',
110                 'user.dublincore.date': 'upload_date',
111                 'user.dublincore.description': 'description',
112                 'user.dublincore.contributor': 'uploader',
113                 'user.dublincore.format': 'format',
114             }
115
116             for xattrname, infoname in xattr_mapping.items():
117
118                 value = info.get(infoname)
119
120                 if value:
121                     if infoname == "upload_date":
122                         value = hyphenate_date(value)
123
124                     byte_value = value.encode(preferredencoding())
125                     write_xattr(filename, xattrname, byte_value)
126
127             return True, info
128
129         except OSError:
130             self._downloader.report_error("This filesystem doesn't support extended attributes. (You may have to enable them in your /etc/fstab)")
131             return False, info
132