forked from numenta/nupic-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpre-commit
executable file
·85 lines (70 loc) · 2.73 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
#!/usr/bin/env python
# ----------------------------------------------------------------------
# Numenta Platform for Intelligent Computing (NuPIC)
# Copyright (C) 2013, Numenta, Inc. Unless you have an agreement
# with Numenta, Inc., for a separate license for this software code, the
# following terms and conditions apply:
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# 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, see http://www.gnu.org/licenses.
#
# http://numenta.org/licenses/
# ----------------------------------------------------------------------
"""Git pre-commit hook.
Currently does the following checks:
* Runs unit tests.
* Checks for pylint errors.
"""
import os
import subprocess
import sys
def runTests(rootDir):
try:
subprocess.check_call([os.path.join(rootDir, 'run_tests.sh')])
except subprocess.CalledProcessError:
print ('Please fix unit tests before committing. If this is in '
'error, you can override with "git commit --no-verify ...".')
sys.exit(1)
def checkLint():
# Get a list of non-deleted Python files modified since last commit.
sub = subprocess.Popen('git diff --staged --name-only HEAD'.split(),
stdout=subprocess.PIPE)
sub.wait()
py_files_changed = [file for file in [f.strip() for f in
sub.stdout.readlines()]
if (file.endswith('.py') and os.path.exists(file)) or
isPythonScript(file)]
for file in py_files_changed:
try:
subprocess.check_call(['pylint', '--errors-only', file])
except subprocess.CalledProcessError:
print ('Please fix pylint errors before committing. If this is in '
'error, you can override with "git commit --no-verify ...".')
sys.exit(1)
def isPythonScript(filename):
"""Returns True if a file is a python executable."""
if not os.access(filename, os.X_OK):
return False
else:
try:
first_line = open(filename, 'r').next().strip()
return '#!' in first_line and 'python' in first_line
except StopIteration:
return False
def main():
sub = subprocess.Popen('git rev-parse --show-toplevel'.split(), stdout=subprocess.PIPE)
sub.wait()
rootDir = sub.stdout.readlines()[0].strip()
runTests(rootDir)
checkLint()
if __name__ == '__main__':
main()