Skip to content
Snippets Groups Projects
pre-commit 4.40 KiB
#!/usr/bin/env python
# pylint: disable=invalid-name

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']
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: