Files
chia-blockchain/chia/util/path.py
Matt HauffandGitHub 40db4635a8 [LABS-245] Enable PEP604 Ruff rules (#20269)
* Enable PEP604 Ruff rules

* Fix harcoded signature in test

* Hack CLVMStreamable test with note to fast follow
2025-11-18 12:34:00 -08:00

31 lines
683 B
Python

# Package: utils
from __future__ import annotations
import os
from pathlib import Path
def path_from_root(root: Path, path_str: str | Path) -> Path:
"""
If path is relative, prepend root
If path is absolute, return it directly.
"""
root = Path(os.path.expanduser(str(root)))
path = Path(path_str)
if not path.is_absolute():
path = root / path
return path.resolve()
def make_path_relative(path_str: str | Path, root: Path) -> Path:
"""
Try to make the given path relative, given the default root.
"""
path = Path(path_str)
try:
path = path.relative_to(root)
except ValueError:
pass
return path