Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove tmp_dir arg from AdbCallParser.__init__() + file leak fix. #228

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions android_env/components/adb_call_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@
class AdbCallParser:
"""Parses AdbRequest messages and executes corresponding adb commands."""

def __init__(self, adb_controller: adb_control.AdbController, tmp_dir: str):
def __init__(self, adb_controller: adb_control.AdbController):
self._adb_controller = adb_controller
self._tmp_dir = tmp_dir
self._handlers = {
'install_apk': self._install_apk,
'start_activity': self._start_activity,
Expand Down Expand Up @@ -276,22 +275,25 @@ def _install_apk(
response.status = adb_pb2.AdbResponse.Status.INTERNAL_ERROR
response.error_message = f'Could not find local_apk_path: {fpath}'
return response

response, _ = self._execute_command(
['install', '-r', '-t', '-g', fpath], timeout=timeout
)
case 'blob':
with tempfile.NamedTemporaryFile(
dir=self._tmp_dir, suffix='.apk', delete=False
) as f:
with tempfile.NamedTemporaryFile(suffix='.apk') as f:
fpath = f.name
f.write(install_apk.blob.contents)

response, _ = self._execute_command(
['install', '-r', '-t', '-g', fpath], timeout=timeout
)
case _:
response.status = adb_pb2.AdbResponse.Status.FAILED_PRECONDITION
response.error_message = (
f'Unsupported `install_apk.location` type: {location_type}'
)
return response

response, _ = self._execute_command(
['install', '-r', '-t', '-g', fpath], timeout=timeout
)
return response

def _start_activity(
Expand Down Expand Up @@ -541,7 +543,7 @@ def _push(
error_message='Push.path is empty.')

# Create temporary file with `push` contents.
with tempfile.NamedTemporaryFile(dir=self._tmp_dir, delete=False) as f:
with tempfile.NamedTemporaryFile(delete=False) as f:
fname = f.name
f.write(request.push.content)
# Issue `adb push` command to upload file.
Expand Down Expand Up @@ -572,7 +574,7 @@ def _pull(
error_message='Pull.path is empty.')

# Issue `adb pull` command to copy it to a temporary file.
with tempfile.NamedTemporaryFile(dir=self._tmp_dir, delete=False) as f:
with tempfile.NamedTemporaryFile(delete=False) as f:
fname = f.name
logging.info('Downloading %r to %r.', path, fname)
response, _ = self._execute_command(['pull', path, fname],
Expand Down
Loading
Loading