-
Notifications
You must be signed in to change notification settings - Fork 449
Expand file tree
/
Copy pathsource_utils.py
More file actions
264 lines (213 loc) · 8.56 KB
/
source_utils.py
File metadata and controls
264 lines (213 loc) · 8.56 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
# -*- coding: utf-8 -*-
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License 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.
#
import functools
import os
import pathlib
import shutil
import subprocess
import sys
import tempfile
from typing import Optional, Sequence, Callable
from google.auth import credentials as auth_credentials
from google.cloud.aiplatform import base
from google.cloud.aiplatform import utils
_LOGGER = base.Logger(__name__)
def _get_python_executable() -> str:
"""Returns Python executable.
Returns:
Python executable to use for setuptools packaging.
Raises:
EnvironmentError: If Python executable is not found.
"""
python_executable = sys.executable
if not python_executable:
raise EnvironmentError("Cannot find Python executable for packaging.")
return python_executable
class _TrainingScriptPythonPackager:
"""Converts a Python script into Python package suitable for aiplatform
training.
Copies the script to specified location.
Class Attributes:
_TRAINER_FOLDER: Constant folder name to build package.
_ROOT_MODULE: Constant root name of module.
_TEST_MODULE_NAME: Constant name of module that will store script.
_SETUP_PY_VERSION: Constant version of this created python package.
_SETUP_PY_TEMPLATE: Constant template used to generate setup.py file.
_SETUP_PY_SOURCE_DISTRIBUTION_CMD:
Constant command to generate the source distribution package.
Attributes:
script_path: local path of script or folder to package
requirements: list of Python dependencies to add to package
Usage:
packager = TrainingScriptPythonPackager('my_script.py', ['pandas', 'pytorch'])
gcs_path = packager.package_and_copy_to_gcs(
gcs_staging_dir='my-bucket',
project='my-project')
module_name = packager.module_name
The package after installed can be executed as:
python -m aiplatform_custom_trainer_script.task
"""
_TRAINER_FOLDER = "trainer"
_ROOT_MODULE = "aiplatform_custom_trainer_script"
_SETUP_PY_VERSION = "0.1"
_SETUP_PY_TEMPLATE = """from setuptools import find_packages
from setuptools import setup
setup(
name='{name}',
version='{version}',
packages=find_packages(),
install_requires=({requirements}),
include_package_data=True,
description='My training application.'
)"""
_SETUP_PY_SOURCE_DISTRIBUTION_CMD = "setup.py sdist --formats=gztar"
def __init__(
self,
script_path: str,
task_module_name: str = "task",
requirements: Optional[Sequence[str]] = None,
):
"""Initializes packager.
Args:
script_path (str): Required. Local path to script.
requirements (Sequence[str]):
List of python packages dependencies of script.
"""
self.script_path = script_path
self.task_module_name = task_module_name
self.requirements = requirements or []
@property
def module_name(self) -> str:
# Module name that can be executed during training. ie. python -m
return f"{self._ROOT_MODULE}.{self.task_module_name}"
def make_package(self, package_directory: str) -> str:
"""Converts script into a Python package suitable for python module
execution.
Args:
package_directory (str): Directory to build package in.
Returns:
source_distribution_path (str): Path to built package.
Raises:
RunTimeError: If package creation fails.
"""
# The root folder to builder the package in
package_path = pathlib.Path(package_directory)
# Root directory of the package
trainer_root_path = package_path / self._TRAINER_FOLDER
# The root module of the python package
trainer_path = trainer_root_path / self._ROOT_MODULE
# __init__.py path in root module
init_path = trainer_path / "__init__.py"
# The path to setup.py in the package.
setup_py_path = trainer_root_path / "setup.py"
# The path to the generated source distribution.
source_distribution_path = (
trainer_root_path
/ "dist"
/ f"{self._ROOT_MODULE}-{self._SETUP_PY_VERSION}.tar.gz"
)
trainer_root_path.mkdir()
trainer_path.mkdir()
# Make empty __init__.py
with init_path.open("w"):
pass
# Format the setup.py file.
setup_py_output = self._SETUP_PY_TEMPLATE.format(
name=self._ROOT_MODULE,
requirements=",".join(f'"{r}"' for r in self.requirements),
version=self._SETUP_PY_VERSION,
)
# Write setup.py
with setup_py_path.open("w") as fp:
fp.write(setup_py_output)
if os.path.isdir(self.script_path):
# Remove destination path if it already exists
shutil.rmtree(trainer_path)
# Copy folder recursively
shutil.copytree(src=self.script_path, dst=trainer_path)
else:
# The module that will contain the script
script_out_path = trainer_path / f"{self.task_module_name}.py"
# Copy script as module of python package.
shutil.copy(self.script_path, script_out_path)
# Ensure setuptools is installed
install_setuptools_cmd = ["uv", "pip", "install", "setuptools"]
p_install = subprocess.Popen(
args=install_setuptools_cmd,
cwd=trainer_root_path,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
output, error = p_install.communicate()
if p_install.returncode != 0:
raise RuntimeError(
"Failed to install setuptools, code %d\n%s \n%s"
% (p_install.returncode, output.decode(), error.decode())
)
# Run setup.py to create the source distribution.
setup_cmd = [
_get_python_executable()
] + self._SETUP_PY_SOURCE_DISTRIBUTION_CMD.split()
p = subprocess.Popen(
args=setup_cmd,
cwd=trainer_root_path,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
output, error = p.communicate()
# Raise informative error if packaging fails.
if p.returncode != 0:
raise RuntimeError(
"Packaging of training script failed with code %d\n%s \n%s"
% (p.returncode, output.decode(), error.decode())
)
return str(source_distribution_path)
def package_and_copy(self, copy_method: Callable[[str], str]) -> str:
"""Packages the script and executes copy with given copy_method.
Args:
copy_method Callable[[str], str]
Takes a string path, copies to a desired location, and returns the
output path location.
Returns:
output_path str: Location of copied package.
"""
with tempfile.TemporaryDirectory() as tmpdirname:
source_distribution_path = self.make_package(tmpdirname)
output_location = copy_method(source_distribution_path)
_LOGGER.info("Training script copied to:\n%s." % output_location)
return output_location
def package_and_copy_to_gcs(
self,
gcs_staging_dir: str,
project: str = None,
credentials: Optional[auth_credentials.Credentials] = None,
) -> str:
"""Packages script in Python package and copies package to GCS bucket.
Args
gcs_staging_dir (str): Required. GCS Staging directory.
project (str): Required. Project where GCS Staging bucket is located.
credentials (auth_credentials.Credentials):
Optional credentials used with GCS client.
Returns:
GCS location of Python package.
"""
copy_method = functools.partial(
utils._timestamped_copy_to_gcs,
gcs_dir=gcs_staging_dir,
project=project,
credentials=credentials,
)
return self.package_and_copy(copy_method=copy_method)