9
9
10
10
def main ():
11
11
"""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
12
13
config = read_pyvenv ()
13
14
sys .real_prefix = sys .base_prefix = config ["base-prefix" ]
14
15
sys .base_exec_prefix = config ["base-exec-prefix" ]
15
16
sys .base_executable = config ["base-executable" ]
16
17
global_site_package_enabled = config .get ("include-system-site-packages" , False ) == "true"
17
18
rewrite_standard_library_sys_path ()
18
19
disable_user_site_package ()
19
- load_host_site ()
20
+ load_host_site (here )
20
21
if global_site_package_enabled :
21
22
add_global_site_package ()
23
+ rewrite_getsitepackages (here )
22
24
23
25
24
- def load_host_site ():
26
+ def load_host_site (here ):
25
27
"""trigger reload of site.py - now it will use the standard library instance that will take care of init"""
26
28
# we have a duality here, we generate the platform and pure library path based on what distutils.install specifies
27
29
# 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():
36
38
# to facilitate when the two match, or not we first reload the site.py, now triggering the import of host site.py,
37
39
# as this will ensure that initialization code within host site.py runs
38
40
39
- here = __file__ # the distutils.install patterns will be injected relative to this site.py, save it here
40
-
41
41
# ___RELOAD_CODE___
42
42
43
43
# and then if the distutils site packages are not on the sys.path we add them via add_site_dir; note we must add
44
44
# 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
45
53
import os
46
54
47
55
site_packages = r"""
48
56
___EXPECTED_SITE_PACKAGES___
49
57
"""
50
- import json
51
58
52
- add_site_dir = sys .modules ["site" ].addsitedir
53
59
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" )))
56
61
57
62
58
63
sep = "\\ " if sys .platform == "win32" else "/" # no os module here yet - poor mans version
@@ -161,4 +166,25 @@ def add_global_site_package():
161
166
site .PREFIXES = orig_prefixes + site .PREFIXES
162
167
163
168
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
+
164
190
main ()
0 commit comments