mirror of
https://github.com/PyCQA/flake8.git
synced 2026-03-29 10:36:53 +00:00
added a git hook - fixes #8
This commit is contained in:
parent
6eb3405dd0
commit
e439682c85
2 changed files with 68 additions and 9 deletions
45
README
45
README
|
|
@ -37,7 +37,7 @@ To run flake8 just invoke it against any directory or Python module::
|
|||
The output of PyFlakes *and* pep8 is merged and returned.
|
||||
|
||||
flake8 offers an extra option: --max-complexity, which will emit a warning if the
|
||||
McCabe complexityu of a function is higher that the value. By default it's
|
||||
McCabe complexityu of a function is higher that the value. By default it's
|
||||
deactivated::
|
||||
|
||||
$ bin/flake8 --max-complexity 12 flake8
|
||||
|
|
@ -50,7 +50,7 @@ deactivated::
|
|||
coolproject/mod.py:1204:1: 'selftest' is too complex (14)
|
||||
|
||||
This feature is quite useful to detect over-complex code. According to McCabe, anything
|
||||
that goes beyond 10 is too complex.
|
||||
that goes beyond 10 is too complex.
|
||||
See https://en.wikipedia.org/wiki/Cyclomatic_complexity.
|
||||
|
||||
|
||||
|
|
@ -72,27 +72,56 @@ like this::
|
|||
If *strict* option is set to **1**, any warning will block the commit. When
|
||||
*strict* is set to **0**, warnings are just displayed in the standard output.
|
||||
|
||||
*complexity* defines the maximum McCabe complexity allowed before a warning
|
||||
*complexity* defines the maximum McCabe complexity allowed before a warning
|
||||
is emited. If you don't specify it it's just ignored. If specified, must
|
||||
be a positive value. 12 is usually a good value.
|
||||
|
||||
Git hook
|
||||
========
|
||||
|
||||
To use the Git hook on any *commit*, add a **pre-commit** file in the
|
||||
*.git/hooks* directory containing::
|
||||
|
||||
#!/usr/bin/python
|
||||
import sys
|
||||
from flake8.run import git_hook
|
||||
|
||||
COMPLEXITY = 10
|
||||
STRICT = False
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(git_hook(complexity=COMPLEXITY, strict=STRICT))
|
||||
|
||||
|
||||
If *strict* option is set to **True**, any warning will block the commit. When
|
||||
*strict* is set to **False** or omited, warnings are just displayed in the
|
||||
standard output.
|
||||
|
||||
*complexity* defines the maximum McCabe complexity allowed before a warning
|
||||
is emited. If you don't specify it or set it to **-1**, it's just ignored.
|
||||
If specified, it must be a positive value. 12 is usually a good value.
|
||||
|
||||
Also, make sure the file is executable and adapt the shebang line so it
|
||||
point to your python interpreter.
|
||||
|
||||
|
||||
Buildout integration
|
||||
=====================
|
||||
|
||||
In order to use Flake8 inside a buildout, edit your buildout.cfg and add this::
|
||||
|
||||
[buildout]
|
||||
|
||||
|
||||
parts +=
|
||||
...
|
||||
flake8
|
||||
|
||||
|
||||
[flake8]
|
||||
recipe = zc.recipe.egg
|
||||
eggs = flake8
|
||||
${buildout:eggs}
|
||||
entry-points =
|
||||
flake8=flake8.run:main
|
||||
flake8=flake8.run:main
|
||||
|
||||
Original projects
|
||||
=================
|
||||
|
|
@ -111,7 +140,9 @@ CHANGES
|
|||
1.2 - ?
|
||||
-------
|
||||
|
||||
?
|
||||
- added a git hook
|
||||
-
|
||||
|
||||
|
||||
1.1 - 2012-02-14
|
||||
----------------
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ Implementation of the command-line I{flake8} tool.
|
|||
import sys
|
||||
import os
|
||||
import os.path
|
||||
from subprocess import PIPE, Popen
|
||||
|
||||
from flake8.util import skip_file
|
||||
from flake8 import pep8
|
||||
|
|
@ -90,8 +91,7 @@ class _PEP8Options(object):
|
|||
testsuite = ''
|
||||
doctest = False
|
||||
|
||||
|
||||
def hg_hook(ui, repo, **kwargs):
|
||||
def _initpep8():
|
||||
# default pep8 setup
|
||||
pep8.options = _PEP8Options()
|
||||
pep8.options.physical_checks = pep8.find_checks('physical_line')
|
||||
|
|
@ -99,6 +99,34 @@ def hg_hook(ui, repo, **kwargs):
|
|||
pep8.options.counters = dict.fromkeys(pep8.BENCHMARK_KEYS, 0)
|
||||
pep8.options.messages = {}
|
||||
pep8.args = []
|
||||
|
||||
|
||||
def run(command):
|
||||
p = Popen(command.split(), stdout=PIPE, stderr=PIPE)
|
||||
p.wait()
|
||||
return (p.returncode, [line.strip() for line in p.stdout.readlines()],
|
||||
[line.strip() for line in p.stderr.readlines()])
|
||||
|
||||
|
||||
def git_hook(complexity=-1, strict=False):
|
||||
_initpep8()
|
||||
warnings = 0
|
||||
|
||||
_, files_modified, _ = run("git diff-index --name-only HEAD")
|
||||
for filename in files_modified:
|
||||
ext = os.path.splitext(filename)[-1]
|
||||
if ext != '.py':
|
||||
continue
|
||||
warnings += check_file(filename, complexity)
|
||||
|
||||
if strict:
|
||||
return warnings
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
def hg_hook(ui, repo, **kwargs):
|
||||
_initpep8()
|
||||
complexity = ui.configint('flake8', 'complexity', default=-1)
|
||||
warnings = 0
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue