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

Function setup_root

rootutils/rootutils.py:115–156  ·  view source on GitHub ↗

Combines `get_root()` and `set_root()` into one method. Recursively searches for files from the `indicators` list, starting from given path. Args: search_from (str): Path to file or folder to start search from. indicator (Union[str, Iterable[str]], optional): List of filena

(
    search_from: Union[str, Path],
    indicator: Union[str, Iterable[str]] = (
        ".project-root",
        "setup.cfg",
        "setup.py",
        ".git",
        "pyproject.toml",
    ),
    project_root_env_var: bool = True,
    dotenv: bool = True,
    pythonpath: bool = False,
    cwd: bool = False,
)

Source from the content-addressed store, hash-verified

113
114
115def setup_root(
116 search_from: Union[str, Path],
117 indicator: Union[str, Iterable[str]] = (
118 ".project-root",
119 "setup.cfg",
120 "setup.py",
121 ".git",
122 "pyproject.toml",
123 ),
124 project_root_env_var: bool = True,
125 dotenv: bool = True,
126 pythonpath: bool = False,
127 cwd: bool = False,
128) -> Path:
129 """Combines `get_root()` and `set_root()` into one method.
130
131 Recursively searches for files from the `indicators` list, starting from given path.
132
133 Args:
134 search_from (str): Path to file or folder to start search from.
135 indicator (Union[str, Iterable[str]], optional): List of filenames to search for. Finding at least one on these files indicates the project root.
136 project_root_env_var (bool, optional): Whether to set PROJECT_ROOT environment variable.
137 dotenv (bool, optional): Whether to load `.env` file from project root.
138 pythonpath (bool, optional): Whether to add project root to pythonpath.
139 cwd (bool, optional): Whether to set current working directory to project root.
140
141 Raises:
142 TypeError: If any input type is incorrect.
143 FileNotFoundError: If root is not found.
144
145 Returns:
146 Path: Path to project root.
147 """
148 path = find_root(search_from, indicator)
149 set_root(
150 path=path,
151 project_root_env_var=project_root_env_var,
152 dotenv=dotenv,
153 pythonpath=pythonpath,
154 cwd=cwd,
155 )
156 return path
157
158
159def autosetup(

Callers 2

test_setup_rootFunction · 0.90
autosetupFunction · 0.85

Calls 2

find_rootFunction · 0.85
set_rootFunction · 0.85

Tested by 1

test_setup_rootFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…