Milosz Wasilewski | 9e068e0 | 2025-02-11 12:09:11 +0000 | [diff] [blame] | 1 | """Shared Python 3 utility code.""" |
Karsten Tausche | 19d0648 | 2019-01-21 14:16:41 +0100 | [diff] [blame] | 2 | |
| 3 | from pathlib import Path |
| 4 | import subprocess |
| 5 | from typing import Dict, Optional |
| 6 | |
| 7 | AUTOMATED_LIB_DIR = Path(__file__).resolve().parent |
| 8 | |
| 9 | |
| 10 | def 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 |