Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/local_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,13 +397,18 @@ def get_name(self):
return os.name

# Work with dirs
def makedirs(self, path, remove_existing=False):
def makedirs(
self,
path: str,
remove_existing: bool = False,
) -> None:
assert type(path) is str
assert type(remove_existing) is bool

if remove_existing:
shutil.rmtree(path, ignore_errors=True)
try:
os.makedirs(path)
except FileExistsError:
pass

os.makedirs(path, exist_ok=True)
return

def makedir(self, path: str):
Expand Down
8 changes: 7 additions & 1 deletion src/os_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,13 @@ def get_name(self):
raise NotImplementedError()

# Work with dirs
def makedirs(self, path, remove_existing=False):
def makedirs(
self,
path: str,
remove_existing: bool = False,
) -> None:
assert type(path) is str
assert type(remove_existing) is bool
raise NotImplementedError()

def makedir(self, path: str):
Expand Down
21 changes: 12 additions & 9 deletions src/remote_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,24 +346,27 @@ def get_name(self):
return stdout.strip()

# Work with dirs
def makedirs(self, path, remove_existing=False):
def makedirs(
self,
path: str,
remove_existing: bool = False,
) -> None:
"""
Create a directory in the remote server.
Args:
- path (str): The path to the directory to be created.
- remove_existing (bool): If True, the existing directory at the path will be removed.
"""
if remove_existing:
cmd = "rm -rf {} && mkdir -p {}".format(path, path)
cmd = ["rm", "-rf", path, "&&", "mkdir", "-p", path]
else:
cmd = "mkdir -p {}".format(path)
try:
result = self.exec_command(cmd)
except ExecUtilException as e:
raise Exception("Couldn't create dir {} because of error {}".format(path, e.message))
cmd = ["mkdir", "-p", path]

assert type(result) is bytes
return result
self.exec_command(
cmd,
encoding=get_default_encoding(),
)
return

def makedir(self, path: str):
assert type(path) is str
Expand Down
22 changes: 20 additions & 2 deletions tests/test_os_ops_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,11 +467,18 @@ def test_makedirs_failure(

RunConditions.skip_if_windows()

path = "/root/test_dir"
path = "/root/test_dir-{}".format(uuid.uuid4().bytes.hex())

# Test makedirs
with pytest.raises(Exception):
with pytest.raises(Exception) as x:
os_ops.makedirs(path)

if type(os_ops).__name__ == "LocalOperations":
assert type(x.value) is PermissionError
elif type(os_ops).__name__ == "RemoteOperations":
assert type(x.value) is ExecUtilException
else:
__class__.helper__bug_check__unknown_os_ops_type(os_ops)
return

def test_listdir(
Expand Down Expand Up @@ -2461,3 +2468,14 @@ def test_readlines__BIN(

assert lines == result_bin
return

@staticmethod
def helper__bug_check__unknown_os_ops_type(
os_ops: OsOperations,
) -> typing.NoReturn:
assert isinstance(os_ops, OsOperations)

err_msg = "[BUG CHECK] Unknown os_ops type [{}].".format(
type(os_ops).__name__,
)
raise RuntimeError(err_msg)