为flake8 python安装预推钩

时间:2018-06-12 13:44:05

标签: git githooks flake8

我已经阅读了官方的flake8文档,它定义了一个安装git pre-commit hook的2步过程:

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

但它似乎并没有提供任何其他类型的钩子。有没有办法安装预推钩。只需将pre-commit.exe重命名为pre-push.exe吗?

这是pre-commit文件:

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

1 个答案:

答案 0 :(得分:0)

最后写了我自己的预推钩子。如果有人需要,这是代码:

#!/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()

在执行此操作之前,您必须先安装预推挂钩。这个钩子测试了flake8和ember测试。