22
22
from time import time , sleep
23
23
from types import ListType
24
24
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
26
26
from inspect import getmro
27
27
from copy import deepcopy
28
28
from tools .config import Config
@@ -428,14 +428,31 @@ def is_ignored(self, file_path):
428
428
return True
429
429
return False
430
430
431
+ # Create a Resources object from the path pointed to by *path* by either traversing a
432
+ # a directory structure, when *path* is a directory, or adding *path* to the resources,
433
+ # when *path* is a file.
434
+ # The parameter *base_path* is used to set the base_path attribute of the Resources
435
+ # object and the parameter *exclude_paths* is used by the directory traversal to
436
+ # exclude certain paths from the traversal.
431
437
def scan_resources (self , path , exclude_paths = None , base_path = None ):
432
- labels = self .get_labels ()
433
-
434
438
resources = Resources (path )
435
439
if not base_path :
436
- base_path = path
440
+ if isfile (path ):
441
+ base_path = dirname (path )
442
+ else :
443
+ base_path = path
437
444
resources .base_path = base_path
438
445
446
+ if isfile (path ):
447
+ self ._add_file (path , resources , base_path , exclude_paths = exclude_paths )
448
+ else :
449
+ self ._add_dir (path , resources , base_path , exclude_paths = exclude_paths )
450
+ return resources
451
+
452
+ # A helper function for scan_resources. _add_dir traverses *path* (assumed to be a
453
+ # directory) and heeds the ".mbedignore" files along the way. _add_dir calls _add_file
454
+ # on every file it considers adding to the resources object.
455
+ def _add_dir (self , path , resources , base_path , exclude_paths = None ):
439
456
""" os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])
440
457
When topdown is True, the caller can modify the dirnames list in-place
441
458
(perhaps using del or slice assignment), and walk() will only recurse into
@@ -446,6 +463,7 @@ def scan_resources(self, path, exclude_paths=None, base_path=None):
446
463
bottom-up mode the directories in dirnames are generated before dirpath
447
464
itself is generated.
448
465
"""
466
+ labels = self .get_labels ()
449
467
for root , dirs , files in walk (path , followlinks = True ):
450
468
# Check if folder contains .mbedignore
451
469
if ".mbedignore" in files :
@@ -467,7 +485,7 @@ def scan_resources(self, path, exclude_paths=None, base_path=None):
467
485
if d == '.hg' :
468
486
resources .repo_dirs .append (dir_path )
469
487
resources .repo_files .extend (self .scan_repository (dir_path ))
470
-
488
+
471
489
if ((d .startswith ('.' ) or d in self .legacy_ignore_dirs ) or
472
490
# Ignore targets that do not match the TARGET in extra_labels list
473
491
(d .startswith ('TARGET_' ) and d [7 :] not in labels ['TARGET' ]) or
@@ -495,58 +513,61 @@ def scan_resources(self, path, exclude_paths=None, base_path=None):
495
513
496
514
for file in files :
497
515
file_path = join (root , file )
516
+ self ._add_file (file_path , resources , base_path )
498
517
499
- resources .file_basepath [file_path ] = base_path
518
+ # A helper function for both scan_resources and _add_dir. _add_file adds one file
519
+ # (*file_path*) to the resources object based on the file type.
520
+ def _add_file (self , file_path , resources , base_path , exclude_paths = None ):
521
+ resources .file_basepath [file_path ] = base_path
500
522
501
- if self .is_ignored (file_path ):
502
- continue
523
+ if self .is_ignored (file_path ):
524
+ return
503
525
504
- _ , ext = splitext (file )
505
- ext = ext .lower ()
526
+ _ , ext = splitext (file_path )
527
+ ext = ext .lower ()
506
528
507
- if ext == '.s' :
508
- resources .s_sources .append (file_path )
529
+ if ext == '.s' :
530
+ resources .s_sources .append (file_path )
509
531
510
- elif ext == '.c' :
511
- resources .c_sources .append (file_path )
532
+ elif ext == '.c' :
533
+ resources .c_sources .append (file_path )
512
534
513
- elif ext == '.cpp' :
514
- resources .cpp_sources .append (file_path )
535
+ elif ext == '.cpp' :
536
+ resources .cpp_sources .append (file_path )
515
537
516
- elif ext == '.h' or ext == '.hpp' :
517
- resources .headers .append (file_path )
538
+ elif ext == '.h' or ext == '.hpp' :
539
+ resources .headers .append (file_path )
518
540
519
- elif ext == '.o' :
520
- resources .objects .append (file_path )
541
+ elif ext == '.o' :
542
+ resources .objects .append (file_path )
521
543
522
- elif ext == self .LIBRARY_EXT :
523
- resources .libraries .append (file_path )
524
- resources .lib_dirs .add (root )
544
+ elif ext == self .LIBRARY_EXT :
545
+ resources .libraries .append (file_path )
546
+ resources .lib_dirs .add (dirname ( file_path ) )
525
547
526
- elif ext == self .LINKER_EXT :
527
- if resources .linker_script is not None :
528
- self .info ("Warning: Multiple linker scripts detected: %s -> %s" % (resources .linker_script , file_path ))
529
- resources .linker_script = file_path
548
+ elif ext == self .LINKER_EXT :
549
+ if resources .linker_script is not None :
550
+ self .info ("Warning: Multiple linker scripts detected: %s -> %s" % (resources .linker_script , file_path ))
551
+ resources .linker_script = file_path
530
552
531
- elif ext == '.lib' :
532
- resources .lib_refs .append (file_path )
553
+ elif ext == '.lib' :
554
+ resources .lib_refs .append (file_path )
533
555
534
- elif ext == '.bld' :
535
- resources .lib_builds .append (file_path )
556
+ elif ext == '.bld' :
557
+ resources .lib_builds .append (file_path )
536
558
537
- elif file == '.hgignore' :
538
- resources .repo_files .append (file_path )
559
+ elif file == '.hgignore' :
560
+ resources .repo_files .append (file_path )
539
561
540
- elif ext == '.hex' :
541
- resources .hex_files .append (file_path )
562
+ elif ext == '.hex' :
563
+ resources .hex_files .append (file_path )
542
564
543
- elif ext == '.bin' :
544
- resources .bin_files .append (file_path )
565
+ elif ext == '.bin' :
566
+ resources .bin_files .append (file_path )
545
567
546
- elif ext == '.json' :
547
- resources .json_files .append (file_path )
568
+ elif ext == '.json' :
569
+ resources .json_files .append (file_path )
548
570
549
- return resources
550
571
551
572
def scan_repository (self , path ):
552
573
resources = []
0 commit comments