Skip to content

Commit

Permalink
Remove ignoring paths on Folder and FolderLoader
Browse files Browse the repository at this point in the history
Preference goes to #3653
  • Loading branch information
JCZuurmond committed Feb 6, 2025
1 parent a203030 commit e7471ed
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 64 deletions.
43 changes: 4 additions & 39 deletions src/databricks/labs/ucx/source_code/folders.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,7 @@


class Folder(SourceContainer):
"""A source container that represents a folder.
Args:
ignore_relative_child_paths (set[Path] | None) : The child path relative
to this folder to be ignored while building a dependency graph.
If None, no child paths are ignored.
NOTE: The child paths are only ignored during dependency graph
building of **this** folder. If a child path is referenced via
another route, like an import of the file by a non-ignored file,
then a dependency for that child will be still be added to the
graph.
"""
"""A source container that represents a folder."""

# The following paths names are ignore as they do not contain source code
_IGNORE_PATH_NAMES = {
Expand All @@ -48,14 +36,11 @@ def __init__(
notebook_loader: NotebookLoader,
file_loader: FileLoader,
folder_loader: FolderLoader,
*,
ignore_relative_child_paths: set[Path] | None = None,
):
self._path = path
self._notebook_loader = notebook_loader
self._file_loader = file_loader
self._folder_loader = folder_loader
self._ignore_relative_child_paths = ignore_relative_child_paths or set[Path]()

def build_dependency_graph(self, parent: DependencyGraph) -> list[DependencyProblem]:
"""Build the dependency graph for the folder.
Expand All @@ -70,8 +55,6 @@ def build_dependency_graph(self, parent: DependencyGraph) -> list[DependencyProb
def _build_dependency_graph(self, parent: DependencyGraph) -> Iterable[DependencyProblem]:
"""Build the dependency graph for the contents of the folder."""
for child_path in self._path.iterdir():
if child_path.relative_to(self._path) in self._ignore_relative_child_paths:
continue
is_file = child_path.is_file()
is_notebook = is_a_notebook(child_path)
loader = self._notebook_loader if is_notebook else self._file_loader if is_file else self._folder_loader
Expand All @@ -83,33 +66,15 @@ def __repr__(self):


class FolderLoader(DependencyLoader):
"""Load a folder.
"""Load a folder."""

Args:
ignore_relative_child_paths (set[Path] | None) : see :class:Folder.
"""

def __init__(
self,
notebook_loader: NotebookLoader,
file_loader: FileLoader,
*,
ignore_relative_child_paths: set[Path] | None = None,
):
def __init__(self, notebook_loader: NotebookLoader, file_loader: FileLoader):
self._notebook_loader = notebook_loader
self._file_loader = file_loader
self._ignore_relative_child_paths = ignore_relative_child_paths or set[Path]()

def load_dependency(self, path_lookup: PathLookup, dependency: Dependency) -> Folder | None:
"""Load the folder as a dependency."""
absolute_path = path_lookup.resolve(dependency.path)
if not absolute_path:
return None
folder = Folder(
absolute_path,
self._notebook_loader,
self._file_loader,
self,
ignore_relative_child_paths=self._ignore_relative_child_paths,
)
return folder
return Folder(absolute_path, self._notebook_loader, self._file_loader, self)
25 changes: 0 additions & 25 deletions tests/unit/source_code/test_folders.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,3 @@ def test_folder_cannot_load_unresolved_path(graph_parent_child_context) -> None:
folder = graph_parent_child_context.dependency.load(path_lookup)
assert folder is None
path_lookup.resolve.assert_called_once_with(graph_parent_child_context.dependency.path)


def test_folder_ignores_child_path(mock_path_lookup, simple_dependency_resolver) -> None:
expected_dependencies = set[Dependency]()
# The grand_parent.py is left out intentionally if that is kept in,
# the dependencies for all files are still added to the graph as the
# grand_parent **import**s the other files, thus circumventing the
# ignore on the `FolderLoader`
for relative_path in "parent.py", "child.py":
path = mock_path_lookup.resolve(Path("parent-child-context") / relative_path)
dependency = Dependency(FileLoader(), path)
expected_dependencies.add(dependency)

path = mock_path_lookup.resolve(Path("parent-child-context/"))
dependency = Dependency(
FolderLoader(NotebookLoader(), FileLoader(), ignore_relative_child_paths={Path("grand_parent.py")}), path, False
)
graph = DependencyGraph(dependency, None, simple_dependency_resolver, mock_path_lookup, CurrentSessionState())
expected_dependencies.add(dependency)

folder = graph.dependency.load(mock_path_lookup)
assert folder is not None
folder.build_dependency_graph(graph)

assert graph.all_dependencies == expected_dependencies

0 comments on commit e7471ed

Please sign in to comment.