[postprocessor:exec] fix filename quoting on Windows (#421)

This commit is contained in:
Mike Fährmann
2019-10-06 14:51:45 +02:00
parent b06c372e4d
commit 35958bebd4

View File

@@ -11,24 +11,29 @@
from .common import PostProcessor
from .. import util
import subprocess
import shlex
import os
if os.name == "nt":
def quote(s):
return '"' + s.replace('"', '\\"') + '"'
else:
from shlex import quote
class ExecPP(PostProcessor):
def __init__(self, pathfmt, options):
PostProcessor.__init__(self)
args = options["command"]
if isinstance(args, str):
self.args = util.Formatter(args.replace("{}", "{_temppath}"))
if "{}" not in args:
args += " {}"
self.args = args
self.shell = True
self._format = self._format_args_string
else:
for i, arg in enumerate(args):
if "{}" in arg:
args[i] = arg.replace("{}", "{_temppath}")
self.args = [util.Formatter(arg) for arg in args]
self.shell = False
self._format = self._format_args_list
@@ -37,19 +42,19 @@ class ExecPP(PostProcessor):
self._exec = self._exec_async
def run(self, pathfmt):
self._exec(self._format(pathfmt))
def _format_args_string(self, pathfmt):
return self.args.replace("{}", quote(pathfmt.temppath))
def _format_args_list(self, pathfmt):
kwdict = pathfmt.kwdict
kwdict["_directory"] = pathfmt.realdirectory
kwdict["_filename"] = pathfmt.filename
kwdict["_temppath"] = pathfmt.temppath
kwdict["_path"] = pathfmt.realpath
self._exec(self._format(kwdict))
def _format_args_list(self, kwdict):
return [arg.format_map(kwdict) for arg in self.args]
def _format_args_string(self, kwdict):
return self.args.format_map(_quote(kwdict))
def _exec(self, args):
retcode = subprocess.Popen(args, shell=self.shell).wait()
if retcode:
@@ -61,18 +66,4 @@ class ExecPP(PostProcessor):
subprocess.Popen(args, shell=self.shell)
def _quote(kwdict):
"""Create a copy of 'kwdict' and apply shlex.quote to its string values"""
kwdict = kwdict.copy()
for key, value in kwdict.items():
cls = value.__class__
if cls is str:
kwdict[key] = shlex.quote(value)
elif cls is list:
kwdict[key] = shlex.quote(", ".join(value))
elif cls is dict:
kwdict[key] = _quote(value)
return kwdict
__postprocessor__ = ExecPP