Abhijeet Khangarot
Abhijeet Khangarot

Reputation: 1567

Install pre-push hook for flake8 python

I have read the official flake8 docs, it has defined a 2 step process to install git pre-commit hook:

flake8 --install-hook git
git config --bool flake8.strict true

But it doesn't seem to provide any other type of hooks. Is there a way to install pre-push hook. Will just renaming pre-commit.exe to pre-push.exe work?

This is the pre-commit file:

import sys
from flake8.main import git
if __name__ == '__main__':
    sys.exit(
        git.hook(
            strict=git.config_for('strict'),
            lazy=git.config_for('lazy'),
        )
    )

Upvotes: 1

Views: 1390

Answers (1)

Abhijeet Khangarot
Abhijeet Khangarot

Reputation: 1567

Finally wrote my own pre-push hook. Here is the code if anyone wants:

#!/usr/local/bin/python3

import os
import subprocess
import sys

# Pre-push hook that executes the Python/JS linters on all files that
# deviate from develop.
# To bypass the validation upon `git push` use the following command:
# `git push REMOTE BRANCH --no-verify`
# Change the first line of this file if your python3 installation
# is elsewhere.


def main():

    # flake8 linting tests for backend.
    # Make sure flake8 is installed in the virtual environment or give
    # the path where flake8 is installed.
    print('Running flake8 tests...')
    result = subprocess.run([os.getcwd() + '/venv/bin/flake8', 'api/'], stdout=subprocess.PIPE)
    check = result.stdout.decode('utf-8')
    if check:
        print('Please correct the linting errors: ')
        print(result.stdout.decode('utf-8'))
        sys.exit(1)
    print('Flake8 tests passed.')

    # ember tests for frontend.
    print('Running ember tests...')
    result_front = subprocess.run(['ember', 'test'], cwd='frontend', stdout=subprocess.PIPE)
    if result_front.returncode:
        print(result_front.stdout.decode('utf-8'))
        sys.exit(1)
    print('Ember tests passed.')


if __name__ == '__main__':
    main()

You have to first install the pre-push hook before executing this. This hook tests for flake8 and ember tests.

Upvotes: 2

Related Questions