diff --git a/owncloud/owncloud.py b/owncloud/owncloud.py index 6d963e8..6cff0f1 100644 --- a/owncloud/owncloud.py +++ b/owncloud/owncloud.py @@ -703,6 +703,26 @@ def mkdir(self, path): path += '/' return self._make_dav_request('MKCOL', path) + def makedirs(self, path, exist_ok=False): + """Creates recursively remote directories + + :param path: path to the remote directory to create + :param exist_ok: if True, it does not throw an error if a folder already exist + :returns: True if the operation succeeded, False otherwise + :raises: HTTPResponseError in case an HTTP error status was returned + """ + try: + self.mkdir(path) + except HTTPResponseError as e: + if e.status_code == 409: + self.makedirs(os.path.dirname(path)) + self.makedirs(path) + elif e.status_code == 405 and exist_ok: + pass + else: + raise e + return True + def delete(self, path): """Deletes a remote file or directory diff --git a/owncloud/test/test.py b/owncloud/test/test.py index 222f11e..0d43b8a 100644 --- a/owncloud/test/test.py +++ b/owncloud/test/test.py @@ -115,6 +115,17 @@ def test_mkdir(self, file_name, content, subdir): self.assertTrue(self.client.mkdir(self.test_root + subdir)) self.assertIsNotNone(self.client.file_info(self.test_root + subdir)) + @data_provider(files_content) + def test_makedirs(self, file_name, content, subdir): + """Test recursive subdirectory creation with existing folder""" + new_dir = os.path.join(self.test_root, subdir, subdir) + self.assertTrue(self.client.makedirs(new_dir)) + self.assertIsNotNone(self.client.file_info(new_dir)) + + new_dir = os.path.join(new_dir, subdir) + self.assertTrue(self.client.makedirs(new_dir, exist_ok=True)) + self.assertIsNotNone(self.client.file_info(new_dir)) + @data_provider(files_content) def test_put_file_contents(self, file_name, content, subdir): """Test creating remote file with given contents"""