Skip to content

Commit e2849e6

Browse files
committed
Implement and test filtering resources by libraries
1 parent e10adff commit e2849e6

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

tools/resources/__init__.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ def __init__(self, notify, collect_ignores=False):
126126
# publicly accessible things
127127
self.ignored_dirs = []
128128

129+
# library requirements
130+
self._libs_filtered = None
131+
129132
# Pre-mbed 2.0 ignore dirs
130133
self._legacy_ignore_dirs = (LEGACY_IGNORE_DIRS)
131134

@@ -257,7 +260,25 @@ def add_file_ref(self, file_type, file_name, file_path):
257260

258261
def get_file_refs(self, file_type):
259262
"""Return a list of FileRef for every file of the given type"""
260-
return list(self._file_refs[file_type])
263+
if self._libs_filtered is None:
264+
return list(self._file_refs[file_type])
265+
else:
266+
to_ret = []
267+
for ref in self._file_refs[file_type]:
268+
_, path = ref
269+
if not any(
270+
path.startswith(dirname(e.path)) for e in self._excluded_libs
271+
):
272+
to_ret.append(ref)
273+
return to_ret
274+
275+
def filter_by_libraries(self, library_refs):
276+
"""
277+
Call after completely done scanning to filter resources based on
278+
libraries
279+
"""
280+
self._libs_filtered = library_refs
281+
self._excluded_libs = set(self._file_refs[FileType.JSON]) - set(library_refs)
261282

262283
def _get_from_refs(self, file_type, key):
263284
return sorted([key(f) for f in self.get_file_refs(file_type)])

tools/test/resources/resource_test.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,31 @@ def test_detect_duplicates(self):
9999
assert(" not-main.o " in error_messages)
100100
assert(" main.o " in error_messages)
101101

102+
def test_filter_by_all_libraries(self):
103+
"""
104+
Assert something
105+
"""
106+
res = Resources(MockNotifier())
107+
res._add_labels('TARGET', ['K64F'])
108+
res._add_labels('TARGET', ['FRDM'])
109+
for name, loc in SRC_PATHS.items():
110+
res.add_directory(loc, into_path=name)
111+
res.filter_by_libraries(res.get_file_refs(FileType.JSON))
112+
assert("main.cpp" in res.get_file_names(FileType.CPP_SRC))
113+
114+
def test_filter_by_bm_lib(self):
115+
res = Resources(MockNotifier())
116+
res._add_labels('TARGET', ['K64F'])
117+
res._add_labels('TARGET', ['FRDM'])
118+
for name, loc in SRC_PATHS.items():
119+
res.add_directory(loc, into_path=name)
120+
res.win_to_unix()
121+
res.filter_by_libraries(
122+
ref for ref in res.get_file_refs(FileType.JSON)
123+
if "/bm/" in ref.name
124+
)
125+
assert("main.cpp" not in res.get_file_names(FileType.CPP_SRC))
126+
102127

103128
if __name__ == '__main__':
104129
unittest.main()

0 commit comments

Comments
 (0)