|
19 | 19 | import tempfile
|
20 | 20 | import colorama
|
21 | 21 |
|
22 |
| - |
| 22 | +from copy import copy |
23 | 23 | from types import ListType
|
24 | 24 | from shutil import rmtree
|
25 | 25 | from os.path import join, exists, basename, abspath, normpath
|
26 |
| -from os import getcwd |
| 26 | +from os import getcwd, walk |
27 | 27 | from time import time
|
| 28 | +import fnmatch |
28 | 29 |
|
29 | 30 | from tools.utils import mkdir, run_cmd, run_cmd_ext, NotSupportedException, ToolException
|
30 | 31 | from tools.paths import MBED_TARGETS_PATH, MBED_LIBRARIES, MBED_API, MBED_HAL, MBED_COMMON
|
@@ -827,3 +828,63 @@ def write_build_report(build_report, template_filename, filename):
|
827 | 828 |
|
828 | 829 | with open(filename, 'w+') as f:
|
829 | 830 | f.write(template.render(failing_builds=build_report_failing, passing_builds=build_report_passing))
|
| 831 | + |
| 832 | + |
| 833 | +def scan_for_source_paths(path, exclude_paths=None): |
| 834 | + ignorepatterns = [] |
| 835 | + paths = [] |
| 836 | + |
| 837 | + def is_ignored(file_path): |
| 838 | + for pattern in ignorepatterns: |
| 839 | + if fnmatch.fnmatch(file_path, pattern): |
| 840 | + return True |
| 841 | + return False |
| 842 | + |
| 843 | + |
| 844 | + """ os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]]) |
| 845 | + When topdown is True, the caller can modify the dirnames list in-place |
| 846 | + (perhaps using del or slice assignment), and walk() will only recurse into |
| 847 | + the subdirectories whose names remain in dirnames; this can be used to prune |
| 848 | + the search, impose a specific order of visiting, or even to inform walk() |
| 849 | + about directories the caller creates or renames before it resumes walk() |
| 850 | + again. Modifying dirnames when topdown is False is ineffective, because in |
| 851 | + bottom-up mode the directories in dirnames are generated before dirpath |
| 852 | + itself is generated. |
| 853 | + """ |
| 854 | + for root, dirs, files in walk(path, followlinks=True): |
| 855 | + # Remove ignored directories |
| 856 | + # Check if folder contains .mbedignore |
| 857 | + if ".mbedignore" in files : |
| 858 | + with open (join(root,".mbedignore"), "r") as f: |
| 859 | + lines=f.readlines() |
| 860 | + lines = [l.strip() for l in lines] # Strip whitespaces |
| 861 | + lines = [l for l in lines if l != ""] # Strip empty lines |
| 862 | + lines = [l for l in lines if not re.match("^#",l)] # Strip comment lines |
| 863 | + # Append root path to glob patterns |
| 864 | + # and append patterns to ignorepatterns |
| 865 | + ignorepatterns.extend([join(root,line.strip()) for line in lines]) |
| 866 | + |
| 867 | + for d in copy(dirs): |
| 868 | + dir_path = join(root, d) |
| 869 | + |
| 870 | + # Always ignore hidden directories |
| 871 | + if d.startswith('.'): |
| 872 | + dirs.remove(d) |
| 873 | + |
| 874 | + # Remove dirs that already match the ignorepatterns |
| 875 | + # to avoid travelling into them and to prevent them |
| 876 | + # on appearing in include path. |
| 877 | + if is_ignored(join(dir_path,"")): |
| 878 | + dirs.remove(d) |
| 879 | + |
| 880 | + if exclude_paths: |
| 881 | + for exclude_path in exclude_paths: |
| 882 | + rel_path = relpath(dir_path, exclude_path) |
| 883 | + if not (rel_path.startswith('..')): |
| 884 | + dirs.remove(d) |
| 885 | + break |
| 886 | + |
| 887 | + # Add root to include paths |
| 888 | + paths.append(root) |
| 889 | + |
| 890 | + return paths |
0 commit comments