Source code for simstack.util.project_root_finder
import os
from pathlib import Path
[docs]
def find_project_root(current_file=None, marker_files=(".git", "simstack.toml", "setup.py"),
skip_files=("simstack_marker.com",)) -> Path:
"""
Find the project root directory by searching for common marker files
Args:
skip_files: directories with these names will be skipped
current_file: Path to the current file, defaults to __file__ if None
marker_files: Tuple of files/directories that indicate the project root
Returns:
Absolute path to the project root directory
"""
if "SIMSTACK_PROJECT_ROOT" in os.environ:
return Path(os.environ["SIMSTACK_PROJECT_ROOT"]).resolve()
if current_file is None:
current_file = __file__
# Get the directory from where the process was started
current_dir = Path.cwd()
# Walk up the directory tree until we find a marker file
prev_dir = None
while current_dir != prev_dir:
# Check if any marker files/directories exist in the current directory
found_marker = any((current_dir / marker).exists() for marker in marker_files)
found_skip = any((current_dir / marker).exists() for marker in skip_files)
if found_marker and not found_skip:
return current_dir
# Move up one directory
prev_dir = current_dir
current_dir = current_dir.parent
# If we can't find any markers, return the directory of the current file
return Path(current_file).resolve().parent