X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=youtube_dl%2Fpostprocessor%2Fexecafterdownload.py;h=74f66d669c0679a9eece06b1924ecc9f5dae00d2;hb=4333d56494a48929303ce306330ee3cded989d48;hp=431ab7f08d50a50aea0ee30be0f0b97727081cd0;hpb=a7cacbca2b140f668785d57264914de5585ed699;p=youtube-dl diff --git a/youtube_dl/postprocessor/execafterdownload.py b/youtube_dl/postprocessor/execafterdownload.py index 431ab7f08..74f66d669 100644 --- a/youtube_dl/postprocessor/execafterdownload.py +++ b/youtube_dl/postprocessor/execafterdownload.py @@ -1,36 +1,28 @@ -# ExecAfterDownload written by AaronM / mcd1992. -# If there are any issues with this postprocessor please contact me via github or admin@fgthou.se +from __future__ import unicode_literals -import os, re, shlex -from ..utils import PostProcessingError - -class ExecAfterDownload( object ): - _downloader = None +import subprocess - def __init__( self, downloader = None, commandString = None ): - self._downloader = downloader - self.commandString = commandString +from .common import PostProcessor +from ..compat import shlex_quote +from ..utils import PostProcessingError - def set_downloader( self, downloader ): - """Sets the downloader for this PP.""" - self._downloader = downloader - def run( self, information ): - self.targetFile = information["filepath"] - self.finalCommand = None; +class ExecAfterDownloadPP(PostProcessor): + def __init__(self, downloader, exec_cmd): + super(ExecAfterDownloadPP, self).__init__(downloader) + self.exec_cmd = exec_cmd - if( re.search( '{}', self.commandString ) ): # Find and replace all occurrences of {} with the file name. - self.finalCommand = re.sub( "{}", '\'' + self.targetFile + '\'', self.commandString ) - else: - self.finalCommand = self.commandString + ' \'' + self.targetFile + '\'' + def run(self, information): + cmd = self.exec_cmd + if '{}' not in cmd: + cmd += ' {}' - if( self.finalCommand ): - print( "[exec] Executing command: " + self.finalCommand ) - os.system( self.finalCommand ) - else: - raise PostProcessingExecError( "Invalid syntax for --exec post processor" ) + cmd = cmd.replace('{}', shlex_quote(information['filepath'])) - return None, information # by default, keep file and do nothing + self._downloader.to_screen('[exec] Executing command: %s' % cmd) + retCode = subprocess.call(cmd, shell=True) + if retCode != 0: + raise PostProcessingError( + 'Command returned error code %d' % retCode) -class PostProcessingExecError( PostProcessingError ): - pass + return [], information