Skip to content

Commit ed61e89

Browse files
committed
codeformat: require filenames, filter them by include/exclude lists
1 parent d0e3848 commit ed61e89

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

tools/codeformat.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import argparse
2929
import glob
30+
import fnmatch
3031
import itertools
3132
import os
3233
import re
@@ -75,12 +76,13 @@
7576
PY_EXTS = (".py",)
7677

7778

78-
def list_files(paths, exclusions=None, prefix=""):
79+
def list_files(args, paths, exclusions=None, prefix=""):
7980
files = set()
81+
args = [os.path.join(prefix, arg) for arg in args]
8082
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)))
8284
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)))
8486
return sorted(files)
8587

8688

@@ -128,23 +130,19 @@ def fixup_c(filename):
128130

129131

130132
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.")
132134
cmd_parser.add_argument("-c", action="store_true", help="Format C code only")
133135
cmd_parser.add_argument("-p", action="store_true", help="Format Python code only")
134136
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")
136138
args = cmd_parser.parse_args()
137139

138140
# Setting only one of -c or -p disables the other. If both or neither are set, then do both.
139141
format_c = args.c or not args.p
140142
format_py = args.p or not args.c
141143

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)
148146

149147
# Extract files matching a specific language.
150148
def lang_files(exts):

0 commit comments

Comments
 (0)