aboutsummaryrefslogtreecommitdiffstats
path: root/gallery_dl/postprocessor/exec.py
diff options
context:
space:
mode:
authorLibravatarUnit 193 <unit193@unit193.net>2025-07-31 01:22:01 -0400
committerLibravatarUnit 193 <unit193@unit193.net>2025-07-31 01:22:01 -0400
commita6e995c093de8aae2e91a0787281bb34c0b871eb (patch)
tree2d79821b05300d34d8871eb6c9662b359a2de85d /gallery_dl/postprocessor/exec.py
parent7672a750cb74bf31e21d76aad2776367fd476155 (diff)
New upstream version 1.30.2.upstream/1.30.2
Diffstat (limited to 'gallery_dl/postprocessor/exec.py')
-rw-r--r--gallery_dl/postprocessor/exec.py76
1 files changed, 56 insertions, 20 deletions
diff --git a/gallery_dl/postprocessor/exec.py b/gallery_dl/postprocessor/exec.py
index 7d2be2b..0bfe1a2 100644
--- a/gallery_dl/postprocessor/exec.py
+++ b/gallery_dl/postprocessor/exec.py
@@ -10,13 +10,14 @@
from .common import PostProcessor
from .. import util, formatter
+import subprocess
import os
-import re
if util.WINDOWS:
def quote(s):
- return '"' + s.replace('"', '\\"') + '"'
+ s = s.replace('"', '\\"')
+ return f'"{s}"'
else:
from shlex import quote
@@ -26,17 +27,21 @@ class ExecPP(PostProcessor):
def __init__(self, job, options):
PostProcessor.__init__(self, job)
- if options.get("async", False):
- self._exec = self._exec_async
-
- args = options["command"]
- if isinstance(args, str):
- self.args = args
- self._sub = re.compile(r"\{(_directory|_filename|_path|)\}").sub
- execute = self.exec_string
+ if cmds := options.get("commands"):
+ self.cmds = [self._prepare_cmd(c) for c in cmds]
+ execute = self.exec_many
else:
- self.args = [formatter.parse(arg) for arg in args]
- execute = self.exec_list
+ execute, self.args = self._prepare_cmd(options["command"])
+ if options.get("async", False):
+ self._exec = self._popen
+
+ self.session = False
+ self.creationflags = 0
+ if options.get("session"):
+ if util.WINDOWS:
+ self.creationflags = subprocess.CREATE_NEW_PROCESS_GROUP
+ else:
+ self.session = True
events = options.get("event")
if events is None:
@@ -47,6 +52,13 @@ class ExecPP(PostProcessor):
self._init_archive(job, options)
+ def _prepare_cmd(self, cmd):
+ if isinstance(cmd, str):
+ self._sub = util.re(r"\{(_directory|_filename|_path|)\}").sub
+ return self.exec_string, cmd
+ else:
+ return self.exec_list, [formatter.parse(arg) for arg in cmd]
+
def exec_list(self, pathfmt):
archive = self.archive
kwdict = pathfmt.kwdict
@@ -60,10 +72,11 @@ class ExecPP(PostProcessor):
args = [arg.format_map(kwdict) for arg in self.args]
args[0] = os.path.expanduser(args[0])
- self._exec(args, False)
+ retcode = self._exec(args, False)
if archive:
archive.add(kwdict)
+ return retcode
def exec_string(self, pathfmt):
archive = self.archive
@@ -72,24 +85,47 @@ class ExecPP(PostProcessor):
self.pathfmt = pathfmt
args = self._sub(self._replace, self.args)
- self._exec(args, True)
+ retcode = self._exec(args, True)
if archive:
archive.add(pathfmt.kwdict)
+ return retcode
+
+ def exec_many(self, pathfmt):
+ if archive := self.archive:
+ if archive.check(pathfmt.kwdict):
+ return
+ self.archive = False
+
+ retcode = 0
+ for execute, args in self.cmds:
+ self.args = args
+ if retcode := execute(pathfmt):
+ # non-zero exit status
+ break
+
+ if archive:
+ self.archive = archive
+ archive.add(pathfmt.kwdict)
+ return retcode
def _exec(self, args, shell):
- self.log.debug("Running '%s'", args)
- retcode = util.Popen(args, shell=shell).wait()
- if retcode:
+ if retcode := self._popen(args, shell).wait():
self.log.warning("'%s' returned with non-zero exit status (%d)",
args, retcode)
+ return retcode
- def _exec_async(self, args, shell):
+ def _popen(self, args, shell):
self.log.debug("Running '%s'", args)
- util.Popen(args, shell=shell)
+ return util.Popen(
+ args,
+ shell=shell,
+ creationflags=self.creationflags,
+ start_new_session=self.session,
+ )
def _replace(self, match):
- name = match.group(1)
+ name = match[1]
if name == "_directory":
return quote(self.pathfmt.realdirectory)
if name == "_filename":