chiark / gitweb /
pep8: start obeying E226
[fdroidserver.git] / hooks / pre-commit
1 #!/bin/bash
2 #
3 # Simple pre-commit hook to check that there are no errors in the fdroidserver
4 # source files.
5
6 # Redirect output to stderr.
7 exec 1>&2
8
9 files=`git diff-index --cached HEAD 2>&1 | sed 's/^:.*     //' | uniq | cut -b100-500`
10 if [ -z "$files" ]; then
11     PY_FILES="fdroid makebuildserver setup.py examples/*.py buildserver/*.py fdroidserver/*.py"
12     PY_TEST_FILES="tests/*.TestCase"
13     SH_FILES="hooks/pre-commit"
14     BASH_FILES="fd-commit jenkins-build docs/update.sh completion/bash-completion"
15     RB_FILES="buildserver/cookbooks/*/recipes/*.rb"
16 else
17     # if actually committing right now, then only run on the files
18     # that are going to be committed at this moment
19     PY_FILES=
20     PY_TEST_FILES=
21     SH_FILES=
22     BASH_FILES=
23     RB_FILES=
24
25     for f in $files; do
26         case $f in
27             *.py)
28                 PY_FILES+=" $f"
29                 ;;
30             *.TestCase)
31                 PY_TEST_FILES+=" $f"
32                 ;;
33             *.rb)
34                 RB_FILES+=" $f"
35                 ;;
36             *)
37                 if head -1 $f | grep '^#!/bin/sh' > /dev/null 2>&1; then
38                     SH_FILES+=" $f"
39                 elif head -1 $f | grep '^#!/bin/bash' > /dev/null 2>&1; then
40                     BASH_FILES+=" $f"
41                 elif head -1 $f | grep '^#!.*python' > /dev/null 2>&1; then
42                     PY_FILES+=" $f"
43                 fi
44                 ;;
45         esac
46     done
47 fi
48
49 # We ignore the following PEP8 warnings
50 # * E123: closing bracket does not match indentation of opening bracket's line
51 #   - Broken if multiple indentation levels start on a single line
52 # * E501: line too long (82 > 79 characters)
53 #   - Recommended for readability but not enforced
54 #   - Some lines are awkward to wrap around a char limit
55 # * W503: line break before binary operator
56 #   - Quite pedantic
57
58 PEP8_IGNORE="E123,E501,W503"
59
60 err() {
61         echo ERROR: "$@"
62         exit 1
63 }
64
65 warn() {
66         echo WARNING: "$@"
67 }
68
69 cmd_exists() {
70         command -v $1 1>/dev/null
71 }
72
73 if cmd_exists pyflakes-python3; then
74         PYFLAKES=pyflakes-python3
75 elif cmd_exists pyflakes; then
76         PYFLAKES=pyflakes
77 else
78         PYFLAKES=echo
79         warn "pyflakes is not installed, using dummy placeholder!"
80 fi
81
82 if cmd_exists pep8-python3; then
83         PEP8=pep8-python3
84 elif cmd_exists pep8; then
85         PEP8=pep8
86 else
87         err "pep8 is not installed!"
88 fi
89
90 if [ "$PY_FILES $PY_TEST_FILES" != " " ]; then
91     if ! $PYFLAKES $PY_FILES $PY_TEST_FILES; then
92         err "pyflakes tests failed!"
93     fi
94 fi
95
96 if [ "$PY_FILES" != "" ]; then
97     if ! $PEP8 --ignore=$PEP8_IGNORE $PY_FILES; then
98         err "pep8 tests failed!"
99     fi
100 fi
101
102 # The tests use a little hack in order to cleanly import the fdroidserver
103 # package locally like a regular package.  pep8 doesn't see that, so this
104 # makes pep8 skip E402 on the test files that need that hack.
105 if [ "$PY_TEST_FILES" != "" ]; then
106     if ! $PEP8 --ignore=$PEP8_IGNORE,E402 $PY_TEST_FILES; then
107         err "pep8 tests failed!"
108     fi
109 fi
110
111 for f in $SH_FILES; do
112         if ! dash -n $f; then
113                 err "dash tests failed!"
114         fi
115 done
116
117 for f in $BASH_FILES; do
118         if ! bash -n $f; then
119                 err "bash tests failed!"
120         fi
121 done
122
123 for f in $RB_FILES; do
124         if ! ruby -c $f 1>/dev/null; then
125                 err "ruby tests failed!"
126         fi
127 done
128
129 exit 0