Skip to content

Commit 82f195c

Browse files
authored
Merge pull request #8696 from theotherjimmy/handle-parent-renames
Resources: Avoid assuming that deps have a sane name
2 parents e237194 + 3db1a8a commit 82f195c

File tree

1 file changed

+27
-37
lines changed

1 file changed

+27
-37
lines changed

tools/resources/__init__.py

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -250,39 +250,17 @@ def _not_current_label(self, dirname, label_type):
250250
dirname[len(label_type) + 1:] not in self._labels[label_type])
251251

252252
def add_file_ref(self, file_type, file_name, file_path):
253-
if sep != self._sep:
254-
ref = FileRef(file_name.replace(sep, self._sep), file_path)
255-
else:
256-
ref = FileRef(file_name, file_path)
257-
self._file_refs[file_type].add(ref)
253+
if file_type:
254+
if sep != self._sep:
255+
file_name = file_name.replace(sep, self._sep)
256+
self._file_refs[file_type].add(FileRef(file_name, file_path))
258257

259258
def get_file_refs(self, file_type):
260259
"""Return a list of FileRef for every file of the given type"""
261260
return list(self._file_refs[file_type])
262261

263-
def _all_parents(self, files):
264-
for name, path in files:
265-
components = name.split(self._sep)
266-
start_at = 0
267-
for index, directory in reversed(list(enumerate(components))):
268-
if directory in self._prefixed_labels:
269-
start_at = index + 1
270-
break
271-
prefix = path.replace(name, "")
272-
for n in range(start_at, len(components)):
273-
parent_name = self._sep.join(components[:n])
274-
parent_path = join(prefix, *components[:n])
275-
yield FileRef(parent_name, parent_path)
276-
277262
def _get_from_refs(self, file_type, key):
278-
if file_type is FileType.INC_DIR:
279-
parents = set(self._all_parents(self._file_refs[FileType.HEADER]))
280-
else:
281-
parents = set()
282-
return sorted(
283-
[key(f) for f in list(parents) + self.get_file_refs(file_type)]
284-
)
285-
263+
return sorted([key(f) for f in self.get_file_refs(file_type)])
286264

287265
def get_file_names(self, file_type):
288266
return self._get_from_refs(file_type, lambda f: f.name)
@@ -447,6 +425,19 @@ def add_directory(
447425
".ar": FileType.LIB_DIR,
448426
}
449427

428+
def _all_parents(self, file_path, base_path, into_path):
429+
suffix = relpath(file_path, base_path)
430+
components = suffix.split(self._sep)
431+
start_at = 0
432+
for index, directory in reversed(list(enumerate(components))):
433+
if directory in self._prefixed_labels:
434+
start_at = index + 1
435+
break
436+
for n in range(start_at, len(components)):
437+
parent_name = self._sep.join([into_path] + components[:n])
438+
parent_path = join(base_path, *components[:n])
439+
yield FileRef(parent_name, parent_path)
440+
450441
def _add_file(self, file_path, base_path, into_path):
451442
""" Add a single file into the resources object that was found by
452443
scanning starting as base_path
@@ -459,16 +450,15 @@ def _add_file(self, file_path, base_path, into_path):
459450

460451
fake_path = join(into_path, relpath(file_path, base_path))
461452
_, ext = splitext(file_path)
462-
try:
463-
file_type = self._EXT[ext.lower()]
464-
self.add_file_ref(file_type, fake_path, file_path)
465-
except KeyError:
466-
pass
467-
try:
468-
dir_type = self._DIR_EXT[ext.lower()]
469-
self.add_file_ref(dir_type, dirname(fake_path), dirname(file_path))
470-
except KeyError:
471-
pass
453+
454+
file_type = self._EXT.get(ext.lower())
455+
self.add_file_ref(file_type, fake_path, file_path)
456+
if file_type == FileType.HEADER:
457+
for name, path in self._all_parents(file_path, base_path, into_path):
458+
self.add_file_ref(FileType.INC_DIR, name, path)
459+
460+
dir_type = self._DIR_EXT.get(ext.lower())
461+
self.add_file_ref(dir_type, dirname(fake_path), dirname(file_path))
472462

473463

474464
def scan_with_toolchain(self, src_paths, toolchain, dependencies_paths=None,

0 commit comments

Comments
 (0)