Skip to content

Commit

Permalink
remove more libs and optimized code
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed Apr 26, 2024
1 parent 30f5f11 commit 60a1e43
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 29 deletions.
11 changes: 9 additions & 2 deletions docs/2to3.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
### Lib removes
- Remove logzero
- Remove filelocks
- Remove progress
- Remove packaging
- Remove Pillow

### Module removes
- Remove module uiautomator2.ext.xpath
Expand Down Expand Up @@ -60,7 +63,7 @@
- Remove "uiautomator2 healthcheck"
- Remove "uiautomator2 identify"

## Updates
## Function changes
### connect_usb
- 2.x connect_usb(serial, init: bool)
- 3.x connect_usb(serial)
Expand Down Expand Up @@ -107,4 +110,8 @@
- 2.x sess = d.session("com.netease.cloudmusic", attach=True, strict=True)
- 3.x sess = d.session("com.netease.cloudmusic", attach=True)

It seems the strict is useless, so I delete it.
It seems the strict is useless, so I delete it.

### push
- 2.x push(src, dst, mode, show_process:bool=False)
- 3.x push(src, dst, mode)
7 changes: 1 addition & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
name = "uiautomator2"
version = "0.0.0"
description = "automator for anroid device"
description = "uiautomator for android device"
homepage = "https://github.com/openatx/uiautomator2"
authors = ["codeskyblue <codeskyblue@gmail.com>"]
license = "MIT"
Expand All @@ -14,12 +14,7 @@ requests = "*"
lxml = "*"
adbutils = "^2.2.3,!=2.3.0"
retry = ">=0,<1"
packaging = ">=20.3"
pillow = "*"

# TODO: remove later
Deprecated = "*"
progress = "^1.6"

[tool.poetry.group.dev.dependencies]
pytest = "^8.1.1"
Expand Down
5 changes: 1 addition & 4 deletions uiautomator2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,15 @@ def reset_uiautomator(self):
self.stop_uiautomator()
self.start_uiautomator()

Check warning on line 183 in uiautomator2/__init__.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/__init__.py#L182-L183

Added lines #L182 - L183 were not covered by tests

def push(self, src, dst: str, mode=0o644, show_progress=False):
def push(self, src, dst: str, mode=0o644):
"""
Push file into device
Args:
src (path or fileobj): source file
dst (str): destination can be folder or file path
mode (int): file mode
show_progress (bool): not supported now
"""
if show_progress:
logger.warning("show_progress is not supported now")
self._dev.sync.push(src, dst, mode=mode)

Check warning on line 194 in uiautomator2/__init__.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/__init__.py#L194

Added line #L194 was not covered by tests

def pull(self, src: str, dst: str):
Expand Down
42 changes: 28 additions & 14 deletions uiautomator2/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""

import atexit
import re
import threading
import time
import logging
Expand All @@ -15,7 +16,7 @@
import adbutils
import requests

from uiautomator2.exceptions import InjectPermissionError, RPCInvalidError, UiAutomationNotConnectedError, HTTPError, LaunchUiautomatorError, UiObjectNotFoundError, RPCUnknownError, APkSignatureError
from uiautomator2.exceptions import RPCInvalidError, UiAutomationNotConnectedError, HTTPError, LaunchUiAutomationError, UiObjectNotFoundError, RPCUnknownError, APkSignatureError, AccessibilityServiceAlreadyRegisteredError
from uiautomator2.abstract import AbstractUiautomatorServer


Expand Down Expand Up @@ -205,35 +206,43 @@ def _install_apk(self, apk_path: Path):
self._dev.push(apk_path, target_path)
self._dev.shell(['pm', 'install', '-r', '-t', target_path])

Check warning on line 207 in uiautomator2/core.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/core.py#L203-L207

Added lines #L203 - L207 were not covered by tests

def _wait_instrument_ready(self, timeout=10):
def _wait_instrument_ready(self, timeout: float):
deadline = time.time() + timeout

Check warning on line 210 in uiautomator2/core.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/core.py#L210

Added line #L210 was not covered by tests
while time.time() < deadline:
output = self._process.output.decode("utf-8", errors="ignore")

Check warning on line 212 in uiautomator2/core.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/core.py#L212

Added line #L212 was not covered by tests
if "does not have a signature matching the target" in output:
raise APkSignatureError("app-uiautomator.apk does not have a signature matching the target")

Check warning on line 214 in uiautomator2/core.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/core.py#L214

Added line #L214 was not covered by tests
if "INSTRUMENTATION_STATUS: Error=" in output:
error_message = output[output.find("INSTRUMENTATION_STATUS: Error="):].splitlines()[0]
raise LaunchUiautomatorError(error_message, output)
if "INSTRUMENTATION_STATUS_CODE: -1" in output:
raise LaunchUiautomatorError("am instrument error", output)
if "INSTRUMENTATION_STATUS_CODE: 1" in output:
# success
return
raise LaunchUiAutomationError(error_message, output)

Check warning on line 217 in uiautomator2/core.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/core.py#L216-L217

Added lines #L216 - L217 were not covered by tests
if "INSTRUMENTATION_STATUS_CODE:" in output:
status_code = int(re.search(r"INSTRUMENTATION_STATUS_CODE: (-?\d+)", output).group(1))

Check warning on line 219 in uiautomator2/core.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/core.py#L219

Added line #L219 was not covered by tests
if status_code == 1: # success
logger.debug("am instrument success, status_code: %d", status_code)
return
raise LaunchUiAutomationError("am instrument error", f'CODE:{status_code}', output)

Check warning on line 223 in uiautomator2/core.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/core.py#L221-L223

Added lines #L221 - L223 were not covered by tests
if self._process.pool() is not None:
raise LaunchUiAutomationError("am instrument quit", output)
time.sleep(.5)
raise LaunchUiautomatorError("am instrument timeout")
raise LaunchUiAutomationError("am instrument launch timeout", f"{timeout}s", output)

Check warning on line 227 in uiautomator2/core.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/core.py#L225-L227

Added lines #L225 - L227 were not covered by tests

def _wait_stub_ready(self, timeout=20):
def _wait_stub_ready(self, timeout: float):
deadline = time.time() + timeout

Check warning on line 230 in uiautomator2/core.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/core.py#L230

Added line #L230 was not covered by tests
while time.time() < deadline:
output = self._process.output.decode("utf-8", errors="ignore")

Check warning on line 232 in uiautomator2/core.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/core.py#L232

Added line #L232 was not covered by tests
if "already registered" in output:
raise AccessibilityServiceAlreadyRegisteredError("Possibly another UiAutomation service is running, you may find it output by \"adb shell ps -u shell\"",)

Check warning on line 234 in uiautomator2/core.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/core.py#L234

Added line #L234 was not covered by tests
if self._process.pool() is not None:
raise LaunchUiAutomationError("uiautomator2 server quit", output)
try:
response = _http_request(self._dev, "GET", "/ping")

Check warning on line 238 in uiautomator2/core.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/core.py#L236-L238

Added lines #L236 - L238 were not covered by tests
if response.content == b"pong":
return
except HTTPError:
time.sleep(.5)
raise LaunchUiautomatorError("uiautomator2 server not ready")
raise LaunchUiAutomationError("uiautomator2 server not ready")

Check warning on line 243 in uiautomator2/core.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/core.py#L240-L243

Added lines #L240 - L243 were not covered by tests

def _wait_ready(self, launch_timeout=10, service_timeout=30):
def _wait_ready(self, launch_timeout=30, service_timeout=30):
"""Wait until uiautomator2 server is ready"""
# wait am instrument start
self._wait_instrument_ready(launch_timeout)

Check warning on line 248 in uiautomator2/core.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/core.py#L248

Added line #L248 was not covered by tests
Expand All @@ -252,7 +261,13 @@ def jsonrpc_call(self, method: str, params: Any = None, timeout: float = 10) ->
"""Send jsonrpc call to uiautomator2 server"""
try:
return _jsonrpc_call(self._dev, method, params, timeout, self._debug)

Check warning on line 263 in uiautomator2/core.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/core.py#L262-L263

Added lines #L262 - L263 were not covered by tests
except (HTTPError, RPCInvalidError, UiAutomationNotConnectedError) as e:
except APkSignatureError:
logger.debug("APkSignatureError: %s", e)
self._dev.uninstall("com.github.uiautomator")
self._dev.uninstall("com.github.uiautomator.test")
self.start_uiautomator()
return _jsonrpc_call(self._dev, method, params, timeout, self._debug)
except (HTTPError, UiAutomationNotConnectedError) as e:
logger.debug("uiautomator2 is not ok, error: %s", e)
try:
self.start_uiautomator()
Expand All @@ -263,7 +278,6 @@ def jsonrpc_call(self, method: str, params: Any = None, timeout: float = 10) ->
self.start_uiautomator()
return _jsonrpc_call(self._dev, method, params, timeout, self._debug)

Check warning on line 279 in uiautomator2/core.py

View check run for this annotation

Codecov / codecov/patch

uiautomator2/core.py#L265-L279

Added lines #L265 - L279 were not covered by tests


class SimpleUiautomatorServer(BasicUiautomatorServer, AbstractUiautomatorServer):
@property
def info(self) -> Dict[str, Any]:
Expand Down
5 changes: 2 additions & 3 deletions uiautomator2/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,18 @@ class DeviceError(BaseException):
pass

class AdbShellError(DeviceError):...
class LaunchUiautomatorError(DeviceError):...
class ConnectError(DeviceError):...
class HTTPError(DeviceError):...



class UiAutomationError(DeviceError):
pass


class UiAutomationNotConnectedError(UiAutomationError):...
class InjectPermissionError(UiAutomationError):... #开发者选项中: 模拟点击没有打开
class APkSignatureError(UiAutomationError):...
class LaunchUiAutomationError(UiAutomationError):...
class AccessibilityServiceAlreadyRegisteredError(UiAutomationError):...


## RPCError
Expand Down

0 comments on commit 60a1e43

Please sign in to comment.