From 0361a966ed1f8bc8fcf024e211ed35097d6cffa3 Mon Sep 17 00:00:00 2001 From: Simeon Simeonov Date: Thu, 13 Sep 2018 23:45:55 +0200 Subject: Use pygit2 instead of gitapi and flake8 instead of pep8 --- pygitchecker.py | 42 +++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) (limited to 'pygitchecker.py') diff --git a/pygitchecker.py b/pygitchecker.py index 29c9167..77f85ec 100755 --- a/pygitchecker.py +++ b/pygitchecker.py @@ -3,8 +3,30 @@ import argparse import sys -import gitapi -import pep8 +import pygit2 + +from flake8.api import legacy as flake8_legacy + + +def fetch_git_files(args): + """ + Fetches new and modified .py-files using pygit2 + """ + repo = pygit2.Repository(args.repo_path) + status = repo.status() + git_files_list = list() + if not status: + return git_files_list + for gfile, state in status.items(): + if ( + state in (pygit2.GIT_STATUS_WT_MODIFIED, + pygit2.GIT_STATUS_WT_NEW, + pygit2.GIT_STATUS_INDEX_NEW, + pygit2.GIT_STATUS_INDEX_MODIFIED) and + gfile.lower().endswith('.py') + ): + git_files_list.append(gfile) + return git_files_list if __name__ == '__main__': @@ -23,16 +45,10 @@ if __name__ == '__main__': default=False, help='Disable pep8 checks') args = parser.parse_args() - git_repo = gitapi.Repo(args.repo_path) - status = git_repo.git_status() - if 'M' not in status: + git_files = fetch_git_files(args) + if not git_files: print('No modifications') sys.exit(0) - remarks = False - for mfile in status.get('M'): - if not args.disable_pep8: - checker = pep8.Checker(mfile) - if checker.check_all(): - remarks = True - if remarks: - print('Done!') + report = flake8_legacy.get_style_guide().check_files(git_files) + if report.total_errors: + print('Total errors: {errors}'.format(errors=report.total_errors)) -- cgit v1.3