summaryrefslogtreecommitdiff
path: root/hooks/pre-commit.flake8
blob: 0d61a2e518b23c0f3c2f719cf672fd7ddbba381d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/usr/bin/env python

import glob
import os
import sys

os.environ['PYFLAKES_NODOCTEST'] = '1'

# pycodestyle.py uses sys.argv to find setup.cfg
sys.argv = [os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)]

# git usurbs your bin path for hooks and will always run system python
if 'VIRTUAL_ENV' in os.environ:
    site_packages = glob.glob(
        '%s/lib/*/site-packages' % os.environ['VIRTUAL_ENV'])[0]
    sys.path.insert(0, site_packages)


def main():
    from flake8.api.legacy import get_style_guide
    from flake8.hooks import run

    gitcmd = "git diff-index --cached --name-only HEAD"

    _, files_modified, _ = run(gitcmd)

    try:
        text_type = unicode
    except NameError:
        text_type = str

    files_modified = [text_type(x) for x in files_modified]

    # remove non-py files and files which no longer exist
    files_modified = filter(
        lambda x: x.endswith('.py') and os.path.exists(x),
        files_modified)

    flake8_style = get_style_guide(parse_argv=True)
    report = flake8_style.check_files(files_modified)

    return report.total_errors != 0

if __name__ == '__main__':
    sys.exit(main())