diff options
| author | Simeon Simeonov | 2018-09-17 00:01:46 +0200 |
|---|---|---|
| committer | Simeon Simeonov | 2018-09-17 00:01:46 +0200 |
| commit | 7c0dde7dca016177ba05c04632a470a06f1daccd (patch) | |
| tree | 53d7a8be5fa89eec52aad9cc15de4065f40bd741 /pygitchecker.py | |
| parent | 0361a966ed1f8bc8fcf024e211ed35097d6cffa3 (diff) | |
Added new options -m -r. Some code restructuring
Diffstat (limited to 'pygitchecker.py')
| -rwxr-xr-x | pygitchecker.py | 120 |
1 files changed, 114 insertions, 6 deletions
diff --git a/pygitchecker.py b/pygitchecker.py index 77f85ec..d826084 100755 --- a/pygitchecker.py +++ b/pygitchecker.py | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | #!/usr/bin/env python3 | 1 | #!/usr/bin/env python3 |
| 2 | 2 | ||
| 3 | import argparse | 3 | import argparse |
| 4 | import os | ||
| 4 | import sys | 5 | import sys |
| 5 | 6 | ||
| 6 | import pygit2 | 7 | import pygit2 |
| @@ -8,13 +9,83 @@ import pygit2 | |||
| 8 | from flake8.api import legacy as flake8_legacy | 9 | from flake8.api import legacy as flake8_legacy |
| 9 | 10 | ||
| 10 | 11 | ||
| 12 | def is_git_repo(abspath): | ||
| 13 | """ | ||
| 14 | True of abspath points to a git repo, False otherwise | ||
| 15 | """ | ||
| 16 | if not os.path.isdir(abspath): | ||
| 17 | return False | ||
| 18 | try: | ||
| 19 | pygit2.Repository(abspath) | ||
| 20 | return True | ||
| 21 | except pygit2.GitError: | ||
| 22 | return False | ||
| 23 | |||
| 24 | |||
| 25 | def modifications_exist(abspath): | ||
| 26 | """ | ||
| 27 | True if modifications exist for git repo representing abspath | ||
| 28 | False if no modifications exist | ||
| 29 | |||
| 30 | pygit2.GitError raised if abspath points to illegal repo | ||
| 31 | """ | ||
| 32 | repo = pygit2.Repository(abspath) | ||
| 33 | status = repo.status() | ||
| 34 | for gfile, state in status.items(): | ||
| 35 | if ( | ||
| 36 | state in (pygit2.GIT_STATUS_WT_MODIFIED, | ||
| 37 | pygit2.GIT_STATUS_WT_NEW, | ||
| 38 | pygit2.GIT_STATUS_INDEX_NEW, | ||
| 39 | pygit2.GIT_STATUS_INDEX_MODIFIED) | ||
| 40 | ): | ||
| 41 | return True | ||
| 42 | return False | ||
| 43 | |||
| 44 | |||
| 45 | def print_modification_status(abspath, status, args): | ||
| 46 | """ | ||
| 47 | Prints the modification status for the give abspath | ||
| 48 | """ | ||
| 49 | if status: | ||
| 50 | if args.verbose: | ||
| 51 | print(f'{abspath} -> modified') | ||
| 52 | else: | ||
| 53 | print(abspath) | ||
| 54 | return status | ||
| 55 | if args.verbose: | ||
| 56 | print(f'{abspath} -> unchanged') | ||
| 57 | return status | ||
| 58 | |||
| 59 | |||
| 60 | def fetch_git_repos(abspath): | ||
| 61 | """ | ||
| 62 | Returns a list of git repo paths under abspath | ||
| 63 | """ | ||
| 64 | repo_list = list() | ||
| 65 | for root, dirs, files in os.walk(abspath): | ||
| 66 | if '.git' in dirs: | ||
| 67 | try: | ||
| 68 | pygit2.Repository(root) | ||
| 69 | repo_list.append(root) | ||
| 70 | except pygit2.GitError: | ||
| 71 | continue | ||
| 72 | return repo_list | ||
| 73 | |||
| 74 | |||
| 11 | def fetch_git_files(args): | 75 | def fetch_git_files(args): |
| 12 | """ | 76 | """ |
| 13 | Fetches new and modified .py-files using pygit2 | 77 | Fetches new and modified .py-files using pygit2 |
| 14 | """ | 78 | """ |
| 15 | repo = pygit2.Repository(args.repo_path) | ||
| 16 | status = repo.status() | ||
| 17 | git_files_list = list() | 79 | git_files_list = list() |
| 80 | try: | ||
| 81 | repo = pygit2.Repository(args.repo_path) | ||
| 82 | status = repo.status() | ||
| 83 | except pygit2.GitError: | ||
| 84 | print('{repo_path} is not a valid git repository'.format( | ||
| 85 | repo_path=args.repo_path), | ||
| 86 | file=sys.stderr, | ||
| 87 | flush=True) | ||
| 88 | sys.exit(1) | ||
| 18 | if not status: | 89 | if not status: |
| 19 | return git_files_list | 90 | return git_files_list |
| 20 | for gfile, state in status.items(): | 91 | for gfile, state in status.items(): |
| @@ -25,6 +96,8 @@ def fetch_git_files(args): | |||
| 25 | pygit2.GIT_STATUS_INDEX_MODIFIED) and | 96 | pygit2.GIT_STATUS_INDEX_MODIFIED) and |
| 26 | gfile.lower().endswith('.py') | 97 | gfile.lower().endswith('.py') |
| 27 | ): | 98 | ): |
| 99 | if args.verbose: | ||
| 100 | print(gfile) | ||
| 28 | git_files_list.append(gfile) | 101 | git_files_list.append(gfile) |
| 29 | return git_files_list | 102 | return git_files_list |
| 30 | 103 | ||
| @@ -35,16 +108,51 @@ if __name__ == '__main__': | |||
| 35 | parser.add_argument( | 108 | parser.add_argument( |
| 36 | 'repo_path', | 109 | 'repo_path', |
| 37 | metavar='REPO', | 110 | metavar='REPO', |
| 38 | default='.', | 111 | default=os.getcwd(), |
| 39 | type=str, | 112 | type=str, |
| 40 | help='Git repo path (default: cwd)') | 113 | help='Git repo path (default: cwd)') |
| 41 | parser.add_argument( | 114 | parser.add_argument( |
| 42 | '--disable-pep8-checks', | 115 | '-m', '--modifications-only', |
| 116 | action='store_true', | ||
| 117 | dest='mod_only', | ||
| 118 | default=False, | ||
| 119 | help='Only check for git modifications') | ||
| 120 | parser.add_argument( | ||
| 121 | '-r', '--recursive', | ||
| 43 | action='store_true', | 122 | action='store_true', |
| 44 | dest='disable_pep8', | 123 | dest='recursive', |
| 45 | default=False, | 124 | default=False, |
| 46 | help='Disable pep8 checks') | 125 | help='Look for several repos recursively') |
| 126 | parser.add_argument( | ||
| 127 | '-v', '--verbose', | ||
| 128 | action='store_true', | ||
| 129 | dest='verbose', | ||
| 130 | default=False, | ||
| 131 | help='Verbosity') | ||
| 47 | args = parser.parse_args() | 132 | args = parser.parse_args() |
| 133 | abs_repo_path = os.path.abspath(os.path.expanduser(args.repo_path)) | ||
| 134 | if args.mod_only: | ||
| 135 | if not args.recursive: | ||
| 136 | if not is_git_repo(abs_repo_path): | ||
| 137 | print(f'{abs_repo_path} is not a valid git repository', | ||
| 138 | file=sys.stderr, | ||
| 139 | flush=True) | ||
| 140 | sys.exit(1) | ||
| 141 | print_modification_status(abs_repo_path, | ||
| 142 | modifications_exist(abs_repo_path), | ||
| 143 | args) | ||
| 144 | sys.exit(0) | ||
| 145 | # resursive | ||
| 146 | repo_list = fetch_git_repos(abs_repo_path) | ||
| 147 | if not repo_list: | ||
| 148 | print('No git-repos found') | ||
| 149 | sys.exit(0) | ||
| 150 | repo_list.sort() | ||
| 151 | for repo_path in repo_list: | ||
| 152 | print_modification_status(repo_path, | ||
| 153 | modifications_exist(repo_path), | ||
| 154 | args) | ||
| 155 | sys.exit(0) | ||
| 48 | git_files = fetch_git_files(args) | 156 | git_files = fetch_git_files(args) |
| 49 | if not git_files: | 157 | if not git_files: |
| 50 | print('No modifications') | 158 | print('No modifications') |
