Skip to content

Commit

Permalink
Stop expanding env vars for adb_path.
Browse files Browse the repository at this point in the history
This change is also part of simplifying the construction of internal AndroidEnv
components by using strongly-typed dataclasses. This change removes environment
variable expansions for `AdbControllerConfig.adb_path` from internal
components, which simplifies the construction by not having to modify the path
when loading it.

PiperOrigin-RevId: 599905978
  • Loading branch information
kenjitoyama authored and copybara-github committed Jan 19, 2024
1 parent 1e7f33d commit 9f7cbcd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
17 changes: 9 additions & 8 deletions android_env/components/adb_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,8 @@ class AdbController:
def __init__(self, config: config_classes.AdbControllerConfig):
"""Instantiates an AdbController object."""

self._device_name = config.device_name
self._adb_path = os.path.expandvars(config.adb_path)
self._adb_server_port = str(config.adb_server_port)
self._default_timeout = config.default_timeout
logging.info('adb_path: %r', self._adb_path)
self._config = config
logging.info('config: %r', self._config)

# Unset problematic environment variables. ADB commands will fail if these
# are set. They are normally exported by AndroidStudio.
Expand All @@ -52,9 +49,13 @@ def __init__(self, config: config_classes.AdbControllerConfig):

def command_prefix(self, include_device_name: bool = True) -> list[str]:
"""The command for instantiating an adb client to this server."""
command_prefix = [self._adb_path, '-P', self._adb_server_port]
command_prefix = [
self._config.adb_path,
'-P',
str(self._config.adb_server_port),
]
if include_device_name:
command_prefix.extend(['-s', self._device_name])
command_prefix.extend(['-s', self._config.device_name])
return command_prefix

def init_server(self, timeout: float | None = None):
Expand Down Expand Up @@ -108,7 +109,7 @@ def execute_command(
Returns:
The output of running such command as a binary string.
"""
timeout = self._default_timeout if timeout is None else timeout
timeout = self._config.default_timeout if timeout is None else timeout
command = self.command_prefix(include_device_name=device_specific) + args
command_str = 'adb ' + ' '.join(command[1:])

Expand Down
7 changes: 5 additions & 2 deletions android_env/components/config_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@
class AdbControllerConfig:
"""Settings for instatiating an `AdbController` instance."""

# Name of the device to communicate with.
device_name: str = ''
# Filesystem path to the `adb` binary.
# NOTE: This must be a full path and must not contain environment variables
# or user folder shorthands (e.g. `~/some/path/to/adb`) since they will not be
# expanded internally by AndroidEnv.
adb_path: str = 'adb'
# Port for adb server.
adb_server_port: int = 5037
# Default timeout in seconds for internal commands.
default_timeout: float = 120.0
# Name of the device to communicate with.
device_name: str = ''


@dataclasses.dataclass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@ def __init__(
"""

self._adb_controller_config = adb_controller_config
self._adb_controller_config.adb_path = os.path.expandvars(
self._adb_controller_config.adb_path
)

self._adb_port = adb_port
self._emulator_console_port = emulator_console_port
self._grpc_port = grpc_port
Expand Down

0 comments on commit 9f7cbcd

Please sign in to comment.