Ejemplo n.º 1
0
    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',
        ))
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
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)
Ejemplo n.º 4
0
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)
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
0
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",
    )
Ejemplo n.º 7
0
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",
    )
Ejemplo n.º 8
0
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",
    )
Ejemplo n.º 9
0
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",
    )
Ejemplo n.º 10
0
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",
    )
Ejemplo n.º 11
0
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",
    )
Ejemplo n.º 12
0
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")
Ejemplo n.º 13
0
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"
        )
Ejemplo n.º 14
0
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)
Ejemplo n.º 15
0
    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',
        ))
Ejemplo n.º 16
0
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()
Ejemplo n.º 17
0
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()
Ejemplo n.º 18
0
def sys_stdout_write_wrapper(s, stream=stdout_byte_stream):
    stream.write(five.to_bytes(s))
Ejemplo n.º 19
0
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"
    )
Ejemplo n.º 20
0
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()
Ejemplo n.º 21
0
def md5(s):  # pragma: windows no cover
    return hashlib.md5(five.to_bytes(s)).hexdigest()
Ejemplo n.º 22
0
def write(s, stream=stdout_byte_stream):
    stream.write(five.to_bytes(s))
    stream.flush()
Ejemplo n.º 23
0
def sys_stdout_write_wrapper(s, stream=stdout_byte_stream):
    stream.write(five.to_bytes(s))
Ejemplo n.º 24
0
def write(s, stream=stdout_byte_stream):
    stream.write(five.to_bytes(s))
    stream.flush()
Ejemplo n.º 25
0
def md5(s):  # pragma: windows no cover
    return hashlib.md5(five.to_bytes(s)).hexdigest()