-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathpath_utils.py
64 lines (47 loc) · 1.64 KB
/
path_utils.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""Utility functions related to getting paths to various important places
"""
import os
import sys
# path to the root directory of FATES, based on the path of this file
# it's important that this NOT end with a trailing slash
_FATES_ROOT = os.path.normpath(
os.path.join(os.path.dirname(os.path.abspath(__file__)), os.pardir)
)
def add_cime_lib_to_path() -> str:
"""Adds the CIME python library to the python path, to allow importing
modules from that library
Returns:
str: path to top-level cime directory
"""
cime_path = path_to_cime()
prepend_to_python_path(cime_path)
cime_lib_path = os.path.join(cime_path, "CIME", "Tools")
prepend_to_python_path(cime_lib_path)
return cime_path
def path_to_cime() -> str:
"""Returns the path to cime, if it can be found
Raises:
RuntimeError: can't find path to cime
Returns:
str: full path to cime
"""
cime_path = os.path.join(path_to_fates_root(), "../../cime")
if os.path.isdir(cime_path):
return cime_path
raise RuntimeError("Cannot find cime.")
def path_to_fates_root():
"""Returns Returns the path to the root directory of FATES
Returns:
str: path to the root directory of FATES
"""
return _FATES_ROOT
def prepend_to_python_path(path: str):
"""Adds the given path to python's sys.path if not already there
Path is added near the beginning, so that it takes precedence over existing
entries in path.
Args:
path (str): input path
"""
if not path in sys.path:
# insert at location 1 rather than 0, because 0 is special
sys.path.insert(1, path)