Skip to content

Commit c7acb7c

Browse files
committed
Enable passing individual files to scan_resources
1 parent ec1ec7c commit c7acb7c

File tree

1 file changed

+50
-40
lines changed

1 file changed

+50
-40
lines changed

tools/toolchains/__init__.py

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from time import time, sleep
2323
from types import ListType
2424
from shutil import copyfile
25-
from os.path import join, splitext, exists, relpath, dirname, basename, split, abspath
25+
from os.path import join, splitext, exists, relpath, dirname, basename, split, abspath, isfile, isdir
2626
from inspect import getmro
2727
from copy import deepcopy
2828
from tools.config import Config
@@ -431,13 +431,21 @@ def is_ignored(self, file_path):
431431
return False
432432

433433
def scan_resources(self, path, exclude_paths=None, base_path=None):
434-
labels = self.get_labels()
435-
436434
resources = Resources(path)
437435
if not base_path:
438-
base_path = path
436+
if isfile(path):
437+
base_path = dirname(path)
438+
else:
439+
base_path = path
439440
resources.base_path = base_path
440441

442+
if isfile(path):
443+
self.add_file(path, resources, base_path, exclude_paths=exclude_paths)
444+
else:
445+
self.add_dir(path, resources, base_path, exclude_paths=exclude_paths)
446+
return resources
447+
448+
def add_dir(self, path, resources, base_path, exclude_paths=None):
441449
""" os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])
442450
When topdown is True, the caller can modify the dirnames list in-place
443451
(perhaps using del or slice assignment), and walk() will only recurse into
@@ -448,6 +456,7 @@ def scan_resources(self, path, exclude_paths=None, base_path=None):
448456
bottom-up mode the directories in dirnames are generated before dirpath
449457
itself is generated.
450458
"""
459+
labels = self.get_labels()
451460
for root, dirs, files in walk(path, followlinks=True):
452461
# Check if folder contains .mbedignore
453462
if ".mbedignore" in files:
@@ -469,7 +478,7 @@ def scan_resources(self, path, exclude_paths=None, base_path=None):
469478
if d == '.hg':
470479
resources.repo_dirs.append(dir_path)
471480
resources.repo_files.extend(self.scan_repository(dir_path))
472-
481+
473482
if ((d.startswith('.') or d in self.legacy_ignore_dirs) or
474483
# Ignore targets that do not match the TARGET in extra_labels list
475484
(d.startswith('TARGET_') and d[7:] not in labels['TARGET']) or
@@ -497,58 +506,59 @@ def scan_resources(self, path, exclude_paths=None, base_path=None):
497506

498507
for file in files:
499508
file_path = join(root, file)
509+
self.add_file(file_path, resources, base_path)
500510

501-
resources.file_basepath[file_path] = base_path
511+
def add_file(self, file_path, resources, base_path, exclude_paths=None):
512+
resources.file_basepath[file_path] = base_path
502513

503-
if self.is_ignored(file_path):
504-
continue
514+
if self.is_ignored(file_path):
515+
return
505516

506-
_, ext = splitext(file)
507-
ext = ext.lower()
517+
_, ext = splitext(file_path)
518+
ext = ext.lower()
508519

509-
if ext == '.s':
510-
resources.s_sources.append(file_path)
520+
if ext == '.s':
521+
resources.s_sources.append(file_path)
511522

512-
elif ext == '.c':
513-
resources.c_sources.append(file_path)
523+
elif ext == '.c':
524+
resources.c_sources.append(file_path)
514525

515-
elif ext == '.cpp':
516-
resources.cpp_sources.append(file_path)
526+
elif ext == '.cpp':
527+
resources.cpp_sources.append(file_path)
517528

518-
elif ext == '.h' or ext == '.hpp':
519-
resources.headers.append(file_path)
529+
elif ext == '.h' or ext == '.hpp':
530+
resources.headers.append(file_path)
520531

521-
elif ext == '.o':
522-
resources.objects.append(file_path)
532+
elif ext == '.o':
533+
resources.objects.append(file_path)
523534

524-
elif ext == self.LIBRARY_EXT:
525-
resources.libraries.append(file_path)
526-
resources.lib_dirs.add(root)
535+
elif ext == self.LIBRARY_EXT:
536+
resources.libraries.append(file_path)
537+
resources.lib_dirs.add(dirname(file_path))
527538

528-
elif ext == self.LINKER_EXT:
529-
if resources.linker_script is not None:
530-
self.info("Warning: Multiple linker scripts detected: %s -> %s" % (resources.linker_script, file_path))
531-
resources.linker_script = file_path
539+
elif ext == self.LINKER_EXT:
540+
if resources.linker_script is not None:
541+
self.info("Warning: Multiple linker scripts detected: %s -> %s" % (resources.linker_script, file_path))
542+
resources.linker_script = file_path
532543

533-
elif ext == '.lib':
534-
resources.lib_refs.append(file_path)
544+
elif ext == '.lib':
545+
resources.lib_refs.append(file_path)
535546

536-
elif ext == '.bld':
537-
resources.lib_builds.append(file_path)
547+
elif ext == '.bld':
548+
resources.lib_builds.append(file_path)
538549

539-
elif file == '.hgignore':
540-
resources.repo_files.append(file_path)
550+
elif file == '.hgignore':
551+
resources.repo_files.append(file_path)
541552

542-
elif ext == '.hex':
543-
resources.hex_files.append(file_path)
553+
elif ext == '.hex':
554+
resources.hex_files.append(file_path)
544555

545-
elif ext == '.bin':
546-
resources.bin_files.append(file_path)
556+
elif ext == '.bin':
557+
resources.bin_files.append(file_path)
547558

548-
elif ext == '.json':
549-
resources.json_files.append(file_path)
559+
elif ext == '.json':
560+
resources.json_files.append(file_path)
550561

551-
return resources
552562

553563
def scan_repository(self, path):
554564
resources = []

0 commit comments

Comments
 (0)