Files
LihonT/sdk/lihont-ltp/lihont_ltp/phone.py
2026-11-01 17:52:48 +03:00

46 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Отправка .ltp на телефон по USB-кабелю через adb."""
import os
import shutil
import subprocess
from pathlib import Path
from .errors import LtpError
REMOTE = "/sdcard/Download/ltp"
def find_adb():
found = shutil.which("adb")
if found:
return found
candidates = [
Path(os.environ.get("LOCALAPPDATA", "")) / "Android" / "Sdk" / "platform-tools" / "adb.exe",
Path.home() / "AppData" / "Local" / "Android" / "Sdk" / "platform-tools" / "adb.exe",
Path.home() / "Android" / "Sdk" / "platform-tools" / "adb",
]
for candidate in candidates:
if candidate.is_file():
return str(candidate)
raise LtpError("Не нашёл adb. Поставь Android platform-tools и добавь в PATH, или скопируй .ltp на телефон вручную "
"(любым способом) в папку Download/ltp")
def push(path):
path = Path(path)
if not path.is_file():
raise LtpError("Файл %s не найден" % path)
adb = find_adb()
devices = subprocess.run([adb, "devices"], capture_output=True, text=True, timeout=30).stdout
lines = [line for line in devices.splitlines()[1:] if line.strip()]
if not any(line.split()[-1] == "device" for line in lines):
raise LtpError("Телефон не виден. Подключи кабель, включи «Отладку по USB» и подтверди запрос на телефоне")
remote = "%s/%s" % (REMOTE, path.name)
done = subprocess.run([adb, "push", str(path), remote], capture_output=True, text=True, timeout=120)
if done.returncode != 0:
raise LtpError("Не получилось отправить файл: %s" % (done.stderr or done.stdout).strip())
# чтобы системный выбор файла сразу увидел новый файл
subprocess.run([adb, "shell", "content call --uri content://media/external/file --method scan_file --arg %s" % remote],
capture_output=True, text=True, timeout=30)
return remote