Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
aBozowski committed Oct 20, 2023
1 parent c480a02 commit 0091588
Show file tree
Hide file tree
Showing 25 changed files with 131 additions and 60 deletions.
4 changes: 2 additions & 2 deletions src/tools/interop/idt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ Follow the Linux installation steps above, but do not use Docker.
> **_IMPORTANT_**
> `idt_` commands are shell aliases helpful for administrative commands.
> `idt` invokes the `idt` python package.
> Output from `idt` will always be colorized while output from subprocesses is white.
> `idt` invokes the `idt` python package.
> Output from `idt` will always be colorized while output from subprocesses is generally not.
RPi users, as needed:
Expand Down
1 change: 1 addition & 0 deletions src/tools/interop/idt/capture/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
import logging

log_level = logging.INFO
async_timeout = 45.0
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

import os

from capture.file_utils import add_border, create_standard_log_name
from capture.log_format import print_and_write
from capture.utils.artifact import create_standard_log_name
from capture.utils.log import print_and_write, add_border
from capture.platform.android import Android


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
import os
from typing import Dict

from capture import log_format
from capture.base import EcosystemCapture, UnsupportedCapturePlatformException
from capture.file_utils import create_standard_log_name
from capture.utils.artifact import create_standard_log_name
from capture.platform.android import Android

from . import config
Expand All @@ -30,13 +29,17 @@
from .prober import PlayServicesProber


from capture.utils import log

logger = log.get_logger(__file__)

class PlayServices(EcosystemCapture):
"""
Implementation of capture and analysis for Play Services
"""

def __init__(self, platform: Android, artifact_dir: str) -> None:
self.logger = log_format.get_logger(__file__)
self.logger = logger
self.artifact_dir = artifact_dir

if not isinstance(platform, Android):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@

import os

from capture import log_format
from capture.shell_utils import Bash
from capture.utils.shell import Bash


class PlayServicesProber:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import os

from capture.base import EcosystemCapture, UnsupportedCapturePlatformException
from capture.file_utils import create_standard_log_name
from capture.log_format import print_and_write
from capture.utils.artifact import create_standard_log_name
from capture.utils.log import print_and_write
from capture.platform.android.android import Android


Expand Down
20 changes: 9 additions & 11 deletions src/tools/interop/idt/capture/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,19 @@
import typing

import capture
from capture import log_format
from capture.utils import log
from capture.utils.async_control import get_timeout
from capture.base import EcosystemCapture, PlatformLogStreamer, UnsupportedCapturePlatformException
from capture.file_utils import safe_mkdir
from capture.log_format import border_print
from capture.utils.artifact import safe_mkdir
from capture.utils.log import border_print

_CONFIG_TIMEOUT = 45.0
_PLATFORM_MAP: typing.Dict[str, PlatformLogStreamer] = {}
_ECOSYSTEM_MAP: typing.Dict[str, PlatformLogStreamer] = {}
_ERROR_REPORT: typing.Dict[str, list[str]] = {}
logger = log_format.get_logger(__file__)

from capture.utils import log

def _get_timeout():
return asyncio.get_running_loop().time() + _CONFIG_TIMEOUT

logger = log.get_logger(__file__)

class PlatformFactory:

Expand Down Expand Up @@ -82,15 +80,15 @@ async def get_ecosystem_impl(

@staticmethod
async def init_ecosystems(platform, ecosystem, artifact_dir):
async with asyncio.timeout_at(_get_timeout()):
async with asyncio.timeout_at(get_timeout()):
platform = await PlatformFactory.get_platform_impl(
platform, artifact_dir)
ecosystems_to_load = EcosystemFactory.list_available_ecosystems() \
if ecosystem == 'ALL' \
else [ecosystem]
for ecosystem in ecosystems_to_load:
try:
async with asyncio.timeout_at(_get_timeout()):
async with asyncio.timeout_at(get_timeout()):
await EcosystemFactory.get_ecosystem_impl(
ecosystem, platform, artifact_dir)
except UnsupportedCapturePlatformException:
Expand All @@ -116,7 +114,7 @@ async def handle_capture(attr):
for ecosystem in _ECOSYSTEM_MAP:
try:
border_print(f"{attr} for {ecosystem}")
async with asyncio.timeout_at(_get_timeout()):
async with asyncio.timeout_at(get_timeout()):
await getattr(_ECOSYSTEM_MAP[ecosystem], attr)()
except TimeoutError:
logger.error(f"timeout {attr} {ecosystem}")
Expand Down
4 changes: 2 additions & 2 deletions src/tools/interop/idt/capture/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import traceback
from typing import Any

from capture import log_format
from capture.utils import log

logger = log_format.get_logger(__file__)
logger = log.get_logger(__file__)


class CaptureImplsLoader:
Expand Down
11 changes: 7 additions & 4 deletions src/tools/interop/idt/capture/pcap/pcap.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@
import os
import time

from capture import log_format
from capture.file_utils import create_standard_log_name
from capture.shell_utils import Bash
from capture.utils.artifact import create_standard_log_name
from capture.utils.shell import Bash

from capture.utils import log

logger = log.get_logger(__file__)


class PacketCaptureRunner:

def __init__(self, artifact_dir: str, interface: str) -> None:
self.logger = log_format.get_logger(__file__)
self.logger = logger
self.artifact_dir = artifact_dir
self.output_path = str(
os.path.join(
Expand Down
7 changes: 4 additions & 3 deletions src/tools/interop/idt/capture/platform/android/android.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@
import traceback
import typing

from capture import log_format
from capture.base import PlatformLogStreamer
from capture.shell_utils import Bash
from capture.utils.shell import Bash
from . import config
from . import streams
from .capabilities import Capabilities

logger = log_format.get_logger(__file__)
from capture.utils import log

logger = log.get_logger(__file__)


class Android(PlatformLogStreamer):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from typing import TYPE_CHECKING

from capture import log_format
from capture.shell_utils import Bash
from capture.utils.shell import Bash

if TYPE_CHECKING:
from capture.platform.android import Android

logger = log_format.get_logger(__file__)
from capture.utils import log

logger = log.get_logger(__file__)


class Capabilities:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# limitations under the License.
#

from capture.file_utils import create_standard_log_name
from capture.utils.artifact import create_standard_log_name
from typing import TYPE_CHECKING
from ..base import AndroidStream

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
# limitations under the License.
#

from .android_pcap import AndroidPcap
from .pcap import AndroidPcap

__all__ = ["AndroidPcap"]
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export CFLAGS=-static
export CPPFLAGS=-static
export LDFLAGS=-static

./configure --host=arm-linux # --disable-ipv6
./configure --host=arm-linux
make

aarch64-linux-gnu-strip tcpdump
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@
import asyncio
import os

from capture import log_format
from capture.file_utils import create_standard_log_name, safe_mkdir
from capture.shell_utils import Bash
from capture.utils.artifact import create_standard_log_name, safe_mkdir
from capture.utils.shell import Bash
from typing import TYPE_CHECKING
from ... import config
from ..base import AndroidStream

if TYPE_CHECKING:
from capture.platform.android import Android

logger = log_format.get_logger(__file__)
from capture.utils import log

logger = log.get_logger(__file__)


class AndroidPcap(AndroidStream):
Expand All @@ -53,16 +54,16 @@ async def pull_packet_capture(self) -> None:
self.pcap_pull = False

async def start(self):
safe_mkdir(self.build_dir)
if self.platform.capabilities.has_tcpdump:
self.logger.info("tcpdump already available on this phone; using!")
self.logger.info("tcpdump already available; using!")
self.pcap_proc.start_command()
self.pcap_pull = True
return
if not config.enable_build_push_tcpdump:
self.logger.critical("Android TCP Dump build and push disabled in configs!")
return
if not os.path.exists(os.path.join(self.build_dir, "tcpdump")):
safe_mkdir(self.build_dir)
self.logger.warning("tcpdump bin not found, attempting to build, please wait a few moments!")
build_script = os.path.join(os.path.dirname(__file__), "build_tcpdump_64.sh")
Bash(f"{build_script} 2>&1 >> BUILD_LOG.txt", sync=True, cwd=self.build_dir).start_command()
Expand All @@ -74,6 +75,7 @@ async def start(self):
self.platform.run_adb_command("chmod +x /sdcard/Movies/tcpdump")
else:
self.logger.info("tcpdump already in the expected location, not pushing!")
self.logger.info("Starting Android pcap command")
self.pcap_proc.start_command()
self.pcap_pull = True

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@
import asyncio
import os

from capture import log_format
from capture.file_utils import create_standard_log_name
from capture.utils import log
from capture.utils.artifact import create_standard_log_name
from typing import TYPE_CHECKING
from ..base import AndroidStream

if TYPE_CHECKING:
from capture.platform.android import Android

logger = log_format.get_logger(__file__)
from capture.utils import log

logger = log.get_logger(__file__)


class ScreenRecorder(AndroidStream):
Expand Down
25 changes: 25 additions & 0 deletions src/tools/interop/idt/capture/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#
# Copyright (c) 2023 Project CHIP Authors
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from . import artifact, async_control, log, shell

__all__ = [
'artifact',
'async_control',
'log',
'shell',
]
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@
from pathlib import Path


def add_border(to_print: str) -> str:
"""Add star borders to important strings"""
return '\n' + '*' * len(to_print) + '\n' + to_print


def create_file_timestamp() -> str:
"""Conventional file timestamp suffix"""
return time.strftime("%Y%m%d_%H%M%S")
Expand Down
24 changes: 24 additions & 0 deletions src/tools/interop/idt/capture/utils/async_control.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#
# Copyright (c) 2023 Project CHIP Authors
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import asyncio

from capture import config


def get_timeout():
return asyncio.get_running_loop().time() + config.async_timeout
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import logging
from typing import TextIO

from . import config
from capture import config

_CONFIG_LEVEL = config.log_level

Expand Down Expand Up @@ -74,3 +74,8 @@ def border_print(to_print: str, important: bool = False) -> None:
def print_and_write(to_print: str, file: TextIO) -> None:
print(f"\x1b[32;1m{to_print}\x1b[0m")
file.write(to_print)


def add_border(to_print: str) -> str:
"""Add star borders to important strings"""
return '\n' + '*' * len(to_print) + '\n' + to_print
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
import shlex
import subprocess

from capture import log_format
from mobly.utils import stop_standing_subprocess

logger = log_format.get_logger(__file__)
from capture.utils import log

logger = log.get_logger(__file__)


class Bash:
Expand Down
Loading

0 comments on commit 0091588

Please sign in to comment.