forked from unknown-horizons/unknown-horizons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre-commit
executable file
·132 lines (113 loc) · 4.19 KB
/
pre-commit
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python2
#-*- mode: python -*-
# ###################################################
# Copyright (C) 2008-2016 The Unknown Horizons Team
# team@unknown-horizons.org
# This file is part of Unknown Horizons.
#
# Unknown Horizons is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
# ###################################################
"""
INFORMATION:
This is the pre-commit hook used on the unknown horizons team. It checks the
staged files using pyflakes and checks for added print statements. It gives you
the choice to cancel a commit if errors are found.
= Installation =
== Windows ==
Copy this file to .git/hooks/
Your done! If the hook is updated you have to copy it again manually.
== Linux/MacOS ==
cd .git/hooks
ln -s ../../development/pre-commit pre-commit
This will create a soft-link to the file in development and therefore keep it
updated automatically.
"""
from subprocess import Popen, PIPE
import sys
import re
import Tkinter
import tkMessageBox
syntax_checker = "pyflakes-python2"
class bcolors:
"""Colors used to color the output"""
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
def cleanup(pyflakeserrorlist):
"""Methode used to filter bad errors from pyflakes"""
for line in list(pyflakeserrorlist):
if "undefined name '_'" in line or "undefined name 'N_'" in line:
# These messages are produced because of gettext and cannot be removed
# Therefore we ignore the error
del pyflakeserrorlist[pyflakeserrorlist.index(line)]
return pyflakeserrorlist
def run(command):
"""Runs a command and returns a triple with the output: (returnvalue, stdout, stderr)"""
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()]
_, files_modified, _= run("git diff-index --name-only HEAD")
exitcode = 0
# Find words not suitable to be included in commits
invalid_regexes = [
'print ',
'pdb',
'set_trace']
for fname in files_modified:
if fname.strip().endswith(".py"):
# Check using pyflakes
exit_code, stderrors, errors = run("%s %s"%(syntax_checker, fname))
stderrors = cleanup(stderrors)
errors = cleanup(errors)
stderrors.extend(errors)
if stderrors:
print >>sys.stderr, "\rChecking syntax on %s:" %(fname), bcolors.FAIL, "FAILED!", bcolors.ENDC
for line in stderrors:
print >>sys.stderr, "\t", line
exitcode = exit_code
else:
print >>sys.stderr, "\rChecking syntax on %s:" %(fname), bcolors.OKGREEN, "OK!", bcolors.ENDC
# Check for added prints or other stuff
print >>sys.stderr, "Checking for 'print', 'pdb' and 'set_trace' on %s: ... " %(fname),
_, output, _ = run("git diff-index -u HEAD %s" %(fname))
found_re = False
for i, line in enumerate(output):
if not line.startswith('+'):
continue
for r in invalid_regexes:
if re.search(r, line):
if not found_re:
print >>sys.stderr, "\rChecking for '%s' on %s:" % (r, fname), bcolors.FAIL, "FAILED!", bcolors.ENDC
found_re = True
if i > 0:
print "\t", output[i-1]
print "\t", bcolors.FAIL, line, bcolors.ENDC
if len(output) > i + 1:
print "\t", output[i+1]
if found_re:
exitcode = 1
else:
print >>sys.stderr, "\rChecking for 'print', 'pdb' and 'set_trace' on %s:" %(fname), bcolors.OKGREEN, "OK!", bcolors.ENDC
if exitcode == 1:
# Hide root Tk window that is always created
window = Tkinter.Tk()
window.wm_withdraw()
if not tkMessageBox.askyesno("Errors have been detected", "Cancel Commit?"):
exitcode = 0
sys.exit(exitcode)