# Copyright (c) OpenMMLab. All rights reserved.
import ctypes
import glob
import logging
import os


def get_ops_path() -> str:
    """Get path of the TensorRT plugin library.

    Returns:
        str: A path of the TensorRT plugin library.
    """
    wildcard = os.path.abspath(
        os.path.join(
            os.path.dirname(__file__),
            '../../../build/lib/libmmdeploy_tensorrt_ops.so'))

    paths = glob.glob(wildcard)
    lib_path = paths[0] if len(paths) > 0 else ''
    return lib_path


def load_tensorrt_plugin() -> bool:
    """Load TensorRT plugins library.

    Returns:
        bool: True if TensorRT plugin library is successfully loaded.
    """
    lib_path = get_ops_path()
    success = False
    if os.path.exists(lib_path):
        ctypes.CDLL(lib_path)
        logging.info(f'Successfully loaded tensorrt plugins from {lib_path}')
        success = True
    else:
        logging.warning(f'Could not load the library of tensorrt plugins. \
            Because the file does not exist: {lib_path}')
    return success

[((19, 12, 19, 31), 'glob.glob', 'glob.glob', ({(19, 22, 19, 30): 'wildcard'}, {}), '(wildcard)', False, 'import glob\n'), ((32, 7, 32, 31), 'os.path.exists', 'os.path.exists', ({(32, 22, 32, 30): 'lib_path'}, {}), '(lib_path)', False, 'import os\n'), ((33, 8, 33, 29), 'ctypes.CDLL', 'ctypes.CDLL', ({(33, 20, 33, 28): 'lib_path'}, {}), '(lib_path)', False, 'import ctypes\n'), ((34, 8, 34, 77), 'logging.info', 'logging.info', ({(34, 21, 34, 76): 'f"""Successfully loaded tensorrt plugins from {lib_path}"""'}, {}), "(f'Successfully loaded tensorrt plugins from {lib_path}')", False, 'import logging\n'), ((37, 8, 38, 57), 'logging.warning', 'logging.warning', ({(37, 24, 38, 56): 'f"""Could not load the library of tensorrt plugins.             Because the file does not exist: {lib_path}"""'}, {}), "(\n    f'Could not load the library of tensorrt plugins.             Because the file does not exist: {lib_path}'\n    )", False, 'import logging\n'), ((16, 12, 16, 37), 'os.path.dirname', 'os.path.dirname', ({(16, 28, 16, 36): '__file__'}, {}), '(__file__)', False, 'import os\n')]