#!/usr/bin/env python3 # # Copyright (C) Advanced Micro Devices. All rights reserved. # # Permission is hereby granted, free of charge, to any person obtaining a copy of # this software and associated documentation files (the "Software"), to deal in # the Software without restriction, including without limitation the rights to # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of # the Software, and to permit persons to whom the Software is furnished to do so, # subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS # FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """Core shared infrastructure for the AMD SMI Python test suite. Owns three responsibilities: * amdsmi module discovery -- resolves the install via AMDSMI_PATH -> ROCM_HOME -> ROCM_PATH -> /opt/rocm, inserts it on sys.path, and guards against a shadowing site-packages copy (see ROCM-1552 / PR #6359). * the ``Common`` helper base -- enum tables, ``Test_API_*`` drivers, ``check_ret`` and friends, held as ``self.common`` by the API tests (intentionally NOT a unittest.TestCase). * the unittest runner machinery -- ``run_test_dir``, ``GTestSummaryRunner``, verbosity/-k/-l parsing and the help/legend printers used by the three top-level runners. Imported by every functional/unit leaf test and by all three runners (integration_test.py, cli_unit_test.py, unit_tests.py). """ import contextlib import inspect import io import json import os import pathlib import sys import time import unittest def _print_path_remediation(script, file): """Print the shared 'fix with one of:' remediation block. Used by both print_amdsmi_path_help() (-h context) and print_shadow_error() (import-error context) to avoid duplicating the step list. """ print( "\t 1. Run the tests from the installed amd-smi-lib-tests package location (RECOMMENDED):\n" f"\t\t `sudo $ROCM_PATH/share/amd_smi/tests/python_unittest/{script} -v`\n" f"\t\t e.g. `sudo /opt/rocm/share/amd_smi/tests/python_unittest/{script} -v`\n\n" "\t 2. Set AMDSMI_PATH to point at the correct build:\n" "\t\t `export AMDSMI_PATH=$ROCM_PATH/share/amd_smi`\n\n" "\t 3. Reinstall the correct amd-smi-lib package (removes the stale Python package too):\n" "\t\t `sudo apt remove amd-smi-lib && sudo apt install amd-smi-lib`\n" "\t\t or, if amd-smi was installed directly via pip:\n" "\t\t `sudo pip uninstall amdsmi`", file=file, ) def print_amdsmi_path_help(file=sys.stdout): """Print env-var documentation, path remediation guidance, and usage examples for -h output.""" _script = os.path.basename(sys.argv[0]) or "