Skip to content

Commit 39c8d14

Browse files
ystreetrgonzalezfluendo
authored andcommitted
build-tools: copy the removed site.py from setuptools
Fixes python programs (like meson) from using libraries from incorrect places. Upstream reference: pypa/setuptools#2295 Fixes: https://gitlab.freedesktop.org/gstreamer/cerbero/-/issues/307 Part-of: <https://gitlab.freedesktop.org/gstreamer/cerbero/-/merge_requests/655>
1 parent 18e009f commit 39c8d14

File tree

2 files changed

+111
-1
lines changed

2 files changed

+111
-1
lines changed

cerbero/bootstrap/build_tools.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,19 @@
1717
# Boston, MA 02111-1307, USA.
1818

1919
import os
20+
import sysconfig
21+
import shutil
2022

2123
from cerbero.config import Config, Platform, DistroVersion
2224
from cerbero.bootstrap import BootstrapperBase
2325
from cerbero.build.oven import Oven
2426
from cerbero.build.cookbook import CookBook
2527
from cerbero.commands.fetch import Fetch
2628
from cerbero.utils import _, shell
29+
from cerbero.utils import messages as m
2730
from cerbero.errors import FatalError, ConfigurationError
2831

32+
from pathlib import PurePath
2933

3034
class BuildTools (BootstrapperBase, Fetch):
3135

@@ -120,8 +124,36 @@ def _setup_env(self):
120124
self.recipes = self.BUILD_TOOLS
121125
self.recipes += self.PLAT_BUILD_TOOLS.get(self.config.platform, [])
122126

123-
def start(self):
127+
def insert_python_site(self):
128+
try:
129+
import setuptools.version as stv
130+
except ImportError:
131+
return
132+
133+
version = [int(v) for v in stv.__version__.split('.')]
134+
if len(version) < 1 or version[:1] < [49]:
135+
return
136+
137+
m.warning('detected setuptools >= 49.0.0, installing fallback site.py file. '
138+
'See https://github.com/pypa/setuptools/issues/2295')
139+
140+
# Since python-setuptools 49.0.0, site.py is not installed by
141+
# easy_install/setup.py anymore which breaks python installs outside
142+
# the system prefix.
143+
# https://github.com/pypa/setuptools/issues/2295
144+
#
145+
# Install the previously installed site.py ourselves as a workaround
146+
config = self.cookbook.get_config()
147+
148+
py_prefix = sysconfig.get_path('purelib', vars={'base': ''})
149+
# Must strip \/ to ensure that the path is relative
150+
py_prefix = PurePath(config.prefix) / PurePath(py_prefix.strip('\\/'))
151+
src_file = os.path.join(os.path.dirname(__file__), 'site-patch.py')
152+
shutil.copy(src_file, py_prefix / 'site.py')
153+
154+
def start(self, jobs=0):
124155
self._setup_env()
156+
self.insert_python_site()
125157
# Check build tools at the last minute because we may have installed them
126158
# in system bootstrap
127159
self.recipes += self.check_build_tools()

cerbero/bootstrap/site-patch.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Originally kept from https://github.com/pypa/setuptools/pull/2166/files
2+
3+
def __boot():
4+
import sys
5+
import os
6+
PYTHONPATH = os.environ.get('PYTHONPATH')
7+
if PYTHONPATH is None or (sys.platform == 'win32' and not PYTHONPATH):
8+
PYTHONPATH = []
9+
else:
10+
PYTHONPATH = PYTHONPATH.split(os.pathsep)
11+
12+
pic = getattr(sys, 'path_importer_cache', {})
13+
stdpath = sys.path[len(PYTHONPATH):]
14+
mydir = os.path.dirname(__file__)
15+
16+
for item in stdpath:
17+
if item == mydir or not item:
18+
continue # skip if current dir. on Windows, or my own directory
19+
importer = pic.get(item)
20+
if importer is not None:
21+
loader = importer.find_module('site')
22+
if loader is not None:
23+
# This should actually reload the current module
24+
loader.load_module('site')
25+
break
26+
else:
27+
try:
28+
import imp # Avoid import loop in Python 3
29+
stream, path, descr = imp.find_module('site', [item])
30+
except ImportError:
31+
continue
32+
if stream is None:
33+
continue
34+
try:
35+
# This should actually reload the current module
36+
imp.load_module('site', stream, path, descr)
37+
finally:
38+
stream.close()
39+
break
40+
else:
41+
raise ImportError("Couldn't find the real 'site' module")
42+
43+
# 2.2 comp
44+
known_paths = dict([(
45+
makepath(item)[1], 1) for item in sys.path]) # noqa
46+
47+
oldpos = getattr(sys, '__egginsert', 0) # save old insertion position
48+
sys.__egginsert = 0 # and reset the current one
49+
50+
for item in PYTHONPATH:
51+
addsitedir(item) # noqa
52+
53+
sys.__egginsert += oldpos # restore effective old position
54+
55+
d, nd = makepath(stdpath[0]) # noqa
56+
insert_at = None
57+
new_path = []
58+
59+
for item in sys.path:
60+
p, np = makepath(item) # noqa
61+
62+
if np == nd and insert_at is None:
63+
# We've hit the first 'system' path entry, so added entries go here
64+
insert_at = len(new_path)
65+
66+
if np in known_paths or insert_at is None:
67+
new_path.append(item)
68+
else:
69+
# new path after the insert point, back-insert it
70+
new_path.insert(insert_at, item)
71+
insert_at += 1
72+
73+
sys.path[:] = new_path
74+
75+
76+
if __name__ == 'site':
77+
__boot()
78+
del __boot

0 commit comments

Comments
 (0)