From b7d115d58fe7a0d8189e1bf48ab8397f47f6044a Mon Sep 17 00:00:00 2001 From: Austin Bozowski Date: Fri, 3 Nov 2023 01:44:58 -0700 Subject: [PATCH] Refactor --- src/tools/interop/idt/capture/pcap/pcap.py | 2 + .../idt/capture/platform/android/android.py | 2 + .../idt/capture/platform/android/config.py | 3 +- src/tools/interop/idt/config.py | 2 - src/tools/interop/idt/idt.py | 27 ++---------- src/tools/interop/idt/utils/host_platform.py | 41 +++++++++++++++++++ 6 files changed, 49 insertions(+), 28 deletions(-) diff --git a/src/tools/interop/idt/capture/pcap/pcap.py b/src/tools/interop/idt/capture/pcap/pcap.py index 2cb2285908b066..17b711e27130aa 100644 --- a/src/tools/interop/idt/capture/pcap/pcap.py +++ b/src/tools/interop/idt/capture/pcap/pcap.py @@ -19,6 +19,7 @@ import time from utils.artifact import create_standard_log_name, log +from utils.host_platform import verify_host_dependencies from utils.shell import Bash logger = log.get_logger(__file__) @@ -27,6 +28,7 @@ class PacketCaptureRunner: def __init__(self, artifact_dir: str, interface: str) -> None: + verify_host_dependencies(["tcpdump"]) self.logger = logger self.artifact_dir = artifact_dir self.output_path = str( diff --git a/src/tools/interop/idt/capture/platform/android/android.py b/src/tools/interop/idt/capture/platform/android/android.py index e9e0ba2345d31c..d4bc4ba5902988 100644 --- a/src/tools/interop/idt/capture/platform/android/android.py +++ b/src/tools/interop/idt/capture/platform/android/android.py @@ -21,6 +21,7 @@ import typing from capture.base import PlatformLogStreamer +from utils.host_platform import verify_host_dependencies from utils.shell import Bash, log from . import config, streams @@ -32,6 +33,7 @@ class Android(PlatformLogStreamer): def __init__(self, artifact_dir: str) -> None: + verify_host_dependencies(["adb"]) self.logger = logger self.artifact_dir = artifact_dir self.device_id: str | None = None diff --git a/src/tools/interop/idt/capture/platform/android/config.py b/src/tools/interop/idt/capture/platform/android/config.py index 18d62437711119..a0b05cd57a68cd 100644 --- a/src/tools/interop/idt/capture/platform/android/config.py +++ b/src/tools/interop/idt/capture/platform/android/config.py @@ -16,6 +16,5 @@ # enable_build_push_tcpdump = True -# TODO: Replace -enable_bug_report = False +enable_bug_report = True hci_log_level = "full" diff --git a/src/tools/interop/idt/config.py b/src/tools/interop/idt/config.py index 1fb447182e4b4a..a7c6cfecc36bc0 100644 --- a/src/tools/interop/idt/config.py +++ b/src/tools/interop/idt/config.py @@ -18,5 +18,3 @@ enable_color = True log_level = logging.INFO -# TODO: RE-Enable -enable_generic_prober_in_capture = False diff --git a/src/tools/interop/idt/idt.py b/src/tools/interop/idt/idt.py index 0af18addc58249..1b4485b685e0b7 100644 --- a/src/tools/interop/idt/idt.py +++ b/src/tools/interop/idt/idt.py @@ -26,6 +26,7 @@ from capture import EcosystemController, EcosystemFactory, PacketCaptureRunner, PlatformFactory from utils.artifact import create_file_timestamp, safe_mkdir +from utils.host_platform import get_available_interfaces from utils.shell import Bash from discovery import MatterBleScanner, MatterDnssdListener from utils.log import border_print @@ -82,12 +83,8 @@ def __init__(self) -> None: self.available_ecosystems_default = 'ALL' self.available_ecosystems.append(self.available_ecosystems_default) - net_interface_path = "/sys/class/net/" - self.available_net_interfaces = os.listdir(net_interface_path) \ - if os.path.exists(net_interface_path) \ - else [] - self.available_net_interfaces.append("any") - self.available_net_interfaces_default = "any" + self.available_net_interfaces = get_available_interfaces() + self.available_net_interfaces_default = "any" if "any" in self.available_net_interfaces else None self.pcap_artifact_dir = os.path.join(self.artifact_dir, "pcap") self.net_interface_required = self.available_net_interfaces_default is None @@ -97,25 +94,7 @@ def __init__(self) -> None: self.process_args() - @staticmethod - def command_is_available(cmd_name) -> bool: - cmd = Bash(f"which {cmd_name}", sync=True, capture_output=True) - cmd.start_command() - return cmd.finished_success() - - def verify_host_dependencies(self) -> None: - deps = ["tcpdump", "adb"] - missing_deps = [] - for dep in deps: - if not self.command_is_available(dep): - missing_deps.append(dep) - if len(missing_deps) > 0: - for missing_dep in missing_deps: - border_print(f"Missing dependency, please install {missing_dep}!") - sys.exit(1) - def process_args(self) -> None: - self.verify_host_dependencies() # TODO: host dependency should be checked *per feature* parser = argparse.ArgumentParser( prog="idt", description="Interop Debugging Tool for Matter") diff --git a/src/tools/interop/idt/utils/host_platform.py b/src/tools/interop/idt/utils/host_platform.py index caa441c49d91cf..c92d69567534b8 100644 --- a/src/tools/interop/idt/utils/host_platform.py +++ b/src/tools/interop/idt/utils/host_platform.py @@ -17,7 +17,15 @@ import os import platform as host_platform +import sys +import psutil + +from utils import log +from utils.log import border_print +from utils.shell import Bash + +logger = log.get_logger(__file__) def is_mac(): p = host_platform.platform().lower() @@ -35,3 +43,36 @@ def get_ll_interface(): for interface in available_net_interfaces: if "wl" in interface: return interface + + +def get_available_interfaces(): + net_interface_path = "/sys/class/net/" + available_net_interfaces = os.listdir(net_interface_path) \ + if os.path.exists(net_interface_path) \ + else [] + available_net_interfaces.append("any") + return available_net_interfaces + + +def command_is_available(cmd_name) -> bool: + cmd = Bash(f"which {cmd_name}", sync=True, capture_output=True) + cmd.start_command() + return cmd.finished_success() + + +def verify_host_dependencies(deps: [str]) -> None: + missing_deps = [] + for dep in deps: + logger.info(f"Verifying host dependency {dep}") + if not command_is_available(dep): + missing_deps.append(dep) + if len(missing_deps) > 0: + for missing_dep in missing_deps: + border_print(f"Missing dependency, please install {missing_dep}!", important=True) + kill_idt_children() + sys.exit(1) + + +def kill_idt_children() -> None: + for child_proc in psutil.Process(os.getpid()).children(recursive=True): + Bash("").stop_single_proc(child_proc)