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