Skip to content

Commit

Permalink
feat(binding/python): Support Copy operation for Python binding (#3454)
Browse files Browse the repository at this point in the history
* feat(binding/python): Support Copy operation for Python binding

Signed-off-by: Manjusaka <[email protected]>

* sort Python import by isort

Signed-off-by: Manjusaka <[email protected]>

* Update code

Signed-off-by: Manjusaka <[email protected]>

* Update comment

Signed-off-by: Manjusaka <[email protected]>

---------

Signed-off-by: Manjusaka <[email protected]>
  • Loading branch information
Zheaoli authored Nov 1, 2023
1 parent 0721a6e commit 82802b1
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 7 deletions.
6 changes: 4 additions & 2 deletions bindings/python/benchmark/async_opendal_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
# specific language governing permissions and limitations
# under the License.

from pydantic import BaseSettings
import asyncio
import opendal
import timeit

from pydantic import BaseSettings

import opendal


class Config(BaseSettings):
aws_endpoint: str
Expand Down
1 change: 0 additions & 1 deletion bindings/python/python/opendal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@

from ._opendal import *


__doc__ = _opendal.__doc__
__all__ = _opendal.__all__
2 changes: 2 additions & 0 deletions bindings/python/python/opendal/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Operator:
def list(self, path: str) -> Iterable[Entry]: ...
def scan(self, path: str) -> Iterable[Entry]: ...
def capability(self) -> Capability: ...
def copy(self, source: str, target: str): ...

class AsyncOperator:
def __init__(self, scheme: str, **kwargs): ...
Expand All @@ -65,6 +66,7 @@ class AsyncOperator:
self, path: str, expire_second: int
) -> PresignedRequest: ...
def capability(self) -> Capability: ...
async def copy(self, source: str, target: str): ...

class Reader:
def read(self, size: Optional[int] = None) -> memoryview: ...
Expand Down
13 changes: 13 additions & 0 deletions bindings/python/src/asyncio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,19 @@ impl AsyncOperator {
})
}

/// Copy source to target.``
pub fn copy<'p>(
&'p self,
py: Python<'p>,
source: String,
target: String,
) -> PyResult<&'p PyAny> {
let this = self.0.clone();
future_into_py(py, async move {
this.copy(&source, &target).await.map_err(format_pyerr)
})
}

/// Create a dir at given path.
///
/// # Notes
Expand Down
5 changes: 5 additions & 0 deletions bindings/python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ impl Operator {
self.0.stat(path).map_err(format_pyerr).map(Metadata)
}

/// Copy source to target.
pub fn copy(&self, source: &str, target: &str) -> PyResult<()> {
self.0.copy(source, target).map_err(format_pyerr)
}

/// Create a dir at given path.
///
/// # Notes
Expand Down
4 changes: 2 additions & 2 deletions bindings/python/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

import os

from dotenv import load_dotenv
import pytest
import opendal
from dotenv import load_dotenv

import opendal

load_dotenv()
pytest_plugins = ("pytest_asyncio",)
Expand Down
114 changes: 114 additions & 0 deletions bindings/python/tests/test_async_copy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

import os
from random import randint
from uuid import uuid4

import pytest


@pytest.mark.asyncio
@pytest.mark.need_capability("read", "write", "copy")
async def test_async_copy(service_name, operator, async_operator):
source_path = f"random_file_{str(uuid4())}"
content = os.urandom(1024)
await async_operator.write(source_path, content)
target_path = f"random_file_{str(uuid4())}"
await async_operator.copy(source_path, target_path)
read_content = await async_operator.read(target_path)
assert read_content is not None
assert read_content == content
await async_operator.delete(source_path)
await async_operator.delete(target_path)


@pytest.mark.asyncio
@pytest.mark.need_capability("read", "write", "copy")
async def test_async_copy_non_exist(service_name, operator, async_operator):
source_path = f"random_file_{str(uuid4())}"
target_path = f"random_file_{str(uuid4())}"
with pytest.raises(Exception) as e_info:
await async_operator.copy(source_path, target_path)


@pytest.mark.asyncio
@pytest.mark.need_capability("read", "write", "copy")
async def test_async_copy_source_directory(service_name, operator, async_operator):
source_path = f"random_file_{str(uuid4())}/"
await async_operator.create_dir(source_path)
target_path = f"random_file_{str(uuid4())}"
with pytest.raises(Exception) as e_info:
await async_operator.copy(source_path, target_path)


@pytest.mark.asyncio
@pytest.mark.need_capability("read", "write", "copy")
async def test_async_copy_target_directory(service_name, operator, async_operator):
source_path = f"random_file_{str(uuid4())}"
content = os.urandom(1024)
await async_operator.write(source_path, content)
target_path = f"random_file_{str(uuid4())}/"
await async_operator.create_dir(target_path)
with pytest.raises(Exception) as e_info:
await async_operator.copy(source_path, target_path)
await async_operator.delete(source_path)
await async_operator.delete(target_path)


@pytest.mark.asyncio
@pytest.mark.need_capability("read", "write", "copy")
async def test_async_copy_self(service_name, operator, async_operator):
source_path = f"random_file_{str(uuid4())}"
content = os.urandom(1024)
await async_operator.write(source_path, content)
with pytest.raises(Exception) as e_info:
await async_operator.copy(source_path, source_path)
await async_operator.delete(source_path)


@pytest.mark.asyncio
@pytest.mark.need_capability("read", "write", "copy")
async def test_async_copy_nested(service_name, operator, async_operator):
source_path = f"random_file_{str(uuid4())}"
target_path = f"random_file_{str(uuid4())}/{str(uuid4())}/{str(uuid4())}"
content = os.urandom(1024)
await async_operator.write(source_path, content)
await async_operator.copy(source_path, target_path)
target_content = await async_operator.read(target_path)
assert target_content is not None
assert target_content == content
await async_operator.delete(source_path)
await async_operator.delete(target_path)


@pytest.mark.asyncio
@pytest.mark.need_capability("read", "write", "copy")
async def test_async_copy_overwrite(service_name, operator, async_operator):
source_path = f"random_file_{str(uuid4())}"
target_path = f"random_file_{str(uuid4())}"
source_content = os.urandom(1024)
target_content = os.urandom(1024)
assert source_content != target_content
await async_operator.write(source_path, source_content)
await async_operator.write(target_path, target_content)
await async_operator.copy(source_path, target_path)
target_content = await async_operator.read(target_path)
assert target_content is not None
assert target_content == source_content
await async_operator.delete(source_path)
await async_operator.delete(target_path)
2 changes: 1 addition & 1 deletion bindings/python/tests/test_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
# under the License.

import os
from uuid import uuid4
from random import randint
from uuid import uuid4

import pytest

Expand Down
107 changes: 107 additions & 0 deletions bindings/python/tests/test_sync_copy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

import os
from random import randint
from uuid import uuid4

import pytest


@pytest.mark.need_capability("read", "write", "copy")
def test_sync_copy(service_name, operator, async_operator):
source_path = f"random_file_{str(uuid4())}"
content = os.urandom(1024)
operator.write(source_path, content)
target_path = f"random_file_{str(uuid4())}"
operator.copy(source_path, target_path)
read_content = operator.read(target_path)
assert read_content is not None
assert read_content == content
operator.delete(source_path)
operator.delete(target_path)


@pytest.mark.need_capability("read", "write", "copy")
def test_sync_copy_non_exist(service_name, operator, async_operator):
source_path = f"random_file_{str(uuid4())}"
target_path = f"random_file_{str(uuid4())}"
with pytest.raises(Exception) as e_info:
operator.copy(source_path, target_path)


@pytest.mark.need_capability("read", "write", "copy")
def test_sync_copy_source_directory(service_name, operator, async_operator):
source_path = f"random_file_{str(uuid4())}/"
operator.create_dir(source_path)
target_path = f"random_file_{str(uuid4())}"
with pytest.raises(Exception) as e_info:
operator.copy(source_path, target_path)


@pytest.mark.need_capability("read", "write", "copy")
def test_sync_copy_target_directory(service_name, operator, async_operator):
source_path = f"random_file_{str(uuid4())}"
content = os.urandom(1024)
operator.write(source_path, content)
target_path = f"random_file_{str(uuid4())}/"
operator.create_dir(target_path)
with pytest.raises(Exception) as e_info:
operator.copy(source_path, target_path)
operator.delete(source_path)
operator.delete(target_path)


@pytest.mark.need_capability("read", "write", "copy")
def test_sync_copy_self(service_name, operator, async_operator):
source_path = f"random_file_{str(uuid4())}"
content = os.urandom(1024)
operator.write(source_path, content)
with pytest.raises(Exception) as e_info:
operator.copy(source_path, source_path)
operator.delete(source_path)


@pytest.mark.need_capability("read", "write", "copy")
def test_sync_copy_nested(service_name, operator, async_operator):
source_path = f"random_file_{str(uuid4())}"
target_path = f"random_file_{str(uuid4())}/{str(uuid4())}/{str(uuid4())}"
content = os.urandom(1024)
operator.write(source_path, content)
operator.copy(source_path, target_path)
target_content = operator.read(target_path)
assert target_content is not None
assert target_content == content
operator.delete(source_path)
operator.delete(target_path)


@pytest.mark.need_capability("read", "write", "copy")
def test_sync_copy_overwrite(service_name, operator, async_operator):
source_path = f"random_file_{str(uuid4())}"
target_path = f"random_file_{str(uuid4())}"
source_content = os.urandom(1024)
target_content = os.urandom(1024)
assert source_content != target_content
operator.write(source_path, source_content)
operator.write(target_path, target_content)
operator.copy(source_path, target_path)
target_content = operator.read(target_path)
assert target_content is not None
assert target_content == source_content
operator.delete(source_path)
operator.delete(target_path)
2 changes: 1 addition & 1 deletion bindings/python/tests/test_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
# under the License.

import os
from uuid import uuid4
from random import randint
from uuid import uuid4

import pytest

Expand Down

0 comments on commit 82802b1

Please sign in to comment.