Skip to content

Commit

Permalink
Check for directories
Browse files Browse the repository at this point in the history
  • Loading branch information
kesmit13 committed Jan 9, 2024
1 parent 579c845 commit 1a7138e
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions singlestoredb/management/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ def mkdir(self, stage_path: PathLike, overwrite: bool = False) -> StageObject:
self.remove(stage_path)

self._manager._put(
f'stage/{self._workspace_group.id}/fs/{stage_path}',
f'stage/{self._workspace_group.id}/fs/{stage_path}?isFile=false',
)

return self.info(stage_path)
Expand Down Expand Up @@ -583,11 +583,34 @@ def rename(
if not self.exists(old_path):
raise OSError(f'stage path does not exist: {old_path}')

old_is_dir = self.is_dir(old_path)
new_is_dir = old_is_dir

if self.exists(new_path):
if not overwrite:
raise OSError(f'stage path already exists: {new_path}')

self.remove(new_path)
new_is_dir = self.is_dir(new_path)

if old_is_dir != new_is_dir:
raise OSError('original and new paths are not the same type')

new_path = str(new_path)
if new_is_dir and not new_path.endswith('/'):
new_path = new_path + '/'

if new_is_dir:
self.removedirs(new_path)
else:
self.remove(new_path)

old_path = str(old_path)
if old_is_dir and not old_path.endswith('/'):
old_path = old_path + '/'

new_path = str(new_path)
if new_is_dir and not new_path.endswith('/'):
new_path = new_path + '/'

self._manager._patch(
f'stage/{self._workspace_group.id}/fs/{old_path}',
Expand Down Expand Up @@ -851,6 +874,10 @@ def removedirs(self, stage_path: PathLike) -> None:
if info.type != 'directory':
raise NotADirectoryError(f'stage path is not a directory: {stage_path}')

stage_path = str(stage_path)
if not stage_path.endswith('/'):
stage_path = stage_path + '/'

self._manager._delete(f'stage/{self._workspace_group.id}/fs/{stage_path}')

def rmdir(self, stage_path: PathLike) -> None:
Expand All @@ -870,6 +897,10 @@ def rmdir(self, stage_path: PathLike) -> None:
if self.listdir(stage_path):
raise OSError(f'stage folder is not empty, use removedirs: {stage_path}')

stage_path = str(stage_path)
if not stage_path.endswith('/'):
stage_path = stage_path + '/'

self._manager._delete(f'stage/{self._workspace_group.id}/fs/{stage_path}')

def __str__(self) -> str:
Expand Down

0 comments on commit 1a7138e

Please sign in to comment.