diff options
| author | Ian Cordasco <graffatcolmingov@gmail.com> | 2013-01-02 16:04:55 -0500 |
|---|---|---|
| committer | Ian Cordasco <graffatcolmingov@gmail.com> | 2013-01-02 16:04:55 -0500 |
| commit | d8a30e19af37aa5295a551cf516ddf43065bed65 (patch) | |
| tree | 9955d02dfa99b5aff83b378bc4f264cfc9010ded /flake8/hooks.py | |
| parent | 3bd8a21f62db68074289960b1a2ca5bc0b0e2308 (diff) | |
| download | flake8-d8a30e19af37aa5295a551cf516ddf43065bed65.tar.gz | |
Mention changes in preparation for 2.0.0
Working version of --install-hook.
Diffstat (limited to 'flake8/hooks.py')
| -rw-r--r-- | flake8/hooks.py | 66 |
1 files changed, 65 insertions, 1 deletions
diff --git a/flake8/hooks.py b/flake8/hooks.py index 970fffa..7d493b8 100644 --- a/flake8/hooks.py +++ b/flake8/hooks.py @@ -1,5 +1,7 @@ import os -from flake8.util import _initpep8, pep8style, skip_file +import sys +from flake8.util import (_initpep8, pep8style, skip_file, get_parser, + ConfigParser) from subprocess import Popen, PIPE @@ -78,3 +80,65 @@ def find_vcs(): elif os.path.isdir('.hg'): return '.hg/hgrc' return '' + + +git_hook_file = """#!/usr/bin/env python +import sys +import os +from flake8.hooks import git_hook + +COMPLEXITY = os.getenv('FLAKE8_COMPLEXITY', 10) +STRICT = os.getenv('FLAKE8_STRICT', False) + + +if __name__ == '__main__': + sys.exit(git_hook(complexity=COMPLEXITY, strict=STRICT)) +""" + + +def _install_hg_hook(path): + c = ConfigParser() + c.readfp(open(path, 'r')) + if not c.has_section('hooks'): + c.add_section('hooks') + + if not c.has_option('hooks', 'commit'): + c.set('hooks', 'commit', 'python:flake8.hooks.hg_hook') + + if not c.has_option('hooks', 'qrefresh'): + c.set('hooks', 'qrefresh', 'python:flake8.hooks.hg_hook') + + if not c.has_section('flake8'): + c.add_section('flake8') + + if not c.has_option('flake8', 'complexity'): + c.set('flake8', 'complexity', str(os.getenv('FLAKE8_COMPLEXITY', 10))) + + if not c.has_option('flake8', 'strict'): + c.set('flake8', 'strict', os.getenv('FLAKE8_STRICT', False)) + + c.write(open(path, 'w+')) + + +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) |
