Skip to content

Commit 639750f

Browse files
committed
_TrainingScriptPythonPackager to support folders
Allow _TrainingScriptPythonPackager to support a folder in addition to a single script.
1 parent d72a254 commit 639750f

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

google/cloud/aiplatform/utils/source_utils.py

+21-10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717

1818
import functools
19+
import os
1920
import pathlib
2021
import shutil
2122
import subprocess
@@ -79,7 +80,6 @@ class _TrainingScriptPythonPackager:
7980

8081
_TRAINER_FOLDER = "trainer"
8182
_ROOT_MODULE = "aiplatform_custom_trainer_script"
82-
_TASK_MODULE_NAME = "task"
8383
_SETUP_PY_VERSION = "0.1"
8484

8585
_SETUP_PY_TEMPLATE = """from setuptools import find_packages
@@ -96,10 +96,12 @@ class _TrainingScriptPythonPackager:
9696

9797
_SETUP_PY_SOURCE_DISTRIBUTION_CMD = "setup.py sdist --formats=gztar"
9898

99-
# Module name that can be executed during training. ie. python -m
100-
module_name = f"{_ROOT_MODULE}.{_TASK_MODULE_NAME}"
101-
102-
def __init__(self, script_path: str, requirements: Optional[Sequence[str]] = None):
99+
def __init__(
100+
self,
101+
script_path: str,
102+
task_module_name: str = "task",
103+
requirements: Optional[Sequence[str]] = None,
104+
):
103105
"""Initializes packager.
104106
105107
Args:
@@ -109,8 +111,14 @@ def __init__(self, script_path: str, requirements: Optional[Sequence[str]] = Non
109111
"""
110112

111113
self.script_path = script_path
114+
self.task_module_name = task_module_name
112115
self.requirements = requirements or []
113116

117+
@property
118+
def module_name(self) -> str:
119+
# Module name that can be executed during training. ie. python -m
120+
return f"{self._ROOT_MODULE}.{self.task_module_name}"
121+
114122
def make_package(self, package_directory: str) -> str:
115123
"""Converts script into a Python package suitable for python module
116124
execution.
@@ -134,9 +142,6 @@ def make_package(self, package_directory: str) -> str:
134142
# __init__.py path in root module
135143
init_path = trainer_path / "__init__.py"
136144

137-
# The module that will contain the script
138-
script_out_path = trainer_path / f"{self._TASK_MODULE_NAME}.py"
139-
140145
# The path to setup.py in the package.
141146
setup_py_path = trainer_root_path / "setup.py"
142147

@@ -165,8 +170,14 @@ def make_package(self, package_directory: str) -> str:
165170
with setup_py_path.open("w") as fp:
166171
fp.write(setup_py_output)
167172

168-
# Copy script as module of python package.
169-
shutil.copy(self.script_path, script_out_path)
173+
if os.path.isdir(self.script_path):
174+
shutil.copytree(self.script_path, trainer_path, dirs_exist_ok=True)
175+
else:
176+
# The module that will contain the script
177+
script_out_path = trainer_path / f"{self.task_module_name}.py"
178+
179+
# Copy script as module of python package.
180+
shutil.copy(self.script_path, script_out_path)
170181

171182
# Run setup.py to create the source distribution.
172183
setup_cmd = [

0 commit comments

Comments
 (0)