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