[pp:exec] support more replacement fields for '--exec' (#4633)
- {_directory}
- {_filename}
- {_path} (alias for {})
This commit is contained in:
@@ -121,11 +121,13 @@
|
|||||||
--write-tags Write image tags to separate text files
|
--write-tags Write image tags to separate text files
|
||||||
--mtime-from-date Set file modification times according to 'date'
|
--mtime-from-date Set file modification times according to 'date'
|
||||||
metadata
|
metadata
|
||||||
--exec CMD Execute CMD for each downloaded file. Example:
|
--exec CMD Execute CMD for each downloaded file. Supported
|
||||||
--exec "convert {} {}.png && rm {}"
|
replacement fields are {} or {_path},
|
||||||
|
{_directory}, {_filename}. Example: --exec
|
||||||
|
"convert {} {}.png && rm {}"
|
||||||
--exec-after CMD Execute CMD after all files were downloaded
|
--exec-after CMD Execute CMD after all files were downloaded
|
||||||
successfully. Example: --exec-after "cd {} &&
|
successfully. Example: --exec-after "cd
|
||||||
convert * ../doc.pdf"
|
{_directory} && convert * ../doc.pdf"
|
||||||
-P, --postprocessor NAME Activate the specified post processor
|
-P, --postprocessor NAME Activate the specified post processor
|
||||||
-O, --postprocessor-option OPT
|
-O, --postprocessor-option OPT
|
||||||
Additional '<key>=<value>' post processor
|
Additional '<key>=<value>' post processor
|
||||||
|
|||||||
@@ -510,6 +510,8 @@ def build_parser():
|
|||||||
dest="postprocessors", metavar="CMD",
|
dest="postprocessors", metavar="CMD",
|
||||||
action=AppendCommandAction, const={"name": "exec"},
|
action=AppendCommandAction, const={"name": "exec"},
|
||||||
help=("Execute CMD for each downloaded file. "
|
help=("Execute CMD for each downloaded file. "
|
||||||
|
"Supported replacement fields are "
|
||||||
|
"{} or {_path}, {_directory}, {_filename}. "
|
||||||
"Example: --exec \"convert {} {}.png && rm {}\""),
|
"Example: --exec \"convert {} {}.png && rm {}\""),
|
||||||
)
|
)
|
||||||
postprocessor.add_argument(
|
postprocessor.add_argument(
|
||||||
@@ -518,7 +520,8 @@ def build_parser():
|
|||||||
action=AppendCommandAction, const={
|
action=AppendCommandAction, const={
|
||||||
"name": "exec", "event": "finalize"},
|
"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 {_directory} "
|
||||||
|
"&& convert * ../doc.pdf\""),
|
||||||
)
|
)
|
||||||
postprocessor.add_argument(
|
postprocessor.add_argument(
|
||||||
"-P", "--postprocessor",
|
"-P", "--postprocessor",
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ from .common import PostProcessor
|
|||||||
from .. import util, formatter
|
from .. import util, formatter
|
||||||
import subprocess
|
import subprocess
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
if util.WINDOWS:
|
if util.WINDOWS:
|
||||||
@@ -32,6 +33,7 @@ class ExecPP(PostProcessor):
|
|||||||
args = options["command"]
|
args = options["command"]
|
||||||
if isinstance(args, str):
|
if isinstance(args, str):
|
||||||
self.args = args
|
self.args = args
|
||||||
|
self._sub = re.compile(r"\{(_directory|_filename|_path|)\}").sub
|
||||||
execute = self.exec_string
|
execute = self.exec_string
|
||||||
else:
|
else:
|
||||||
self.args = [formatter.parse(arg) for arg in args]
|
self.args = [formatter.parse(arg) for arg in args]
|
||||||
@@ -69,11 +71,8 @@ class ExecPP(PostProcessor):
|
|||||||
if archive and archive.check(pathfmt.kwdict):
|
if archive and archive.check(pathfmt.kwdict):
|
||||||
return
|
return
|
||||||
|
|
||||||
if pathfmt.realpath:
|
self.pathfmt = pathfmt
|
||||||
args = self.args.replace("{}", quote(pathfmt.realpath))
|
args = self._sub(self._replace, self.args)
|
||||||
else:
|
|
||||||
args = self.args.replace("{}", quote(pathfmt.realdirectory))
|
|
||||||
|
|
||||||
self._exec(args, True)
|
self._exec(args, True)
|
||||||
|
|
||||||
if archive:
|
if archive:
|
||||||
@@ -90,5 +89,13 @@ class ExecPP(PostProcessor):
|
|||||||
self.log.debug("Running '%s'", args)
|
self.log.debug("Running '%s'", args)
|
||||||
subprocess.Popen(args, shell=shell)
|
subprocess.Popen(args, shell=shell)
|
||||||
|
|
||||||
|
def _replace(self, match):
|
||||||
|
name = match.group(1)
|
||||||
|
if name == "_directory":
|
||||||
|
return quote(self.pathfmt.realdirectory)
|
||||||
|
if name == "_filename":
|
||||||
|
return quote(self.pathfmt.filename)
|
||||||
|
return quote(self.pathfmt.realpath)
|
||||||
|
|
||||||
|
|
||||||
__postprocessor__ = ExecPP
|
__postprocessor__ = ExecPP
|
||||||
|
|||||||
@@ -168,7 +168,7 @@ class ExecTest(BasePostprocessorTest):
|
|||||||
|
|
||||||
def test_command_string(self):
|
def test_command_string(self):
|
||||||
self._create({
|
self._create({
|
||||||
"command": "echo {} && rm {};",
|
"command": "echo {} {_path} {_directory} {_filename} && rm {};",
|
||||||
})
|
})
|
||||||
|
|
||||||
with patch("subprocess.Popen") as p:
|
with patch("subprocess.Popen") as p:
|
||||||
@@ -178,7 +178,11 @@ class ExecTest(BasePostprocessorTest):
|
|||||||
self._trigger(("after",))
|
self._trigger(("after",))
|
||||||
|
|
||||||
p.assert_called_once_with(
|
p.assert_called_once_with(
|
||||||
"echo {0} && rm {0};".format(self.pathfmt.realpath), shell=True)
|
"echo {0} {0} {1} {2} && rm {0};".format(
|
||||||
|
self.pathfmt.realpath,
|
||||||
|
self.pathfmt.realdirectory,
|
||||||
|
self.pathfmt.filename),
|
||||||
|
shell=True)
|
||||||
i.wait.assert_called_once_with()
|
i.wait.assert_called_once_with()
|
||||||
|
|
||||||
def test_command_list(self):
|
def test_command_list(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user