diff --git a/swh/scanner/dashboard/dashboard.py b/swh/scanner/dashboard/dashboard.py --- a/swh/scanner/dashboard/dashboard.py +++ b/swh/scanner/dashboard/dashboard.py @@ -3,7 +3,7 @@ # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information -from pathlib import PosixPath +from pathlib import Path from ..model import Tree @@ -15,7 +15,7 @@ from dash.dependencies import Input, Output -def generate_table_body(dir_path: PosixPath, source: Tree): +def generate_table_body(dir_path: Path, source: Tree): """ Generate the data_table from the path taken from the chart. @@ -25,7 +25,7 @@ data = [] for file_info in source.getFilesFromDir(dir_path): for file_path, attr in file_info.items(): - file_path = PosixPath(file_path) + file_path = Path(file_path) file_name = file_path.parts[len(file_path.parts) - 1] data.append( html.Tr( @@ -92,7 +92,7 @@ full_path = ( source.path.joinpath(raw_path) if raw_path != str(source.path) - else PosixPath(raw_path) + else Path(raw_path) ) return table_header + generate_table_body(full_path, source), str(full_path) else: diff --git a/swh/scanner/model.py b/swh/scanner/model.py --- a/swh/scanner/model.py +++ b/swh/scanner/model.py @@ -6,7 +6,7 @@ from __future__ import annotations import sys import json -from pathlib import PosixPath +from pathlib import Path from typing import Any, Dict, Tuple, Iterable, List from enum import Enum @@ -32,20 +32,20 @@ """Representation of a file system structure """ - def __init__(self, path: PosixPath, father: Tree = None): + def __init__(self, path: Path, father: Tree = None): self.father = father self.path = path self.otype = DIRECTORY if path.is_dir() else CONTENT self.swhid = "" self.known = False - self.children: Dict[PosixPath, Tree] = {} + self.children: Dict[Path, Tree] = {} - def addNode(self, path: PosixPath, swhid: str, known: bool) -> None: + def addNode(self, path: Path, swhid: str, known: bool) -> None: """Recursively add a new path. """ relative_path = path.relative_to(self.path) - if relative_path == PosixPath("."): + if relative_path == Path("."): self.swhid = swhid self.known = known return @@ -172,7 +172,7 @@ if child_node.otype == DIRECTORY: yield from child_node.__iterNodesAttr() - def getFilesFromDir(self, dir_path: PosixPath) -> List: + def getFilesFromDir(self, dir_path: Path) -> List: """ Retrieve files information about a specific directory path @@ -215,7 +215,7 @@ if child_node.has_dirs(): child_node.__getSubDirsInfo(root, directories) - def getDirectoriesInfo(self, root: PosixPath) -> Dict[PosixPath, Tuple[int, int]]: + def getDirectoriesInfo(self, root: Path) -> Dict[Path, Tuple[int, int]]: """Get information about all directories under the given root. Returns: diff --git a/swh/scanner/plot.py b/swh/scanner/plot.py --- a/swh/scanner/plot.py +++ b/swh/scanner/plot.py @@ -16,7 +16,7 @@ """ from typing import List, Dict, Tuple -from pathlib import PosixPath +from pathlib import Path from plotly.offline import offline import plotly.graph_objects as go @@ -160,7 +160,7 @@ return complete_df -def compute_max_depth(dirs_path: List[PosixPath], root: PosixPath) -> int: +def compute_max_depth(dirs_path: List[Path], root: Path) -> int: """Compute the maximum depth level of the given directory paths. Example: for `var/log/kernel/` the depth level is 3 @@ -179,10 +179,7 @@ def generate_df_from_dirs( - dirs: Dict[PosixPath, Tuple[int, int]], - columns: List[str], - root: PosixPath, - max_depth: int, + dirs: Dict[Path, Tuple[int, int]], columns: List[str], root: Path, max_depth: int, ) -> pd.DataFrame: """Generate a dataframe from the directories given in input. @@ -208,7 +205,7 @@ """ - def get_parents(path: PosixPath): + def get_parents(path: Path): parts = path.parts[1:] if path.parts[0] == "/" else path.parts for i in range(1, len(parts) + 1): @@ -237,7 +234,7 @@ def generate_sunburst( - directories: Dict[PosixPath, Tuple[int, int]], root: PosixPath + directories: Dict[Path, Tuple[int, int]], root: Path ) -> go.Sunburst: """Generate a sunburst chart from the directories given in input. diff --git a/swh/scanner/scanner.py b/swh/scanner/scanner.py --- a/swh/scanner/scanner.py +++ b/swh/scanner/scanner.py @@ -8,7 +8,7 @@ import glob import itertools import os -from pathlib import PosixPath +from pathlib import Path import re from typing import List, Dict, Tuple, Iterator, Union, Iterable, Pattern, Any @@ -84,7 +84,7 @@ False if the directory has to be ignored, True otherwise """ - path = PosixPath(path_name.decode() if isinstance(path_name, bytes) else path_name) + path = Path(path_name.decode() if isinstance(path_name, bytes) else path_name) for sre_pattern in exclude_patterns: if sre_pattern.match(str(path)): return False @@ -92,8 +92,8 @@ def get_subpaths( - path: PosixPath, exclude_patterns: Iterable[Pattern[str]] -) -> Iterator[Tuple[PosixPath, str]]: + path: Path, exclude_patterns: Iterable[Pattern[str]] +) -> Iterator[Tuple[Path, str]]: """Find the SoftWare Heritage persistent IDentifier (SWHID) of the directories and files under a given path. @@ -126,12 +126,12 @@ dirpath, dnames, fnames = next(os.walk(path)) for node in itertools.chain(dnames, fnames): - sub_path = PosixPath(dirpath).joinpath(node) + sub_path = Path(dirpath).joinpath(node) yield (sub_path, swhid_of(sub_path)) async def parse_path( - path: PosixPath, + path: Path, session: aiohttp.ClientSession, api_url: str, exclude_patterns: Iterable[Pattern[str]], @@ -200,7 +200,7 @@ def extract_regex_objs( - root_path: PosixPath, patterns: Iterable[str] + root_path: Path, patterns: Iterable[str] ) -> Iterator[Pattern[str]]: """Generates a regex object for each pattern given in input and checks if the path is a subdirectory or relative to the root path. @@ -210,7 +210,7 @@ """ for pattern in patterns: for path in glob.glob(pattern): - dirpath = PosixPath(path) + dirpath = Path(path) if root_path not in dirpath.parents: error_msg = ( f'The path "{dirpath}" is not a subdirectory or relative ' @@ -234,16 +234,15 @@ sre_patterns = set() if exclude_patterns: sre_patterns = { - reg_obj - for reg_obj in extract_regex_objs(PosixPath(root_path), exclude_patterns) + reg_obj for reg_obj in extract_regex_objs(Path(root_path), exclude_patterns) } - source_tree = Tree(PosixPath(root_path)) + source_tree = Tree(Path(root_path)) loop = asyncio.get_event_loop() loop.run_until_complete(run(config, root_path, source_tree, sre_patterns)) if interactive: - root = PosixPath(root_path) + root = Path(root_path) directories = source_tree.getDirectoriesInfo(root) figure = generate_sunburst(directories, root) run_app(figure, source_tree) diff --git a/swh/scanner/tests/conftest.py b/swh/scanner/tests/conftest.py --- a/swh/scanner/tests/conftest.py +++ b/swh/scanner/tests/conftest.py @@ -9,7 +9,7 @@ import os import shutil -from pathlib import PosixPath +from pathlib import Path from aioresponses import aioresponses # type: ignore from swh.model.cli import swhid_of_file, swhid_of_dir @@ -125,10 +125,10 @@ @pytest.fixture def test_sample_folder(datadir, tmp_path): """Location of the "data" folder """ - archive_path = PosixPath(os.path.join(datadir, "sample-folder.tgz")) + archive_path = Path(os.path.join(datadir, "sample-folder.tgz")) assert archive_path.exists() shutil.unpack_archive(archive_path, extract_dir=tmp_path) - test_sample_folder = PosixPath(os.path.join(tmp_path, "sample-folder")) + test_sample_folder = Path(os.path.join(tmp_path, "sample-folder")) assert test_sample_folder.exists() return test_sample_folder