From 7812310b2b80e5b355ba1446663da8ecd46dd5d0 Mon Sep 17 00:00:00 2001 From: Osyotr Date: Thu, 5 Dec 2024 23:44:25 +0300 Subject: [PATCH] [scripts] More isolation for VFAP(PYTHON3) (#41874) --- .../vcpkg_find_acquire_program(PYTHON3).cmake | 11 +++++----- .../z_vcpkg_make_python_less_embedded.cmake | 20 +++++++++++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 scripts/cmake/z_vcpkg_make_python_less_embedded.cmake diff --git a/scripts/cmake/vcpkg_find_acquire_program(PYTHON3).cmake b/scripts/cmake/vcpkg_find_acquire_program(PYTHON3).cmake index e740918567..9006e9b9e1 100644 --- a/scripts/cmake/vcpkg_find_acquire_program(PYTHON3).cmake +++ b/scripts/cmake/vcpkg_find_acquire_program(PYTHON3).cmake @@ -26,13 +26,14 @@ if(CMAKE_HOST_WIN32) set(download_sha512 15542080E0CC25C574391218107FE843006E8C5A7161D1CD48CF14A3C47155C0244587273D9C747F35B15EA17676869ECCE079214824214C1A62ABFC86AD9F9B) endif() + # Remove this after the next update + string(APPEND tool_subdirectory "-1") + set(paths_to_search "${DOWNLOADS}/tools/python/${tool_subdirectory}") - # We want to be able to import stuff from outside of this embeddable package. - # https://docs.python.org/3/library/sys_path_init.html#pth-files - string(REGEX MATCH "^3\\.[0-9]+" _python_version_plain "${program_version}") - string(REPLACE "." "" _python_version_plain "${_python_version_plain}") - vcpkg_list(SET post_install_command "${CMAKE_COMMAND}" -E rm "python${_python_version_plain}._pth") + vcpkg_list(SET post_install_command + "${CMAKE_COMMAND}" "-DPYTHON_DIR=${paths_to_search}" "-DPYTHON_VERSION=${program_version}" -P "${CMAKE_CURRENT_LIST_DIR}/z_vcpkg_make_python_less_embedded.cmake" + ) else() set(program_name python3) set(brew_package_name "python") diff --git a/scripts/cmake/z_vcpkg_make_python_less_embedded.cmake b/scripts/cmake/z_vcpkg_make_python_less_embedded.cmake new file mode 100644 index 0000000000..d09fec1e87 --- /dev/null +++ b/scripts/cmake/z_vcpkg_make_python_less_embedded.cmake @@ -0,0 +1,20 @@ +if(NOT DEFINED PYTHON_VERSION) + message(FATAL_ERROR "PYTHON_VERSION should be defined.") +endif() + +if(NOT DEFINED PYTHON_DIR) + message(FATAL_ERROR "PYTHON_DIR should be defined.") +endif() + +# We want to be able to import stuff from outside of this embeddable package. +# https://docs.python.org/3/library/sys_path_init.html#pth-files +string(REGEX MATCH "^3\\.[0-9]+" _python_version_plain "${PYTHON_VERSION}") +string(REPLACE "." "" _python_version_plain "${_python_version_plain}") +file(REMOVE "${PYTHON_DIR}/python${_python_version_plain}._pth") + +# Since this embeddable package is not isolated anymore, we should make sure +# it doesn't accidentally pick up stuff from windows registry. +file(WRITE "${PYTHON_DIR}/sitecustomize.py" [[import os +import sys +sys.path.insert(1, os.path.dirname(os.path.realpath(__file__))) +]])