exec.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import subprocess
  2. from .common import PostProcessor
  3. from ..compat import compat_shlex_quote
  4. from ..utils import PostProcessingError, encodeArgument, variadic
  5. class ExecPP(PostProcessor):
  6. def __init__(self, downloader, exec_cmd):
  7. PostProcessor.__init__(self, downloader)
  8. self.exec_cmd = variadic(exec_cmd)
  9. def parse_cmd(self, cmd, info):
  10. tmpl, tmpl_dict = self._downloader.prepare_outtmpl(cmd, info)
  11. if tmpl_dict: # if there are no replacements, tmpl_dict = {}
  12. return self._downloader.escape_outtmpl(tmpl) % tmpl_dict
  13. filepath = info.get('filepath', info.get('_filename'))
  14. # If video, and no replacements are found, replace {} for backard compatibility
  15. if filepath:
  16. if '{}' not in cmd:
  17. cmd += ' {}'
  18. cmd = cmd.replace('{}', compat_shlex_quote(filepath))
  19. return cmd
  20. def run(self, info):
  21. for tmpl in self.exec_cmd:
  22. cmd = self.parse_cmd(tmpl, info)
  23. self.to_screen('Executing command: %s' % cmd)
  24. retCode = subprocess.call(encodeArgument(cmd), shell=True)
  25. if retCode != 0:
  26. raise PostProcessingError('Command returned error code %d' % retCode)
  27. return [], info
  28. # Deprecated
  29. class ExecAfterDownloadPP(ExecPP):
  30. def __init__(self, *args, **kwargs):
  31. super().__init__(*args, **kwargs)
  32. self.deprecation_warning(
  33. 'hypervideo_dl.postprocessor.ExecAfterDownloadPP is deprecated '
  34. 'and may be removed in a future version. Use hypervideo_dl.postprocessor.ExecPP instead')