diff --git a/tools/resources/__init__.py b/tools/resources/__init__.py index 5d3d6446a33..17ecc11dc9e 100644 --- a/tools/resources/__init__.py +++ b/tools/resources/__init__.py @@ -250,39 +250,17 @@ def _not_current_label(self, dirname, label_type): dirname[len(label_type) + 1:] not in self._labels[label_type]) def add_file_ref(self, file_type, file_name, file_path): - if sep != self._sep: - ref = FileRef(file_name.replace(sep, self._sep), file_path) - else: - ref = FileRef(file_name, file_path) - self._file_refs[file_type].add(ref) + if file_type: + if sep != self._sep: + file_name = file_name.replace(sep, self._sep) + self._file_refs[file_type].add(FileRef(file_name, file_path)) def get_file_refs(self, file_type): """Return a list of FileRef for every file of the given type""" return list(self._file_refs[file_type]) - def _all_parents(self, files): - for name, path in files: - components = name.split(self._sep) - start_at = 0 - for index, directory in reversed(list(enumerate(components))): - if directory in self._prefixed_labels: - start_at = index + 1 - break - prefix = path.replace(name, "") - for n in range(start_at, len(components)): - parent_name = self._sep.join(components[:n]) - parent_path = join(prefix, *components[:n]) - yield FileRef(parent_name, parent_path) - def _get_from_refs(self, file_type, key): - if file_type is FileType.INC_DIR: - parents = set(self._all_parents(self._file_refs[FileType.HEADER])) - else: - parents = set() - return sorted( - [key(f) for f in list(parents) + self.get_file_refs(file_type)] - ) - + return sorted([key(f) for f in self.get_file_refs(file_type)]) def get_file_names(self, file_type): return self._get_from_refs(file_type, lambda f: f.name) @@ -447,6 +425,19 @@ def add_directory( ".ar": FileType.LIB_DIR, } + def _all_parents(self, file_path, base_path, into_path): + suffix = relpath(file_path, base_path) + components = suffix.split(self._sep) + start_at = 0 + for index, directory in reversed(list(enumerate(components))): + if directory in self._prefixed_labels: + start_at = index + 1 + break + for n in range(start_at, len(components)): + parent_name = self._sep.join([into_path] + components[:n]) + parent_path = join(base_path, *components[:n]) + yield FileRef(parent_name, parent_path) + def _add_file(self, file_path, base_path, into_path): """ Add a single file into the resources object that was found by scanning starting as base_path @@ -459,16 +450,15 @@ def _add_file(self, file_path, base_path, into_path): fake_path = join(into_path, relpath(file_path, base_path)) _, ext = splitext(file_path) - try: - file_type = self._EXT[ext.lower()] - self.add_file_ref(file_type, fake_path, file_path) - except KeyError: - pass - try: - dir_type = self._DIR_EXT[ext.lower()] - self.add_file_ref(dir_type, dirname(fake_path), dirname(file_path)) - except KeyError: - pass + + file_type = self._EXT.get(ext.lower()) + self.add_file_ref(file_type, fake_path, file_path) + if file_type == FileType.HEADER: + for name, path in self._all_parents(file_path, base_path, into_path): + self.add_file_ref(FileType.INC_DIR, name, path) + + dir_type = self._DIR_EXT.get(ext.lower()) + self.add_file_ref(dir_type, dirname(fake_path), dirname(file_path)) def scan_with_toolchain(self, src_paths, toolchain, dependencies_paths=None,