From 8e5b86d0461466a6960fa4874a4f9286e63a9653 Mon Sep 17 00:00:00 2001 From: Simeon Simeonov Date: Wed, 3 Feb 2021 09:18:15 +0100 Subject: Remove hardcoded tests and use JSON config instead --- pygitchecker.py | 79 +++++++++++++++++++++++++----------------------------- sample_config.json | 15 +++++++++++ 2 files changed, 52 insertions(+), 42 deletions(-) create mode 100644 sample_config.json diff --git a/pygitchecker.py b/pygitchecker.py index 4f4a872..469dcdf 100755 --- a/pygitchecker.py +++ b/pygitchecker.py @@ -2,6 +2,8 @@ # -*- coding: utf-8 -*- """Simple, on demand git checker""" import argparse +import errno +import json import os import subprocess import sys @@ -91,54 +93,33 @@ def fetch_all_files(abspath): return all_files_list -def validate_flake8(pfile): - """Validate using: flake8""" - try: - subprocess.run(['/usr/bin/flake8', pfile], check=True) - return True - except subprocess.CalledProcessError: - return False +def validate_python_file(pfile, tests, args): + """ + Validates a single .py file against tests + :param tests: A sequence of test dicts + :type tests: collections.Sequence -def validate_isort(pfile): - """Validate using: isort --check --diff""" - try: - subprocess.run(['/usr/bin/isort', '--check', '--diff', pfile], - check=True) - return True - except subprocess.CalledProcessError: - return False - + :param pfile: Python file (abs. path) + :type pfile: str -def validate_pylint(pfile): - """Validate using: pylint -r no --exit-zero""" - try: - subprocess.run(['/usr/bin/pylint', '-r', 'no', '--exit-zero', pfile], - check=True) - return True - except subprocess.CalledProcessError: - return False - - -def validate_python_file(pfile, args): - """ - Validates a single .py file + :param args: Argparse Namespace-object + :type args: argparse.Namespace :return: False if validation fails, False otherwise :rtype: bool """ - if not validate_flake8(pfile) and not args.continue_checks: - if args.verbose: - eprint(f'{pfile}: flake8 validation failed') - sys.exit(0) - if not validate_isort(pfile) and not args.continue_checks: + for test in tests: if args.verbose: - eprint(f'{pfile}: isort validation failed') - sys.exit(0) - if not validate_pylint(pfile) and not args.continue_checks: - if args.verbose: - eprint(f'{pfile}: pylint validation failed') - sys.exit(0) + print(f'{pfile} <- {test["name"]}') + try: + params = [param.replace('%p', pfile) for param in test['params']] + subprocess.run(params, check=test['check']) + except subprocess.CalledProcessError: + if not args.continue_checks: + if args.verbose: + eprint(f'{pfile}: {test["name"]} validation failed') + sys.exit(0) def main(inargs=None): @@ -157,10 +138,17 @@ def main(inargs=None): dest='all_files', help='Check all files, not only the modified ones') parser.add_argument( - '-c', '--continue-checks', + '-C', '--continue-checks', action='store_true', dest='continue_checks', help='Continue with the checks if validation fails') + parser.add_argument( + '-c', '--config', + metavar='FILE', + type=str, + default=os.path.expanduser('~/.pygitchecker.json'), + dest='config_file', + help='Config file (default: ~/.pygitchecker.json)') parser.add_argument( '-f', '--full', action='store_true', @@ -210,10 +198,17 @@ def main(inargs=None): if not selected_files: print('No files selected') sys.exit(0) + # now run the real tests + try: + with open(args.config_file, 'r') as fp: + config_dict = json.load(fp) + except Exception as e: + eprint(f'Unable to parse {args.config_file}: {e}') + sys.exit(errno.EIO) for target_file in selected_files: if args.verbose: print(f'Validating {target_file}:') - validate_python_file(target_file, args) + validate_python_file(target_file, config_dict.get('tests'), args) if __name__ == '__main__': diff --git a/sample_config.json b/sample_config.json new file mode 100644 index 0000000..81b1e15 --- /dev/null +++ b/sample_config.json @@ -0,0 +1,15 @@ +{ + "tests": [ + {"name": "black", + "params": ["black", "--check", "--diff", "--no-color", "-q", "%p"], + "check": true}, + {"name": "pyflakes", "params": ["pyflakes", "%p"], "check": true}, + {"name": "isort", + "params": ["isort", "--check", "--diff", "%p"], + "check": true}, + {"name": "pylint", + "params": ["pylint", "-r", "no", "--exit-zero", "%p"], + "check": true} + ] + +} -- cgit v1.3