-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathpath_utils.py
More file actions
43 lines (32 loc) · 1.17 KB
/
path_utils.py
File metadata and controls
43 lines (32 loc) · 1.17 KB
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
36
37
38
39
40
41
42
43
"""Path helpers for running python code within Bazel."""
import os
from pathlib import Path
def runfiles_dir() -> Path:
"""Returns the runfiles directory for the current Bazel build."""
...
def bazel_root() -> Path:
"""
Returns the location of MODULE.bazel file.
TODO: which one?
Only works when called from bazel.
"""
env_root = os.getenv("BUILD_WORKSPACE_DIRECTORY")
if env_root:
return Path(env_root).resolve()
else:
return None
def git_root() -> Path:
"""Returns a path to the git repository."""
return _find_upwards(Path(__file__).resolve(), marker=".git")
def cwd() -> Path:
"""Returns the current working directory = invocation directory."""
return Path(os.getenv("BUILD_WORKING_DIRECTORY") or os.getcwd()).resolve()
def _find_upwards(start: Path, marker: str) -> Path:
"""
Walks up from `start` to find a directory containing `marker`.
Raises FileNotFoundError if not found.
"""
for parent in [start] + list(start.parents):
if (parent / marker).exists():
return parent
raise FileNotFoundError(f"Could not find '{marker}' in any parent directory of {start}")