Skip to content

fix: make repack_model only removes py file when new entry_point provided #1352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/sagemaker/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,9 @@ def _create_or_update_code_dir(
"""
code_dir = os.path.join(model_dir, "code")
if os.path.exists(code_dir):
shutil.rmtree(code_dir, ignore_errors=True)
for filename in os.listdir(code_dir):
if filename.endswith(".py"):
os.remove(os.path.join(code_dir, filename))
if source_directory and source_directory.lower().startswith("s3://"):
local_code_path = os.path.join(tmp, "local_code.tar.gz")
download_file_from_url(source_directory, local_code_path, sagemaker_session)
Expand All @@ -539,9 +541,12 @@ def _create_or_update_code_dir(
t.extractall(path=code_dir)

elif source_directory:
if os.path.exists(code_dir):
shutil.rmtree(code_dir)
shutil.copytree(source_directory, code_dir)
else:
os.mkdir(code_dir)
if not os.path.exists(code_dir):
os.mkdir(code_dir)
shutil.copy2(inference_script, code_dir)

for dependency in dependencies:
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,35 @@ def test_repack_model_from_file_to_folder(tmp):
}


def test_repack_model_with_inference_code_and_requirements(tmp, fake_s3):
create_file_tree(
tmp,
[
"new-inference.py",
"model-dir/model",
"model-dir/code/old-inference.py",
"model-dir/code/requirements.txt",
],
)

fake_s3.tar_and_upload("model-dir", "s3://fake/location")

sagemaker.utils.repack_model(
os.path.join(tmp, "new-inference.py"),
None,
None,
"s3://fake/location",
"s3://destination-bucket/repacked-model",
fake_s3.sagemaker_session,
)

assert list_tar_files(fake_s3.fake_upload_path, tmp) == {
"/code/requirements.txt",
"/code/new-inference.py",
"/model",
}


class FakeS3(object):
def __init__(self, tmp):
self.tmp = tmp
Expand Down