diff --git a/scripts/validate_unwanted_patterns.py b/scripts/validate_unwanted_patterns.py index 193fef026a96b..16bf9a677641a 100755 --- a/scripts/validate_unwanted_patterns.py +++ b/scripts/validate_unwanted_patterns.py @@ -18,8 +18,6 @@ import tokenize from typing import IO, Callable, FrozenSet, Iterable, List, Tuple -PATHS_TO_IGNORE: Tuple[str, ...] = ("asv_bench/env",) - def _get_literal_string_prefix_len(token_string: str) -> int: """ @@ -293,6 +291,7 @@ def main( source_path: str, output_format: str, file_extensions_to_check: str, + excluded_file_paths: str, ) -> bool: """ Main entry point of the script. @@ -305,6 +304,10 @@ def main( Source path representing path to a file/directory. output_format : str Output format of the error message. + file_extensions_to_check: str + Coma seperated values of what file extensions to check. + excluded_file_paths: str + Coma seperated values of what file paths to exclude during the check. Returns ------- @@ -322,13 +325,9 @@ def main( is_failed: bool = False file_path: str = "" - FILE_EXTENSIONS_TO_CHECK: FrozenSet[str] = frozenset( - file_extensions_to_check.split(",") - ) - if os.path.isfile(source_path): file_path = source_path - with open(file_path, "r") as file_obj: + with open(file_path) as file_obj: for line_number, msg in function(file_obj): is_failed = True print( @@ -337,9 +336,15 @@ def main( ) ) + FILE_EXTENSIONS_TO_CHECK = frozenset( + file_extensions_to_check.split(",") + ) + PATHS_TO_IGNORE = frozenset(excluded_file_paths.split(",")) + for subdir, _, files in os.walk(source_path): if any(path in subdir for path in PATHS_TO_IGNORE): continue + for file_name in files: if not any( file_name.endswith(extension) for extension in FILE_EXTENSIONS_TO_CHECK @@ -347,7 +352,7 @@ def main( continue file_path = os.path.join(subdir, file_name) - with open(file_path, "r") as file_obj: + with open(file_path) as file_obj: for line_number, msg in function(file_obj): is_failed = True print( @@ -387,7 +392,12 @@ def main( parser.add_argument( "--included-file-extensions", default="py,pyx,pxd,pxi", - help="Coma seperated file extensions to check.", + help="Comma separated file extensions to check.", + ) + parser.add_argument( + "--excluded-file-paths", + default="asv_bench/env", + help="Comma separated file extensions to check.", ) args = parser.parse_args() @@ -398,5 +408,6 @@ def main( source_path=args.path, output_format=args.format, file_extensions_to_check=args.included_file_extensions, + excluded_file_paths=args.excluded_file_paths, ) )