Repository navigation
Expand file tree
/
Copy pathutils.py
More file actions
278 lines (230 loc) · 10.1 KB
/
Copy pathutils.py
File metadata and controls
278 lines (230 loc) · 10.1 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
"""Utils module."""
from __future__ import absolute_import
import os
import json
import subprocess
import tempfile
from pathlib import Path
from datetime import datetime
from typing import Literal, Any
from sagemaker.core.git_utils import _redact_credentials_from_url
from sagemaker.core.shapes import Unassigned
from sagemaker.core.modules import logger
def _is_valid_s3_uri(path: str, path_type: Literal["File", "Directory", "Any"] = "Any") -> bool:
"""Check if the path is a valid S3 URI.
This method checks if the path is a valid S3 URI. If the path_type is specified,
it will also check if the path is a file or a directory.
This method does not check if the S3 bucket or object exists.
Args:
path (str): S3 URI to validate
path_type (Optional(Literal["File", "Directory", "Any"])): The type of the path to validate.
Defaults to "Any".
Returns:
bool: True if the path is a valid S3 URI, False otherwise
"""
# Check if the path is a valid S3 URI
if not path.startswith("s3://"):
return False
if path_type == "File":
# If it's a file, it should not end with a slash
return not path.endswith("/")
if path_type == "Directory":
# If it's a directory, it should end with a slash
return path.endswith("/")
return path_type == "Any"
def _is_valid_path(path: str, path_type: Literal["File", "Directory", "Any"] = "Any") -> bool:
"""Check if the path is a valid local path.
Args:
path (str): Local path to validate
path_type (Optional(Literal["File", "Directory", "Any"])): The type of the path to validate.
Defaults to "Any".
Returns:
bool: True if the path is a valid local path, False otherwise
"""
if not os.path.exists(path):
return False
if path_type == "File":
return os.path.isfile(path)
if path_type == "Directory":
return os.path.isdir(path)
return path_type == "Any"
def _get_unique_name(base, max_length=63):
"""Generate a unique name based on the base name.
This method generates a unique name based on the base name.
The unique name is generated by appending the current timestamp
to the base name.
Args:
base (str): The base name to use
max_length (int): The maximum length of the unique name. Defaults to 63.
Returns:
str: The unique name
"""
current_time = datetime.now().strftime("%Y%m%d%H%M%S")
base = base.replace("_", "-")
unique_name = f"{base}-{current_time}"
unique_name = unique_name[:max_length] # Truncate to max_length
return unique_name
def _get_repo_name_from_image(image: str) -> str:
"""Get the repository name from the image URI.
Example:
``` python
_get_repo_name_from_image("123456789012.dkr.ecr.us-west-2.amazonaws.com/my-repo:latest")
# Returns "my-repo"
```
Args:
image (str): The image URI
Returns:
str: The repository name
"""
return image.split("/")[-1].split(":")[0]
def convert_unassigned_to_none(instance) -> Any:
"""Convert Unassigned values to None for any instance."""
for name, value in instance.__dict__.items():
if isinstance(value, Unassigned):
setattr(instance, name, None)
return instance
def safe_serialize(data):
"""Serialize the data without wrapping strings in quotes.
This function handles the following cases:
1. If `data` is a string, it returns the string as-is without wrapping in quotes.
2. If `data` is serializable (e.g., a dictionary, list, int, float), it returns
the JSON-encoded string using `json.dumps()`.
3. If `data` cannot be serialized (e.g., a custom object), it returns the string
representation of the data using `str(data)`.
Args:
data (Any): The data to serialize.
Returns:
str: The serialized JSON-compatible string or the string representation of the input.
"""
if isinstance(data, str):
return data
try:
return json.dumps(data)
except TypeError:
return str(data)
def _run_clone_command_silent(repo_url, dest_dir):
"""Run the 'git clone' command with the repo url and the directory to clone the repo into.
Args:
repo_url (str): Git repo url to be cloned.
dest_dir: (str): Local path where the repo should be cloned into.
Raises:
CalledProcessError: If failed to clone git repo.
"""
my_env = os.environ.copy()
if repo_url.startswith("https://"):
try:
my_env["GIT_TERMINAL_PROMPT"] = "0"
subprocess.check_call(
["git", "clone", repo_url, dest_dir],
env=my_env,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
except subprocess.CalledProcessError as e:
safe_url = _redact_credentials_from_url(repo_url)
error = subprocess.CalledProcessError(
e.returncode,
["git", "clone", safe_url, dest_dir],
output=e.output,
stderr=e.stderr,
)
logger.error(f"Failed to clone repository: {safe_url}")
logger.error(f"Error output:\n{error}")
raise error from None
elif repo_url.startswith("git@") or repo_url.startswith("ssh://"):
try:
with tempfile.TemporaryDirectory() as tmp_dir:
custom_ssh_executable = Path(tmp_dir) / "ssh_batch"
with open(custom_ssh_executable, "w") as pipe:
print("#!/bin/sh", file=pipe)
print("ssh -oBatchMode=yes $@", file=pipe)
os.chmod(custom_ssh_executable, 0o511)
my_env["GIT_SSH"] = str(custom_ssh_executable)
subprocess.check_call(
["git", "clone", repo_url, dest_dir],
env=my_env,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
except subprocess.CalledProcessError as e:
del my_env["GIT_SSH"]
logger.error(f"Failed to clone repository: {repo_url}")
logger.error(f"Error output:\n{e}")
raise
def validate_instance_preferences(compute) -> None:
"""Client-side validation for Compute.instance_preferences (server remains the source of truth).
- instance_preferences is mutually exclusive with the classic
single-cluster fields instance_type / instance_groups /
instance_placement_config, and with managed spot training.
- Instance types must not repeat across preferences.
- Count mode: exactly one of the top-level instance_count (applies to
whichever preference wins) with no per-preference instance_count, or an
instance_count on EVERY element with the top-level unset. Both-set,
partial, and neither are rejected.
- Training plans: the top-level (whole-job) training_plan_arn is mutually
exclusive with per-preference training_plan_arns.
List-size limits (max preferences, max plans per preference) are
deliberately NOT enforced client-side: they are server-side configurable,
so raising them must not require a new SDK release.
No-op when instance_preferences is not set.
"""
preferences = getattr(compute, "instance_preferences", None)
if not preferences or isinstance(preferences, Unassigned):
return
def _value(obj, field):
value = getattr(obj, field, None)
if value is None or isinstance(value, Unassigned):
return None
return value
for field in ("instance_type", "instance_groups", "instance_placement_config"):
if _value(compute, field) is not None:
raise ValueError(
f"instance_preferences is mutually exclusive with {field}; "
"specify either a single fixed cluster or instance_preferences, not both."
)
# Spot is a bool: only an explicit True conflicts (False/None is the default).
if _value(compute, "enable_managed_spot_training") is True:
raise ValueError(
"instance_preferences is mutually exclusive with managed spot training "
"(enable_managed_spot_training=True)."
)
instance_types = [_value(p, "instance_type") for p in preferences]
duplicates = sorted(
{t for t in instance_types if t is not None and instance_types.count(t) > 1}
)
if duplicates:
raise ValueError(
"instance_preferences must not contain duplicate instance types: " f"{duplicates}."
)
per_preference_counts = [_value(p, "instance_count") is not None for p in preferences]
if _value(compute, "instance_count") is not None:
if any(per_preference_counts):
raise ValueError(
"The top-level instance_count and per-preference instance_count are "
"mutually exclusive; set the top-level instance_count (applies to "
"whichever preference wins) or an instance_count on every element of "
"instance_preferences, not both."
)
elif not all(per_preference_counts):
raise ValueError(
"When the top-level instance_count is not set, every element of "
"instance_preferences must set its own instance_count."
)
per_preference_plans = [_value(p, "training_plan_arns") for p in preferences]
if _value(compute, "training_plan_arn") is not None and any(per_preference_plans):
raise ValueError(
"The top-level (whole-job) training_plan_arn and per-preference "
"training_plan_arns are mutually exclusive; set one or the other, not both."
)