diff --git a/swh/scanner/model.py b/swh/scanner/model.py --- a/swh/scanner/model.py +++ b/swh/scanner/model.py @@ -7,13 +7,13 @@ import sys import json from pathlib import PosixPath -from typing import Any, Dict, Tuple, Iterable +from typing import Any, Dict, Tuple, Iterable, List from enum import Enum import ndjson from .plot import generate_sunburst, offline_plot -from .exceptions import InvalidObjectType +from .exceptions import InvalidObjectType, InvalidDirectoryPath from swh.model.identifiers import DIRECTORY, CONTENT @@ -172,6 +172,32 @@ if child_node.otype == DIRECTORY: yield from child_node.__iterNodesAttr() + def getFilesFromDir(self, dir_path: PosixPath) -> List: + """ + Retrieve files information about a specific directory path + + Returns: + A list containing the files attributes present inside the directory given + in input + """ + + def getFiles(node): + files = [] + for _, node in node.children.items(): + if node.otype == CONTENT: + files.append(node.attributes) + return files + + if dir_path == self.path: + return getFiles(self) + else: + for node in self.iterate(): + if node.path == dir_path: + return getFiles(node) + raise InvalidDirectoryPath( + "The directory provided doesn't match any stored directory" + ) + def __getSubDirsInfo(self, root, directories): """Fills the directories given in input with the contents information stored inside the directory child, only if they have contents. diff --git a/swh/scanner/tests/test_model.py b/swh/scanner/tests/test_model.py --- a/swh/scanner/tests/test_model.py +++ b/swh/scanner/tests/test_model.py @@ -3,6 +3,10 @@ # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information +import pytest + +from swh.scanner.exceptions import InvalidDirectoryPath + def test_tree_add_node(example_tree, temp_folder): avail_paths = temp_folder["paths"].keys() @@ -76,3 +80,29 @@ assert subsubdir_path not in directories assert directories[subdir_path] == (2, 2) + + +def test_get_files_from_dir(example_tree, temp_folder): + subdir_path = temp_folder["subdir"] + + for path, pid in temp_folder["paths"].items(): + example_tree.addNode(path, pid, True) + + files = example_tree.getFilesFromDir(subdir_path) + assert len(files) == 2 + + +def test_get_files_source_path(example_tree, temp_folder): + for path, pid in temp_folder["paths"].items(): + example_tree.addNode(path, pid, True) + + files = example_tree.getFilesFromDir(example_tree.path) + assert len(files) == 1 + + +def test_get_files_from_dir_raise_exception(example_tree, temp_folder): + for path, pid in temp_folder["paths"].items(): + example_tree.addNode(path, pid, True) + + with pytest.raises(InvalidDirectoryPath): + example_tree.getFilesFromDir("test/")