-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathutils.py
More file actions
105 lines (88 loc) · 3.92 KB
/
utils.py
File metadata and controls
105 lines (88 loc) · 3.92 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from pathlib import Path
from typing import Optional, Tuple
import yaml
from pydantic import ValidationError
from dstack._internal.core.errors import ConfigurationError
from dstack._internal.core.models.configurations import AnyRunConfiguration
from dstack._internal.core.models.configurations import (
parse_run_configuration as parse_configuration,
)
from dstack._internal.core.models.profiles import Profile, ProfilesConfig
from dstack._internal.utils.common import get_dstack_dir
from dstack._internal.utils.path import PathLike, path_in_dir
def load_profile(repo_dir: PathLike, profile_name: Optional[str]) -> Profile:
"""
Loads a repo profile from `.dstack/profiles.yml` or global profile from `~/.dstack/profiles.yml`.
If `profile_name` is not specified, the default profile is loaded.
:param repo_dir: The path to the repository root directory.
:param profile_name: The name of the profile to load.
:return: Loaded profile.
"""
repo_profiles_path = Path(repo_dir) / ".dstack/profiles.yml"
if not repo_profiles_path.exists():
repo_profiles_path = repo_profiles_path.with_suffix(".yaml")
repo_profile = _load_profile_from_path(
profiles_path=repo_profiles_path,
profile_name=profile_name,
)
if repo_profile is not None:
return repo_profile
dstack_dir = get_dstack_dir()
global_profiles_path = dstack_dir / "profiles.yml"
if not global_profiles_path.exists():
global_profiles_path = global_profiles_path.with_suffix(".yaml")
global_profile = _load_profile_from_path(
profiles_path=global_profiles_path,
profile_name=profile_name,
)
if global_profile is not None:
return global_profile
if profile_name is None:
return Profile(name="default")
raise ConfigurationError(f"No such profile: {profile_name}")
def load_configuration(
repo_dir: PathLike,
work_dir: Optional[PathLike] = None,
configuration_file: Optional[PathLike] = None,
) -> Tuple[str, AnyRunConfiguration]:
"""
Loads a configuration from file. If the file is not specified, loads from `.dstack.yml` if working directory.
:param repo_dir: The path to the repository root directory.
:param work_dir: The path to the working directory, relative to the repository root directory.
:param configuration_file: The path to the configuration file, relative to the repository root directory.
:return: Path to the configuration file and loaded configuration.
"""
repo_dir = Path(repo_dir)
work_dir = repo_dir / (work_dir or ".")
if not path_in_dir(work_dir, repo_dir):
raise ConfigurationError("Working directory is outside of the repo")
if configuration_file is None:
configuration_path = work_dir / ".dstack.yml"
if not configuration_path.exists():
configuration_path = configuration_path.with_suffix(".yaml")
else:
configuration_path = repo_dir / configuration_file
if not path_in_dir(configuration_path, repo_dir):
raise ConfigurationError("Configuration file is outside of the repo")
try:
with open(configuration_path, "r") as f:
conf = parse_configuration(yaml.safe_load(f))
except OSError:
raise ConfigurationError(f"Failed to load configuration from {configuration_path}")
return str(configuration_path.relative_to(repo_dir)), conf
def _load_profile_from_path(profiles_path: Path, profile_name: Optional[str]) -> Optional[Profile]:
if not profiles_path.exists():
profiles_path = profiles_path.with_suffix(".yaml")
try:
with profiles_path.open("r") as f:
config = ProfilesConfig.parse_obj(yaml.safe_load(f))
except FileNotFoundError:
return None
except ValidationError as e:
raise ConfigurationError(e)
if profile_name is None:
return config.default()
try:
return config.get(profile_name)
except KeyError:
return None