xargs returns nonzero for negate + not found exe (fixes pcre + not found #447)

This commit is contained in:
Anthony Sottile
2016-12-04 13:31:05 -08:00
parent 0e2c3c1ff9
commit a157e1a63f
7 changed files with 49 additions and 11 deletions

View File

@@ -1,11 +1,12 @@
from __future__ import unicode_literals
from sys import platform
import sys
from pre_commit.xargs import xargs
ENVIRONMENT_DIR = None
GREP = 'ggrep' if sys.platform == 'darwin' else 'grep'
def install_environment(
@@ -19,10 +20,7 @@ def install_environment(
def run_hook(repo_cmd_runner, hook, file_args):
# For PCRE the entry is the regular expression to match
cmd = (
'ggrep' if platform == 'darwin' else 'grep',
'-H', '-n', '-P',
) + tuple(hook['args']) + (hook['entry'],)
cmd = (GREP, '-H', '-n', '-P') + tuple(hook['args']) + (hook['entry'],)
# Grep usually returns 0 for matches, and nonzero for non-matches so we
# negate it here.

View File

@@ -11,7 +11,8 @@ printable = frozenset(string.printable)
class ExecutableNotFoundError(OSError):
pass
def to_output(self):
return (1, self.args[0].encode('UTF-8'), b'')
def parse_bytesio(bytesio):

View File

@@ -172,16 +172,16 @@ def cmd_output(*cmd, **kwargs):
try:
cmd = parse_shebang.normalize_cmd(cmd)
except parse_shebang.ExecutableNotFoundError as e:
returncode, stdout, stderr = (-1, e.args[0].encode('UTF-8'), b'')
returncode, stdout, stderr = e.to_output()
else:
popen_kwargs.update(kwargs)
proc = __popen(cmd, **popen_kwargs)
stdout, stderr = proc.communicate()
if encoding is not None and stdout is not None:
stdout = stdout.decode(encoding)
if encoding is not None and stderr is not None:
stderr = stderr.decode(encoding)
returncode = proc.returncode
if encoding is not None and stdout is not None:
stdout = stdout.decode(encoding)
if encoding is not None and stderr is not None:
stderr = stderr.decode(encoding)
if retcode is not None and retcode != returncode:
raise CalledProcessError(

View File

@@ -1,6 +1,7 @@
from __future__ import absolute_import
from __future__ import unicode_literals
from pre_commit import parse_shebang
from pre_commit.util import cmd_output
@@ -52,6 +53,11 @@ def xargs(cmd, varargs, **kwargs):
stdout = b''
stderr = b''
try:
parse_shebang.normexe(cmd[0])
except parse_shebang.ExecutableNotFoundError as e:
return e.to_output()
for run_cmd in partition(cmd, varargs, **kwargs):
proc_retcode, proc_out, proc_err = cmd_output(
*run_cmd, encoding=None, retcode=None