[postprocessor:exec] add 'event' option

and remove 'final' option -- use '"event": "finalize"' instead.
pull/1195/head
Mike Fährmann 4 years ago
parent 9fffa9c343
commit 9c3568c397
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88

@ -376,7 +376,8 @@ def build_parser():
postprocessor.add_argument(
"--exec-after",
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. "
"Example: --exec-after 'cd {} && convert * ../doc.pdf'"),
)

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

Loading…
Cancel
Save