Skip to content

Commit 0ea544f

Browse files
authored
Merge pull request #3560 from theotherjimmy/refactor-ignores
Refactor scan resources to account for base_paths
2 parents addd2b2 + c14a5b9 commit 0ea544f

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

tools/toolchains/__init__.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -507,11 +507,26 @@ def need_update(self, target, dependencies):
507507
return False
508508

509509
def is_ignored(self, file_path):
510+
"""Check if file path is ignored by any .mbedignore thus far"""
510511
for pattern in self.ignore_patterns:
511512
if fnmatch.fnmatch(file_path, pattern):
512513
return True
513514
return False
514515

516+
def add_ignore_patterns(self, root, base_path, patterns):
517+
"""Add a series of patterns to the ignored paths
518+
519+
Positional arguments:
520+
root - the directory containing the ignore file
521+
base_path - the location that the scan started from
522+
patterns - the list of patterns we will ignore in the future
523+
"""
524+
real_base = relpath(root, base_path)
525+
if real_base == ".":
526+
self.ignore_patterns.extend(patterns)
527+
else:
528+
self.ignore_patterns.extend(join(real_base, pat) for pat in patterns)
529+
515530
# Create a Resources object from the path pointed to by *path* by either traversing a
516531
# a directory structure, when *path* is a directory, or adding *path* to the resources,
517532
# when *path* is a file.
@@ -559,10 +574,11 @@ def _add_dir(self, path, resources, base_path, exclude_paths=None):
559574
lines = [l for l in lines if l != ""] # Strip empty lines
560575
lines = [l for l in lines if not re.match("^#",l)] # Strip comment lines
561576
# Append root path to glob patterns and append patterns to ignore_patterns
562-
self.ignore_patterns.extend([join(root,line.strip()) for line in lines])
577+
self.add_ignore_patterns(root, base_path, lines)
563578

564579
# Skip the whole folder if ignored, e.g. .mbedignore containing '*'
565-
if self.is_ignored(join(root,"")):
580+
if self.is_ignored(join(relpath(root, base_path),"")):
581+
dirs[:] = []
566582
continue
567583

568584
for d in copy(dirs):
@@ -577,7 +593,7 @@ def _add_dir(self, path, resources, base_path, exclude_paths=None):
577593
# Ignore toolchain that do not match the current TOOLCHAIN
578594
(d.startswith('TOOLCHAIN_') and d[10:] not in labels['TOOLCHAIN']) or
579595
# Ignore .mbedignore files
580-
self.is_ignored(join(dir_path,"")) or
596+
self.is_ignored(join(relpath(root, base_path), d,"")) or
581597
# Ignore TESTS dir
582598
(d == 'TESTS')):
583599
dirs.remove(d)
@@ -606,7 +622,7 @@ def _add_dir(self, path, resources, base_path, exclude_paths=None):
606622
def _add_file(self, file_path, resources, base_path, exclude_paths=None):
607623
resources.file_basepath[file_path] = base_path
608624

609-
if self.is_ignored(file_path):
625+
if self.is_ignored(relpath(file_path, base_path)):
610626
return
611627

612628
_, ext = splitext(file_path)

0 commit comments

Comments
 (0)