Skip to content

Commit

Permalink
add folder http endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jlewitt1 committed Aug 10, 2024
1 parent 00a675b commit bbd72e7
Show file tree
Hide file tree
Showing 3 changed files with 361 additions and 13 deletions.
26 changes: 15 additions & 11 deletions runhouse/resources/folders/folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,20 @@ def _path_absolute_to_rh_workdir(path):
else str(Path(locate_working_dir()) / path)
)

@staticmethod
def _delete_contents(contents: List, folder_path: Path, recursive: bool):
for content in contents:
content_path = folder_path / content
if content_path.exists():
if content_path.is_file():
content_path.unlink()
elif content_path.is_dir() and recursive:
shutil.rmtree(content_path)
else:
raise ValueError(
f"Path {content_path} is a directory and recursive is set to False"
)

@property
def fsspec_url(self):
"""Generate the FSSpec style URL using the file system and path of the folder"""
Expand Down Expand Up @@ -685,17 +699,7 @@ def rm(self, contents: list = None, recursive: bool = True):
folder_path = Path(self.path).expanduser()

if contents:
for content in contents:
content_path = folder_path / content
if content_path.exists():
if content_path.is_file():
content_path.unlink()
elif content_path.is_dir() and recursive:
shutil.rmtree(content_path)
else:
raise ValueError(
f"Path {content_path} is a directory and recursive is set to False"
)
Folder._delete_contents(contents, folder_path, recursive)
else:
if recursive:
shutil.rmtree(folder_path)
Expand Down
104 changes: 103 additions & 1 deletion runhouse/servers/http/http_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,25 @@
from runhouse.servers.http.http_utils import (
CallParams,
DeleteObjectParams,
folder_get,
folder_ls,
folder_mkdir,
folder_mv,
folder_put,
folder_rm,
FolderGetParams,
FolderLsParams,
FolderMvParams,
FolderParams,
FolderPutParams,
FolderRmParams,
get_token_from_request,
handle_exception_response,
OutputType,
PutObjectParams,
PutResourceParams,
RenameObjectParams,
resolve_folder_path,
Response,
serialize_data,
ServerSettings,
Expand All @@ -53,7 +66,6 @@
)
from runhouse.utils import sync_function


app = FastAPI(docs_url=None, redoc_url=None)


Expand Down Expand Up @@ -612,6 +624,96 @@ async def rename_object(request: Request, params: RenameObjectParams):
e, traceback.format_exc(), from_http_server=True
)

@staticmethod
@app.post("/folder/method/ls")
@validate_cluster_access
async def folder_ls_cmd(request: Request, ls_params: FolderLsParams):
try:
path = resolve_folder_path(ls_params.path)
return folder_ls(path, full_paths=ls_params.full_paths, sort=ls_params.sort)

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

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

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

@staticmethod
@app.post("/folder/method/get")
@validate_cluster_access
async def folder_get_cmd(request: Request, get_params: FolderGetParams):
try:
path = resolve_folder_path(get_params.path)
return folder_get(path, mode=get_params.mode, encoding=get_params.encoding)

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

@staticmethod
@app.post("/folder/method/put")
@validate_cluster_access
async def folder_put_cmd(request: Request, put_params: FolderPutParams):
try:
path = resolve_folder_path(put_params.path)
return folder_put(
path,
overwrite=put_params.overwrite,
mode=put_params.mode,
serialization=put_params.serialization,
contents=put_params.contents,
)

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

@staticmethod
@app.post("/folder/method/rm")
@validate_cluster_access
async def folder_rm_cmd(request: Request, rm_params: FolderRmParams):
try:
path = resolve_folder_path(rm_params.path)
return folder_rm(
path, contents=rm_params.contents, recursive=rm_params.recursive
)

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

@staticmethod
@app.post("/folder/method/mv")
@validate_cluster_access
async def folder_mv_cmd(request: Request, mv_params: FolderMvParams):
try:
path = resolve_folder_path(mv_params.path)
return folder_mv(
src_path=path,
dest_path=mv_params.dest_path,
overwrite=mv_params.overwrite,
)

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
Loading

0 comments on commit bbd72e7

Please sign in to comment.