1#!/usr/bin/env bash
2_USAGE="avatar [OPTIONS] <COMMAND> ...
3  OPTIONS:
4    -h, --help           Show this message and exit.
5  COMMANDS:
6    help                 Show this message and exit.
7    format [FILES..]     Format python test files.
8                         Files in 'p/m/B/android/pandora/test/*.py' are included by default.
9    lint [FILES..]       Lint python test files.
10                         Files in 'p/m/B/android/pandora/test/*.py' are included by default.
11    run [OPTIONS..]      Run avatar tests through tradefed.
12      OPTIONS: (subset, see 'avatar run --help-all')
13        --include-filter=<ClassA[#test_a]>
14                         Add a test filter in form of 'ClassA[#test_a]'.
15        --test-bed       Set mobly test bed (default is 'android.bumbles').
16        --mobly-std-log  Print mobly logs to standard outputs.
17        --mobly-options=<'--opt-a --opt-b'>
18                         Pass additional options to mobly, like '--verbose' or '--list_tests'.
19        ...              All other tradefed options, like '--log-level VERBOSE'.
20                         See 'avatar run --help-all'
21    "
22
23_VENV_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/avatar/venv"
24_BT_ROOT="${ANDROID_BUILD_TOP}/packages/modules/Bluetooth"
25_TEST_ROOT="${_BT_ROOT}/android/pandora/test"
26_PY_SOURCES=(
27  "${_BT_ROOT}/pandora/server/bumble_experimental"
28  "${_TEST_ROOT}/"*.py
29)
30
31_PANDORA_PYTHON_PATHS=(
32  "${_BT_ROOT}/pandora/server/"
33  "${ANDROID_BUILD_TOP}/external/pandora/avatar/"
34  "${ANDROID_BUILD_TOP}/external/python/bumble/"
35  "${ANDROID_BUILD_TOP}/external/python/mobly/"
36  "${ANDROID_BUILD_TOP}/external/python/pyee/"
37  "${ANDROID_BUILD_TOP}/external/python/portpicker/src/"
38  "${ANDROID_BUILD_TOP}/out/soong/.intermediates/external/pandora/bt-test-interfaces/python/pandora-python-gen-src/gen/"
39  "${ANDROID_BUILD_TOP}/out/soong/.intermediates/packages/modules/Bluetooth/pandora/interfaces/python/pandora_experimental-python-gen-src/gen/"
40)
41
42if [[ "$1" =~ ^('format'|'lint'|'run')$ ]]; then
43  [ ! -d "${_VENV_DIR}" ] && python3 -m venv "${_VENV_DIR}"
44  source "${_VENV_DIR}"/bin/activate
45  pip install \
46    'grpcio==1.57' \
47    'cryptography==35' \
48    'numpy==1.25.2' \
49    'protobuf==4.24.2' \
50    'mypy==1.5.1' \
51    'pyright==1.1.298' \
52    'types-protobuf==4.24.0.1' \
53    'black==23.7.0' \
54    'isort==5.12.0'
55fi
56
57case "$1" in
58  'format') shift
59    black -S -l 119 "$@" "${_PY_SOURCES[@]}"
60    isort --profile black -l 119 --ds --lbt 1 --ca "$@" "${_PY_SOURCES[@]}"
61  ;;
62  'lint') shift
63    export PYTHONPATH="$(IFS=:; echo "${_PANDORA_PYTHON_PATHS[*]}"):${PYTHONPATH}"
64    mypy \
65      --pretty --show-column-numbers --strict --no-warn-unused-ignores --ignore-missing-imports \
66      "$@" "${_PY_SOURCES[@]}" || exit 1
67    pyright \
68      -p "${_TEST_ROOT}" \
69      "$@" "${_PY_SOURCES[@]}"
70  ;;
71  'run') shift
72    tradefed.sh \
73      run commandAndExit template/local_min --template:map test=avatar --log-level INFO \
74        --venv-dir "${_VENV_DIR}" \
75        "$@"
76  ;;
77  'help'|'--help'|'-h') shift
78    echo "${_USAGE}"
79    exit 0
80  ;;
81  '')
82    echo "no command provided (try help)"
83    echo "${_USAGE}"
84    exit 1
85  ;;
86  *)
87    echo "$1: invalid command (try help)"
88    echo "${_USAGE}"
89    exit 1
90  ;;
91esac
92