]> jfr.im git - yt-dlp.git/blob - yt_dlp/postprocessor/execafterdownload.py
336671d14a557dac4ec4f1740695ac5b7c3df78f
[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 )
11
12
13 class ExecAfterDownloadPP(PostProcessor):
14
15 def __init__(self, downloader, exec_cmd):
16 super(ExecAfterDownloadPP, self).__init__(downloader)
17 self.exec_cmd = exec_cmd
18
19 @classmethod
20 def pp_key(cls):
21 return 'Exec'
22
23 def parse_cmd(self, cmd, info):
24 tmpl, tmpl_dict = self._downloader.prepare_outtmpl(cmd, info)
25 if tmpl_dict: # if there are no replacements, tmpl_dict = {}
26 return tmpl % tmpl_dict
27
28 # If no replacements are found, replace {} for backard compatibility
29 if '{}' not in cmd:
30 cmd += ' {}'
31 return cmd.replace('{}', compat_shlex_quote(
32 info.get('filepath') or info['_filename']))
33
34 def run(self, info):
35 cmd = self.parse_cmd(self.exec_cmd, info)
36 self.to_screen('Executing command: %s' % cmd)
37 retCode = subprocess.call(encodeArgument(cmd), shell=True)
38 if retCode != 0:
39 raise PostProcessingError('Command returned error code %d' % retCode)
40 return [], info