|
27 | 27 |
|
28 | 28 | import argparse |
29 | 29 | import glob |
| 30 | +import fnmatch |
30 | 31 | import itertools |
31 | 32 | import os |
32 | 33 | import re |
|
75 | 76 | PY_EXTS = (".py",) |
76 | 77 |
|
77 | 78 |
|
78 | | -def list_files(paths, exclusions=None, prefix=""): |
| 79 | +def list_files(args, paths, exclusions=None, prefix=""): |
79 | 80 | files = set() |
| 81 | + args = [os.path.join(prefix, arg) for arg in args] |
80 | 82 | for pattern in paths: |
81 | | - files.update(glob.glob(os.path.join(prefix, pattern), recursive=True)) |
| 83 | + files.update(fnmatch.filter(args, os.path.join(prefix, pattern))) |
82 | 84 | for pattern in exclusions or []: |
83 | | - files.difference_update(glob.fnmatch.filter(files, os.path.join(prefix, pattern))) |
| 85 | + files.difference_update(fnmatch.filter(files, os.path.join(prefix, pattern))) |
84 | 86 | return sorted(files) |
85 | 87 |
|
86 | 88 |
|
@@ -128,23 +130,19 @@ def fixup_c(filename): |
128 | 130 |
|
129 | 131 |
|
130 | 132 | def main(): |
131 | | - cmd_parser = argparse.ArgumentParser(description="Auto-format C and Python files.") |
| 133 | + cmd_parser = argparse.ArgumentParser(description="Auto-format C and Python files -- to be used via pre-commit only.") |
132 | 134 | cmd_parser.add_argument("-c", action="store_true", help="Format C code only") |
133 | 135 | cmd_parser.add_argument("-p", action="store_true", help="Format Python code only") |
134 | 136 | cmd_parser.add_argument("-v", action="store_true", help="Enable verbose output") |
135 | | - cmd_parser.add_argument("files", nargs="*", help="Run on specific globs") |
| 137 | + cmd_parser.add_argument("files", nargs="+", help="Run on specific globs") |
136 | 138 | args = cmd_parser.parse_args() |
137 | 139 |
|
138 | 140 | # Setting only one of -c or -p disables the other. If both or neither are set, then do both. |
139 | 141 | format_c = args.c or not args.p |
140 | 142 | format_py = args.p or not args.c |
141 | 143 |
|
142 | | - # Expand the globs passed on the command line, or use the default globs above. |
143 | | - files = [] |
144 | | - if args.files: |
145 | | - files = list_files(args.files, EXCLUSIONS) |
146 | | - else: |
147 | | - files = list_files(PATHS, EXCLUSIONS, TOP) |
| 144 | + # Expand the arguments passed on the command line, subject to the PATHS and EXCLUSIONS |
| 145 | + files = list_files(args.files, PATHS, EXCLUSIONS, TOP) |
148 | 146 |
|
149 | 147 | # Extract files matching a specific language. |
150 | 148 | def lang_files(exts): |
|
0 commit comments