linux/tools/perf/tests/shell/lib/setup_python.sh
Ian Rogers 1331150d7d perf test: Clean up mypy and pylint issues in shell test libraries
Clean up mypy type errors and pylint errors/warnings in
tools/perf/tests/shell/lib/ (attr.py, perf_json_output_lint.py, and
perf_metric_validation.py):
- Fix E0606 (possibly-used-before-assignment) in perf_metric_validation.py
  by importing sys at module level.
- Add type annotations for stack, second_results, collectlist,
  get_bounds(), and main() in perf_metric_validation.py.
- Avoid assigning -1 to list[int] expected_items in
  perf_json_output_lint.py, rename shadowing variables, and remove
  redundant lambdas.
- Remove unnecessary semicolons, use lazy logging arguments, specify
  exception types and file encodings, and initialize module-level logger
  in attr.py.

Also update tools/perf/tests/shell/lib/setup_python.sh to:
- Avoid x-prefix string comparisons.
- Export PYTHON and resolve PYTHONPATH and PERF_EXEC_PATH from
  BASH_SOURCE[0], ../../../python (for nested test directories such as
  coresight/), and the directory containing the perf binary so out-of-tree
  builds and standalone perf python scripts work seamlessly.

Assisted-by: Antigravity:gemini-3.1-pro
Signed-off-by: Ian Rogers <irogers@google.com>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2026-09-26 20:49:28 +02:00

51 lines
1.7 KiB
Bash

#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
if [ -z "$PYTHON" ]
then
python3 --version >/dev/null 2>&1 && PYTHON=python3
fi
if [ -z "$PYTHON" ]
then
python --version >/dev/null 2>&1 && PYTHON=python
fi
if [ -z "$PYTHON" ]
then
echo Skipping test, python not detected please set environment variable PYTHON.
exit 2
fi
export PYTHON
# Set PYTHONPATH to find the built perf.so and standalone scripts first,
# avoiding system-wide perf.so
if [ -n "$PERF_EXEC_PATH" ] && [ -d "$PERF_EXEC_PATH/python" ]; then
PYTHONPATH_DIR="$PERF_EXEC_PATH/python"
elif [ -n "${BASH_SOURCE[0]}" ] && [ -d "$(dirname "${BASH_SOURCE[0]}")/../../../python" ]; then
PYTHONPATH_DIR="$(dirname "${BASH_SOURCE[0]}")/../../../python"
elif [ -d "$(dirname "$0")/../../../python" ]; then
PYTHONPATH_DIR="$(dirname "$0")/../../../python"
elif [ -d "$(dirname "$0")/../../python" ]; then
PYTHONPATH_DIR="$(dirname "$0")/../../python"
elif [ -d "$(dirname "$0")/../python" ]; then
PYTHONPATH_DIR="$(dirname "$0")/../python"
fi
if [ -n "${BASH_SOURCE[0]}" ] && [ -d "$(dirname "${BASH_SOURCE[0]}")/../../../python" ]; then
SRC_PYTHON_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../../python" && pwd)"
export PYTHONPATH="$SRC_PYTHON_DIR${PYTHONPATH:+:$PYTHONPATH}"
if [ -z "$PERF_EXEC_PATH" ] || [ ! -f "$PERF_EXEC_PATH/python/perf_live.py" ]; then
PERF_EXEC_PATH="$(dirname "$SRC_PYTHON_DIR")"
export PERF_EXEC_PATH
fi
fi
if [ -n "$PYTHONPATH_DIR" ]; then
export PYTHONPATH="$PYTHONPATH_DIR${PYTHONPATH:+:$PYTHONPATH}"
fi
PERF_BIN=$(which perf 2>/dev/null || true)
if [ -n "$PERF_BIN" ] && [ -d "$(dirname "$PERF_BIN")/python" ]; then
PERF_BIN_PYTHON="$(dirname "$PERF_BIN")/python"
export PYTHONPATH="$PERF_BIN_PYTHON${PYTHONPATH:+:$PYTHONPATH}"
fi