6363
6464# Tracing flags
6565TRACE = False
66- TRACE_DEEP = True
66+ TRACE_DEEP = False
6767
6868
6969def logger_debug (* args ):
@@ -110,7 +110,7 @@ def skip_ignored(location):
110110 return is_special (location ) or ignored (location )
111111
112112
113- def is_ignored (location , includes = None , excludes = None ):
113+ def is_ignored (location , includes = tuple () , excludes = tuple () ):
114114
115115 excludes = {
116116 pattern : 'User ignore: Supplied by --ignore' for pattern in excludes
@@ -339,8 +339,8 @@ def __init__(
339339
340340 # finally populate
341341 self .paths = self ._prepare_clean_paths (paths )
342+ self .includes = self ._prepare_clean_paths (includes )
342343 self .ignores = ignores
343- self .includes = includes
344344 self ._populate ()
345345
346346 def _prepare_clean_paths (self , paths = tuple ()):
@@ -593,30 +593,48 @@ def err(_error):
593593 f"ERROR: cannot populate codebase: { _error } \n { traceback .format_exc ()} "
594594 )
595595
596- skip_ignored = partial (is_ignored , includes = includes , excludes = ignores )
596+ # ignore creating resources based on path patterns
597+ skip_ignored = partial (is_ignored , excludes = ignores )
597598
598599 if TRACE_DEEP :
599600 logger_debug (f"parents_by_loc: { parents_by_loc } , ignores: { ignores } , includes: { includes } " )
600601
601- # Walk over the directory and build the resource tree
602- for top , dirs , files in depth_walk (
603- root_location = root .location ,
604- skip_ignored = skip_ignored ,
605- max_depth = self .max_depth ,
606- error_handler = err ,
607- ):
608- parent = parents_by_loc .pop (top )
609- for created in self ._create_resources (
610- parent = parent ,
611- top = top ,
612- dirs = dirs ,
613- files = files ,
614- skip_ignored = skip_ignored ,
602+ # in the case of a single input location, walking starts from
603+ # the root and only the root location
604+ if not includes :
605+ includes = [root .location ]
606+ else :
607+ # create the directory resources between the common
608+ # prefix and the included locations so that they are
609+ # connected to the root
610+ for created in self ._create_resources_common_prefix_to_inputs (
611+ root = root ,
612+ includes = includes ,
615613 ):
616- # on the plain, bare FS, files cannot be parents
617614 if not created .is_file :
618615 parents_by_loc [created .location ] = created
619616
617+ # we start walking through all the input locations
618+ for included_location in includes :
619+ # Walk over the directory and build the resource tree
620+ for top , dirs , files in depth_walk (
621+ root_location = included_location ,
622+ skip_ignored = skip_ignored ,
623+ max_depth = self .max_depth ,
624+ error_handler = err ,
625+ ):
626+ parent = parents_by_loc .pop (top )
627+ for created in self ._create_resources (
628+ parent = parent ,
629+ top = top ,
630+ dirs = dirs ,
631+ files = files ,
632+ skip_ignored = skip_ignored ,
633+ ):
634+ # on the plain, bare FS, files cannot be parents
635+ if not created .is_file :
636+ parents_by_loc [created .location ] = created
637+
620638 def _create_resources (self , parent , top , dirs , files , skip_ignored = skip_ignored ):
621639 """
622640 Create and yield ``files`` and ``dirs`` children Resources of a
@@ -641,6 +659,28 @@ def _create_resources(self, parent, top, dirs, files, skip_ignored=skip_ignored)
641659 logger_debug ("Codebase.create_resources:" , res )
642660 yield res
643661
662+ def _create_resources_common_prefix_to_inputs (self , root , includes ):
663+
664+ if TRACE_DEEP :
665+ logger_debug (f"_create_resources_common_prefix_to_inputs: root:{ root .location } , includes: { includes } " )
666+
667+ for included_path in includes :
668+ _ , _ , extra_dir_path = included_path .rpartition (root .location )
669+ extra_dirs = extra_dir_path .strip ("/" ).split ("/" )
670+ if TRACE_DEEP :
671+ logger_debug (f"_create_resources_common_prefix_to_inputs: root:{ root .location } , includes: { includes } " )
672+
673+ dir_resource = root
674+ for dir_segment in extra_dirs :
675+ dir_resource = self ._get_or_create_resource (
676+ name = dir_segment ,
677+ parent = dir_resource ,
678+ is_file = False ,
679+ )
680+ if TRACE :
681+ logger_debug ("Codebase.create_resources:" , dir_resource )
682+ yield dir_resource
683+
644684 def _create_root_resource (self ):
645685 """
646686 Create and return the root Resource of this codebase.
@@ -1606,8 +1646,8 @@ def clean_path(path):
16061646 Return a cleaned and normalized POSIX ``path``.
16071647 """
16081648 path = path or ""
1609- # convert to posix and ensure we have no slash at both ends
1610- path = posixpath_normpath (path .replace ("\\ " , "/" ).strip ("/" ))
1649+ # convert to posix and ensure we have no slash at the end
1650+ path = posixpath_normpath (path .replace ("\\ " , "/" ).rstrip ("/" ))
16111651 if path == "." :
16121652 path = ""
16131653 return path
0 commit comments