From 86b3d2d0250c725cfb6d83bbb4b69aa8001833af Mon Sep 17 00:00:00 2001 From: schneider Date: Sat, 16 Mar 2024 20:59:19 +0100 Subject: [PATCH] python: use upstream Python dir detection Distutils is deprecated since Python 3.12. Upstream GNURadio already improved upon the original solution. --- python/CMakeLists.txt | 42 +++++++++++++++++++++++++++++++++++----- python/get_python_lib.py | 23 ---------------------- 2 files changed, 37 insertions(+), 28 deletions(-) delete mode 100644 python/get_python_lib.py diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index 5c142ed..69eb0cc 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -17,11 +17,43 @@ endif() message(STATUS "CMAKE_INSTALL_PREFIX: ${CMAKE_INSTALL_PREFIX}") message(STATUS "GR_PYTHON_DIR: ${GR_PYTHON_DIR}") -execute_process( - COMMAND ${PYTHON_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/get_python_lib.py" "${CMAKE_INSTALL_PREFIX}" - OUTPUT_STRIP_TRAILING_WHITESPACE - OUTPUT_VARIABLE GR_PYTHON_DIR -) +######################################################################## +# Sets the python installation directory GR_PYTHON_DIR +# From https://github.com/pothosware/SoapySDR/tree/master/python +# https://github.com/pothosware/SoapySDR/blob/master/LICENSE_1_0.txt +######################################################################## +if(NOT DEFINED GR_PYTHON_DIR) + execute_process( + COMMAND + ${PYTHON_EXECUTABLE} -c + "import os +import sysconfig +import site + +install_dir = None +prefix = '${CMAKE_INSTALL_PREFIX}' + +#use sites when the prefix is already recognized +try: + paths = [p for p in site.getsitepackages() if p.startswith(prefix)] + if len(paths) == 1: install_dir = paths[0] +except AttributeError: pass + +if not install_dir: + # Newer Python versions have 'venv' scheme, use for all OSs. + if 'venv' in sysconfig.get_scheme_names(): + scheme = 'venv' + elif os.name == 'nt': + scheme = 'nt' + else: + scheme = 'posix_prefix' + install_dir = sysconfig.get_path('platlib', scheme=scheme, vars={'base': prefix, 'platbase': prefix}) +print(os.path.relpath(install_dir, prefix))" + OUTPUT_STRIP_TRAILING_WHITESPACE + OUTPUT_VARIABLE GR_PYTHON_DIR) +endif() +file(TO_CMAKE_PATH ${GR_PYTHON_DIR} GR_PYTHON_DIR) + message(STATUS "GR_PYTHON_DIR: ${GR_PYTHON_DIR}") add_subdirectory(bindings) diff --git a/python/get_python_lib.py b/python/get_python_lib.py deleted file mode 100644 index 036c343..0000000 --- a/python/get_python_lib.py +++ /dev/null @@ -1,23 +0,0 @@ -# From https://github.com/pothosware/SoapySDR/tree/master/python -# https://github.com/pothosware/SoapySDR/blob/master/LICENSE_1_0.txt -import os -import sys -import site -from distutils.sysconfig import get_python_lib - -if __name__ == '__main__': - prefix = sys.argv[1] - - #ask distutils where to install the python module - install_dir = get_python_lib(plat_specific=True, prefix=prefix) - - #use sites when the prefix is already recognized - try: - paths = [p for p in site.getsitepackages() if p.startswith(prefix)] - if len(paths) == 1: - install_dir = paths[0] - except AttributeError: - pass - - #strip the prefix to return a relative path - print(os.path.relpath(install_dir, prefix))