Skip to content

Commit

Permalink
tools/utils: Add function to filter files from the project
Browse files Browse the repository at this point in the history
A new function is introduced called `get_filtered_files` to
recursively find and filter files in the current working directory
and its subdirectories. It allows to simplify the check scripts.

Signed-off-by: Leandro Belli <[email protected]>
Change-Id: I10707e4f2cbc53b32194514a543e2d244e18356c
  • Loading branch information
leandro-arm committed Feb 2, 2024
1 parent c1ca431 commit df64bd3
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
# SPDX-License-Identifier: BSD-3-Clause

import subprocess
import fnmatch
import os
from typing import List, Tuple


Expand Down Expand Up @@ -81,3 +83,64 @@ def __str__(self) -> str:
)

return out_str


def get_excluded_directories(exclude_dir_patterns):
"""
Converts a list of directory names or patterns into a list of their
absolute paths, intended to be used as an exclusion list.
Parameters:
exclude_dir_patterns (list): A list of directory names or patterns to
exclude. These can be relative or absolute paths.
Returns:
list: A list of absolute paths corresponding to the input directory names
or patterns.
"""

filtered_dirs = list()
for dir_pattern in exclude_dir_patterns:
filtered_dirs.append(os.path.abspath(dir_pattern))
print(f'\tAdding to the exclude list: {filtered_dirs[-1]}')

return filtered_dirs


def get_filtered_files(exclude_dir_patterns, file_types):
"""
Recursively find and filter files in the current working directory and its
subdirectories.
Parameters:
exclude_dir_patterns (list): A list of directory paths patterns to exclude
from the search.
file_types (list): A list of file patterns to include in the result.
Returns:
list: A list of absolute paths to filtered files.
"""

filtered_files = list()
exclude_directories = get_excluded_directories(exclude_dir_patterns)
cwd = os.getcwd()
for root, dirs, files in os.walk(cwd, topdown=True):
#
# Exclude directories based on the EXCLUDE_DIRECTORIES pattern list
#
dirs[:] = list(filter(lambda d: os.path.join(root, d) not in
exclude_directories, dirs))

#
# List files based on the FILE_TYPES pattern list
#
files = list(filter(lambda f: any(fnmatch.fnmatch(f, pattern)
for pattern in file_types), files))

#
# Append all filtered files with the path
#
filtered_files.extend(list(map(lambda f: os.path.join(root, f),
files)))

return filtered_files

0 comments on commit df64bd3

Please sign in to comment.