Ejemplo n.º 1
0
def main():
    global pep8style
    # parse out our flags so pep8 doesn't get confused
    parser = get_parser()
    # This is required so we can parse out our added options
    opts, args = pep8.process_options(parse_argv=True, parser=parser)

    if opts.install_hook:
        from flake8.hooks import install_hook
        install_hook()

    if opts.builtins:
        s = '--builtins={0}'.format(opts.builtins)
        sys.argv.remove(s)

    if opts.exit_zero:
        sys.argv.remove('--exit-zero')

    if opts.install_hook:
        sys.argv.remove('--install-hook')

    complexity = opts.max_complexity
    if complexity > 0:
        sys.argv.remove('--max-complexity={0}'.format(complexity))

    # make sure pep8 gets the information it expects
    sys.argv.pop(0)
    sys.argv.insert(0, 'pep8')

    read_config(opts, parser)

    # We then have to re-parse argv to make sure pep8 is properly initialized
    pep8style = pep8.StyleGuide(parse_argv=True, config_file=True)
    merge_opts(pep8style.options, opts)
    warnings = 0
    stdin = None

    builtins = set(opts.builtins.split(','))
    if builtins:
        orig_builtins = set(flakey.checker._MAGIC_GLOBALS)
        flakey.checker._MAGIC_GLOBALS = orig_builtins | builtins

    if pep8style.paths and pep8style.options.filename is not None:
        for path in _get_python_files(pep8style.paths):
            if path == '-':
                if stdin is None:
                    stdin = read_stdin()
                warnings += check_code(stdin, opts.ignore, complexity)
            else:
                warnings += check_file(path, opts.ignore, complexity)
    else:
        stdin = read_stdin()
        warnings += check_code(stdin, opts.ignore, complexity)

    if opts.exit_zero:
        raise SystemExit(0)

    raise SystemExit(warnings)
Ejemplo n.º 2
0
def install_hook():
    vcs = find_vcs()

    if not vcs:
        p = get_parser()
        sys.stderr.write('Error: could not find either a git or mercurial '
                         'directory. Please re-run this in a proper '
                         'repository.')
        p.print_help()
        sys.exit(1)

    status = 0
    if 'git' in vcs:
        with open(vcs, 'w+') as fd:
            fd.write(git_hook_file)
        os.chmod(vcs, 744)
    elif 'hg' in vcs:
        _install_hg_hook(vcs)
    else:
        status = 1

    sys.exit(status)