16
16
17
17
18
18
import functools
19
+ import os
19
20
import pathlib
20
21
import shutil
21
22
import subprocess
@@ -79,7 +80,6 @@ class _TrainingScriptPythonPackager:
79
80
80
81
_TRAINER_FOLDER = "trainer"
81
82
_ROOT_MODULE = "aiplatform_custom_trainer_script"
82
- _TASK_MODULE_NAME = "task"
83
83
_SETUP_PY_VERSION = "0.1"
84
84
85
85
_SETUP_PY_TEMPLATE = """from setuptools import find_packages
@@ -96,10 +96,12 @@ class _TrainingScriptPythonPackager:
96
96
97
97
_SETUP_PY_SOURCE_DISTRIBUTION_CMD = "setup.py sdist --formats=gztar"
98
98
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
+ ):
103
105
"""Initializes packager.
104
106
105
107
Args:
@@ -109,8 +111,14 @@ def __init__(self, script_path: str, requirements: Optional[Sequence[str]] = Non
109
111
"""
110
112
111
113
self .script_path = script_path
114
+ self .task_module_name = task_module_name
112
115
self .requirements = requirements or []
113
116
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
+
114
122
def make_package (self , package_directory : str ) -> str :
115
123
"""Converts script into a Python package suitable for python module
116
124
execution.
@@ -134,9 +142,6 @@ def make_package(self, package_directory: str) -> str:
134
142
# __init__.py path in root module
135
143
init_path = trainer_path / "__init__.py"
136
144
137
- # The module that will contain the script
138
- script_out_path = trainer_path / f"{ self ._TASK_MODULE_NAME } .py"
139
-
140
145
# The path to setup.py in the package.
141
146
setup_py_path = trainer_root_path / "setup.py"
142
147
@@ -165,8 +170,14 @@ def make_package(self, package_directory: str) -> str:
165
170
with setup_py_path .open ("w" ) as fp :
166
171
fp .write (setup_py_output )
167
172
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 )
170
181
171
182
# Run setup.py to create the source distribution.
172
183
setup_cmd = [
0 commit comments