Fail gracefully on undecodable install output.

This commit is contained in:
Anthony Sottile 2016-01-12 09:51:40 -08:00
parent 75aaadd4c4
commit 2aaaddb5cc
9 changed files with 104 additions and 24 deletions

View file

@ -18,8 +18,19 @@ class PreCommitSystemExit(SystemExit):
pass
def _to_bytes(exc):
try:
return bytes(exc)
except Exception:
return five.text(exc).encode('UTF-8')
def _log_and_exit(msg, exc, formatted, write_fn=sys_stdout_write_wrapper):
error_msg = '{0}: {1}: {2}\n'.format(msg, type(exc).__name__, exc)
error_msg = b''.join((
five.to_bytes(msg), b': ',
five.to_bytes(type(exc).__name__), b': ',
_to_bytes(exc), b'\n',
))
write_fn(error_msg)
write_fn('Check the log at ~/.pre-commit/pre-commit.log\n')
store = Store()

View file

@ -45,13 +45,14 @@ def install_environment(
repo_cmd_runner.run(cmd)
with in_env(repo_cmd_runner, version) as node_env:
node_env.run("cd '{prefix}' && npm install -g")
node_env.run("cd '{prefix}' && npm install -g", encoding=None)
if additional_dependencies:
node_env.run(
"cd '{prefix}' && npm install -g " +
' '.join(
shell_escape(dep) for dep in additional_dependencies
)
),
encoding=None,
)

View file

@ -63,13 +63,14 @@ def install_environment(
venv_cmd.extend(['-p', norm_version(version)])
repo_cmd_runner.run(venv_cmd)
with in_env(repo_cmd_runner, version) as env:
env.run("cd '{prefix}' && pip install .")
env.run("cd '{prefix}' && pip install .", encoding=None)
if additional_dependencies:
env.run(
"cd '{prefix}' && pip install " +
' '.join(
shell_escape(dep) for dep in additional_dependencies
)
),
encoding=None,
)

View file

@ -95,13 +95,15 @@ def install_environment(
ruby_env.run(
'cd {prefix} && gem build *.gemspec && '
'gem install --no-ri --no-rdoc *.gem',
encoding=None,
)
if additional_dependencies:
ruby_env.run(
'cd {prefix} && gem install --no-ri --no-rdoc ' +
' '.join(
shell_escape(dep) for dep in additional_dependencies
)
),
encoding=None,
)

View file

@ -124,26 +124,38 @@ class CalledProcessError(RuntimeError):
self.expected_returncode = expected_returncode
self.output = output
def __str__(self):
def to_bytes(self):
output = []
for text in self.output:
if text:
output.append('\n ' + text.replace('\n', '\n '))
for maybe_text in self.output:
if maybe_text:
output.append(
b'\n ' +
five.to_bytes(maybe_text).replace(b'\n', b'\n ')
)
else:
output.append('(none)')
output.append(b'(none)')
return (
'Command: {0!r}\n'
'Return code: {1}\n'
'Expected return code: {2}\n'
'Output: {3}\n'
'Errors: {4}\n'.format(
self.cmd,
self.returncode,
self.expected_returncode,
*output
)
)
return b''.join((
five.to_bytes(
'Command: {0!r}\n'
'Return code: {1}\n'
'Expected return code: {2}\n'.format(
self.cmd, self.returncode, self.expected_returncode
)
),
b'Output: ', output[0], b'\n',
b'Errors: ', output[1], b'\n',
))
def to_text(self):
return self.to_bytes().decode('UTF-8')
if five.PY3: # pragma: no cover
__bytes__ = to_bytes
__str__ = to_text
else:
__str__ = to_bytes
__unicode__ = to_text
def cmd_output(*cmd, **kwargs):