Skip to content

Commit

Permalink
fix: rename copy function and related arguments
Browse files Browse the repository at this point in the history
Signed-off-by: mao3267 <[email protected]>
  • Loading branch information
mao3267 committed Sep 4, 2024
1 parent 4f9dafe commit 76520a0
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 16 deletions.
9 changes: 4 additions & 5 deletions flytekit/image_spec/default_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,9 @@ def create_docker_context(image_spec: ImageSpec, tmp_dir: Path):
run_commands = ""

extra_copy_cmds = ""
if image_spec.copy_src_dest:
for src, dest in image_spec.copy_src_dest:
src_files_and_dirs = " ".join(src)
extra_copy_cmds += f"COPY --chown=flytekit {src_files_and_dirs} {dest}\n"
if image_spec.copy:
for src in image_spec.copy:
extra_copy_cmds += f'COPY --chown=flytekit {" ".join(src)} /root\n'
else:
extra_copy_cmds = ""

Expand Down Expand Up @@ -257,7 +256,7 @@ class DefaultImageBuilder(ImageSpecBuilder):
"pip_extra_index_url",
# "registry_config",
"commands",
"copy_src_dest",
"copy",
}

def build_image(self, image_spec: ImageSpec) -> str:
Expand Down
12 changes: 6 additions & 6 deletions flytekit/image_spec/image_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class ImageSpec:
commands: Command to run during the building process
tag_format: Custom string format for image tag. The ImageSpec hash passed in as `spec_hash`. For example,
to add a "dev" suffix to the image tag, set `tag_format="{spec_hash}-dev"`
copy_src_dest: List of tuples containing source files/directories and their corresponding destination directory for copying.
copy: List of files/directories to copy to /root. e.g. ["src/file1.txt", "src/file2.txt"]
"""

name: str = "flytekit"
Expand All @@ -74,7 +74,7 @@ class ImageSpec:
entrypoint: Optional[List[str]] = None
commands: Optional[List[str]] = None
tag_format: Optional[str] = None
copy_src_dest: Optional[List[Tuple[List[str], str]]] = None
copy: Optional[List[List[str]]] = None

def __post_init__(self):
self.name = self.name.lower()
Expand Down Expand Up @@ -234,15 +234,15 @@ def with_apt_packages(self, apt_packages: Union[str, List[str]]) -> "ImageSpec":

return new_image_spec

def copy(self, src: List[str], dest: str = ".") -> "ImageSpec":
def with_copy(self, src: List[str]) -> "ImageSpec":
"""
Builder that returns a new image spec with the source files copied to the destination directory.
"""
new_image_spec = copy.deepcopy(self)
if new_image_spec.copy_src_dest is None:
new_image_spec.copy_src_dest = []
if new_image_spec.copy is None:
new_image_spec.copy = []

new_image_spec.copy_src_dest.append((src, dest))
new_image_spec.copy.append(src)

return new_image_spec

Expand Down
4 changes: 2 additions & 2 deletions tests/flytekit/unit/core/image_spec/test_default_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_create_docker_context(tmp_path):
commands=["mkdir my_dir"],
entrypoint=["/bin/bash"],
pip_extra_index_url=["https://extra-url.com"],
copy_src_dest=[(["file1.txt", "/dir1"], "/root")]
copy=[["file1.txt", "/dir1"]]
)

create_docker_context(image_spec, docker_context_path)
Expand Down Expand Up @@ -179,7 +179,7 @@ def test_build(tmp_path):
requirements=os.fspath(other_requirements_path),
source_root=os.fspath(source_root),
commands=["mkdir my_dir"],
copy_src_dest=[([f"{tmp_path}/hello_world.txt", f"{tmp_path}/requirements.txt"], "/my_dir")]
copy=[[f"{tmp_path}/hello_world.txt", f"{tmp_path}/requirements.txt"]]
)

builder = DefaultImageBuilder()
Expand Down
6 changes: 3 additions & 3 deletions tests/flytekit/unit/core/image_spec/test_image_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ def test_image_spec(mock_image_spec_builder):
requirements=REQUIREMENT_FILE,
registry_config=REGISTRY_CONFIG_FILE,
entrypoint=["/bin/bash"],
copy_src_dest=[(["/src/file1.txt"], "/dest")]
copy=[["/src/file1.txt"]]
)
assert image_spec._is_force_push is False

image_spec = image_spec.with_commands("echo hello")
image_spec = image_spec.with_packages("numpy")
image_spec = image_spec.with_apt_packages("wget")
image_spec = image_spec.copy(["/src", "/src/file2.txt"], "/dest")
image_spec = image_spec.with_copy(["/src", "/src/file2.txt"])
image_spec = image_spec.force_push()

assert image_spec.python_version == "3.8"
Expand All @@ -54,7 +54,7 @@ def test_image_spec(mock_image_spec_builder):
assert image_spec.commands == ["echo hello"]
assert image_spec._is_force_push is True
assert image_spec.entrypoint == ["/bin/bash"]
assert image_spec.copy_src_dest == [(["/src/file1.txt"], "/dest"), (["/src", "/src/file2.txt"], "/dest")]
assert image_spec.copy == [["/src/file1.txt"], ["/src", "/src/file2.txt"]]

tag = calculate_hash_from_image_spec(image_spec)
assert "=" != tag[-1]
Expand Down

0 comments on commit 76520a0

Please sign in to comment.