4646
4747
4848compiled_registry = set ()
49+ _venv_site_packages_cache = {}
4950
5051
5152def find_rootdir ():
@@ -69,13 +70,42 @@ def _venv_python(venv_dir: Path) -> Path:
6970 return venv_dir / 'bin' / 'python3'
7071
7172
72- def _venv_site_packages (venv_dir : Path ) -> Path :
73- existing_site_packages = list (venv_dir .glob ("lib/python*/site-packages" ))
73+ def _venv_site_packages (venv_dir : Path , py_executable : Path = None ) -> Path :
74+ # the python creating the venv may be different, so we need to figure out
75+ # the correct path using that python. this is used in polybenchmarks, where
76+ # the executable is not graalpy and thus a different CI-env Python is used.
77+ if py_executable and py_executable .exists ():
78+ cache_key = venv_dir .absolute ()
79+ cached_path = _venv_site_packages_cache .get (cache_key )
80+ if cached_path and cached_path .exists ():
81+ return cached_path
82+ import subprocess
83+ try :
84+ path = subprocess .check_output ([
85+ py_executable ,
86+ "-c" ,
87+ "import sysconfig; print(sysconfig.get_paths()['purelib'])" ,
88+ ], text = True ).strip ()
89+ except (OSError , subprocess .CalledProcessError ):
90+ pass
91+ else :
92+ if path :
93+ site_packages = Path (path )
94+ _venv_site_packages_cache [cache_key ] = site_packages
95+ return site_packages
96+ existing_site_packages = list (venv_dir .glob ("lib*/python*/site-packages" ))
7497 if len (existing_site_packages ) == 1 :
7598 return existing_site_packages [0 ]
99+ non_empty_site_packages = [path for path in existing_site_packages if any (path .iterdir ())]
100+ if len (non_empty_site_packages ) == 1 :
101+ return non_empty_site_packages [0 ]
76102 if sys .platform .startswith ('win32' ):
77103 return venv_dir / 'Lib' / 'site-packages'
78- return venv_dir / 'lib' / f'python{ sys .version_info .major } .{ sys .version_info .minor } ' / 'site-packages'
104+ version_dir = f'python{ sys .version_info .major } .{ sys .version_info .minor } '
105+ lib64_path = venv_dir / 'lib64' / version_dir / 'site-packages'
106+ if lib64_path .exists ():
107+ return lib64_path
108+ return venv_dir / 'lib' / version_dir / 'site-packages'
79109
80110
81111def _package_present (site_packages_dir : Path , package : str , version : str ) -> bool :
@@ -90,13 +120,13 @@ def ensure_packages(**package_specs):
90120 package_names = "-" .join (package_specs .keys ())
91121 venv_dir = find_rootdir () / f'{ sys .implementation .name } -{ package_names } -venv'
92122 py_executable = _venv_python (venv_dir )
93- site_packages_dir = _venv_site_packages (venv_dir )
123+ site_packages_dir = _venv_site_packages (venv_dir , py_executable )
94124 if any (not _package_present (site_packages_dir , p , v ) for p , v in package_specs .items ()):
95125 import subprocess
96126 package_specs = [f'{ p } =={ v } ' for p , v in package_specs .items ()]
97127 print (f'installing { package_specs } in { venv_dir } ' )
98128 system_python = install_venv (venv_dir )
99- site_packages_dir = _venv_site_packages (venv_dir )
129+ site_packages_dir = _venv_site_packages (venv_dir , py_executable )
100130 extra_args = []
101131 if system_python or sys .implementation .name != "graalpy" :
102132 pass
0 commit comments