forked from Chia-Network/chia-blockchain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath.py
35 lines (29 loc) · 867 Bytes
/
path.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import os
from pathlib import Path
from typing import Union
def path_from_root(root: Path, path_str: Union[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 mkdir(path_str: Union[str, Path]) -> None:
"""
Create the existing directory (and its parents) if necessary.
"""
path = Path(path_str)
path.mkdir(parents=True, exist_ok=True)
def make_path_relative(path_str: Union[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