chiark / gitweb /
metadata: Always use regex to verify values
[fdroidserver.git] / hooks / pre-commit
1 #!/bin/sh
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 PY_FILES="fdroid makebuildserver setup.py examples/*.py buildserver/*.py fdroidserver/*.py"
10 PY_TEST_FILES="tests/*.TestCase"
11 SH_FILES="hooks/pre-commit"
12 BASH_FILES="fd-commit jenkins-build docs/update.sh completion/bash-completion"
13 RB_FILES="buildserver/cookbooks/*/recipes/*.rb"
14
15 # In the default configuration, the checks E123, E133, E226, E241 and E242 are
16 # ignored because they are not rules unanimously accepted
17 # On top of those, we ignore:
18 # * E501: line too long (82 > 79 characters)
19 #   - Recommended for readability but not enforced
20 #   - Some lines are awkward to wrap around a char limit
21 # * W503: line break before binary operator
22 #   - It's quite new
23 #   - Quite pedantic
24
25 PEP8_IGNORE="E123,E133,E226,E241,E242,E501,W503"
26
27 err() {
28         echo ERROR: "$@"
29         exit 1
30 }
31
32 cmd_exists() {
33         command -v $1 1>/dev/null
34 }
35
36 if cmd_exists pyflakes-python2; then
37         PYFLAKES=pyflakes-python2
38 elif cmd_exists pyflakes; then
39         PYFLAKES=pyflakes
40 else
41         err "pyflakes is not installed!"
42 fi
43
44 if cmd_exists pep8-python2; then
45         PEP8=pep8-python2
46 elif cmd_exists pep8; then
47         PEP8=pep8
48 else
49         err "pep8 is not installed!"
50 fi
51
52 if ! $PYFLAKES $PY_FILES $PY_TEST_FILES; then
53         err "pyflakes tests failed!"
54 fi
55
56 if ! $PEP8 --ignore=$PEP8_IGNORE $PY_FILES; then
57         err "pep8 tests failed!"
58 fi
59
60 # The tests use a little hack in order to cleanly import the fdroidserver
61 # package locally like a regular package.  pep8 doesn't see that, so this
62 # makes pep8 skip E402 on the test files that need that hack.
63 if ! $PEP8 --ignore=$PEP8_IGNORE,E402 $PY_TEST_FILES; then
64         err "pep8 tests failed!"
65 fi
66
67
68 for f in $SH_FILES; do
69         if ! dash -n $f; then
70                 err "dash tests failed!"
71         fi
72 done
73
74 for f in $BASH_FILES; do
75         if ! bash -n $f; then
76                 err "bash tests failed!"
77         fi
78 done
79
80 for f in $RB_FILES; do
81         if ! ruby -c $f 1>/dev/null; then
82                 err "ruby tests failed!"
83         fi
84 done
85
86 exit 0