[postprocessor:exec] add 'event' option

and remove 'final' option -- use '"event": "finalize"' instead.
This commit is contained in:
Mike Fährmann
2020-11-19 01:31:52 +01:00
parent 9fffa9c343
commit 9c3568c397
2 changed files with 37 additions and 31 deletions

View File

@@ -376,7 +376,8 @@ def build_parser():
postprocessor.add_argument( postprocessor.add_argument(
"--exec-after", "--exec-after",
dest="postprocessors", metavar="CMD", dest="postprocessors", metavar="CMD",
action=AppendCommandAction, const={"name": "exec", "final": True}, action=AppendCommandAction, const={
"name": "exec", "event": "finalize"},
help=("Execute CMD after all files were downloaded successfully. " help=("Execute CMD after all files were downloaded successfully. "
"Example: --exec-after 'cd {} && convert * ../doc.pdf'"), "Example: --exec-after 'cd {} && convert * ../doc.pdf'"),
) )

View File

@@ -24,49 +24,54 @@ class ExecPP(PostProcessor):
def __init__(self, job, options): def __init__(self, job, options):
PostProcessor.__init__(self, job) PostProcessor.__init__(self, job)
args = options["command"]
final = options.get("final", False)
if isinstance(args, str):
if final:
self._format = self._format_args_directory
else:
self._format = self._format_args_path
if "{}" not in args:
args += " {}"
self.args = args
self.shell = True
else:
self._format = self._format_args_list
self.args = [util.Formatter(arg) for arg in args]
self.shell = False
if options.get("async", False): if options.get("async", False):
self._exec = self._exec_async self._exec = self._exec_async
event = "finalize" if final else "after" args = options["command"]
job.hooks[event].append(self.run) if isinstance(args, str):
if "{}" not in args:
args += " {}"
self.args = args
execute = self.exec_string
else:
self.args = [util.Formatter(arg) for arg in args]
execute = self.exec_list
def run(self, pathfmt, status=0): events = options.get("event")
if status == 0: if events is None:
self._exec(self._format(pathfmt)) events = ("after",)
elif isinstance(events, str):
events = events.split(",")
for event in events:
job.hooks[event].append(execute)
def _format_args_path(self, pathfmt): def exec_list(self, pathfmt, status=None):
return self.args.replace("{}", quote(pathfmt.realpath)) if status:
return
def _format_args_directory(self, pathfmt):
return self.args.replace("{}", quote(pathfmt.realdirectory))
def _format_args_list(self, pathfmt):
kwdict = pathfmt.kwdict kwdict = pathfmt.kwdict
kwdict["_directory"] = pathfmt.realdirectory kwdict["_directory"] = pathfmt.realdirectory
kwdict["_filename"] = pathfmt.filename kwdict["_filename"] = pathfmt.filename
kwdict["_path"] = pathfmt.realpath kwdict["_path"] = pathfmt.realpath
return [arg.format_map(kwdict) for arg in self.args]
def _exec(self, args): args = [arg.format_map(kwdict) for arg in self.args]
self._exec(args, False)
def exec_string(self, pathfmt, status=None):
if status:
return
if status is None and pathfmt.realpath:
args = self.args.replace("{}", quote(pathfmt.realpath))
else:
args = self.args.replace("{}", quote(pathfmt.realdirectory))
self._exec(args, True)
def _exec(self, args, shell):
self.log.debug("Running '%s'", args) self.log.debug("Running '%s'", args)
retcode = subprocess.Popen(args, shell=self.shell).wait() retcode = subprocess.Popen(args, shell=shell).wait()
if retcode: if retcode:
self.log.warning( self.log.warning(
"Executing '%s' returned with non-zero exit status (%d)", "Executing '%s' returned with non-zero exit status (%d)",