Skip to content

Commit c8e6cf5

Browse files
committed
Collect ignores from scan resources and use in exporetrs
1 parent aae62bd commit c8e6cf5

File tree

4 files changed

+25
-105
lines changed

4 files changed

+25
-105
lines changed

tools/build_api.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ def merge_region_list(region_list, destination, padding=b'\xFF'):
400400
merged.tofile(output, format='bin')
401401

402402
def scan_resources(src_paths, toolchain, dependencies_paths=None,
403-
inc_dirs=None, base_path=None):
403+
inc_dirs=None, base_path=None, collect_ignores=False):
404404
""" Scan resources using initialized toolcain
405405
406406
Positional arguments
@@ -412,9 +412,11 @@ def scan_resources(src_paths, toolchain, dependencies_paths=None,
412412
"""
413413

414414
# Scan src_path
415-
resources = toolchain.scan_resources(src_paths[0], base_path=base_path)
415+
resources = toolchain.scan_resources(src_paths[0], base_path=base_path,
416+
collect_ignores=collect_ignores)
416417
for path in src_paths[1:]:
417-
resources.add(toolchain.scan_resources(path, base_path=base_path))
418+
resources.add(toolchain.scan_resources(path, base_path=base_path,
419+
collect_ignores=collect_ignores))
418420

419421
# Scan dependency paths for include dirs
420422
if dependencies_paths is not None:

tools/export/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ def export_project(src_paths, export_path, target, ide, libraries_paths=None,
314314
name = basename(normpath(abspath(src_paths[0])))
315315

316316
# Call unified scan_resources
317-
resource_dict = {loc: scan_resources(path, toolchain, inc_dirs=inc_dirs)
317+
resource_dict = {loc: scan_resources(path, toolchain, inc_dirs=inc_dirs, collect_ignores=True)
318318
for loc, path in src_paths.iteritems()}
319319
resources = Resources()
320320
toolchain.build_dir = export_path

tools/export/gnuarmeclipse/__init__.py

Lines changed: 1 addition & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -415,105 +415,10 @@ def compute_exclusions(self):
415415
"""
416416
source_folders = [self.filter_dot(s) for s in set(dirname(
417417
src) for src in self.resources.c_sources + self.resources.cpp_sources + self.resources.s_sources)]
418-
if '.' in source_folders:
419-
source_folders.remove('.')
420-
421-
# print 'source folders'
422-
# print source_folders
423-
424-
# Source folders were converted before and are guaranteed to
425-
# use the POSIX separator.
426-
top_folders = [f for f in set(s.split('/')[0]
427-
for s in source_folders)]
428-
# print 'top folders'
429-
# print top_folders
430-
431-
self.source_tree = {}
432-
for top_folder in top_folders:
433-
for root, dirs, files in os.walk(top_folder, topdown=True):
434-
# print root, dirs, files
435-
436-
# Paths returned by os.walk() must be split with os.dep
437-
# to accomodate Windows weirdness.
438-
parts = root.split(os.sep)
439-
440-
# Ignore paths that include parts starting with dot.
441-
skip = False
442-
for part in parts:
443-
if part.startswith('.'):
444-
skip = True
445-
break
446-
if skip:
447-
continue
448-
449-
# Further process only leaf paths, (that do not have
450-
# sub-folders).
451-
if len(dirs) == 0:
452-
# The path is reconstructed using POSIX separators.
453-
self.add_source_folder_to_tree('/'.join(parts))
454-
455-
for folder in source_folders:
456-
self.add_source_folder_to_tree(folder, True)
457-
458-
# print
459-
# print self.source_tree
460-
# self.dump_paths(self.source_tree)
461-
# self.dump_tree(self.source_tree)
462-
463-
# print 'excludings'
464-
self.excluded_folders = ['BUILD']
465-
self.recurse_excludings(self.source_tree)
466418

419+
self.excluded_folders = set(self.resources.ignored_dirs) - set(self.resources.inc_dirs)
467420
print 'Source folders: {0}, with {1} exclusions'.format(len(source_folders), len(self.excluded_folders))
468421

469-
def add_source_folder_to_tree(self, path, is_used=False):
470-
"""
471-
Decompose a path in an array of folder names and create the tree.
472-
On the second pass the nodes should be already there; mark them
473-
as used.
474-
"""
475-
# print path, is_used
476-
477-
# All paths arriving here are guaranteed to use the POSIX
478-
# separators, os.walk() paths were also explicitly converted.
479-
parts = path.split('/')
480-
# print parts
481-
node = self.source_tree
482-
prev = None
483-
for part in parts:
484-
if part not in node.keys():
485-
new_node = {}
486-
new_node['name'] = part
487-
new_node['children'] = {}
488-
if prev != None:
489-
new_node['parent'] = prev
490-
node[part] = new_node
491-
node[part]['is_used'] = is_used
492-
prev = node[part]
493-
node = node[part]['children']
494-
495-
def recurse_excludings(self, nodes):
496-
"""
497-
Recurse the tree and collect all unused folders; descend
498-
the hierarchy only for used nodes.
499-
"""
500-
for k in nodes.keys():
501-
node = nodes[k]
502-
if node['is_used'] == False:
503-
parts = []
504-
cnode = node
505-
while True:
506-
parts.insert(0, cnode['name'])
507-
if 'parent' not in cnode:
508-
break
509-
cnode = cnode['parent']
510-
511-
# Compose a POSIX path.
512-
path = '/'.join(parts)
513-
# print path
514-
self.excluded_folders.append(path)
515-
else:
516-
self.recurse_excludings(node['children'])
517422

518423
# -------------------------------------------------------------------------
519424

tools/toolchains/__init__.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,9 @@ def closure(v=v):
116116
self.eager = {}
117117

118118
class Resources:
119-
def __init__(self, base_path=None):
119+
def __init__(self, base_path=None, collect_ignores=False):
120120
self.base_path = base_path
121+
self.collect_ignores = collect_ignores
121122

122123
self.file_basepath = {}
123124

@@ -148,6 +149,7 @@ def __init__(self, base_path=None):
148149

149150
# Features
150151
self.features = LazyDict()
152+
self.ignored_dirs = []
151153

152154
def __add__(self, resources):
153155
if resources is None:
@@ -161,6 +163,10 @@ def __radd__(self, resources):
161163
else:
162164
return self.add(resources)
163165

166+
def ignore_dir(self, directory):
167+
if self.collect_ignores:
168+
self.ignored_dirs.append(directory)
169+
164170
def add(self, resources):
165171
for f,p in resources.file_basepath.items():
166172
self.file_basepath[f] = p
@@ -190,6 +196,7 @@ def add(self, resources):
190196
self.json_files += resources.json_files
191197

192198
self.features.update(resources.features)
199+
self.ignored_dirs += resources.ignored_dirs
193200

194201
return self
195202

@@ -612,10 +619,11 @@ def add_ignore_patterns(self, root, base_path, patterns):
612619
# The parameter *base_path* is used to set the base_path attribute of the Resources
613620
# object and the parameter *exclude_paths* is used by the directory traversal to
614621
# exclude certain paths from the traversal.
615-
def scan_resources(self, path, exclude_paths=None, base_path=None):
622+
def scan_resources(self, path, exclude_paths=None, base_path=None,
623+
collect_ignores=False):
616624
self.progress("scan", path)
617625

618-
resources = Resources(path)
626+
resources = Resources(path, collect_ignores=collect_ignores)
619627
if not base_path:
620628
if isfile(path):
621629
base_path = dirname(path)
@@ -656,8 +664,10 @@ def _add_dir(self, path, resources, base_path, exclude_paths=None):
656664
self.add_ignore_patterns(root, base_path, lines)
657665

658666
# Skip the whole folder if ignored, e.g. .mbedignore containing '*'
659-
if (self.is_ignored(join(relpath(root, base_path),"")) or
660-
self.build_dir == join(relpath(root, base_path))):
667+
root_path =join(relpath(root, base_path))
668+
if (self.is_ignored(join(root_path,"")) or
669+
self.build_dir == root_path):
670+
resources.ignore_dir(root_path)
661671
dirs[:] = []
662672
continue
663673

@@ -676,18 +686,21 @@ def _add_dir(self, path, resources, base_path, exclude_paths=None):
676686
self.is_ignored(join(relpath(root, base_path), d,"")) or
677687
# Ignore TESTS dir
678688
(d == 'TESTS')):
689+
resources.ignore_dir(dir_path)
679690
dirs.remove(d)
680691
elif d.startswith('FEATURE_'):
681692
# Recursively scan features but ignore them in the current scan.
682693
# These are dynamically added by the config system if the conditions are matched
683694
def closure (dir_path=dir_path, base_path=base_path):
684695
return self.scan_resources(dir_path, base_path=base_path)
685696
resources.features.add_lazy(d[8:], closure)
697+
resources.ignore_dir(dir_path)
686698
dirs.remove(d)
687699
elif exclude_paths:
688700
for exclude_path in exclude_paths:
689701
rel_path = relpath(dir_path, exclude_path)
690702
if not (rel_path.startswith('..')):
703+
resources.ignore_dir(dir_path)
691704
dirs.remove(d)
692705
break
693706

0 commit comments

Comments
 (0)