From 472806c736d3ff1ae7f9d63aea4f8539e4846845 Mon Sep 17 00:00:00 2001 From: Simeon Simeonov Date: Tue, 4 Jul 2023 19:07:29 +0200 Subject: Add --skip / -s and format using black -S --- pygitchecker.py | 107 +++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 75 insertions(+), 32 deletions(-) diff --git a/pygitchecker.py b/pygitchecker.py index 6af9e0e..2e11542 100755 --- a/pygitchecker.py +++ b/pygitchecker.py @@ -75,12 +75,19 @@ def fetch_git_files(args): """Fetches new and modified .py-files using git""" try: repo = git.Repo(args.repo_path) - modified_files = {mfile.a_path for mfile in repo.index.diff(None) - if mfile.a_path.lower().endswith('.py')} - staged_files = {sfile.a_path for sfile in repo.index.diff('HEAD') - if sfile.a_path.lower().endswith('.py')} + modified_files = { + mfile.a_path + for mfile in repo.index.diff(None) + if mfile.a_path.lower().endswith('.py') + } + staged_files = { + sfile.a_path + for sfile in repo.index.diff('HEAD') + if sfile.a_path.lower().endswith('.py') + } untracked_files = set( - filter(lambda f: f.lower().endswith('.py'), repo.untracked_files)) + filter(lambda f: f.lower().endswith('.py'), repo.untracked_files) + ) return list(modified_files | staged_files | untracked_files) except git.InvalidGitRepositoryError: eprint(f'{args.repo_path} is not a valid git repository') @@ -113,7 +120,14 @@ def validate_python_file(pfile, tests, args): :return: False if validation fails, False otherwise :rtype: bool """ + skipped = [] + if args.skip: + skipped = args.skip.split(',') for test in tests: + if test['name'] in skipped: + if args.verbose: + print(f'{pfile} <- {test["name"]} (skipping)') + continue if args.verbose: print(f'{pfile} <- {test["name"]}') try: @@ -129,65 +143,94 @@ def validate_python_file(pfile, tests, args): def main(inargs=None): """Main entry point""" parser = argparse.ArgumentParser( - description='The following options are available') + description='The following options are available' + ) parser.add_argument( 'repo_path', metavar='REPO', nargs='?', default=os.getcwd(), - help='Git repo path (default: cwd)') + help='Git repo path (default: cwd)', + ) parser.add_argument( - '-a', '--all-files', + '-a', + '--all-files', action='store_true', dest='all_files', - help='Check all files, not only the modified ones') + 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') + help='Continue with the checks if validation fails', + ) parser.add_argument( - '-c', '--config', + '-c', + '--config', metavar='', type=str, default=os.path.expanduser('~/.pygitchecker.json'), dest='config_file', - help='Config file (default: ~/.pygitchecker.json)') + help='Config file (default: ~/.pygitchecker.json)', + ) parser.add_argument( - '-F', '--file', + '-F', + '--file', metavar='', type=str, default='', dest='python_file', - help='Validate a particular .py file') + help='Validate a particular .py file', + ) parser.add_argument( - '-f', '--full', + '-f', + '--full', action='store_true', dest='full_search', - help='Consider repos that contain untracked files (used with -m)') + help='Consider repos that contain untracked files (used with -m)', + ) parser.add_argument( - '-l', '--list-only', + '-l', + '--list-only', action='store_true', dest='list_only', - help='Only lists the target files without performing any checks') + help='Only lists the target files without performing any checks', + ) parser.add_argument( - '-m', '--modifications-only', + '-m', + '--modifications-only', action='store_true', dest='mod_only', - help='Only check for git modifications') + help='Only check for git modifications', + ) parser.add_argument( - '-r', '--recursive', + '-r', + '--recursive', action='store_true', dest='recursive', - help='Look for several repos recursively') + help='Look for several repos recursively', + ) + parser.add_argument( + '-s', + '--skip', + metavar='', + type=str, + default='', + dest='skip', + help='Skip one or more tests', + ) parser.add_argument( - '-v', '--verbose', + '-v', + '--verbose', action='store_true', dest='verbose', - help='Verbosity') + help='Verbosity', + ) args = parser.parse_args(inargs) try: - with io.open(args.config_file, 'r') as fp: + with io.open(args.config_file, 'r', encoding='utf-8') as fp: config_dict = json.load(fp) except Exception as e: eprint(f'Unable to parse {args.config_file}: {e}') @@ -201,9 +244,9 @@ def main(inargs=None): if not is_git_repo(abs_repo_path): eprint(f'{abs_repo_path} is not a valid git repository') sys.exit(1) - print_modification_status(abs_repo_path, - modifications_exist(abs_repo_path), - args) + print_modification_status( + abs_repo_path, modifications_exist(abs_repo_path), args + ) sys.exit(0) # resursive repo_list = fetch_git_repos(abs_repo_path) @@ -212,9 +255,9 @@ def main(inargs=None): sys.exit(0) repo_list.sort() for repo_path in repo_list: - print_modification_status(repo_path, - modifications_exist(repo_path), - args) + print_modification_status( + repo_path, modifications_exist(repo_path), args + ) sys.exit(0) if args.all_files: selected_files = fetch_all_files(abs_repo_path) -- cgit v1.3