MCPcopy Create free account
hub / github.com/ashleve/rootutils / find_root

Function find_root

rootutils/rootutils.py:30–72  ·  view source on GitHub ↗

Recursively searches for project root indicator(s), starting from given path. Args: search_from (str): Path to folder or file to start search from. indicator (Union[str, Iterable[str]], optional): List of filenames to search for. Finding at least one on these files indicates the

(
    search_from: Union[str, Path] = ".",
    indicator: Union[str, Iterable[str]] = (
        ".project-root",
        "setup.cfg",
        "setup.py",
        ".git",
        "pyproject.toml",
    ),
)

Source from the content-addressed store, hash-verified

28
29
30def find_root(
31 search_from: Union[str, Path] = ".",
32 indicator: Union[str, Iterable[str]] = (
33 ".project-root",
34 "setup.cfg",
35 "setup.py",
36 ".git",
37 "pyproject.toml",
38 ),
39) -> Path:
40 """Recursively searches for project root indicator(s), starting from given path.
41
42 Args:
43 search_from (str): Path to folder or file to start search from.
44 indicator (Union[str, Iterable[str]], optional): List of filenames to search for. Finding at least one on these files indicates the project root.
45
46 Raises:
47 TypeError: If any input type is incorrect.
48 FileNotFoundError: If root is not found.
49
50 Returns:
51 Path: Path to project root.
52 """
53 if not isinstance(search_from, (str, Path)):
54 raise TypeError("search_from must be either a string or pathlib object.")
55
56 search_from = Path(search_from).resolve()
57
58 if isinstance(indicator, str):
59 indicator = [indicator]
60
61 if not search_from.exists():
62 raise FileNotFoundError("search_from path does not exist.")
63
64 if not hasattr(indicator, "__iter__") or not all(isinstance(i, str) for i in indicator):
65 raise TypeError("indicator must be a string or list of strings.")
66
67 path = _rootutils_recursive_search(search_from, indicator)
68
69 if not path or not path.exists():
70 raise FileNotFoundError(f"Project root directory not found. Indicators: {indicator}")
71
72 return path
73
74
75def set_root(

Callers 3

test_find_rootFunction · 0.90
test_set_rootFunction · 0.90
setup_rootFunction · 0.85

Calls 1

Tested by 2

test_find_rootFunction · 0.72
test_set_rootFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…