Skip to content

Commit 8d146c3

Browse files
authored
add command copy-assets, update find_jar_apk function (#1125)
1 parent 0c5e36c commit 8d146c3

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,6 +1219,10 @@ To specify a device, pass `--serial`, e.g., `python -m uiautomator2 --serial bff
12191219
uiautomator2 --serial <YOUR_DEVICE_SERIAL> screenshot screenshot.jpg
12201220
```
12211221

1222+
- `copy-assets`: Copy assets/ to current directory
1223+
1224+
Used for pyinstaller、nuitka
1225+
12221226
- `current`: Get current package name and activity
12231227

12241228
```bash

uiautomator2/__main__.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
import argparse
77
import json
88
import logging
9+
import pathlib
10+
import shutil
911
import sys
1012

1113
import adbutils
1214

1315
import uiautomator2 as u2
16+
from uiautomator2.utils import with_package_resource
1417
from uiautomator2.version import __version__
1518
from uiautomator2 import enable_pretty_logging
1619

@@ -49,6 +52,18 @@ def cmd_purge(args):
4952
logger.info("com.github.uiautomator uninstalled, all done !!!")
5053

5154

55+
def cmd_copy_assets(args):
56+
target_dir = pathlib.Path("assets")
57+
target_dir.mkdir(exist_ok=True)
58+
with with_package_resource("assets/u2.jar") as jar_path:
59+
target_path = target_dir / "u2.jar"
60+
shutil.copy2(jar_path, target_path)
61+
print("Copied u2.jar to", target_path)
62+
with with_package_resource("assets/app-uiautomator.apk") as apk_path:
63+
target_path = target_dir / "app-uiautomator.apk"
64+
shutil.copy2(apk_path, target_path)
65+
print("Copied app-uiautomator.apk to", target_path)
66+
5267
def cmd_screenshot(args):
5368
d = u2.connect(args.serial)
5469
d.screenshot().save(args.filename)
@@ -154,6 +169,11 @@ def cmd_console(args):
154169
),
155170
],
156171
),
172+
dict(
173+
action=cmd_copy_assets,
174+
command="copy-assets",
175+
help="copy uiautomator2 assets to current directory",
176+
),
157177
dict(
158178
action=cmd_screenshot,
159179
command="screenshot",

uiautomator2/utils.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import inspect
77
import pathlib
88
import shlex
9+
import sys
910
import threading
1011
import typing
1112
import warnings
@@ -35,9 +36,22 @@ def with_package_resource(filename: str) -> typing.Generator[pathlib.Path, None,
3536
from importlib_resources import as_file, files
3637
anchor = files("uiautomator2") / filename
3738
with as_file(anchor) as f:
38-
if not f.exists():
39-
raise FileNotFoundError(f"Resource {filename} not found in uiautomator2 package.")
40-
yield f
39+
if f.exists():
40+
yield f
41+
return
42+
43+
# check if binary folder contains
44+
binary_path = pathlib.Path(sys.argv[0])
45+
if (binary_path / filename).exists():
46+
yield binary_path / filename
47+
return
48+
49+
# check if running program directory contains
50+
if (pathlib.Path.cwd() / filename).exists():
51+
yield pathlib.Path.cwd() / filename
52+
return
53+
54+
raise FileNotFoundError(f"Resource {filename} not found in uiautomator2 package.")
4155

4256

4357
def check_alive(fn):

0 commit comments

Comments
 (0)