Skip to content

Commit

Permalink
add codecov, fix coveragerc (#106)
Browse files Browse the repository at this point in the history
* add codecov, fix coveragerc
* add more tests, remove useless reqs, close #104 
* remove too many RuntimeError, use AdbError instead
  • Loading branch information
codeskyblue authored Mar 25, 2024
1 parent b5e9cf3 commit 0328f2e
Show file tree
Hide file tree
Showing 16 changed files with 242 additions and 113 deletions.
36 changes: 20 additions & 16 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
[run]
branch = true
omit =
tests/*
test_real_device/*
docs/*
setup.py
build_wheel.py


[report]
# Regexes for lines to exclude from consideration
; Regexes for lines to exclude from consideration
exclude_also =
# Don't complain about missing debug-only code:
"def __repr__",
"if self\\.debug",
; Don't complain about missing debug-only code:
def __repr__
if self\.debug

# Don't complain if tests don't hit defensive assertion code:
"raise AssertionError",
"raise NotImplementedError",
; Don't complain if tests don't hit defensive assertion code:
raise AssertionError
raise NotImplementedError

# Don't complain if non-runnable code isn't run:
"if 0:",
"if __name__ == .__main__.:",
; Don't complain if non-runnable code isn't run:
if 0:
if __name__ == .__main__.:

# Don't complain about abstract methods, they aren't run:
"@(abc\\.)?abstractmethod",
; Don't complain about abstract methods, they aren't run:
@(abc\.)?abstractmethod

ignore_errors = true
omit =
"tests/*",
"docs/*"
ignore_errors = True
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# adbutils
[![PyPI](https://img.shields.io/pypi/v/adbutils.svg?color=blue)](https://pypi.org/project/adbutils/#history)
[![codecov](https://codecov.io/gh/openatx/adbutils/graph/badge.svg?token=OuGOMZUkmi)](https://codecov.io/gh/openatx/adbutils)

Python adb library for adb service (Only support Python3.6+)
Python adb library for adb service (Only support Python3.6+), Recommend 3.8+

**Table of Contents**

Expand Down Expand Up @@ -522,7 +523,7 @@ gh-md-toc --insert README.md
# Thanks
- [swind pure-python-adb](https://github.com/Swind/pure-python-adb)
- [openstf/adbkit](https://github.com/openstf/adbkit)
- [ADB Source Code](https://github.com/aosp-mirror/platform_system_core/blob/master/adb)
- [ADB Source Code](https://android.googlesource.com/platform/system/core/+/android-4.4_r1/adb/adb.c)
- ADB Protocols [OVERVIEW.TXT](https://cs.android.com/android/platform/superproject/+/master:packages/modules/adb/OVERVIEW.TXT) [SERVICES.TXT](https://cs.android.com/android/platform/superproject/+/master:packages/modules/adb/SERVICES.TXT) [SYNC.TXT](https://cs.android.com/android/platform/superproject/+/master:packages/modules/adb/SYNC.TXT)
- [Awesome ADB](https://github.com/mzlogin/awesome-adb)
- [JakeWharton/pidcat](https://github.com/JakeWharton/pidcat)
Expand Down
4 changes: 2 additions & 2 deletions adbutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ def device(self,
if not serial:
ds = self.device_list()
if len(ds) == 0:
raise RuntimeError("Can't find any android device/emulator")
raise AdbError("Can't find any android device/emulator")
if len(ds) > 1:
raise RuntimeError(
raise AdbError(
"more than one device/emulator, please specify the serial number"
)
return ds[0]
Expand Down
19 changes: 11 additions & 8 deletions adbutils/_adb.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
from deprecation import deprecated

from adbutils._utils import adb_path
from adbutils.errors import AdbError, AdbTimeout
from adbutils.errors import AdbConnectionError, AdbError, AdbTimeout

from adbutils._proto import *
from adbutils._utils import list2cmdline
from adbutils._version import __version__

_OKAY = "OKAY"
Expand All @@ -30,15 +29,15 @@ def _check_server(host: str, port: int) -> bool:
""" Returns if server is running """
s = socket.socket()
try:
s.settimeout(.1)
s.connect((host, port))
return True
except socket.error as e:
except (socket.timeout, socket.error) as e:
return False
finally:
s.close()



class AdbConnection(object):
def __init__(self, host: str, port: int):
self.__host = host
Expand All @@ -52,16 +51,20 @@ def _create_socket(self):
adb_port = self.__port
s = socket.socket()
try:
s.settimeout(.1) # prevent socket hang
s.connect((adb_host, adb_port))
s.settimeout(None)
return s
except:
s.close()
raise
except socket.timeout as e:
raise AdbTimeout("connect to adb server timeout")
except socket.error as e:
raise AdbConnectionError("connect to adb server failed: %s" % e)


def _safe_connect(self):
try:
return self._create_socket()
except ConnectionRefusedError:
except AdbConnectionError:
subprocess.run([adb_path(), "start-server"], timeout=20.0) # 20s should enough for adb start
return self._create_socket()

Expand Down
6 changes: 3 additions & 3 deletions adbutils/_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def open_transport(self,
elif self._serial:
c.send_command(f"host-serial:{self._serial}:{command}")
else:
raise RuntimeError
raise RuntimeError("should not reach here")
c.check_okay()
else:
if self._transport_id:
Expand All @@ -108,7 +108,7 @@ def open_transport(self,
# so here use host:transport
c.send_command(f"host:transport:{self._serial}")
else:
raise RuntimeError
raise RuntimeError("should not reach here")
c.check_okay()
return c

Expand Down Expand Up @@ -575,7 +575,7 @@ def iter_content(self, path: str) -> typing.Iterator[bytes]:
chunk_size = struct.unpack("<I", c.read(4))[0]
chunk = c.read(chunk_size)
if len(chunk) != chunk_size:
raise RuntimeError("read chunk missing")
raise AdbError("read chunk missing")
yield chunk
else:
raise AdbError("Invalid sync cmd", cmd)
Expand Down
2 changes: 1 addition & 1 deletion adbutils/_proto.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

__all__ = [
"Network", "DeviceEvent", "ForwardItem", "ReverseItem", "FileInfo",
"WindowSize", "RunningAppInfo", "ShellReturn", "AdbDeviceInfo"
"WindowSize", "RunningAppInfo", "ShellReturn", "AdbDeviceInfo", "AppInfo"
]

import enum
Expand Down
10 changes: 6 additions & 4 deletions adbutils/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
import typing
import pathlib

import whichcraft
from shutil import which
from apkutils2.axml.axmlparser import AXML
from apkutils2.manifest import Manifest

from adbutils.errors import AdbError


MB = 1024 * 1024

Expand Down Expand Up @@ -52,7 +54,7 @@ def get_free_port():
port = random.randint(10000, 20000)
if not is_port_in_use(port):
return port
raise RuntimeError("No free port found")
raise AdbError("No free port found")


def list2cmdline(args: typing.Union[list, tuple]):
Expand Down Expand Up @@ -93,7 +95,7 @@ def adb_path():
return os.getenv("ADBUTILS_ADB_PATH")

# 1. find in $PATH
exe = whichcraft.which("adb")
exe = which("adb")
if exe and _is_valid_exe(exe):
return exe

Expand All @@ -103,7 +105,7 @@ def adb_path():
if os.path.isfile(exe) and _is_valid_exe(exe):
return exe

raise RuntimeError("No adb exe could be found. Install adb on your system")
raise AdbError("No adb exe could be found. Install adb on your system")


def _popen_kwargs(prevent_sigint=False):
Expand Down
4 changes: 4 additions & 0 deletions adbutils/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class AdbTimeout(AdbError):
""" timeout when communicate to adb-server """


class AdbConnectionError(AdbError):
""" connection error """


class AdbInstallError(AdbError):
def __init__(self, output: str):
"""
Expand Down
1 change: 0 additions & 1 deletion adbutils/pidcat.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import argparse
import sys
import re
import subprocess
from subprocess import PIPE

import adbutils
Expand Down
58 changes: 0 additions & 58 deletions adbutils/server/__init__.py

This file was deleted.

2 changes: 2 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
codecov:
token: 59cb8341-87db-45bd-b828-e8ae19cd4062
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
whichcraft
requests
deprecation>=2.0.6,<3.0
retry>=0.9
Expand Down
Loading

0 comments on commit 0328f2e

Please sign in to comment.