Snippets

Pierre-Marc Jodoin VITAL pre-commit file by Frederic B-Charron. Copy file in VITALAB_AI_FOLDER/.git/hooks/pre-commit and chmod +x

Created by Pierre-Marc Jodoin
#!/usr/bin/env python
import subprocess
import sys

def get_staged():
    proc = subprocess.Popen(['git', 'diff', '--name-only', '--cached'], stdout=subprocess.PIPE)
    staged_files = proc.stdout.readlines()
    staged_files = [f.decode('utf-8') for f in staged_files]
    staged_files = [f.strip() for f in staged_files]
    staged_files = [f for f in staged_files if f.endswith('.py')]
    return staged_files

def main():
    abort = False
    staged = get_staged()
    print("RUNNING PEP8")
    try:
        out = subprocess.check_output((sys.executable, '-m', 'pytest', '--pep8',
                                       '-m',  'pep8', '-n0', *staged),
                                stderr=subprocess.STDOUT)
    except subprocess.CalledProcessError as e:
        out = e.stdout
    out = out.decode('utf8')
    print(out)
    if 'FAILURES' in out:
        abort = True

    print("Running Tests")
    try:
        out = subprocess.check_output((sys.executable, '-m', 'pytest', 'tests'),
                                stderr=subprocess.STDOUT)
    except subprocess.CalledProcessError as e:
        out = e.stdout
    out = out.decode('utf8')
    print(out)
    if 'FAILURE' in out:
        abort = True

    if abort:
        sys.exit(1)
    else:
        sys.exit(0)

if __name__ == '__main__':
    main()

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.