You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.7 KiB

  1. from __future__ import unicode_literals
  2. from .common import PostProcessor
  3. from ..utils import PostProcessingError
  4. import subprocess
  5. import shlex
  6. class ExecAfterDownloadPP(PostProcessor):
  7. def __init__(self, downloader=None, verboseOutput=None, commandString=None):
  8. self.verboseOutput = verboseOutput
  9. self.commandString = commandString
  10. def run(self, information):
  11. self.targetFile = information['filepath'].replace('\'', '\'\\\'\'') # Replace single quotes with '\''
  12. self.commandList = shlex.split(self.commandString)
  13. self.commandString = ''
  14. # Replace all instances of '{}' with the file name and convert argument list to single string.
  15. for index, arg in enumerate(self.commandList):
  16. if(arg == '{}'):
  17. self.commandString += '\'' + self.targetFile + '\' '
  18. else:
  19. self.commandString += arg + ' '
  20. if self.targetFile not in self.commandString: # Assume user wants the file appended to the end of the command if no {}'s were given.
  21. self.commandString += '\'' + self.targetFile + '\''
  22. print("[exec] Executing command: " + self.commandString)
  23. self.retCode = subprocess.call(self.commandString, shell=True)
  24. if(self.retCode < 0):
  25. print("[exec] WARNING: Command exited with a negative return code, the process was killed externally. Your command may not of completed succesfully!")
  26. elif(self.verboseOutput):
  27. print("[exec] Command exited with return code: " + str(self.retCode))
  28. return None, information # by default, keep file and do nothing
  29. class PostProcessingExecError(PostProcessingError):
  30. pass