blob: e695017b07de9789aa33c94fc304b84dc540dd59 [file] [log] [blame]
Milosz Wasilewski9e068e02025-02-11 12:09:11 +00001"""Shared Python 3 utility code."""
Karsten Tausche19d06482019-01-21 14:16:41 +01002
3from pathlib import Path
4import subprocess
5from typing import Dict, Optional
6
7AUTOMATED_LIB_DIR = Path(__file__).resolve().parent
8
9
10def call_shell_lib(
11 command: str,
12 environment: Optional[Dict[str, str]] = None,
13 device: Optional[str] = None,
14) -> int:
15 """Python-to-shell adaptor, facilitating code reuse.
16
17 This executes a given command line on a shell with sourced sh-test-lib and
18 android-test-lib.
19
20 Arguments:
21 command: Function or command line including parameters to execute in a
22 shell.
23 environment: Environment to execute the shell command in. This is a
24 mapping of environment variable names to their values.
25 device: ADB identifier (serial or IP and port) of a device. If set, this
26 will be appended as ANDROID_SERIAL to the environment.
27 Return:
28 The exit code of the invoked shell command.
29 """
30 if device:
31 if not environment:
32 environment = {}
33 environment["ANDROID_SERIAL"] = device
34 return subprocess.run(
35 [
36 "sh",
37 "-c",
38 ". {}/sh-test-lib && "
39 ". {}/android-test-lib && {}".format(
40 AUTOMATED_LIB_DIR, AUTOMATED_LIB_DIR, command
41 ),
42 ],
43 env=environment,
44 ).returncode