Repository navigation
Expand file tree
/
Copy pathclient.py
More file actions
202 lines (184 loc) · 5.93 KB
/
Copy pathclient.py
File metadata and controls
202 lines (184 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
"""Dependency-free ADB discovery and transport shared by Android-family targets."""
from __future__ import annotations
from dataclasses import dataclass
import os
from pathlib import Path
import re
import shutil
import subprocess
from typing import Any, Sequence
class AdbError(RuntimeError):
"""ADB is unavailable or cannot satisfy a transport operation."""
@dataclass(frozen=True)
class AdbDevice:
serial: str
state: str
attributes: dict[str, str]
def find_adb(
explicit: str | None = None,
*,
environment_variable: str | None = None,
) -> str:
candidates: list[Path] = []
configured = explicit
if not configured and environment_variable:
configured = os.environ.get(environment_variable)
if configured:
candidates.append(Path(configured).expanduser())
path_adb = shutil.which("adb")
if path_adb:
candidates.append(Path(path_adb))
for variable in ("ANDROID_HOME", "ANDROID_SDK_ROOT"):
value = os.environ.get(variable)
if value:
candidates.append(Path(value).expanduser() / "platform-tools" / "adb")
local_app_data = os.environ.get("LOCALAPPDATA")
if local_app_data:
candidates.append(
Path(local_app_data) / "Android" / "Sdk" / "platform-tools" / "adb.exe"
)
candidates.extend(
[
Path.home() / "Android" / "Sdk" / "platform-tools" / "adb",
Path.home() / "Library" / "Android" / "sdk" / "platform-tools" / "adb",
]
)
for candidate in candidates:
if candidate.is_file():
return str(candidate)
setting = f" or set {environment_variable}" if environment_variable else ""
raise AdbError(
f"adb was not found; install Android SDK Platform Tools{setting}"
)
class AdbClient:
def __init__(
self,
adb_path: str | None = None,
*,
serial: str | None = None,
adb_environment_variable: str | None = None,
) -> None:
self.adb = find_adb(
adb_path, environment_variable=adb_environment_variable
)
self.requested_serial = serial
def run(
self,
argv: Sequence[str],
*,
serial: str | None = None,
check: bool = True,
capture: bool = True,
text: bool = True,
input_data: str | bytes | None = None,
timeout: float | None = None,
) -> subprocess.CompletedProcess[Any]:
command = [self.adb]
if serial:
command.extend(["-s", serial])
command.extend(argv)
return subprocess.run(
command,
check=check,
capture_output=capture,
text=text,
stdin=subprocess.DEVNULL if input_data is None else None,
input=input_data,
timeout=timeout,
)
def shell(
self,
serial: str,
argv: Sequence[str],
*,
check: bool = True,
capture: bool = True,
timeout: float | None = None,
) -> subprocess.CompletedProcess[str]:
return self.run(
["shell", *argv],
serial=serial,
check=check,
capture=capture,
text=True,
timeout=timeout,
)
def shell_text(
self,
serial: str,
argv: Sequence[str],
*,
check: bool = True,
timeout: float | None = None,
) -> str:
return self.shell(
serial, argv, check=check, timeout=timeout
).stdout.replace("\r", "").strip()
def devices(self) -> list[AdbDevice]:
result = self.run(["devices", "-l"])
devices: list[AdbDevice] = []
for raw_line in result.stdout.replace("\r", "").splitlines()[1:]:
line = raw_line.strip()
if not line or line.startswith("*"):
continue
fields = line.split()
if len(fields) < 2:
continue
attributes: dict[str, str] = {}
for field in fields[2:]:
if ":" in field:
key, value = field.split(":", 1)
attributes[key] = value
devices.append(AdbDevice(fields[0], fields[1], attributes))
return devices
def parse_battery(text: str) -> dict[str, Any]:
result: dict[str, Any] = {
"level": None,
"status": None,
"ac_powered": False,
"usb_powered": False,
"wireless_powered": False,
"dock_powered": False,
}
keys = {
"AC powered": "ac_powered",
"USB powered": "usb_powered",
"Wireless powered": "wireless_powered",
"Dock powered": "dock_powered",
}
for raw_line in text.replace("\r", "").splitlines():
line = raw_line.strip()
if line.startswith("level:"):
value = line.split(":", 1)[1].strip()
result["level"] = int(value) if value.isdecimal() else None
elif line.startswith("status:"):
value = line.split(":", 1)[1].strip()
result["status"] = int(value) if value.isdecimal() else None
else:
for label, key in keys.items():
if line.startswith(label + ":"):
result[key] = line.split(":", 1)[1].strip().lower() == "true"
result["powered"] = any(
result[key]
for key in ("ac_powered", "usb_powered", "wireless_powered", "dock_powered")
)
return result
def parse_wake_state(text: str) -> str:
patterns = (
r"mWakefulness=([A-Za-z]+)",
r"Wakefulness:\s*([A-Za-z]+)",
r"Display Power:\s*state=([A-Za-z]+)",
)
for pattern in patterns:
match = re.search(pattern, text)
if not match:
continue
value = match.group(1).lower()
if value in {"awake", "on"}:
return "awake"
if value in {"asleep", "off"}:
return "asleep"
if value in {"dozing", "doze"}:
return "dozing"
return value
return "unknown"