Reorganize output writing

This commit is contained in:
Anthony Sottile
2016-11-26 12:15:55 -08:00
parent 1adfa24124
commit 0dda19f691
20 changed files with 202 additions and 142 deletions

View File

@@ -4,13 +4,13 @@ from __future__ import unicode_literals
import argparse
import os.path
import re
import sys
import jsonschema
import jsonschema.exceptions
import pkg_resources
import yaml
from pre_commit import output
from pre_commit.jsonschema_extensions import apply_defaults
@@ -69,7 +69,6 @@ def get_validator(
def get_run_function(filenames_help, validate_strategy, exception_cls):
def run(argv=None):
argv = argv if argv is not None else sys.argv[1:]
parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='*', help=filenames_help)
parser.add_argument(
@@ -87,7 +86,7 @@ def get_run_function(filenames_help, validate_strategy, exception_cls):
try:
validate_strategy(filename)
except exception_cls as e:
print(e.args[0])
output.write_line(e.args[0])
retval = 1
return retval
return run

View File

@@ -2,12 +2,12 @@ from __future__ import print_function
from __future__ import unicode_literals
import logging
import sys
from aspy.yaml import ordered_dump
from aspy.yaml import ordered_load
import pre_commit.constants as C
from pre_commit import output
from pre_commit.clientlib.validate_config import CONFIG_JSON_SCHEMA
from pre_commit.clientlib.validate_config import is_local_hooks
from pre_commit.clientlib.validate_config import load_config
@@ -86,26 +86,23 @@ def autoupdate(runner):
if is_local_hooks(repo_config):
output_configs.append(repo_config)
continue
sys.stdout.write('Updating {}...'.format(repo_config['repo']))
sys.stdout.flush()
output.write('Updating {}...'.format(repo_config['repo']))
try:
new_repo_config = _update_repository(repo_config, runner)
except RepositoryCannotBeUpdatedError as error:
print(error.args[0])
output.write_line(error.args[0])
output_configs.append(repo_config)
retv = 1
continue
if new_repo_config['sha'] != repo_config['sha']:
changed = True
print(
'updating {} -> {}.'.format(
repo_config['sha'], new_repo_config['sha'],
)
)
output.write_line('updating {} -> {}.'.format(
repo_config['sha'], new_repo_config['sha'],
))
output_configs.append(new_repo_config)
else:
print('already up to date.')
output.write_line('already up to date.')
output_configs.append(repo_config)
if changed:

View File

@@ -3,11 +3,12 @@ from __future__ import unicode_literals
import os.path
from pre_commit import output
from pre_commit.util import rmtree
def clean(runner):
if os.path.exists(runner.store.directory):
rmtree(runner.store.directory)
print('Cleaned {}.'.format(runner.store.directory))
output.write_line('Cleaned {}.'.format(runner.store.directory))
return 0

View File

@@ -7,6 +7,7 @@ import os
import os.path
import sys
from pre_commit import output
from pre_commit.logging_handler import LoggingHandler
from pre_commit.util import make_executable
from pre_commit.util import mkdirp
@@ -61,7 +62,7 @@ def install(runner, overwrite=False, hooks=False, hook_type='pre-commit'):
if overwrite and os.path.exists(legacy_path):
os.remove(legacy_path)
elif os.path.exists(legacy_path):
print(
output.write_line(
'Running in migration mode with existing hooks at {}\n'
'Use -f to use only pre-commit.'.format(
legacy_path,
@@ -83,7 +84,7 @@ def install(runner, overwrite=False, hooks=False, hook_type='pre-commit'):
pre_commit_file_obj.write(contents)
make_executable(hook_path)
print('pre-commit installed at {}'.format(hook_path))
output.write_line('pre-commit installed at {}'.format(hook_path))
# If they requested we install all of the hooks, do so.
if hooks:
@@ -110,10 +111,10 @@ def uninstall(runner, hook_type='pre-commit'):
return 0
os.remove(hook_path)
print('{} uninstalled'.format(hook_type))
output.write_line('{} uninstalled'.format(hook_type))
if os.path.exists(legacy_path):
os.rename(legacy_path, hook_path)
print('Restored previous hooks to {}'.format(hook_path))
output.write_line('Restored previous hooks to {}'.format(hook_path))
return 0

View File

@@ -7,9 +7,9 @@ import sys
from pre_commit import color
from pre_commit import git
from pre_commit import output
from pre_commit.logging_handler import LoggingHandler
from pre_commit.output import get_hook_message
from pre_commit.output import sys_stdout_write_wrapper
from pre_commit.staged_files_only import staged_files_only
from pre_commit.util import cmd_output
from pre_commit.util import noop_context
@@ -56,10 +56,10 @@ SKIPPED = 'Skipped'
NO_FILES = '(no files to check)'
def _run_single_hook(hook, repo, args, write, skips, cols):
def _run_single_hook(hook, repo, args, skips, cols):
filenames = get_filenames(args, hook['files'], hook['exclude'])
if hook['id'] in skips:
write(get_hook_message(
output.write(get_hook_message(
_hook_msg_start(hook, args.verbose),
end_msg=SKIPPED,
end_color=color.YELLOW,
@@ -68,7 +68,7 @@ def _run_single_hook(hook, repo, args, write, skips, cols):
))
return 0
elif not filenames and not hook['always_run']:
write(get_hook_message(
output.write(get_hook_message(
_hook_msg_start(hook, args.verbose),
postfix=NO_FILES,
end_msg=SKIPPED,
@@ -80,7 +80,7 @@ def _run_single_hook(hook, repo, args, write, skips, cols):
# Print the hook and the dots first in case the hook takes hella long to
# run.
write(get_hook_message(
output.write(get_hook_message(
_hook_msg_start(hook, args.verbose), end_len=6, cols=cols,
))
sys.stdout.flush()
@@ -104,26 +104,25 @@ def _run_single_hook(hook, repo, args, write, skips, cols):
print_color = color.GREEN
pass_fail = 'Passed'
write(color.format_color(pass_fail, print_color, args.color) + '\n')
output.write_line(color.format_color(pass_fail, print_color, args.color))
if (stdout or stderr or file_modifications) and (retcode or args.verbose):
write('hookid: {}\n'.format(hook['id']))
write('\n')
output.write_line('hookid: {}\n'.format(hook['id']))
# Print a message if failing due to file modifications
if file_modifications:
write('Files were modified by this hook.')
output.write('Files were modified by this hook.')
if stdout or stderr:
write(' Additional output:\n')
output.write_line(' Additional output:')
write('\n')
output.write_line()
for output in (stdout, stderr):
assert type(output) is bytes, type(output)
if output.strip():
write(output.strip() + b'\n')
write('\n')
for out in (stdout, stderr):
assert type(out) is bytes, type(out)
if out.strip():
output.write_line(out.strip())
output.write_line()
return retcode
@@ -147,13 +146,13 @@ def _compute_cols(hooks, verbose):
return max(cols, 80)
def _run_hooks(repo_hooks, args, write, environ):
def _run_hooks(repo_hooks, args, environ):
"""Actually run the hooks."""
skips = _get_skips(environ)
cols = _compute_cols([hook for _, hook in repo_hooks], args.verbose)
retval = 0
for repo, hook in repo_hooks:
retval |= _run_single_hook(hook, repo, args, write, skips, cols)
retval |= _run_single_hook(hook, repo, args, skips, cols)
return retval
@@ -177,10 +176,10 @@ def _has_unstaged_config(runner):
return retcode == 1
def run(runner, args, write=sys_stdout_write_wrapper, environ=os.environ):
def run(runner, args, environ=os.environ):
no_stash = args.no_stash or args.all_files or bool(args.files)
# Set up our logging handler
logger.addHandler(LoggingHandler(args.color, write=write))
logger.addHandler(LoggingHandler(args.color))
logger.setLevel(logging.INFO)
# Check if we have unresolved merge conflict files and fail fast.
@@ -220,7 +219,7 @@ def run(runner, args, write=sys_stdout_write_wrapper, environ=os.environ):
if hook['id'] == args.hook
]
if not repo_hooks:
write('No hook with id `{}`\n'.format(args.hook))
output.write_line('No hook with id `{}`'.format(args.hook))
return 1
# Filter hooks for stages
@@ -229,4 +228,4 @@ def run(runner, args, write=sys_stdout_write_wrapper, environ=os.environ):
if not hook['stages'] or args.hook_stage in hook['stages']
]
return _run_hooks(repo_hooks, args, write, environ)
return _run_hooks(repo_hooks, args, environ)

View File

@@ -8,8 +8,8 @@ import os.path
import traceback
from pre_commit import five
from pre_commit import output
from pre_commit.errors import FatalError
from pre_commit.output import sys_stdout_write_wrapper
from pre_commit.store import Store
@@ -25,19 +25,19 @@ def _to_bytes(exc):
return five.text(exc).encode('UTF-8')
def _log_and_exit(msg, exc, formatted, write_fn=sys_stdout_write_wrapper):
def _log_and_exit(msg, exc, formatted):
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')
output.write(error_msg)
output.write_line('Check the log at ~/.pre-commit/pre-commit.log')
store = Store()
store.require_created()
with io.open(os.path.join(store.directory, 'pre-commit.log'), 'wb') as log:
log.write(five.to_bytes(error_msg))
log.write(five.to_bytes(formatted) + b'\n')
output.write(error_msg, stream=log)
output.write_line(formatted, stream=log)
raise PreCommitSystemExit(1)

View File

@@ -1,9 +1,9 @@
from __future__ import unicode_literals
import logging
import sys
from pre_commit import color
from pre_commit import output
LOG_LEVEL_COLORS = {
@@ -15,14 +15,13 @@ LOG_LEVEL_COLORS = {
class LoggingHandler(logging.Handler):
def __init__(self, use_color, write=sys.stdout.write):
def __init__(self, use_color):
super(LoggingHandler, self).__init__()
self.use_color = use_color
self.__write = write
def emit(self, record):
self.__write(
'{}{}\n'.format(
output.write_line(
'{}{}'.format(
color.format_color(
'[{}]'.format(record.levelname),
LOG_LEVEL_COLORS[record.levelname],
@@ -31,4 +30,3 @@ class LoggingHandler(logging.Handler):
record.getMessage(),
)
)
sys.stdout.flush()

View File

@@ -6,6 +6,7 @@ import os.path
import tarfile
from pre_commit import five
from pre_commit import output
from pre_commit.util import cmd_output
from pre_commit.util import cwd
from pre_commit.util import rmtree
@@ -61,7 +62,9 @@ def make_archive(name, repo, ref, destdir):
def main():
for archive_name, repo, ref in REPOS:
print('Making {}.tar.gz for {}@{}'.format(archive_name, repo, ref))
output.write_line('Making {}.tar.gz for {}@{}'.format(
archive_name, repo, ref,
))
make_archive(archive_name, repo, ref, RESOURCES_DIR)

View File

@@ -66,5 +66,13 @@ def get_hook_message(
stdout_byte_stream = getattr(sys.stdout, 'buffer', sys.stdout)
def sys_stdout_write_wrapper(s, stream=stdout_byte_stream):
def write(s, stream=stdout_byte_stream):
stream.write(five.to_bytes(s))
stream.flush()
def write_line(s=None, stream=stdout_byte_stream):
if s is not None:
stream.write(five.to_bytes(s))
stream.write(b'\n')
stream.flush()