1- import pathlib
21import sys
3- from io import StringIO
2+ import pathlib
3+ import subprocess
4+ import conan
45from conan import ConanFile
5- from conan .tools .cmake import CMake , CMakeToolchain
6+ from conan .tools .cmake import CMake , CMakeToolchain , cmake_layout
67
78project_root = pathlib .Path (__file__ ).parent
89
@@ -16,13 +17,33 @@ def _read_env(name):
1617class TestEmbeddedPython (ConanFile ):
1718 name = "test_embedded_python"
1819 settings = "os" , "compiler" , "build_type" , "arch"
19- generators = "CMakeDeps"
20+ generators = "CMakeDeps" , "VirtualRunEnv"
2021 options = {"env" : [None , "ANY" ]}
2122 default_options = {
2223 "env" : None ,
23- "embedded_python-core:version" : "3.11.5" ,
24+ "embedded_python-core/* :version" : "3.11.5" ,
2425 }
2526
27+ @property
28+ def _core_package_path (self ):
29+ if conan .__version__ .startswith ("2" ):
30+ return pathlib .Path (self .dependencies ["embedded_python-core" ].package_folder )
31+ else :
32+ return pathlib .Path (self .deps_cpp_info ["embedded_python-core" ].rootpath )
33+
34+ @property
35+ def _package_path (self ):
36+ if conan .__version__ .startswith ("2" ):
37+ return pathlib .Path (self .dependencies ["embedded_python" ].package_folder )
38+ else :
39+ return pathlib .Path (self .deps_cpp_info ["embedded_python" ].rootpath )
40+
41+ def layout (self ):
42+ cmake_layout (self )
43+
44+ def requirements (self ):
45+ self .requires (self .tested_reference_str )
46+
2647 def configure (self ):
2748 if self .options .env :
2849 self .options ["embedded_python" ].packages = _read_env (self .options .env )
@@ -34,12 +55,19 @@ def generate(self):
3455 tc .generate ()
3556
3657 def build (self ):
58+ sys .path .append (str (self ._package_path ))
59+
3760 import embedded_python_tools
3861
3962 embedded_python_tools .symlink_import (self , dst = "bin/python" )
4063
4164 cmake = CMake (self )
42- cmake .configure ()
65+ cmake .configure (
66+ variables = {
67+ "EXPECTED_PYTHON_CORE_PATH" : self ._core_package_path .as_posix (),
68+ "EXPECTED_PYTHON_PATH" : self ._package_path .as_posix (),
69+ }
70+ )
4371 cmake .build ()
4472
4573 def _test_env (self ):
@@ -52,16 +80,15 @@ def _test_env(self):
5280 self .run (f'{ python_exe } -c "import sys; print(sys.version);"' )
5381
5482 name = str (self .options .env ) if self .options .env else "baseline"
55- self .run (f"{ python_exe } { project_root / name / 'test.py' } " , run_environment = True )
83+ self .run (f"{ python_exe } { project_root / name / 'test.py' } " , env = "conanrun" )
5684
5785 def _test_libpython_path (self ):
5886 if self .settings .os != "Macos" :
5987 return
6088
6189 python_exe = str (pathlib .Path ("./bin/python/bin/python3" ).resolve ())
62- buffer = StringIO ()
63- self .run (f"otool -L { python_exe } " , run_environment = True , output = buffer )
64- lines = buffer .getvalue ().strip ().split ("\n " )[1 :]
90+ p = subprocess .run (["otool" , "-L" , python_exe ], check = True , text = True , capture_output = True )
91+ lines = str (p .stdout ).strip ().split ("\n " )[1 :]
6592 libraries = [line .split ()[0 ] for line in lines ]
6693 candidates = [lib for lib in libraries if "libpython" in lib ]
6794 assert candidates , f"libpython dependency not found in 'otool' output: { libraries } "
@@ -71,11 +98,11 @@ def _test_libpython_path(self):
7198
7299 def _test_embed (self ):
73100 """Ensure that everything is available to compile and link to the embedded Python"""
74- self .run (pathlib .Path ("bin" , "test_package" ), run_environment = True )
101+ self .run (pathlib .Path ("bin" , "test_package" ), env = "conanrun" )
75102
76103 def _test_licenses (self ):
77104 """Ensure that the licenses have been gathered"""
78- license_dir = pathlib . Path ( self .deps_cpp_info [ "embedded_python" ]. rootpath , "licenses" )
105+ license_dir = self ._package_path / "licenses"
79106 license_files = [license_dir / "LICENSE.txt" ]
80107 if self .options .env :
81108 license_files += [license_dir / "package_licenses.txt" ]
0 commit comments