execafterdownload.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. # ExecAfterDownload written by AaronM / mcd1992.
  2. # If there are any issues with this postprocessor please contact me via github or admin@fgthou.se
  3. import os, re, shlex
  4. from ..utils import PostProcessingError
  5. class ExecAfterDownload( object ):
  6. _downloader = None
  7. def __init__( self, downloader = None, commandString = None ):
  8. self._downloader = downloader
  9. self.commandString = commandString
  10. def set_downloader( self, downloader ):
  11. """Sets the downloader for this PP."""
  12. self._downloader = downloader
  13. def run( self, information ):
  14. self.targetFile = information["filepath"]
  15. self.finalCommand = None;
  16. if( re.search( '{}', self.commandString ) ): # Find and replace all occurrences of {} with the file name.
  17. self.finalCommand = re.sub( "{}", '\'' + self.targetFile + '\'', self.commandString )
  18. else:
  19. self.finalCommand = self.commandString + ' \'' + self.targetFile + '\''
  20. if( self.finalCommand ):
  21. print( "[exec] Executing command: " + self.finalCommand )
  22. os.system( self.finalCommand )
  23. else:
  24. raise PostProcessingExecError( "Invalid syntax for --exec post processor" )
  25. return None, information # by default, keep file and do nothing
  26. class PostProcessingExecError( PostProcessingError ):
  27. pass