]> jfr.im git - yt-dlp.git/blob - yt_dlp/postprocessor/execafterdownload.py
[Exec] Ensure backward compatibility when the command contains `%`
[yt-dlp.git] / yt_dlp / postprocessor / execafterdownload.py
1 from __future__ import unicode_literals
2
3 import re
4 import subprocess
5
6 from .common import PostProcessor
7 from ..compat import compat_shlex_quote
8 from ..utils import (
9 encodeArgument,
10 FORMAT_RE,
11 PostProcessingError,
12 )
13
14
15 class ExecAfterDownloadPP(PostProcessor):
16
17 def __init__(self, downloader, exec_cmd):
18 super(ExecAfterDownloadPP, self).__init__(downloader)
19 self.exec_cmd = exec_cmd
20
21 @classmethod
22 def pp_key(cls):
23 return 'Exec'
24
25 def parse_cmd(self, cmd, info):
26 # If no %(key)s is found, replace {} for backard compatibility
27 if not re.search(FORMAT_RE.format(r'[-\w>.+]+'), cmd):
28 if '{}' not in cmd:
29 cmd += ' {}'
30 return cmd.replace('{}', compat_shlex_quote(info['filepath']))
31
32 tmpl, info_copy = self._downloader.prepare_outtmpl(cmd, info)
33 return tmpl % info_copy
34
35 def run(self, info):
36 cmd = self.parse_cmd(self.exec_cmd, info)
37 self.to_screen('Executing command: %s' % cmd)
38 retCode = subprocess.call(encodeArgument(cmd), shell=True)
39 if retCode != 0:
40 raise PostProcessingError('Command returned error code %d' % retCode)
41 return [], info