Skip to content

Commit

Permalink
Use sync ADB server code (#197)
Browse files Browse the repository at this point in the history
* Revert "Use async ADB server code from pure-python-adb (#193)"

This reverts commit 52864be.

* No need to catch index errors

* Update docstrings

* Checkout setup.py from master

* pylint fix
  • Loading branch information
JeffLIrion committed Sep 12, 2020
1 parent 9ab0e5b commit e29c535
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 3 deletions.
42 changes: 39 additions & 3 deletions androidtv/adb_manager/adb_manager_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,50 @@
from adb_shell.adb_device_async import AdbDeviceTcpAsync
from adb_shell.auth.sign_pythonrsa import PythonRSASigner
import aiofiles
from ppadb.client_async import ClientAsync
from ppadb.client import Client

from ..constants import DEFAULT_ADB_TIMEOUT_S, DEFAULT_AUTH_TIMEOUT_S, DEFAULT_LOCK_TIMEOUT_S
from ..exceptions import LockNotAcquiredException

_LOGGER = logging.getLogger(__name__)


class DeviceAsync:
"""An async wrapper for the pure-python-adb ``Device`` class."""
def __init__(self, device):
self._device = device

async def pull(self, device_path, local_path):
"""Download a file."""
return await asyncio.get_running_loop().run_in_executor(None, self._device.pull, device_path, local_path)

async def push(self, local_path, device_path):
"""Upload a file."""
return await asyncio.get_running_loop().run_in_executor(None, self._device.push, local_path, device_path)

async def screencap(self):
"""Take a screencap."""
return await asyncio.get_running_loop().run_in_executor(None, self._device.screencap)

async def shell(self, cmd):
"""Send a shell command."""
return await asyncio.get_running_loop().run_in_executor(None, self._device.shell, cmd)


# pylint: disable=too-few-public-methods
class ClientAsync:
"""An async wrapper for the pure-python-adb ``Client`` class."""
def __init__(self, host, port):
self._client = Client(host, port)

async def device(self, serial):
"""Get a ``DeviceAsync`` instance."""
dev = await asyncio.get_running_loop().run_in_executor(None, self._client.device, serial)
if dev:
return DeviceAsync(dev)
return None


@asynccontextmanager
async def _acquire(lock, timeout=DEFAULT_LOCK_TIMEOUT_S):
"""Handle acquisition and release of an ``asyncio.Lock`` object with a timeout.
Expand Down Expand Up @@ -51,8 +87,8 @@ async def _acquire(lock, timeout=DEFAULT_LOCK_TIMEOUT_S):
raise LockNotAcquiredException
yield acquired

except asyncio.TimeoutError:
raise LockNotAcquiredException
except asyncio.TimeoutError as exc:
raise LockNotAcquiredException from exc

finally:
if acquired:
Expand Down
46 changes: 46 additions & 0 deletions tests/test_adb_manager_async_temp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import sys
import unittest
from unittest.mock import patch

sys.path.insert(0, '..')

from androidtv.adb_manager.adb_manager_async import ClientAsync

from .async_wrapper import awaiter
from . import patchers


class TestAsyncClientDevice(unittest.TestCase):
"""Test the ``ClientAsync`` and ``DeviceAsync`` classes defined in ``adb_manager_async.py``.
This file can be removed once true async support for using an ADB server is available.
"""

@awaiter
async def test_async_client_device(self):
with patch("androidtv.adb_manager.adb_manager_async.Client", patchers.ClientFakeSuccess):
client = ClientAsync("host", "port")

device = await client.device("serial")

with patch("{}.DeviceFake.shell".format(patchers.__name__)):
await device.shell("test")

with patch("{}.DeviceFake.push".format(patchers.__name__)):
await device.push("local_path", "device_path")

with patch("{}.DeviceFake.pull".format(patchers.__name__)):
await device.pull("device_path", "local_path")

with patch("{}.DeviceFake.screencap".format(patchers.__name__)):
await device.screencap()

@awaiter
async def test_async_client_device_fail(self):
with patch("androidtv.adb_manager.adb_manager_async.Client", patchers.ClientFakeFail):
client = ClientAsync("host", "port")

device = await client.device("serial")

self.assertFalse(device)

0 comments on commit e29c535

Please sign in to comment.