From 6d8eb035c8fe2ce00b43230830feac6dd82b8d7b Mon Sep 17 00:00:00 2001 From: Wonjae Park Date: Fri, 10 Jan 2025 16:05:11 +0900 Subject: [PATCH 1/3] Add excluding_files Signed-off-by: Wonjae Park --- src/fosslight_util/exclude.py | 60 +++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/fosslight_util/exclude.py diff --git a/src/fosslight_util/exclude.py b/src/fosslight_util/exclude.py new file mode 100644 index 0000000..3335ec3 --- /dev/null +++ b/src/fosslight_util/exclude.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +import os +import fnmatch + +def excluding_files(patterns: list[str], path_to_scan: str) -> list[str]: + excluded_paths = set() + + # Normalize patterns: e.g., 'sample/', 'sample/*' -> 'sample' + # Replace backslash with slash + normalized_patterns = [] + for pattern in patterns: + if pattern.endswith('/') or pattern.endswith('/*'): + pattern = pattern.rstrip('/*') + pattern = pattern.replace('\\', '/') + normalized_patterns.append(pattern) + + # Traverse directories + for root, dirs, files in os.walk(path_to_scan): + remove_dir_list = [] + + # (1) Directory matching + for d in dirs: + dir_name = d + dir_path = os.path.relpath(os.path.join(root, d), path_to_scan).replace('\\', '/') + matched = False + + for pat in normalized_patterns: + # Match directory name + if fnmatch.fnmatch(dir_name, pat): + matched = True + + # Match the full relative path + if not matched: + if fnmatch.fnmatch(dir_path, pat) or fnmatch.fnmatch(dir_path, pat + "/*"): + matched = True + + # If matched, exclude all files under this directory and stop checking patterns + if matched: + sub_root_path = os.path.join(root, d) + for sr, _, sf in os.walk(sub_root_path): + for sub_file in sf: + sub_file_path = os.path.relpath(os.path.join(sr, sub_file), path_to_scan) + excluded_paths.add(sub_file_path.replace('\\', '/')) + remove_dir_list.append(d) + break + + # (1-2) Prune matched directories from further traversal + for rd in remove_dir_list: + dirs.remove(rd) + + # (2) File matching + for f in files: + file_path = os.path.relpath(os.path.join(root, f), path_to_scan).replace('\\', '/') + for pat in normalized_patterns: + if fnmatch.fnmatch(file_path, pat) or fnmatch.fnmatch(file_path, pat + "/*"): + excluded_paths.add(file_path) + break + + return sorted(excluded_paths) From 1c005e1e5cbb02e09502d91210c4d0f423e30c1a Mon Sep 17 00:00:00 2001 From: Wonjae Park Date: Tue, 21 Jan 2025 19:03:22 +0900 Subject: [PATCH 2/3] add copyright Signed-off-by: Wonjae Park --- src/fosslight_util/exclude.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/fosslight_util/exclude.py b/src/fosslight_util/exclude.py index 3335ec3..75a9fbe 100644 --- a/src/fosslight_util/exclude.py +++ b/src/fosslight_util/exclude.py @@ -1,5 +1,8 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- +# Copyright (c) 2025 LG Electronics Inc. +# SPDX-License-Identifier: Apache-2.0 + import os import fnmatch From 3bd07b3dc36e2523dc050565fdae6298bb7155c0 Mon Sep 17 00:00:00 2001 From: Wonjae Park Date: Wed, 22 Jan 2025 14:48:32 +0900 Subject: [PATCH 3/3] fix bug of exlcude option on Windows Signed-off-by: Wonjae Park --- src/fosslight_util/exclude.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fosslight_util/exclude.py b/src/fosslight_util/exclude.py index 75a9fbe..afc05d8 100644 --- a/src/fosslight_util/exclude.py +++ b/src/fosslight_util/exclude.py @@ -13,9 +13,9 @@ def excluding_files(patterns: list[str], path_to_scan: str) -> list[str]: # Replace backslash with slash normalized_patterns = [] for pattern in patterns: + pattern = pattern.replace('\\', '/') if pattern.endswith('/') or pattern.endswith('/*'): pattern = pattern.rstrip('/*') - pattern = pattern.replace('\\', '/') normalized_patterns.append(pattern) # Traverse directories