99
1010def main ():
1111 """Patch what needed, and invoke the original site.py"""
12+ here = __file__ # the distutils.install patterns will be injected relative to this site.py, save it here
1213 config = read_pyvenv ()
1314 sys .real_prefix = sys .base_prefix = config ["base-prefix" ]
1415 sys .base_exec_prefix = config ["base-exec-prefix" ]
1516 sys .base_executable = config ["base-executable" ]
1617 global_site_package_enabled = config .get ("include-system-site-packages" , False ) == "true"
1718 rewrite_standard_library_sys_path ()
1819 disable_user_site_package ()
19- load_host_site ()
20+ load_host_site (here )
2021 if global_site_package_enabled :
2122 add_global_site_package ()
23+ rewrite_getsitepackages (here )
2224
2325
24- def load_host_site ():
26+ def load_host_site (here ):
2527 """trigger reload of site.py - now it will use the standard library instance that will take care of init"""
2628 # we have a duality here, we generate the platform and pure library path based on what distutils.install specifies
2729 # because this is what pip will be using; the host site.py though may contain it's own pattern for where the
@@ -36,23 +38,26 @@ def load_host_site():
3638 # to facilitate when the two match, or not we first reload the site.py, now triggering the import of host site.py,
3739 # as this will ensure that initialization code within host site.py runs
3840
39- here = __file__ # the distutils.install patterns will be injected relative to this site.py, save it here
40-
4141 # ___RELOAD_CODE___
4242
4343 # and then if the distutils site packages are not on the sys.path we add them via add_site_dir; note we must add
4444 # them by invoking add_site_dir to trigger the processing of pth files
45+
46+ add_site_dir = sys .modules ["site" ].addsitedir
47+ for path in get_site_packages_dirs (here ):
48+ add_site_dir (path )
49+
50+
51+ def get_site_packages_dirs (here ):
52+ import json
4553 import os
4654
4755 site_packages = r"""
4856 ___EXPECTED_SITE_PACKAGES___
4957 """
50- import json
5158
52- add_site_dir = sys .modules ["site" ].addsitedir
5359 for path in json .loads (site_packages ):
54- full_path = os .path .abspath (os .path .join (here , path .encode ("utf-8" )))
55- add_site_dir (full_path )
60+ yield os .path .abspath (os .path .join (here , path .encode ("utf-8" )))
5661
5762
5863sep = "\\ " if sys .platform == "win32" else "/" # no os module here yet - poor mans version
@@ -161,4 +166,25 @@ def add_global_site_package():
161166 site .PREFIXES = orig_prefixes + site .PREFIXES
162167
163168
169+ # Debian and it's derivatives patch this function. We undo the damage
170+ def rewrite_getsitepackages (here ):
171+ site = sys .modules ["site" ]
172+
173+ site_package_dirs = get_site_packages_dirs (here )
174+ orig_getsitepackages = site .getsitepackages
175+
176+ def getsitepackages ():
177+ sitepackages = orig_getsitepackages ()
178+ if sys .prefix not in site .PREFIXES or sys .exec_prefix not in site .PREFIXES :
179+ # Someone messed with the prefixes, so we stop patching
180+ return sitepackages
181+ for path in site_package_dirs :
182+ if path not in sitepackages :
183+ sitepackages .insert (0 , path )
184+
185+ return sitepackages
186+
187+ site .getsitepackages = getsitepackages
188+
189+
164190main ()
0 commit comments