summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimeon Simeonov2023-07-04 19:07:29 +0200
committerSimeon Simeonov2023-07-04 19:07:29 +0200
commit472806c736d3ff1ae7f9d63aea4f8539e4846845 (patch)
tree089f039df023bf722107c6b0e8f9f5266b6c6774
parent526bc3aca201dee45e3b74d0873f05d7321e9583 (diff)
Add --skip / -s and format using black -S
-rwxr-xr-xpygitchecker.py107
1 files 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):
75 """Fetches new and modified .py-files using git""" 75 """Fetches new and modified .py-files using git"""
76 try: 76 try:
77 repo = git.Repo(args.repo_path) 77 repo = git.Repo(args.repo_path)
78 modified_files = {mfile.a_path for mfile in repo.index.diff(None) 78 modified_files = {
79 if mfile.a_path.lower().endswith('.py')} 79 mfile.a_path
80 staged_files = {sfile.a_path for sfile in repo.index.diff('HEAD') 80 for mfile in repo.index.diff(None)
81 if sfile.a_path.lower().endswith('.py')} 81 if mfile.a_path.lower().endswith('.py')
82 }
83 staged_files = {
84 sfile.a_path
85 for sfile in repo.index.diff('HEAD')
86 if sfile.a_path.lower().endswith('.py')
87 }
82 untracked_files = set( 88 untracked_files = set(
83 filter(lambda f: f.lower().endswith('.py'), repo.untracked_files)) 89 filter(lambda f: f.lower().endswith('.py'), repo.untracked_files)
90 )
84 return list(modified_files | staged_files | untracked_files) 91 return list(modified_files | staged_files | untracked_files)
85 except git.InvalidGitRepositoryError: 92 except git.InvalidGitRepositoryError:
86 eprint(f'{args.repo_path} is not a valid git repository') 93 eprint(f'{args.repo_path} is not a valid git repository')
@@ -113,7 +120,14 @@ def validate_python_file(pfile, tests, args):
113 :return: False if validation fails, False otherwise 120 :return: False if validation fails, False otherwise
114 :rtype: bool 121 :rtype: bool
115 """ 122 """
123 skipped = []
124 if args.skip:
125 skipped = args.skip.split(',')
116 for test in tests: 126 for test in tests:
127 if test['name'] in skipped:
128 if args.verbose:
129 print(f'{pfile} <- {test["name"]} (skipping)')
130 continue
117 if args.verbose: 131 if args.verbose:
118 print(f'{pfile} <- {test["name"]}') 132 print(f'{pfile} <- {test["name"]}')
119 try: 133 try:
@@ -129,65 +143,94 @@ def validate_python_file(pfile, tests, args):
129def main(inargs=None): 143def main(inargs=None):
130 """Main entry point""" 144 """Main entry point"""
131 parser = argparse.ArgumentParser( 145 parser = argparse.ArgumentParser(
132 description='The following options are available') 146 description='The following options are available'
147 )
133 parser.add_argument( 148 parser.add_argument(
134 'repo_path', 149 'repo_path',
135 metavar='REPO', 150 metavar='REPO',
136 nargs='?', 151 nargs='?',
137 default=os.getcwd(), 152 default=os.getcwd(),
138 help='Git repo path (default: cwd)') 153 help='Git repo path (default: cwd)',
154 )
139 parser.add_argument( 155 parser.add_argument(
140 '-a', '--all-files', 156 '-a',
157 '--all-files',
141 action='store_true', 158 action='store_true',
142 dest='all_files', 159 dest='all_files',
143 help='Check all files, not only the modified ones') 160 help='Check all files, not only the modified ones',
161 )
144 parser.add_argument( 162 parser.add_argument(
145 '-C', '--continue-checks', 163 '-C',
164 '--continue-checks',
146 action='store_true', 165 action='store_true',
147 dest='continue_checks', 166 dest='continue_checks',
148 help='Continue with the checks if validation fails') 167 help='Continue with the checks if validation fails',
168 )
149 parser.add_argument( 169 parser.add_argument(
150 '-c', '--config', 170 '-c',
171 '--config',
151 metavar='<file>', 172 metavar='<file>',
152 type=str, 173 type=str,
153 default=os.path.expanduser('~/.pygitchecker.json'), 174 default=os.path.expanduser('~/.pygitchecker.json'),
154 dest='config_file', 175 dest='config_file',
155 help='Config file (default: ~/.pygitchecker.json)') 176 help='Config file (default: ~/.pygitchecker.json)',
177 )
156 parser.add_argument( 178 parser.add_argument(
157 '-F', '--file', 179 '-F',
180 '--file',
158 metavar='<file>', 181 metavar='<file>',
159 type=str, 182 type=str,
160 default='', 183 default='',
161 dest='python_file', 184 dest='python_file',
162 help='Validate a particular .py file') 185 help='Validate a particular .py file',
186 )
163 parser.add_argument( 187 parser.add_argument(
164 '-f', '--full', 188 '-f',
189 '--full',
165 action='store_true', 190 action='store_true',
166 dest='full_search', 191 dest='full_search',
167 help='Consider repos that contain untracked files (used with -m)') 192 help='Consider repos that contain untracked files (used with -m)',
193 )
168 parser.add_argument( 194 parser.add_argument(
169 '-l', '--list-only', 195 '-l',
196 '--list-only',
170 action='store_true', 197 action='store_true',
171 dest='list_only', 198 dest='list_only',
172 help='Only lists the target files without performing any checks') 199 help='Only lists the target files without performing any checks',
200 )
173 parser.add_argument( 201 parser.add_argument(
174 '-m', '--modifications-only', 202 '-m',
203 '--modifications-only',
175 action='store_true', 204 action='store_true',
176 dest='mod_only', 205 dest='mod_only',
177 help='Only check for git modifications') 206 help='Only check for git modifications',
207 )
178 parser.add_argument( 208 parser.add_argument(
179 '-r', '--recursive', 209 '-r',
210 '--recursive',
180 action='store_true', 211 action='store_true',
181 dest='recursive', 212 dest='recursive',
182 help='Look for several repos recursively') 213 help='Look for several repos recursively',
214 )
215 parser.add_argument(
216 '-s',
217 '--skip',
218 metavar='<test1[,test2,test3...]>',
219 type=str,
220 default='',
221 dest='skip',
222 help='Skip one or more tests',
223 )
183 parser.add_argument( 224 parser.add_argument(
184 '-v', '--verbose', 225 '-v',
226 '--verbose',
185 action='store_true', 227 action='store_true',
186 dest='verbose', 228 dest='verbose',
187 help='Verbosity') 229 help='Verbosity',
230 )
188 args = parser.parse_args(inargs) 231 args = parser.parse_args(inargs)
189 try: 232 try:
190 with io.open(args.config_file, 'r') as fp: 233 with io.open(args.config_file, 'r', encoding='utf-8') as fp:
191 config_dict = json.load(fp) 234 config_dict = json.load(fp)
192 except Exception as e: 235 except Exception as e:
193 eprint(f'Unable to parse {args.config_file}: {e}') 236 eprint(f'Unable to parse {args.config_file}: {e}')
@@ -201,9 +244,9 @@ def main(inargs=None):
201 if not is_git_repo(abs_repo_path): 244 if not is_git_repo(abs_repo_path):
202 eprint(f'{abs_repo_path} is not a valid git repository') 245 eprint(f'{abs_repo_path} is not a valid git repository')
203 sys.exit(1) 246 sys.exit(1)
204 print_modification_status(abs_repo_path, 247 print_modification_status(
205 modifications_exist(abs_repo_path), 248 abs_repo_path, modifications_exist(abs_repo_path), args
206 args) 249 )
207 sys.exit(0) 250 sys.exit(0)
208 # resursive 251 # resursive
209 repo_list = fetch_git_repos(abs_repo_path) 252 repo_list = fetch_git_repos(abs_repo_path)
@@ -212,9 +255,9 @@ def main(inargs=None):
212 sys.exit(0) 255 sys.exit(0)
213 repo_list.sort() 256 repo_list.sort()
214 for repo_path in repo_list: 257 for repo_path in repo_list:
215 print_modification_status(repo_path, 258 print_modification_status(
216 modifications_exist(repo_path), 259 repo_path, modifications_exist(repo_path), args
217 args) 260 )
218 sys.exit(0) 261 sys.exit(0)
219 if args.all_files: 262 if args.all_files:
220 selected_files = fetch_all_files(abs_repo_path) 263 selected_files = fetch_all_files(abs_repo_path)