2021-01-14 03:21:49 +00:00
|
|
|
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
|
# Licensed under the MIT License.
|
|
|
|
|
|
|
|
|
|
import collections
|
|
|
|
|
import contextlib
|
2023-10-14 22:42:36 +00:00
|
|
|
import datetime
|
2021-01-14 03:21:49 +00:00
|
|
|
import os
|
|
|
|
|
import shutil
|
|
|
|
|
import signal
|
|
|
|
|
import subprocess
|
|
|
|
|
import time
|
|
|
|
|
import typing
|
|
|
|
|
|
2023-10-14 22:42:36 +00:00
|
|
|
from ..logger import get_logger
|
|
|
|
|
from ..platform_helpers import is_linux, is_windows
|
2022-04-26 16:35:16 +00:00
|
|
|
from ..run import run
|
2021-01-14 03:21:49 +00:00
|
|
|
|
2023-10-14 22:42:36 +00:00
|
|
|
_log = get_logger("util.android")
|
2021-01-14 03:21:49 +00:00
|
|
|
|
|
|
|
|
|
2022-04-26 16:35:16 +00:00
|
|
|
SdkToolPaths = collections.namedtuple("SdkToolPaths", ["emulator", "adb", "sdkmanager", "avdmanager"])
|
2021-01-14 03:21:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_sdk_tool_paths(sdk_root: str):
|
|
|
|
|
def filename(name, windows_extension):
|
|
|
|
|
if is_windows():
|
2023-03-24 22:29:03 +00:00
|
|
|
return f"{name}.{windows_extension}"
|
2021-01-14 03:21:49 +00:00
|
|
|
else:
|
|
|
|
|
return name
|
|
|
|
|
|
|
|
|
|
def resolve_path(dirnames, basename):
|
|
|
|
|
dirnames.insert(0, "")
|
|
|
|
|
for dirname in dirnames:
|
2023-10-14 22:42:36 +00:00
|
|
|
path = shutil.which(os.path.join(os.path.expanduser(dirname), basename))
|
2021-01-14 03:21:49 +00:00
|
|
|
if path is not None:
|
|
|
|
|
path = os.path.realpath(path)
|
2023-03-24 22:29:03 +00:00
|
|
|
_log.debug(f"Found {basename} at {path}")
|
2021-01-14 03:21:49 +00:00
|
|
|
return path
|
2023-03-24 22:29:03 +00:00
|
|
|
raise FileNotFoundError(f"Failed to resolve path for {basename}")
|
2021-01-14 03:21:49 +00:00
|
|
|
|
|
|
|
|
return SdkToolPaths(
|
2022-04-26 16:35:16 +00:00
|
|
|
emulator=resolve_path([os.path.join(sdk_root, "emulator")], filename("emulator", "exe")),
|
|
|
|
|
adb=resolve_path([os.path.join(sdk_root, "platform-tools")], filename("adb", "exe")),
|
2021-01-14 03:21:49 +00:00
|
|
|
sdkmanager=resolve_path(
|
2023-01-20 17:27:47 +00:00
|
|
|
[os.path.join(sdk_root, "cmdline-tools", "latest", "bin")],
|
2022-04-26 16:35:16 +00:00
|
|
|
filename("sdkmanager", "bat"),
|
|
|
|
|
),
|
2021-01-14 03:21:49 +00:00
|
|
|
avdmanager=resolve_path(
|
2023-01-20 17:27:47 +00:00
|
|
|
[os.path.join(sdk_root, "cmdline-tools", "latest", "bin")],
|
2022-04-26 16:35:16 +00:00
|
|
|
filename("avdmanager", "bat"),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_virtual_device(sdk_tool_paths: SdkToolPaths, system_image_package_name: str, avd_name: str):
|
|
|
|
|
run(sdk_tool_paths.sdkmanager, "--install", system_image_package_name, input=b"y")
|
|
|
|
|
|
|
|
|
|
run(
|
|
|
|
|
sdk_tool_paths.avdmanager,
|
|
|
|
|
"create",
|
|
|
|
|
"avd",
|
|
|
|
|
"--name",
|
|
|
|
|
avd_name,
|
|
|
|
|
"--package",
|
|
|
|
|
system_image_package_name,
|
2021-01-14 03:21:49 +00:00
|
|
|
"--force",
|
2022-04-26 16:35:16 +00:00
|
|
|
input=b"no",
|
|
|
|
|
)
|
2021-01-14 03:21:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
_process_creationflags = subprocess.CREATE_NEW_PROCESS_GROUP if is_windows() else 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _start_process(*args) -> subprocess.Popen:
|
2023-03-24 22:29:03 +00:00
|
|
|
_log.debug(f"Starting process - args: {[*args]}")
|
2021-01-14 03:21:49 +00:00
|
|
|
return subprocess.Popen([*args], creationflags=_process_creationflags)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_stop_signal = signal.CTRL_BREAK_EVENT if is_windows() else signal.SIGTERM
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _stop_process(proc: subprocess.Popen):
|
2023-10-14 22:42:36 +00:00
|
|
|
if proc.returncode is not None:
|
|
|
|
|
# process has exited
|
|
|
|
|
return
|
|
|
|
|
|
2023-03-24 22:29:03 +00:00
|
|
|
_log.debug(f"Stopping process - args: {proc.args}")
|
2021-01-14 03:21:49 +00:00
|
|
|
proc.send_signal(_stop_signal)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
proc.wait(30)
|
|
|
|
|
except subprocess.TimeoutExpired:
|
|
|
|
|
_log.warning("Timeout expired, forcibly stopping process...")
|
|
|
|
|
proc.kill()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _stop_process_with_pid(pid: int):
|
2023-10-14 22:42:36 +00:00
|
|
|
# minimize scope of external module usage
|
|
|
|
|
import psutil
|
|
|
|
|
|
|
|
|
|
if psutil.pid_exists(pid):
|
|
|
|
|
process = psutil.Process(pid)
|
|
|
|
|
_log.debug(f"Stopping process - pid={pid}")
|
|
|
|
|
process.terminate()
|
|
|
|
|
try:
|
|
|
|
|
process.wait(60)
|
|
|
|
|
except psutil.TimeoutExpired:
|
|
|
|
|
print("Process did not terminate within 60 seconds. Killing.")
|
|
|
|
|
process.kill()
|
|
|
|
|
time.sleep(10)
|
|
|
|
|
if psutil.pid_exists(pid):
|
|
|
|
|
print(f"Process still exists. State:{process.status()}")
|
|
|
|
|
else:
|
|
|
|
|
_log.debug(f"No process exists with pid={pid}")
|
2021-01-14 03:21:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def start_emulator(
|
2022-04-26 16:35:16 +00:00
|
|
|
sdk_tool_paths: SdkToolPaths, avd_name: str, extra_args: typing.Optional[typing.Sequence[str]] = None
|
|
|
|
|
) -> subprocess.Popen:
|
|
|
|
|
with contextlib.ExitStack() as emulator_stack, contextlib.ExitStack() as waiter_stack:
|
2021-01-14 03:21:49 +00:00
|
|
|
emulator_args = [
|
2022-04-26 16:35:16 +00:00
|
|
|
sdk_tool_paths.emulator,
|
|
|
|
|
"-avd",
|
|
|
|
|
avd_name,
|
|
|
|
|
"-memory",
|
|
|
|
|
"4096",
|
|
|
|
|
"-timezone",
|
|
|
|
|
"America/Los_Angeles",
|
2023-10-14 22:42:36 +00:00
|
|
|
"-no-snapstorage",
|
2021-01-14 03:21:49 +00:00
|
|
|
"-no-audio",
|
|
|
|
|
"-no-boot-anim",
|
2023-10-14 22:42:36 +00:00
|
|
|
"-gpu",
|
|
|
|
|
"guest",
|
|
|
|
|
"-delay-adb",
|
2022-04-26 16:35:16 +00:00
|
|
|
]
|
2023-10-14 22:42:36 +00:00
|
|
|
|
|
|
|
|
# For Linux CIs we must use "-no-window" otherwise you'll get
|
|
|
|
|
# Fatal: This application failed to start because no Qt platform plugin could be initialized
|
|
|
|
|
#
|
|
|
|
|
# For macOS CIs use a window so that we can potentially capture the desktop and the emulator screen
|
|
|
|
|
# and publish screenshot.jpg and emulator.png as artifacts to debug issues.
|
|
|
|
|
# screencapture screenshot.jpg
|
|
|
|
|
# $(ANDROID_SDK_HOME)/platform-tools/adb exec-out screencap -p > emulator.png
|
|
|
|
|
#
|
|
|
|
|
# On Windows it doesn't matter (AFAIK) so allow a window which is nicer for local debugging.
|
|
|
|
|
if is_linux():
|
|
|
|
|
emulator_args.append("-no-window")
|
|
|
|
|
|
2021-01-14 03:21:49 +00:00
|
|
|
if extra_args is not None:
|
|
|
|
|
emulator_args += extra_args
|
|
|
|
|
|
2022-04-26 16:35:16 +00:00
|
|
|
emulator_process = emulator_stack.enter_context(_start_process(*emulator_args))
|
2021-01-14 03:21:49 +00:00
|
|
|
emulator_stack.callback(_stop_process, emulator_process)
|
|
|
|
|
|
2023-10-14 22:42:36 +00:00
|
|
|
# we're specifying -delay-adb so use a trivial command to check when adb is available.
|
2021-01-14 03:21:49 +00:00
|
|
|
waiter_process = waiter_stack.enter_context(
|
|
|
|
|
_start_process(
|
2022-04-26 16:35:16 +00:00
|
|
|
sdk_tool_paths.adb,
|
|
|
|
|
"wait-for-device",
|
|
|
|
|
"shell",
|
2023-10-14 22:42:36 +00:00
|
|
|
"ls /data/local/tmp",
|
2022-04-26 16:35:16 +00:00
|
|
|
)
|
|
|
|
|
)
|
2023-10-14 22:42:36 +00:00
|
|
|
|
2021-01-14 03:21:49 +00:00
|
|
|
waiter_stack.callback(_stop_process, waiter_process)
|
|
|
|
|
|
2023-10-14 22:42:36 +00:00
|
|
|
# poll subprocesses.
|
|
|
|
|
# allow 20 minutes for startup as some CIs are slow. TODO: Make timeout configurable if needed.
|
|
|
|
|
sleep_interval_seconds = 10
|
|
|
|
|
end_time = datetime.datetime.now() + datetime.timedelta(minutes=20)
|
|
|
|
|
|
2021-01-14 03:21:49 +00:00
|
|
|
while True:
|
|
|
|
|
waiter_ret, emulator_ret = waiter_process.poll(), emulator_process.poll()
|
|
|
|
|
|
|
|
|
|
if emulator_ret is not None:
|
|
|
|
|
# emulator exited early
|
2023-03-24 22:29:03 +00:00
|
|
|
raise RuntimeError(f"Emulator exited early with return code: {emulator_ret}")
|
2021-01-14 03:21:49 +00:00
|
|
|
|
|
|
|
|
if waiter_ret is not None:
|
|
|
|
|
if waiter_ret == 0:
|
2023-10-14 22:42:36 +00:00
|
|
|
_log.debug("adb wait-for-device process has completed.")
|
2021-01-14 03:21:49 +00:00
|
|
|
break
|
2023-03-24 22:29:03 +00:00
|
|
|
raise RuntimeError(f"Waiter process exited with return code: {waiter_ret}")
|
2021-01-14 03:21:49 +00:00
|
|
|
|
2023-10-14 22:42:36 +00:00
|
|
|
if datetime.datetime.now() > end_time:
|
|
|
|
|
raise RuntimeError("Emulator startup timeout")
|
|
|
|
|
|
2021-01-14 03:21:49 +00:00
|
|
|
time.sleep(sleep_interval_seconds)
|
|
|
|
|
|
2023-10-14 22:42:36 +00:00
|
|
|
# emulator is started
|
2021-01-14 03:21:49 +00:00
|
|
|
emulator_stack.pop_all()
|
2023-10-14 22:42:36 +00:00
|
|
|
|
|
|
|
|
# loop to check for sys.boot_completed being set.
|
|
|
|
|
# in theory `-delay-adb` should be enough but this extra check seems to be required to be sure.
|
|
|
|
|
while True:
|
|
|
|
|
# looping on device with `while` seems to be flaky so loop here and call getprop once
|
|
|
|
|
args = [
|
|
|
|
|
sdk_tool_paths.adb,
|
|
|
|
|
"shell",
|
|
|
|
|
# "while [[ -z $(getprop sys.boot_completed) | tr -d '\r' ]]; do sleep 5; done; input keyevent 82",
|
|
|
|
|
"getprop sys.boot_completed",
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
_log.debug(f"Starting process - args: {args}")
|
|
|
|
|
|
|
|
|
|
getprop_output = subprocess.check_output(args, timeout=10)
|
|
|
|
|
getprop_value = bytes.decode(getprop_output).strip()
|
|
|
|
|
|
|
|
|
|
if getprop_value == "1":
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
elif datetime.datetime.now() > end_time:
|
|
|
|
|
raise RuntimeError("Emulator startup timeout. sys.boot_completed was not set.")
|
|
|
|
|
|
|
|
|
|
_log.debug(f"sys.boot_completed='{getprop_value}'. Sleeping for {sleep_interval_seconds} before retrying.")
|
|
|
|
|
time.sleep(sleep_interval_seconds)
|
|
|
|
|
|
2021-01-14 03:21:49 +00:00
|
|
|
return emulator_process
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def stop_emulator(emulator_proc_or_pid: typing.Union[subprocess.Popen, int]):
|
|
|
|
|
if isinstance(emulator_proc_or_pid, subprocess.Popen):
|
|
|
|
|
_stop_process(emulator_proc_or_pid)
|
|
|
|
|
elif isinstance(emulator_proc_or_pid, int):
|
|
|
|
|
_stop_process_with_pid(emulator_proc_or_pid)
|
|
|
|
|
else:
|
|
|
|
|
raise ValueError("Expected either a PID or subprocess.Popen instance.")
|