Skip to content

Commit 08ea28d

Browse files
authored
Remove pkg resources (#105)
* use importlib instead of pkg_resource, fix dump_hierarchy when ERROR appear * do little fix on github actions
1 parent 075e678 commit 08ea28d

8 files changed

Lines changed: 46 additions & 18 deletions

File tree

.github/dependabot.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ updates:
33
- package-ecosystem: pip
44
directory: "/"
55
schedule:
6-
interval: daily
6+
interval: weekly
77
open-pull-requests-limit: 10

.github/workflows/publish-to-pypi.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
name: Publish Python 🐍 distributions 📦 to PyPI
2-
on: push
2+
on:
3+
push:
4+
tags:
5+
- '*'
36
jobs:
47
build-n-publish:
58
name: Build and publish Python 🐍 distributions 📦 to PyPI

adbutils/_device.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,7 @@ def keyevent(self, key_code: typing.Union[int, str]) -> str:
786786
def __is_percent(self, v):
787787
return isinstance(v, float) and v <= 1.0
788788

789-
def click(self, x, y):
789+
def click(self, x, y) -> None:
790790
"""
791791
simulate android tap
792792
@@ -799,9 +799,9 @@ def click(self, x, y):
799799
x = int(x * w) if is_percent(x) else x
800800
y = int(y * h) if is_percent(y) else y
801801
x, y = map(str, [x, y])
802-
return self.shell(['input', 'tap', x, y])
802+
self.shell(['input', 'tap', x, y])
803803

804-
def swipe(self, sx, sy, ex, ey, duration: float = 1.0):
804+
def swipe(self, sx, sy, ex, ey, duration: float = 1.0) -> None:
805805
"""
806806
swipe from start point to end point
807807
@@ -817,7 +817,7 @@ def swipe(self, sx, sy, ex, ey, duration: float = 1.0):
817817
ex = int(ex * w) if is_percent(ex) else ex
818818
ey = int(ey * h) if is_percent(ey) else ey
819819
x1, y1, x2, y2 = map(str, [sx, sy, ex, ey])
820-
return self.shell(
820+
self.shell(
821821
['input', 'swipe', x1, y1, x2, y2,
822822
str(int(duration * 1000))])
823823

@@ -1225,15 +1225,19 @@ def dump_hierarchy(self) -> str:
12251225
Raises:
12261226
AdbError
12271227
"""
1228+
target = '/data/local/tmp/uidump.xml'
12281229
output = self.shell(
1229-
'uiautomator dump /data/local/tmp/uidump.xml && echo success')
1230-
if "success" not in output:
1230+
f'rm -f {target}; uiautomator dump {target} && echo success')
1231+
if 'ERROR' in output or 'success' not in output:
12311232
raise AdbError("uiautomator dump failed", output)
12321233

12331234
buf = b''
1234-
for chunk in self.sync.iter_content("/data/local/tmp/uidump.xml"):
1235+
for chunk in self.sync.iter_content(target):
12351236
buf += chunk
1236-
return buf.decode("utf-8")
1237+
xml_data = buf.decode("utf-8")
1238+
if not xml_data.startswith('<?xml'):
1239+
raise AdbError("dump output is not xml", xml_data)
1240+
return xml_data
12371241

12381242
@retry(AdbError, delay=.5, tries=3, jitter=.1)
12391243
def app_current(self) -> RunningAppInfo:

adbutils/_utils.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import hashlib
2+
import importlib.resources
23
import os
34
import random
45
import shlex
@@ -16,7 +17,7 @@
1617
import whichcraft
1718
from apkutils2.axml.axmlparser import AXML
1819
from apkutils2.manifest import Manifest
19-
from pkg_resources import resource_filename
20+
2021

2122
MB = 1024 * 1024
2223

@@ -74,6 +75,18 @@ def current_ip():
7475
finally:
7576
s.close()
7677

78+
def _get_bin_dir():
79+
if sys.version_info < (3, 9):
80+
context = importlib.resources.path("adbutils.binaries", "__init__.py")
81+
else:
82+
ref = importlib.resources.files("adbutils.binaries") / "__init__.py"
83+
context = importlib.resources.as_file(ref)
84+
with context as path:
85+
pass
86+
# Return the dir. We assume that the data files are on a normal dir on the fs.
87+
return str(path.parent)
88+
89+
7790
def adb_path():
7891
# 0. check env: ADBUTILS_ADB_PATH
7992
if os.getenv("ADBUTILS_ADB_PATH"):
@@ -85,7 +98,7 @@ def adb_path():
8598
return exe
8699

87100
# 2. use buildin adb
88-
bin_dir = resource_filename("adbutils", "binaries")
101+
bin_dir = _get_bin_dir()
89102
exe = os.path.join(bin_dir, "adb.exe" if os.name == 'nt' else 'adb')
90103
if os.path.isfile(exe) and _is_valid_exe(exe):
91104
return exe

adbutils/_version.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
"""Created on Fri May 06 2022 10:54:04 by codeskyblue
55
"""
66

7-
import pkg_resources
87

8+
from importlib.metadata import version, PackageNotFoundError
99

1010
try:
11-
__version__ = pkg_resources.get_distribution("adbutils").version
12-
except pkg_resources.DistributionNotFound:
11+
__version__ = version("adbutils")
12+
except PackageNotFoundError:
1313
__version__ = "unknown"

adbutils/binaries/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Just here to make importlib.resources work

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616
if sys.argv[-1] == "build_wheel":
1717
subprocess.call([sys.executable, "build_wheel.py"])
1818
else:
19-
setuptools.setup(setup_requires=['pbr'], python_requires='>=3.7', pbr=True)
19+
setuptools.setup(setup_requires=['pbr'], python_requires='>=3.8', pbr=True)

tests/test_utils.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
"""Created on Fri Dec 02 2022 17:00:03 by codeskyblue
55
"""
66

7+
import os
78
import pytest
89
from adbutils import StopEvent
9-
from adbutils._utils import get_free_port
10+
from adbutils._utils import get_free_port, _get_bin_dir
1011

1112

1213
def test_stop_event():
@@ -28,4 +29,10 @@ def test_stop_event():
2829

2930
def test_get_free_port():
3031
port = get_free_port()
31-
assert port > 0
32+
assert port > 0
33+
34+
35+
def test_get_bin_dir():
36+
_dir = _get_bin_dir()
37+
assert isinstance(_dir, str)
38+
assert "README.md" in os.listdir(_dir)

0 commit comments

Comments
 (0)