diff --git a/python/hopsworks/core/dataset_api.py b/python/hopsworks/core/dataset_api.py index eb7fcae56..9ea083d29 100644 --- a/python/hopsworks/core/dataset_api.py +++ b/python/hopsworks/core/dataset_api.py @@ -389,7 +389,7 @@ def mkdir(self, path: str): "POST", path_params, headers=headers, query_params=query_params )["attributes"]["path"] - def copy(self, source_path: str, destination_path: str): + def copy(self, source_path: str, destination_path: str, overwrite: bool = False): """Copy a file or directory in the Hopsworks Filesystem. ```python @@ -406,9 +406,20 @@ def copy(self, source_path: str, destination_path: str): # Arguments source_path: the source path to copy destination_path: the destination path + overwrite: overwrite destination if exists # Raises `RestAPIError`: If unable to perform the copy """ + if self.exists(destination_path): + if overwrite: + self.remove(destination_path) + else: + raise DatasetException( + "{} already exists, set overwrite=True to overwrite it".format( + destination_path + ) + ) + _client = client.get_instance() path_params = ["project", self._project_id, "dataset", source_path] query_params = { @@ -419,7 +430,7 @@ def copy(self, source_path: str, destination_path: str): "POST", path_params, query_params=query_params ) - def move(self, source_path: str, destination_path: str): + def move(self, source_path: str, destination_path: str, overwrite: bool = False): """Move a file or directory in the Hopsworks Filesystem. ```python @@ -436,9 +447,21 @@ def move(self, source_path: str, destination_path: str): # Arguments source_path: the source path to move destination_path: the destination path + overwrite: overwrite destination if exists # Raises `RestAPIError`: If unable to perform the move """ + + if self.exists(destination_path): + if overwrite: + self.remove(destination_path) + else: + raise DatasetException( + "{} already exists, set overwrite=True to overwrite it".format( + destination_path + ) + ) + _client = client.get_instance() path_params = ["project", self._project_id, "dataset", source_path] query_params = {