Skip to content

Commit db68957

Browse files
author
Payton Staub
committed
Fix pylint
1 parent 5b16b4f commit db68957

File tree

3 files changed

+42
-28
lines changed

3 files changed

+42
-28
lines changed

src/sagemaker/workflow/_repack_model.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,16 @@
3333
# repacking is some short-lived hackery, right??
3434
from distutils.dir_util import copy_tree
3535

36+
3637
def repack(inference_script, model_archive, dependencies=None, source_dir=None):
38+
"""Repack custom dependencies and code into an existing model TAR archive
39+
40+
Args:
41+
inference_script (str): The path to the custom entry point.
42+
model_archive (str): The name of the model TAR archive.
43+
dependencies (str): A space-delimited string of paths to custom dependencies.
44+
source_dir (str): The path to a custom source directory.
45+
"""
3746

3847
# the data directory contains a model archive generated by a previous training job
3948
data_directory = "/opt/ml/input/data/training"
@@ -53,7 +62,7 @@ def repack(inference_script, model_archive, dependencies=None, source_dir=None):
5362
with tarfile.open(name=local_path, mode="r:gz") as tf:
5463
tf.extractall(path=src_dir)
5564

56-
# copy the custom inference script to code/
65+
# copy the custom inference script to code/
5766
entry_point = os.path.join("/opt/ml/code", inference_script)
5867
shutil.copy2(entry_point, os.path.join(src_dir, "code", inference_script))
5968

@@ -71,19 +80,23 @@ def repack(inference_script, model_archive, dependencies=None, source_dir=None):
7180

7281
# copy any dependencies to code/lib/
7382
if dependencies:
74-
for dependency in dependencies.split(' '):
83+
for dependency in dependencies.split(" "):
7584
actual_dependency_path = os.path.join("/opt/ml/code", dependency)
7685
lib_dir = os.path.join(code_dir, "lib")
7786
if not os.path.exists(lib_dir):
78-
os.mkdir(lib_dir)
87+
os.mkdir(lib_dir)
7988
if os.path.isdir(actual_dependency_path):
80-
shutil.copytree(actual_dependency_path, os.path.join(lib_dir, os.path.basename(actual_dependency_path)))
89+
shutil.copytree(
90+
actual_dependency_path,
91+
os.path.join(lib_dir, os.path.basename(actual_dependency_path)),
92+
)
8193
else:
8294
shutil.copy2(actual_dependency_path, lib_dir)
8395

8496
# copy the "src" dir, which includes the previous training job's model and the
8597
# custom inference script, to the output of this training job
86-
copy_tree(src_dir, "/opt/ml/model")
98+
copy_tree(src_dir, "/opt/ml/model")
99+
87100

88101
if __name__ == "__main__":
89102
parser = argparse.ArgumentParser()
@@ -96,5 +109,5 @@ def repack(inference_script, model_archive, dependencies=None, source_dir=None):
96109
inference_script=args.inference_script,
97110
dependencies=args.dependencies,
98111
source_dir=args.source_dir,
99-
model_archive=args.model_archive
100-
)
112+
model_archive=args.model_archive,
113+
)

src/sagemaker/workflow/_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def __init__(
148148
# convert dependencies array into space-delimited string
149149
dependencies_hyperparameter = None
150150
if self._dependencies:
151-
dependencies_hyperparameter = ' '.join(self._dependencies)
151+
dependencies_hyperparameter = " ".join(self._dependencies)
152152

153153
# the real estimator and inputs
154154
repacker = SKLearn(
@@ -163,7 +163,7 @@ def __init__(
163163
"inference_script": self._entry_point_basename,
164164
"model_archive": self._model_archive,
165165
"dependencies": dependencies_hyperparameter,
166-
"source_dir": self._source_dir
166+
"source_dir": self._source_dir,
167167
},
168168
subnets=subnets,
169169
security_group_ids=security_group_ids,

tests/unit/sagemaker/workflow/test_repack_model_script.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@
2121
import pytest
2222
import time
2323

24-
@pytest.mark.skip(reason="""This test operates on the root file system
24+
25+
@pytest.mark.skip(
26+
reason="""This test operates on the root file system
2527
and will likely fail due to permission errors.
2628
Temporarily remove this skip decorator and run
27-
the test after making changes to _repack_model.py""")
29+
the test after making changes to _repack_model.py"""
30+
)
2831
def test_repack_entry_point_only(tmp):
2932
model_name = "xg-boost-model"
3033
fake_model_path = os.path.join(tmp, model_name)
@@ -51,19 +54,19 @@ def test_repack_entry_point_only(tmp):
5154
)
5255

5356
# repack
54-
_repack_model.repack(
55-
inference_script="inference.py",
56-
model_archive=model_tar_name
57-
)
57+
_repack_model.repack(inference_script="inference.py", model_archive=model_tar_name)
5858

5959
# /opt/ml/model should now have the original model and the inference script
6060
assert os.path.exists(os.path.join("/opt/ml/model", model_name))
6161
assert os.path.exists(os.path.join("/opt/ml/model/code", "inference.py"))
6262

63-
@pytest.mark.skip(reason="""This test operates on the root file system
63+
64+
@pytest.mark.skip(
65+
reason="""This test operates on the root file system
6466
and will likely fail due to permission errors.
6567
Temporarily remove this skip decorator and run
66-
the test after making changes to _repack_model.py""")
68+
the test after making changes to _repack_model.py"""
69+
)
6770
def test_repack_with_dependencies(tmp):
6871
model_name = "xg-boost-model"
6972
fake_model_path = os.path.join(tmp, model_name)
@@ -84,19 +87,14 @@ def test_repack_with_dependencies(tmp):
8487
# create files that will be added to model.tar.gz
8588
create_file_tree(
8689
"/opt/ml/code",
87-
[
88-
"inference.py",
89-
"dependencies/a",
90-
"bb",
91-
"dependencies/some/dir/b"
92-
],
90+
["inference.py", "dependencies/a", "bb", "dependencies/some/dir/b"],
9391
)
9492

9593
# repack
9694
_repack_model.repack(
9795
inference_script="inference.py",
9896
model_archive=model_tar_name,
99-
dependencies=["dependencies/a", "bb", "dependencies/some/dir"]
97+
dependencies=["dependencies/a", "bb", "dependencies/some/dir"],
10098
)
10199

102100
# /opt/ml/model should now have the original model and the inference script
@@ -106,10 +104,13 @@ def test_repack_with_dependencies(tmp):
106104
assert os.path.exists(os.path.join("/opt/ml/model/code/lib", "bb"))
107105
assert os.path.exists(os.path.join("/opt/ml/model/code/lib/dir", "b"))
108106

109-
@pytest.mark.skip(reason="""This test operates on the root file system
107+
108+
@pytest.mark.skip(
109+
reason="""This test operates on the root file system
110110
and will likely fail due to permission errors.
111111
Temporarily remove this skip decorator and run
112-
the test after making changes to _repack_model.py""")
112+
the test after making changes to _repack_model.py"""
113+
)
113114
def test_repack_with_source_dir_and_dependencies(tmp):
114115
model_name = "xg-boost-model"
115116
fake_model_path = os.path.join(tmp, model_name)
@@ -136,7 +137,7 @@ def test_repack_with_source_dir_and_dependencies(tmp):
136137
"bb",
137138
"dependencies/some/dir/b",
138139
"sourcedir/foo.py",
139-
"sourcedir/some/dir/a"
140+
"sourcedir/some/dir/a",
140141
],
141142
)
142143

@@ -145,7 +146,7 @@ def test_repack_with_source_dir_and_dependencies(tmp):
145146
inference_script="inference.py",
146147
model_archive=model_tar_name,
147148
dependencies=["dependencies/a", "bb", "dependencies/some/dir"],
148-
source_dir="sourcedir"
149+
source_dir="sourcedir",
149150
)
150151

151152
# /opt/ml/model should now have the original model and the inference script

0 commit comments

Comments
 (0)