Skip to content

Commit d265639

Browse files
kenjitoyamaThe android_world Authors
authored andcommitted
Consolidate port picking in AndroidEnv loaders.
Update AndroidWorld to adapt to the new configuration fields while maintaining backward compatibility with older AndroidEnv releases. PiperOrigin-RevId: 945430390
1 parent f14341b commit d265639

3 files changed

Lines changed: 22 additions & 12 deletions

File tree

android_world/env/android_world_controller.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -311,16 +311,20 @@ def get_controller(
311311
) -> AndroidWorldController:
312312
"""Creates a controller by connecting to an existing Android environment."""
313313

314+
emulator_launcher = config_classes.EmulatorLauncherConfig(
315+
emulator_console_port=console_port,
316+
adb_port=console_port + 1,
317+
grpc_port=grpc_port,
318+
)
319+
if hasattr(emulator_launcher, 'connect_to_existing'):
320+
setattr(emulator_launcher, 'connect_to_existing', True)
321+
314322
config = config_classes.AndroidEnvConfig(
315323
task=config_classes.FilesystemTaskConfig(
316324
path=_write_default_task_proto()
317325
),
318326
simulator=config_classes.EmulatorConfig(
319-
emulator_launcher=config_classes.EmulatorLauncherConfig(
320-
emulator_console_port=console_port,
321-
adb_port=console_port + 1,
322-
grpc_port=grpc_port,
323-
),
327+
emulator_launcher=emulator_launcher,
324328
adb_controller=config_classes.AdbControllerConfig(adb_path=adb_path),
325329
),
326330
)

android_world/env/env_launcher_test.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,21 @@ def test_get_env(
4141

4242
env_launcher._get_env(5556, "some_adb_path", 8554)
4343

44+
expected_launcher_config = config_classes.EmulatorLauncherConfig(
45+
emulator_console_port=5556,
46+
adb_port=5557,
47+
grpc_port=8554,
48+
)
49+
if hasattr(expected_launcher_config, "connect_to_existing"):
50+
setattr(expected_launcher_config, "connect_to_existing", True)
51+
4452
mock_loader.assert_called_with(
4553
config=config_classes.AndroidEnvConfig(
4654
task=config_classes.FilesystemTaskConfig(
4755
path=android_world_controller._TASK_PATH
4856
),
4957
simulator=config_classes.EmulatorConfig(
50-
emulator_launcher=config_classes.EmulatorLauncherConfig(
51-
emulator_console_port=5556, adb_port=5557, grpc_port=8554
52-
),
58+
emulator_launcher=expected_launcher_config,
5359
adb_controller=config_classes.AdbControllerConfig(
5460
adb_path="some_adb_path"
5561
),

setup.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515
"""Setup file for AndroidWorld."""
1616

17+
import importlib.resources
1718
import os
1819

19-
import pkg_resources
2020
import setuptools
2121
from setuptools.command import build_py
2222

@@ -42,10 +42,10 @@ def finalize_options(self):
4242
def run(self):
4343
# Import grpc_tools here, after setuptools has installed setup_requires
4444
# dependencies.
45-
from grpc_tools import protoc # pylint: disable=g-import-not-at-top
45+
from grpc_tools import protoc # pylint: disable=g-import-not-at-top # pytype: disable=import-error
4646

47-
grpc_protos_include = pkg_resources.resource_filename(
48-
'grpc_tools', '_proto'
47+
grpc_protos_include = str(
48+
importlib.resources.files('grpc_tools').joinpath('_proto')
4949
)
5050

5151
for proto_path in _PACKAGE_PROTOS:

0 commit comments

Comments
 (0)