Skip to content

Commit 2305a8c

Browse files
Merge pull request #4783 from theotherjimmy/refactor-exporter-ignores
Collect ignored directories in scan and use in exporters
2 parents 50c616e + c8e6cf5 commit 2305a8c

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
@@ -312,7 +312,7 @@ def export_project(src_paths, export_path, target, ide, libraries_paths=None,
312312
name = basename(normpath(abspath(src_paths[0])))
313313

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

tools/export/gnuarmeclipse/__init__.py

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

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

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

522427
# -------------------------------------------------------------------------
523428

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)