Skip to content

Commit

Permalink
Merge branch 'cluster-folder-methods' into test-cluster-folder-methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jlewitt1 committed Aug 10, 2024
2 parents f1125f6 + 7f31f51 commit 667f5a5
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
4 changes: 2 additions & 2 deletions runhouse/resources/folders/folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def local_path(self):
else:
return None

def mv(self, system, path: Optional[str] = None) -> None:
def mv(self, system, path: Optional[str] = None, overwrite: bool = True) -> None:
"""Move the folder to a new filesystem or cluster.
Example:
Expand All @@ -155,7 +155,7 @@ def mv(self, system, path: Optional[str] = None) -> None:
)

# Create the destination directory if it doesn't exist
dest_path.parent.mkdir(parents=True, exist_ok=True)
dest_path.parent.mkdir(parents=True, exist_ok=overwrite)

# Move the directory
shutil.move(str(src_path), str(dest_path))
Expand Down
2 changes: 1 addition & 1 deletion runhouse/resources/hardware/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -1835,7 +1835,7 @@ def _folder_mv(
self,
path: Union[str, Path],
dest_path: Union[str, Path],
overwrite: bool = False,
overwrite: bool = True,
):
return self.client.folder_mv(
path=path, dest_path=dest_path, overwrite=overwrite
Expand Down
16 changes: 15 additions & 1 deletion runhouse/servers/http/http_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from runhouse.servers.http.http_utils import (
CallParams,
DeleteObjectParams,
folder_exists,
folder_get,
folder_ls,
folder_mkdir,
Expand Down Expand Up @@ -688,10 +689,10 @@ async def folder_put_cmd(request: Request, put_params: FolderPutParams):
path = resolve_folder_path(put_params.path)
return folder_put(
path,
contents=put_params.contents,
overwrite=put_params.overwrite,
mode=put_params.mode,
serialization=put_params.serialization,
contents=put_params.contents,
)

except Exception as e:
Expand Down Expand Up @@ -731,6 +732,19 @@ async def folder_mv_cmd(request: Request, mv_params: FolderMvParams):
e, traceback.format_exc(), from_http_server=True
)

@staticmethod
@app.post("/folder/method/exists")
@validate_cluster_access
async def folder_exists_cmd(request: Request, folder_params: FolderParams):
try:
path = resolve_folder_path(folder_params.path)
return folder_exists(path=path)

except Exception as e:
return handle_exception_response(
e, traceback.format_exc(), from_http_server=True
)

@staticmethod
@app.post("/delete_object")
@validate_cluster_access
Expand Down
18 changes: 13 additions & 5 deletions runhouse/servers/http/http_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class FolderRmParams(FolderParams):

class FolderMvParams(FolderParams):
dest_path: str
overwrite: Optional[bool] = False
overwrite: Optional[bool] = True

@validator("dest_path", pre=True, always=True)
def convert_path_to_string(cls, v):
Expand Down Expand Up @@ -350,7 +350,7 @@ def folder_mkdir(path: Path):
return Response(output_type=OutputType.SUCCESS)


def folder_get(path: Path, mode: str = None, encoding: str = None):
def folder_get(path: Path, encoding: str = None, mode: str = None):
mode = mode or "rb"
binary_mode = "b" in mode

Expand Down Expand Up @@ -386,10 +386,10 @@ def folder_get(path: Path, mode: str = None, encoding: str = None):

def folder_put(
path: Path,
contents: Dict[str, Any],
overwrite: bool,
mode: str = None,
serialization: str = None,
contents: Dict[str, Any] = None,
):
mode = mode or "wb"

Expand All @@ -399,8 +399,6 @@ def folder_put(
detail="`contents` argument must be a dict mapping filenames to file-like objects",
)

path.mkdir(parents=True, exist_ok=True)

if overwrite is False:
existing_files = {str(item.name) for item in path.iterdir()}
intersection = existing_files.intersection(set(contents.keys()))
Expand All @@ -410,6 +408,8 @@ def folder_put(
detail=f"File(s) {intersection} already exist(s) at path: {path}",
)

path.mkdir(parents=True, exist_ok=True)

for filename, file_obj in contents.items():
binary_mode = "b" in mode

Expand Down Expand Up @@ -523,3 +523,11 @@ def folder_mv(src_path: Path, dest_path: str, overwrite: bool):
shutil.move(str(src_path), str(dest_path))

return Response(output_type=OutputType.SUCCESS)


def folder_exists(path: Path):
return Response(
data=path.exists() and path.is_dir(),
output_type=OutputType.RESULT_SERIALIZED,
serialization=None,
)
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class TestLevels(str, enum.Enum):
MAXIMAL = "maximal"


DEFAULT_LEVEL = TestLevels.LOCAL
DEFAULT_LEVEL = TestLevels.UNIT

TEST_LEVEL_HIERARCHY = {
TestLevels.UNIT: 0,
Expand Down

0 comments on commit 667f5a5

Please sign in to comment.