Skip to content

Commit

Permalink
Fix to container.upload()'s behavior
Browse files Browse the repository at this point in the history
container.upload() previously only uploaded the contents of a specified directory path to the container.
It now uploads the file or directory pointed to by the specified path.
Example before: container.upload(test.png) would not upload the file as
it would need a directory path.
Example before: container.upload(test/) would upload the contents of
the test directory
Example after: container.upload(test.png) uploads the file test.png and
container.upload(test/) uploads the entire test directory
  • Loading branch information
Soufiane Jounaid authored and Soufiane Jounaid committed Feb 13, 2024
1 parent a83d9e1 commit 9058388
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion chi/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import io
import os
import logging
import tarfile
import time
Expand Down Expand Up @@ -202,14 +203,17 @@ def execute(container_ref: "str", command: "str") -> "dict":
def upload(container_ref: "str", source: "str", dest: "str") -> "dict":
"""Upload a file or directory to a running container.
This method requires your running container to include
the GNU tar utility.
Args:
container_ref (str): The name or ID of the container.
source (str): The (local) path to the file or directory to upload.
dest (str): The (container) path to upload the file or directory to.
"""
fd = io.BytesIO()
with tarfile.open(fileobj=fd, mode="w") as tar:
tar.add(source, arcname=".")
tar.add(source, arcname=os.path.basename(source))
fd.seek(0)
data = fd.read()
fd.close()
Expand All @@ -219,6 +223,9 @@ def upload(container_ref: "str", source: "str", dest: "str") -> "dict":
def download(container_ref: "str", source: "str", dest: "str"):
"""Download a file or directory from a running container.
This method requires your running container to include
both the POSIX sh and GNU tar utilities.
Args:
container_ref (str): The name or ID of the container.
source (str): The (container) path of the file or directory.
Expand Down

0 comments on commit 9058388

Please sign in to comment.