Skip to content

Commit 5e30717

Browse files
committed
BLD: allow multiple path arguments in validate_unwanted_patterns.py
1 parent c1c4bcf commit 5e30717

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

scripts/validate_unwanted_patterns.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import sys
1717
import token
1818
import tokenize
19-
from typing import IO, Callable, FrozenSet, Iterable, List, Set, Tuple
19+
from typing import IO, Callable, FrozenSet, Iterable, List, Sequence, Set, Tuple
2020

2121
PRIVATE_IMPORTS_TO_IGNORE: Set[str] = {
2222
"_extension_array_shared_docs",
@@ -403,7 +403,7 @@ def has_wrong_whitespace(first_line: str, second_line: str) -> bool:
403403

404404
def main(
405405
function: Callable[[IO[str]], Iterable[Tuple[int, str]]],
406-
source_path: str,
406+
source_paths: Sequence[str],
407407
output_format: str,
408408
file_extensions_to_check: str,
409409
excluded_file_paths: str,
@@ -415,8 +415,8 @@ def main(
415415
----------
416416
function : Callable
417417
Function to execute for the specified validation type.
418-
source_path : str
419-
Source file_path representing file_path to a file/directory.
418+
source_paths : list of str
419+
File paths of files and directories to check.
420420
output_format : str
421421
Output format of the error message.
422422
file_extensions_to_check : str
@@ -434,7 +434,7 @@ def main(
434434
ValueError
435435
If the `source_path` is not pointing to existing file/directory.
436436
"""
437-
if not os.path.exists(source_path):
437+
if not all(os.path.exists(path) for path in source_paths):
438438
raise ValueError("Please enter a valid path, pointing to a file/directory.")
439439

440440
FILE_EXTENSIONS_TO_CHECK: FrozenSet[str] = frozenset(
@@ -463,21 +463,22 @@ def is_ignored(path: str):
463463
path = os.path.abspath(os.path.normpath(path))
464464
return any(path.startswith(ignored_path) for ignored_path in PATHS_TO_IGNORE)
465465

466-
if os.path.isfile(source_path):
467-
check_file(source_path)
468-
else:
469-
for subdir, _, files in os.walk(source_path):
470-
if is_ignored(subdir):
471-
continue
472-
for file_name in files:
473-
file_path = os.path.join(subdir, file_name)
474-
if is_ignored(file_path) or not any(
475-
file_name.endswith(extension)
476-
for extension in FILE_EXTENSIONS_TO_CHECK
477-
):
466+
for source_path in source_paths:
467+
if os.path.isfile(source_path):
468+
check_file(source_path)
469+
else:
470+
for subdir, _, files in os.walk(source_path):
471+
if is_ignored(subdir):
478472
continue
473+
for file_name in files:
474+
file_path = os.path.join(subdir, file_name)
475+
if is_ignored(file_path) or not any(
476+
file_name.endswith(extension)
477+
for extension in FILE_EXTENSIONS_TO_CHECK
478+
):
479+
continue
479480

480-
check_file(file_path)
481+
check_file(file_path)
481482

482483
return is_failed
483484

@@ -494,7 +495,13 @@ def is_ignored(path: str):
494495
parser = argparse.ArgumentParser(description="Unwanted patterns checker.")
495496

496497
parser.add_argument(
497-
"path", nargs="?", default=".", help="Source path of file/directory to check."
498+
"paths",
499+
nargs="*",
500+
default=["."],
501+
help=(
502+
"Source path(s) of files and directories to check. If a directory is "
503+
"specified, all its contents are checked recursively."
504+
),
498505
)
499506
parser.add_argument(
500507
"--format",
@@ -525,7 +532,7 @@ def is_ignored(path: str):
525532
sys.exit(
526533
main(
527534
function=globals().get(args.validation_type), # type: ignore
528-
source_path=args.path,
535+
source_paths=args.paths,
529536
output_format=args.format,
530537
file_extensions_to_check=args.included_file_extensions,
531538
excluded_file_paths=args.excluded_file_paths,

0 commit comments

Comments
 (0)