mirror of
https://github.com/mikf/gallery-dl.git
synced 2024-11-26 04:32:51 +01:00
[postprocessor:exec] add 'event' option
and remove 'final' option -- use '"event": "finalize"' instead.
This commit is contained in:
parent
9fffa9c343
commit
9c3568c397
@ -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 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):
|
||||
self._exec = self._exec_async
|
||||
|
||||
event = "finalize" if final else "after"
|
||||
job.hooks[event].append(self.run)
|
||||
args = options["command"]
|
||||
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):
|
||||
if status == 0:
|
||||
self._exec(self._format(pathfmt))
|
||||
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 _format_args_path(self, pathfmt):
|
||||
return self.args.replace("{}", quote(pathfmt.realpath))
|
||||
def exec_list(self, pathfmt, status=None):
|
||||
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["_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…
Reference in New Issue
Block a user