From b62b6c33995b6dd62eef1e730d2176dd04d4c37d Mon Sep 17 00:00:00 2001 From: Roland Kaminski Date: Wed, 27 Sep 2023 19:24:07 +0200 Subject: [PATCH] fix site helper script --- app/pyclingo/CMakeLists.txt | 7 ++-- cmake/python-site.py | 74 +++++++++++++++++++++++++++++-------- 2 files changed, 63 insertions(+), 18 deletions(-) diff --git a/app/pyclingo/CMakeLists.txt b/app/pyclingo/CMakeLists.txt index d738aba2d..869644979 100644 --- a/app/pyclingo/CMakeLists.txt +++ b/app/pyclingo/CMakeLists.txt @@ -9,16 +9,17 @@ set(source if (PYCLINGO_INSTALL_DIR STREQUAL "" AND Python_EXECUTABLE) if (PYCLINGO_INSTALL STREQUAL "user") - execute_process(COMMAND ${Python_EXECUTABLE} ${CLINGO_SOURCE_DIR}/cmake/python-site.py site OUTPUT_VARIABLE PYCLINGO_INSTALL_DIR OUTPUT_STRIP_TRAILING_WHITESPACE) + execute_process(COMMAND ${Python_EXECUTABLE} ${CLINGO_SOURCE_DIR}/cmake/python-site.py target --user OUTPUT_VARIABLE PYCLINGO_INSTALL_DIR OUTPUT_STRIP_TRAILING_WHITESPACE) elseif(PYCLINGO_INSTALL STREQUAL "prefix") - execute_process(COMMAND ${Python_EXECUTABLE} ${CLINGO_SOURCE_DIR}/cmake/python-site.py prefix "${CMAKE_INSTALL_PREFIX}" OUTPUT_VARIABLE PYCLINGO_INSTALL_DIR OUTPUT_STRIP_TRAILING_WHITESPACE) + execute_process(COMMAND ${Python_EXECUTABLE} ${CLINGO_SOURCE_DIR}/cmake/python-site.py target --prefix "${CMAKE_INSTALL_PREFIX}" OUTPUT_VARIABLE PYCLINGO_INSTALL_DIR OUTPUT_STRIP_TRAILING_WHITESPACE) else() - execute_process(COMMAND ${Python_EXECUTABLE} ${CLINGO_SOURCE_DIR}/cmake/python-site.py prefix OUTPUT_VARIABLE PYCLINGO_INSTALL_DIR OUTPUT_STRIP_TRAILING_WHITESPACE) + execute_process(COMMAND ${Python_EXECUTABLE} ${CLINGO_SOURCE_DIR}/cmake/python-site.py target OUTPUT_VARIABLE PYCLINGO_INSTALL_DIR OUTPUT_STRIP_TRAILING_WHITESPACE) endif() endif() if (PYCLINGO_SUFFIX STREQUAL "") if (Python_EXECUTABLE) + # Python_SOABI execute_process(COMMAND ${Python_EXECUTABLE} ${CLINGO_SOURCE_DIR}/cmake/python-site.py suffix OUTPUT_VARIABLE PYCLINGO_SUFFIX OUTPUT_STRIP_TRAILING_WHITESPACE) else() if (CYGWIN) diff --git a/cmake/python-site.py b/cmake/python-site.py index 81b553440..f409dd42c 100644 --- a/cmake/python-site.py +++ b/cmake/python-site.py @@ -1,17 +1,61 @@ -try: - from setuptools.sysconfig import get_python_lib, get_config_vars -except ImportError: - from distutils.sysconfig import get_python_lib, get_config_vars import sys +from argparse import ArgumentParser -if sys.argv[1] == "prefix": - print(get_python_lib(True, False, sys.argv[2] if len(sys.argv) > 2 else None)) -elif sys.argv[1] == "suffix": - SO, SOABI, EXT_SUFFIX = get_config_vars("SO", "SOABI", "EXT_SUFFIX") - if EXT_SUFFIX is not None: - ext = EXT_SUFFIX - elif SOABI is not None: - ext = ''.join('.', SOABI, SO) - else: - ext = SO - print(ext) +if sys.version_info >= (3, 11): + NEW_STYLE = True + from sysconfig import get_config_var, get_config_vars, get_preferred_scheme, get_path +else: + NEW_STYLE = False + from site import USER_SITE + try: + from setuptools.sysconfig import get_python_lib, get_config_vars + except ImportError: + from distutils.sysconfig import get_python_lib, get_config_vars + +parser = ArgumentParser() +if sys.version_info >= (3, 7): + subparser = parser.add_subparsers(required=True, dest="action") +else: + subparser = parser.add_subparsers(dest="action") + +prefix_parser = subparser.add_parser("target") +prefix_group = prefix_parser.add_mutually_exclusive_group() +prefix_group.add_argument("--user", action='store_true', help='get user prefix') +prefix_group.add_argument("--prefix", type=str, help='prepend prefix') + +prefix_parser = subparser.add_parser("suffix") + +result = parser.parse_args() + +if NEW_STYLE: + if result.action == "target": + if result.user: + scheme = get_preferred_scheme("user") + else: + scheme = get_preferred_scheme("prefix") + if result.prefix is not None: + cvars = get_config_vars() + cvars["base"] = result.prefix + cvars["platbase"] = result.prefix + platlib = get_path("platlib", scheme, cvars) + else: + platlib = get_path("platlib", scheme) + print(platlib) + elif result.action == "suffix": + print(get_config_var('EXT_SUFFIX')) +else: + # TODO: remove once python 3.10 is eol + if result.action == "target": + if result.user: + print(USER_SITE) + else: + print(get_python_lib(True, False, result.prefix)) + elif result.action == "suffix": + SO, SOABI, EXT_SUFFIX = get_config_vars("SO", "SOABI", "EXT_SUFFIX") + if EXT_SUFFIX is not None: + ext = EXT_SUFFIX + elif SOABI is not None: + ext = ''.join('.', SOABI, SO) + else: + ext = SO + print(ext)