Skip to content

Commit aee0bd8

Browse files
authored
Cleanup init and init_directory usage (#1368)
1 parent 27dced4 commit aee0bd8

File tree

4 files changed

+45
-139
lines changed

4 files changed

+45
-139
lines changed

truss/cli/cli.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,14 @@ def image():
205205
)
206206
@click.option("-n", "--name", type=click.STRING)
207207
@click.option(
208-
"--python-configuration/--no-python-configuration",
208+
"--python-config/--no-python-config",
209209
type=bool,
210210
default=False,
211211
help="Uses the code first tooling to build models.",
212212
)
213213
@log_level_option
214214
@error_handling
215-
def init(target_directory, backend, name, python_configuration) -> None:
215+
def init(target_directory, backend, name, python_config) -> None:
216216
"""Create a new truss.
217217
218218
TARGET_DIRECTORY: A Truss is created in this directory
@@ -232,7 +232,7 @@ def init(target_directory, backend, name, python_configuration) -> None:
232232
target_directory=target_directory,
233233
build_config=build_config,
234234
model_name=model_name,
235-
python_configuration=python_configuration,
235+
python_config=python_config,
236236
)
237237
click.echo(f"Truss {model_name} was created in {tr_path.absolute()}")
238238

truss/tests/conftest.py

+25-33
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
ServingImageBuilderContext,
2121
)
2222
from truss.contexts.local_loader.docker_build_emulator import DockerBuildEmulator
23-
from truss.truss_handle.build import init
23+
from truss.truss_handle.build import init_directory
2424
from truss.truss_handle.truss_handle import TrussHandle
2525

2626
CUSTOM_MODEL_CODE = """
@@ -435,17 +435,15 @@ def dynamic_config_mount_dir(tmp_path, monkeypatch: pytest.MonkeyPatch):
435435
@pytest.fixture
436436
def custom_model_truss_dir_with_pre_and_post_no_example(tmp_path):
437437
dir_path = tmp_path / "custom_truss_with_pre_post_no_example"
438-
handle = init(str(dir_path))
439-
handle.spec.model_class_filepath.write_text(
440-
CUSTOM_MODEL_CODE_WITH_PRE_AND_POST_PROCESS
441-
)
438+
th = TrussHandle(init_directory(dir_path))
439+
th.spec.model_class_filepath.write_text(CUSTOM_MODEL_CODE_WITH_PRE_AND_POST_PROCESS)
442440
yield dir_path
443441

444442

445443
@pytest.fixture
446444
def custom_model_truss_dir_with_hidden_files(tmp_path):
447445
truss_dir_path: Path = tmp_path / "custom_model_truss_dir_with_hidden_files"
448-
_ = init(str(truss_dir_path))
446+
init_directory(truss_dir_path)
449447
(truss_dir_path / "__pycache__").mkdir(parents=True, exist_ok=True)
450448
(truss_dir_path / ".git").mkdir(parents=True, exist_ok=True)
451449
(truss_dir_path / "__pycache__" / "test.cpython-311.pyc").touch()
@@ -458,7 +456,7 @@ def custom_model_truss_dir_with_hidden_files(tmp_path):
458456
@pytest.fixture
459457
def custom_model_truss_dir_with_truss_ignore(tmp_path):
460458
truss_dir_path: Path = tmp_path / "custom_model_truss_dir_with_truss_ignore"
461-
_ = init(str(truss_dir_path))
459+
init_directory(truss_dir_path)
462460
(truss_dir_path / "random_folder_1").mkdir(parents=True, exist_ok=True)
463461
(truss_dir_path / "random_folder_2").mkdir(parents=True, exist_ok=True)
464462
(truss_dir_path / "random_file_1.txt").touch()
@@ -478,19 +476,17 @@ def custom_model_truss_dir_with_truss_ignore(tmp_path):
478476
@pytest.fixture
479477
def custom_model_truss_dir_with_pre_and_post(tmp_path):
480478
dir_path = tmp_path / "custom_truss_with_pre_post"
481-
handle = init(str(dir_path))
482-
handle.spec.model_class_filepath.write_text(
483-
CUSTOM_MODEL_CODE_WITH_PRE_AND_POST_PROCESS
484-
)
485-
handle.update_examples([Example("example1", {"inputs": [[0]]})])
479+
th = TrussHandle(init_directory(dir_path))
480+
th.spec.model_class_filepath.write_text(CUSTOM_MODEL_CODE_WITH_PRE_AND_POST_PROCESS)
481+
th.update_examples([Example("example1", {"inputs": [[0]]})])
486482
yield dir_path
487483

488484

489485
@pytest.fixture
490486
def custom_model_truss_dir_with_bundled_packages(tmp_path):
491487
truss_dir_path: Path = tmp_path / "custom_model_truss_dir_with_bundled_packages"
492-
handle = init(str(truss_dir_path))
493-
handle.spec.model_class_filepath.write_text(CUSTOM_MODEL_CODE_USING_BUNDLED_PACKAGE)
488+
th = TrussHandle(init_directory(truss_dir_path))
489+
th.spec.model_class_filepath.write_text(CUSTOM_MODEL_CODE_USING_BUNDLED_PACKAGE)
494490
packages_path = truss_dir_path / DEFAULT_BUNDLED_PACKAGES_DIR / "test_package"
495491
packages_path.mkdir(parents=True)
496492
with (packages_path / "test.py").open("w") as file:
@@ -501,11 +497,9 @@ def custom_model_truss_dir_with_bundled_packages(tmp_path):
501497
@pytest.fixture
502498
def custom_model_truss_dir_with_pre_and_post_str_example(tmp_path):
503499
dir_path = tmp_path / "custom_truss_with_pre_post_str_example"
504-
handle = init(str(dir_path))
505-
handle.spec.model_class_filepath.write_text(
506-
CUSTOM_MODEL_CODE_WITH_PRE_AND_POST_PROCESS
507-
)
508-
handle.update_examples(
500+
th = TrussHandle(init_directory(dir_path))
501+
th.spec.model_class_filepath.write_text(CUSTOM_MODEL_CODE_WITH_PRE_AND_POST_PROCESS)
502+
th.update_examples(
509503
[
510504
Example(
511505
"example1",
@@ -525,29 +519,27 @@ def custom_model_truss_dir_with_pre_and_post_str_example(tmp_path):
525519
@pytest.fixture
526520
def custom_model_truss_dir_with_pre_and_post_description(tmp_path):
527521
dir_path = tmp_path / "custom_truss_with_pre_post"
528-
handle = init(str(dir_path))
529-
handle.spec.model_class_filepath.write_text(
530-
CUSTOM_MODEL_CODE_WITH_PRE_AND_POST_PROCESS
531-
)
532-
handle.update_description("This model adds 3 to all inputs")
522+
th = TrussHandle(init_directory(dir_path))
523+
th.spec.model_class_filepath.write_text(CUSTOM_MODEL_CODE_WITH_PRE_AND_POST_PROCESS)
524+
th.update_description("This model adds 3 to all inputs")
533525
yield dir_path
534526

535527

536528
@pytest.fixture
537529
def custom_model_truss_dir_for_gpu(tmp_path):
538530
dir_path = tmp_path / "custom_truss"
539-
handle = init(str(dir_path))
540-
handle.enable_gpu()
541-
handle.spec.model_class_filepath.write_text(CUSTOM_MODEL_CODE_FOR_GPU_TESTING)
531+
th = TrussHandle(init_directory(dir_path))
532+
th.enable_gpu()
533+
th.spec.model_class_filepath.write_text(CUSTOM_MODEL_CODE_FOR_GPU_TESTING)
542534
yield dir_path
543535

544536

545537
@pytest.fixture
546538
def custom_model_truss_dir_for_secrets(tmp_path):
547539
dir_path = tmp_path / "custom_truss"
548-
handle = init(str(dir_path))
549-
handle.add_secret("secret_name", "default_secret_value")
550-
handle.spec.model_class_filepath.write_text(CUSTOM_MODEL_CODE_FOR_SECRETS_TESTING)
540+
th = TrussHandle(init_directory(dir_path))
541+
th.add_secret("secret_name", "default_secret_value")
542+
th.spec.model_class_filepath.write_text(CUSTOM_MODEL_CODE_FOR_SECRETS_TESTING)
551543
yield dir_path
552544

553545

@@ -624,10 +616,10 @@ def _custom_model_from_code(
624616
where_dir: Path, truss_name: str, model_code: str, handle_ops: callable = None
625617
) -> Path:
626618
dir_path = where_dir / truss_name
627-
handle = init(str(dir_path))
619+
th = TrussHandle(init_directory(dir_path))
628620
if handle_ops is not None:
629-
handle_ops(handle)
630-
handle.spec.model_class_filepath.write_text(model_code)
621+
handle_ops(th)
622+
th.spec.model_class_filepath.write_text(model_code)
631623
return dir_path
632624

633625

truss/tests/test_build.py

+3-52
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,18 @@
1-
from pathlib import Path
2-
31
from truss.base.truss_spec import TrussSpec
4-
from truss.truss_handle.build import init, init_directory, load
2+
from truss.truss_handle.build import init_directory, load
53
from truss_chains.deployment import code_gen
64

75

86
def test_truss_init(tmp_path):
9-
dir_name = str(tmp_path)
10-
init(dir_name)
11-
spec = TrussSpec(Path(dir_name))
7+
spec = TrussSpec(init_directory(tmp_path))
128
assert spec.model_module_dir.exists()
139
assert spec.data_dir.exists()
1410
assert spec.truss_dir == tmp_path
1511
assert spec.config_path.exists()
1612

1713

18-
def test_truss_init_with_data_file_and_requirements_file_and_bundled_packages(tmp_path):
19-
dir_path = tmp_path / "truss"
20-
dir_name = str(dir_path)
21-
22-
# Init data files
23-
data_path = tmp_path / "data.txt"
24-
with data_path.open("w") as data_file:
25-
data_file.write("test")
26-
27-
# Init requirements file
28-
req_file_path = tmp_path / "requirements.txt"
29-
requirements = ["tensorflow==2.3.1", "uvicorn==0.12.2"]
30-
with req_file_path.open("w") as req_file:
31-
for req in requirements:
32-
req_file.write(f"{req}\n")
33-
34-
# init bundled packages
35-
packages_path = tmp_path / "dep_pkg"
36-
packages_path.mkdir()
37-
packages_path_file_py = packages_path / "file.py"
38-
packages_path_init_py = packages_path / "__init__.py"
39-
pkg_files = [packages_path_init_py, packages_path_file_py]
40-
for pkg_file in pkg_files:
41-
with pkg_file.open("w") as fh:
42-
fh.write("test")
43-
44-
init(
45-
dir_name,
46-
data_files=[str(data_path)],
47-
requirements_file=str(req_file_path),
48-
bundled_packages=[str(packages_path)],
49-
)
50-
spec = TrussSpec(Path(dir_name))
51-
assert spec.model_module_dir.exists()
52-
assert spec.truss_dir == dir_path
53-
assert spec.config_path.exists()
54-
assert spec.data_dir.exists()
55-
assert spec.bundled_packages_dir.exists()
56-
assert (spec.data_dir / "data.txt").exists()
57-
assert spec.requirements == requirements
58-
assert (spec.bundled_packages_dir / "dep_pkg" / "__init__.py").exists()
59-
assert (spec.bundled_packages_dir / "dep_pkg" / "file.py").exists()
60-
61-
6214
def test_truss_init_with_python_dx(tmp_path):
63-
dir_name = str(tmp_path)
64-
init_directory(dir_name, model_name="Test Model Name", python_configuration=True)
15+
init_directory(tmp_path, model_name="Test Model Name", python_config=True)
6516

6617
generated_truss_dir = code_gen.gen_truss_model_from_source(tmp_path / "my_model.py")
6718
truss_handle = load(generated_truss_dir)

truss/truss_handle/build.py

+14-51
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import os
33
import sys
44
from pathlib import Path
5-
from typing import List, Optional, Union
5+
from typing import Optional, Union
66

77
import yaml
88

@@ -61,11 +61,21 @@ def _populate_python_dx_target_directory(config: TrussConfig, dir_path: Path):
6161

6262

6363
def init_directory(
64-
target_directory: str,
64+
target_directory: Union[str, Path],
6565
build_config: Optional[Build] = None,
6666
model_name: Optional[str] = None,
67-
python_configuration: bool = False,
67+
python_config: bool = False,
6868
) -> Path:
69+
"""
70+
Initialize an empty placeholder Truss. A Truss is a build context designed
71+
to be built as a container locally or uploaded into a baseten serving
72+
environment. This placeholder structure can be filled to represent ML
73+
models.
74+
75+
Args:
76+
target_directory: Absolute or relative path of the directory to create
77+
Truss in. The directory is created if it doesn't exist.
78+
"""
6979
config = TrussConfig(
7080
model_name=model_name, python_version=map_local_to_supported_python_version()
7181
)
@@ -76,7 +86,7 @@ def init_directory(
7686
target_directory_path = Path(target_directory)
7787
target_directory_path.mkdir(parents=True, exist_ok=True)
7888

79-
if not python_configuration:
89+
if not python_config:
8090
_populate_traditional_target_directory(
8191
config=config, dir_path=target_directory_path
8292
)
@@ -88,35 +98,6 @@ def init_directory(
8898
return target_directory_path
8999

90100

91-
def init(
92-
target_directory: str,
93-
data_files: Optional[List[str]] = None,
94-
requirements_file: Optional[str] = None,
95-
bundled_packages: Optional[List[str]] = None,
96-
build_config: Optional[Build] = None,
97-
model_name: Optional[str] = None,
98-
) -> TrussHandle:
99-
"""
100-
Initialize an empty placeholder Truss. A Truss is a build context designed
101-
to be built as a container locally or uploaded into a baseten serving
102-
environment. This placeholder structure can be filled to represent ML
103-
models.
104-
105-
Args:
106-
target_directory: Absolute or relative path of the directory to create
107-
Truss in. The directory is created if it doesn't exist.
108-
"""
109-
target_path = init_directory(
110-
target_directory=target_directory,
111-
build_config=build_config,
112-
model_name=model_name,
113-
)
114-
115-
th = TrussHandle(target_path)
116-
_update_truss_props(th, data_files, requirements_file, bundled_packages)
117-
return th
118-
119-
120101
def load(truss_directory: Union[str, Path]) -> TrussHandle:
121102
"""Get a handle to a Truss. A Truss is a build context designed to be built
122103
as a container locally or uploaded into a model serving environment.
@@ -139,21 +120,3 @@ def cleanup() -> None:
139120
if (not obj.name == "config.yaml") and (obj.is_file()):
140121
os.remove(obj)
141122
return
142-
143-
144-
def _update_truss_props(
145-
scaf: TrussHandle,
146-
data_files: Optional[List[str]] = None,
147-
requirements_file: Optional[str] = None,
148-
bundled_packages: Optional[List[str]] = None,
149-
) -> None:
150-
if data_files is not None:
151-
for data_file in data_files:
152-
scaf.add_data(data_file)
153-
154-
if bundled_packages is not None:
155-
for package in bundled_packages:
156-
scaf.add_bundled_package(package)
157-
158-
if requirements_file is not None:
159-
scaf.update_requirements_from_file(requirements_file)

0 commit comments

Comments
 (0)