Skip to content

Commit 3d3b0a7

Browse files
author
Seppo Takalo
committed
Implement the .mbedignore functionality.
Check the content of `.mbedignore` files and try to match the file patterns against the directories and source files found while travelling the directory structure. Every line in `.mbedignore` file is treated as a unix style glob pattern. Before matching, the pattern is appended with its directory location, so it will match only folder on same level, or deeper in the directory structure. For example: Directory ./source/test contains .mbedignore with content: `unittest/*` Then everything under ./source/test/unittest/ is ignored, including the unittest folder itself. Another example: When .mbedignore file contains just `*`, then everything under the folder it is located, is ignored. It won't even be added to include paths.
1 parent 3c08e7b commit 3d3b0a7

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

tools/toolchains/__init__.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import re
1919
import sys
20-
from os import stat, walk, getcwd
20+
from os import stat, walk, getcwd, sep
2121
from copy import copy
2222
from time import time, sleep
2323
from types import ListType
@@ -30,6 +30,7 @@
3030
from tools.settings import BUILD_OPTIONS, MBED_ORG_USER
3131
import tools.hooks as hooks
3232
from hashlib import md5
33+
import fnmatch
3334

3435

3536
#Disables multiprocessing if set to higher number than the host machine CPUs
@@ -165,6 +166,7 @@ def __str__(self):
165166

166167
class mbedToolchain:
167168
VERBOSE = True
169+
ignorepatterns = []
168170

169171
CORTEX_SYMBOLS = {
170172
"Cortex-M0" : ["__CORTEX_M0", "ARM_MATH_CM0", "__CMSIS_RTOS", "__MBED_CMSIS_RTOS_CM"],
@@ -343,6 +345,12 @@ def need_update(self, target, dependencies):
343345

344346
return False
345347

348+
def is_ignored(self, file_path):
349+
for pattern in self.ignorepatterns:
350+
if fnmatch.fnmatch(file_path, pattern):
351+
return True
352+
return False
353+
346354
def scan_resources(self, path, exclude_paths=None):
347355
labels = self.get_labels()
348356
resources = Resources(path)
@@ -370,10 +378,28 @@ def scan_resources(self, path, exclude_paths=None):
370378
if ((d.startswith('.') or d in self.legacy_ignore_dirs) or
371379
(d.startswith('TARGET_') and d[7:] not in labels['TARGET']) or
372380
(d.startswith('TOOLCHAIN_') and d[10:] not in labels['TOOLCHAIN']) or
373-
(d == 'TESTS') or
374-
exists(join(dir_path, '.buildignore'))):
381+
(d == 'TESTS')):
375382
dirs.remove(d)
376-
383+
384+
# Check if folder contains .mbedignore
385+
try:
386+
with open (join(dir_path,".mbedignore"), "r") as f:
387+
lines=f.readlines()
388+
lines = [l.strip() for l in lines] # Strip whitespaces
389+
lines = [l for l in lines if l != ""] # Strip empty lines
390+
lines = [l for l in lines if not re.match("^#",l)] # Strip comment lines
391+
# Append root path to glob patterns
392+
# and append patterns to ignorepatterns
393+
self.ignorepatterns.extend([join(dir_path,line.strip()) for line in lines])
394+
except IOError:
395+
pass
396+
397+
# Remove dirs that already match the ignorepatterns
398+
# to avoid travelling into them and to prevent them
399+
# on appearing in include path.
400+
if self.is_ignored(join(dir_path,"")):
401+
dirs.remove(d)
402+
377403
if exclude_paths:
378404
for exclude_path in exclude_paths:
379405
rel_path = relpath(dir_path, exclude_path)
@@ -386,6 +412,10 @@ def scan_resources(self, path, exclude_paths=None):
386412

387413
for file in files:
388414
file_path = join(root, file)
415+
416+
if self.is_ignored(file_path):
417+
continue
418+
389419
_, ext = splitext(file)
390420
ext = ext.lower()
391421

0 commit comments

Comments
 (0)