Skip to content

fix: Support file URIs in ProcessingStep's code parameter #3051

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 2 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions src/sagemaker/workflow/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from typing import List, Sequence, Union
import hashlib
from urllib.parse import unquote, urlparse

from sagemaker.workflow.entities import (
Entity,
Expand Down Expand Up @@ -50,6 +51,8 @@ def hash_file(path: str) -> str:
"""
BUF_SIZE = 65536 # read in 64KiB chunks
md5 = hashlib.md5()
if path.lower().startswith("file://"):
path = unquote(urlparse(path).path.strip('/'))
with open(path, "rb") as f:
while True:
data = f.read(BUF_SIZE)
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/sagemaker/workflow/test_utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
# language governing permissions and limitations under the License.
from __future__ import absolute_import

import tempfile
from sagemaker.workflow.utilities import hash_file


def test_hash_file():
with tempfile.NamedTemporaryFile() as tmp:
tmp.write("hashme".encode())
hash = hash_file(tmp.name)
assert hash == "d41d8cd98f00b204e9800998ecf8427e"


def test_hash_file_uri():
with tempfile.NamedTemporaryFile() as tmp:
tmp.write("hashme".encode())
hash = hash_file(f"file://{tmp.name}")
assert hash == "d41d8cd98f00b204e9800998ecf8427e"