def to_bytes(self): output = [] 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(b'(none)') return b''.join(( five.to_bytes( 'Command: {!r}\n' 'Return code: {}\n' 'Expected return code: {}\n'.format( self.cmd, self.returncode, self.expected_returncode, ), ), b'Output: ', output[0], b'\n', b'Errors: ', output[1], b'\n', ))
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) write_fn(error_msg) write_fn('Check the log at ~/.pre-commit/pre-commit.log\n') 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') raise PreCommitSystemExit(1)
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', )) 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: output.write(error_msg, stream=log) output.write_line(formatted, stream=log) raise PreCommitSystemExit(1)
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', )) output.write(error_msg) store = Store() log_path = os.path.join(store.directory, 'pre-commit.log') output.write_line('Check the log at {}'.format(log_path)) with open(log_path, 'wb') as log: output.write(error_msg, stream=log) output.write_line(formatted, stream=log) raise SystemExit(1)
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', )) output.write(error_msg) output.write_line('Check the log at ~/.pre-commit/pre-commit.log') store = Store() store.require_created() with open(os.path.join(store.directory, 'pre-commit.log'), 'wb') as log: output.write(error_msg, stream=log) output.write_line(formatted, stream=log) raise PreCommitSystemExit(1)
def test_versioned_python_hook(tempdir_factory, store): _test_hook_repo( tempdir_factory, store, 'python3_hooks_repo', 'python3-hook', [os.devnull], b"3.5\n['" + five.to_bytes(os.devnull) + b"']\nHello World\n", )
def test_python_hook_weird_setup_cfg(in_git_dir, tempdir_factory, store): in_git_dir.join('setup.cfg').write('[install]\ninstall_scripts=/usr/sbin') _test_hook_repo( tempdir_factory, store, 'python_hooks_repo', 'foo', [os.devnull], b"['" + five.to_bytes(os.devnull) + b"']\nHello World\n", )
def test_python_venv(tempdir_factory, store): # pragma: no cover (no venv) _test_hook_repo( tempdir_factory, store, 'python_venv_hooks_repo', 'foo', [os.devnull], b"['" + five.to_bytes(os.devnull) + b"']\nHello World\n", )
def test_python_hook(tempdir_factory, store): _test_hook_repo( tempdir_factory, store, 'python_hooks_repo', 'foo', [os.devnull], b"['" + five.to_bytes(os.devnull) + b"']\nHello World\n", )
def test_python_hook(tempdir_factory, store): _test_hook_repo( tempdir_factory, store, "python_hooks_repo", "foo", [os.devnull], b"['" + five.to_bytes(os.devnull) + b"']\nHello World\n", )
def test_python_hook_weird_setup_cfg(tempdir_factory, store): path = git_dir(tempdir_factory) with cwd(path): with io.open('setup.cfg', 'w') as setup_cfg: setup_cfg.write('[install]\ninstall_scripts=/usr/sbin\n') _test_hook_repo( tempdir_factory, store, 'python_hooks_repo', 'foo', [os.devnull], b"['" + five.to_bytes(os.devnull) + b"']\nHello World\n")
def test_python_hook_weird_setup_cfg(tempdir_factory, store): path = git_dir(tempdir_factory) with cwd(path): with io.open('setup.cfg', 'w') as setup_cfg: setup_cfg.write('[install]\ninstall_scripts=/usr/sbin\n') _test_hook_repo( tempdir_factory, store, 'python_hooks_repo', 'foo', [os.devnull], b"['" + five.to_bytes(os.devnull) + b"']\nHello World\n" )
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), )) output.write_line(error_msg) store = Store() log_path = os.path.join(store.directory, 'pre-commit.log') output.write_line('Check the log at {}'.format(log_path)) with open(log_path, 'wb') as log: def _log_line(*s): # type: (*str) -> None output.write_line(*s, stream=log) _log_line('### version information') _log_line() _log_line('```') _log_line('pre-commit version: {}'.format(C.VERSION)) _log_line('sys.version:') for line in sys.version.splitlines(): _log_line(' {}'.format(line)) _log_line('sys.executable: {}'.format(sys.executable)) _log_line('os.name: {}'.format(os.name)) _log_line('sys.platform: {}'.format(sys.platform)) _log_line('```') _log_line() _log_line('### error information') _log_line() _log_line('```') _log_line(error_msg) _log_line('```') _log_line() _log_line('```') _log_line(formatted) _log_line('```') raise SystemExit(1)
def to_bytes(self): output = [] 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(b'(none)') 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 write_line(s=None, stream=stdout_byte_stream, logfile_name=None): output_streams = [stream] if logfile_name: ctx = open(logfile_name, 'ab') output_streams.append(ctx) else: ctx = noop_context() with ctx: for output_stream in output_streams: if s is not None: output_stream.write(five.to_bytes(s)) output_stream.write(b'\n') output_stream.flush()
def sys_stdout_write_wrapper(s, stream=stdout_byte_stream): stream.write(five.to_bytes(s))
def test_python_hook(tmpdir_factory, store): _test_hook_repo( tmpdir_factory, store, 'python_hooks_repo', 'foo', [os.devnull], b"['" + five.to_bytes(os.devnull) + b"']\nHello World\n" )
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()
def md5(s): # pragma: windows no cover return hashlib.md5(five.to_bytes(s)).hexdigest()
def write(s, stream=stdout_byte_stream): stream.write(five.to_bytes(s)) stream.flush()