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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
#!/usr/bin/env python
# pylint: disable=invalid-name
# Copyright (c) 2013-2020, SIB - Swiss Institute of Bioinformatics and
# Biozentrum - University of Basel
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import subprocess, sys, re, os, difflib
# prevent creating .pyc files in the source directory
sys.dont_write_bytecode = True
# now extend the PYTHONPATH to eat the ProMod3 coding style checker
sys.path.insert(0, os.path.join(os.getcwd(), 'extras', 'pre_commit'))
import pm3_csc
### SETUP
MIN_GIT_VERSION = "1.7.5.1"
## Files to be excluded from whitespace check have to be declared as regexp
## referring to the full path. So '.*\.pdf' skips ANY PDF file, while
## 'doc/html/.*\.html' only skips HTML files in the doc directory.
WHITESPACECHECK_EXCLUDES = [r'.*\.pdf', r'.*\.pdb']
## Files to be excluded from any check. This is most likely only for 'produced'
## files like documentation as HTML files.
EXCLUDE_COMPLETELY = [r'^doc/html/.*\.html', r'^doc/html/.*\.js',
r'^doc/html/.*\.inv', r'^doc/html/.*\.txt']
PYCODECHECK_EXCLUDES = ['extras/pre_commit/pre-commit','doc/cmake.py',
'core/pymod/__init__.py.in']
ALLOW_LONG_LINES = ['extras/pre_commit/pm3_csc/filecheck/pylint-unittest-rc',
'extras/pre_commit/pm3_csc/filecheck/pylintrc']
### FUNCTIONS - BEGIN
### FUNCTIONS - END
### MAIN - BEGIN
# making script 'hot'
unbuffered = os.fdopen(sys.stdout.fileno(), 'w', 0)
sys.stdout = unbuffered
# check script is not a link
THIS_PATH = os.path.abspath(__file__)
if os.path.islink(THIS_PATH):
pm3_csc.FailMsg("Git hook is a link, that is insecure. Please make it a "+
"copy", 4)
# system checks
pm3_csc.git.CheckVersion(MIN_GIT_VERSION)
pm3_csc.CheckLatestHookCode(THIS_PATH, os.path.join('extras', 'pre_commit',
'pre-commit'),
script_name=os.path.basename(THIS_PATH),
git_path='.git/hooks/')
# check software needed
pm3_csc.CheckInstalled('pylint')
# fetching data
mod_file_dict = pm3_csc.git.GetModifiedFiles(EXCLUDE_COMPLETELY)
# after testing files, remove key, later check if any key is left!
# checks on commit
print "[ Checking for trailing whitespace(s) ]"
pm3_csc.git.CheckWhitespaces(EXCLUDE_COMPLETELY + WHITESPACECHECK_EXCLUDES)
# check forbidden things in Python code
print "[ Checking Python code ]"
if not len(mod_file_dict['python']):
print " Nothing to be done."
for f in mod_file_dict['python']:
if f in PYCODECHECK_EXCLUDES:
print " Excluded from being checked: %s" % f
continue
print " Checking '%s'..." % f,
pf = pm3_csc.filecheck.Python(f)
pf.Check()
print "done"
mod_file_dict.pop('python')
print "[ Checking C/ C++ code ]"
if not len(mod_file_dict['C']):
print " Nothing to be done."
for f in mod_file_dict['C']:
print " Checking '%s'..." % f,
cf = pm3_csc.filecheck.Ccode(f)
cf.Check()
print "done"
mod_file_dict.pop('C')
# check documentation files
print "[ Checking ReST files ]"
if not len(mod_file_dict['rest']):
print " Nothing to be done."
for f in mod_file_dict['rest']:
print " Checking '%s'..." % f,
rf = pm3_csc.filecheck.Rest(f)
rf.Check()
print "done"
mod_file_dict.pop('rest')
# check CMake setup
print "[ Checking CMake files ]"
if not len(mod_file_dict['cmake']):
print " Nothing to be done."
for f in mod_file_dict['cmake']:
print " Checking '%s'..." % f,
cf = pm3_csc.filecheck.Cmake(f)
cf.Check()
print "done"
mod_file_dict.pop('cmake')
print "[ Checking shell scripts ]"
if not len(mod_file_dict['shell-script']):
print " Nothing to be done."
for f in mod_file_dict['shell-script']:
print " Checking '%s'..." % f,
sf = pm3_csc.filecheck.ShellScript(f)
sf.Check()
print "done"
mod_file_dict.pop('shell-script')
print "[ Checking text files ]"
if not len(mod_file_dict['text']):
print " Nothing to be done."
for f in mod_file_dict['text']:
print " Checking '%s'..." % f,
tf = pm3_csc.filecheck.Text(f)
ignore_line_with = False
if f in ALLOW_LONG_LINES:
ignore_line_with = True
tf.Check(ignore_line_with)
print "done"
mod_file_dict.pop('text')
print "[ Files modified but not checked (unknown/ irrelevant format) ]"
if not len(mod_file_dict['ukn']):
print " None."
for f in mod_file_dict['ukn']:
print " %s" % f
mod_file_dict.pop('ukn')
if len(mod_file_dict.keys()):
pm3_csc.FailMsg("Following file types were not considered in any test: %s" \
% ', '.join(mod_file_dict.keys()), 18)
### MAIN - END
## Emacs magic
# Local Variables:
# mode: python
# End:
|