Skip to content

Commit 2b2f2a1

Browse files
Add excluding_files
Signed-off-by: Wonjae Park <[email protected]>
1 parent 0cced58 commit 2b2f2a1

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# Copyright (c) 2025 LG Electronics Inc.
4+
# SPDX-License-Identifier: Apache-2.0
5+
import os
6+
import fnmatch
7+
8+
def excluding_files(patterns: list[str], path_to_scan: str) -> list[str]:
9+
excluded_paths = set()
10+
11+
# Remove "/" or "/*" from the patterns
12+
normalized_patterns = []
13+
for pattern in patterns:
14+
if pattern.endswith("/") or pattern.endswith("/*"):
15+
normalized_patterns.append(pattern.rstrip("/*"))
16+
else:
17+
normalized_patterns.append(pattern)
18+
19+
for root, dirs, files in os.walk(path_to_scan):
20+
# Deal the matched dicrectories
21+
matched_dirs = {
22+
os.path.join(root, d) for d in dirs
23+
if any(
24+
fnmatch.fnmatch(d, pat.rstrip("/")) or
25+
fnmatch.fnmatch(os.path.join(root, d), os.path.join(path_to_scan, pat).replace("/", os.sep))
26+
for pat in normalized_patterns
27+
)
28+
}
29+
for d in matched_dirs:
30+
# Add the files in the matched directories
31+
for sub_root, _, sub_files in os.walk(d):
32+
for sub_file in sub_files:
33+
excluded_paths.add(os.path.relpath(os.path.join(sub_root, sub_file), path_to_scan))
34+
35+
# Match the files
36+
matched_files = {
37+
os.path.join(root, f) for f in files
38+
if any(fnmatch.fnmatch(os.path.join(root, f), os.path.join(path_to_scan, pat).replace("/", os.sep))
39+
for pat in normalized_patterns)
40+
}
41+
excluded_paths.update(os.path.relpath(file_path, path_to_scan) for file_path in matched_files)
42+
43+
# All files for future use
44+
# all_files = {
45+
# os.path.join(root, f) for root, _, files in os.walk(path_to_scan)
46+
# for f in files
47+
# }
48+
# return sorted(list(excluded_paths)), sorted(list(all_files - excluded_paths))
49+
return sorted(list(excluded_paths))
50+
51+
52+
53+
54+
55+
56+
#!/usr/bin/env python
57+
# -*- coding: utf-8 -*-
58+
# Copyright (c) 2025 LG Electronics Inc.
59+
# SPDX-License-Identifier: Apache-2.0
60+
# import os
61+
# import fnmatch
62+
63+
# def excluding_files(patterns: list[str], path_to_scan: str) -> list[str]:
64+
# excluded_paths = set()
65+
66+
# # Remove "/" or "/*" from the patterns
67+
# normalized_patterns = []
68+
# for pattern in patterns:
69+
# if pattern.endswith("/") or pattern.endswith("/*"):
70+
# normalized_patterns.append(pattern.rstrip("/*"))
71+
# else:
72+
# normalized_patterns.append(pattern)
73+
74+
# for root, dirs, files in os.walk(path_to_scan):
75+
# # Deal the matched directories
76+
# matched_dirs = {
77+
# os.path.join(root, d) for d in dirs
78+
# if any(
79+
# fnmatch.fnmatch(d, pat.rstrip("/")) or
80+
# fnmatch.fnmatch(os.path.join(root, d), os.path.join(path_to_scan, pat).replace("/", os.sep))
81+
# for pat in normalized_patterns
82+
# )
83+
# }
84+
# for d in matched_dirs:
85+
# # Add the files in the matched directories
86+
# for sub_root, _, sub_files in os.walk(d):
87+
# for sub_file in sub_files:
88+
# # 수정된 부분: 상대 경로로 변환
89+
# excluded_paths.add(os.path.relpath(os.path.join(sub_root, sub_file), path_to_scan))
90+
91+
# # Match the files
92+
# matched_files = {
93+
# os.path.join(root, f) for f in files
94+
# if any(fnmatch.fnmatch(os.path.join(root, f), os.path.join(path_to_scan, pat).replace("/", os.sep))
95+
# for pat in normalized_patterns)
96+
# }
97+
# # 수정된 부분: matched_files를 상대 경로로 변환하여 추가
98+
# excluded_paths.update(os.path.relpath(file_path, path_to_scan) for file_path in matched_files)
99+
100+
# # Return sorted excluded paths
101+
# return sorted(list(excluded_paths))

0 commit comments

Comments
 (0)