Commit 1beeb406 authored by Matthew Waters's avatar Matthew Waters 🐨
Browse files

build-tools: copy the removed site.py from setuptools

Fixes python programs (like meson) from using libraries from incorrect
places.

Upstream reference: https://github.com/pypa/setuptools/issues/2295

Fixes: #307
Part-of: <!655>
parent e75c9681
Pipeline #220176 passed with stages
in 66 minutes and 30 seconds
......@@ -17,6 +17,8 @@
# Boston, MA 02111-1307, USA.
import os
import sysconfig
import shutil
from cerbero.config import Config
from cerbero.bootstrap import BootstrapperBase
......@@ -24,9 +26,11 @@ from cerbero.build.oven import Oven
from cerbero.build.cookbook import CookBook
from cerbero.commands.fetch import Fetch
from cerbero.utils import _, shell
from cerbero.utils import messages as m
from cerbero.errors import FatalError, ConfigurationError
from cerbero.enums import Platform, Architecture, DistroVersion
from pathlib import PurePath
class BuildTools (BootstrapperBase, Fetch):
......@@ -122,7 +126,36 @@ class BuildTools (BootstrapperBase, Fetch):
self.recipes = self.BUILD_TOOLS
self.recipes += self.PLAT_BUILD_TOOLS.get(self.config.platform, [])
def insert_python_site(self):
try:
import setuptools.version as stv
except ImportError:
return
version = [int(v) for v in stv.__version__.split('.')]
if len(version) < 1 or version[:1] < [49]:
return
m.warning('detected setuptools >= 49.0.0, installing fallback site.py file. '
'See https://github.com/pypa/setuptools/issues/2295')
# Since python-setuptools 49.0.0, site.py is not installed by
# easy_install/setup.py anymore which breaks python installs outside
# the system prefix.
# https://github.com/pypa/setuptools/issues/2295
#
# Install the previously installed site.py ourselves as a workaround
config = self.cookbook.get_config()
py_prefix = sysconfig.get_path('purelib', vars={'base': ''})
# Must strip \/ to ensure that the path is relative
py_prefix = PurePath(config.prefix) / PurePath(py_prefix.strip('\\/'))
src_file = os.path.join(os.path.dirname(__file__), 'site-patch.py')
shutil.copy(src_file, py_prefix / 'site.py')
def start(self, jobs=0):
self.insert_python_site()
# Check and these at the last minute because we may have installed them
# in system bootstrap
self.recipes += self.check_build_tools()
......
# Originally kept from https://github.com/pypa/setuptools/pull/2166/files
def __boot():
import sys
import os
PYTHONPATH = os.environ.get('PYTHONPATH')
if PYTHONPATH is None or (sys.platform == 'win32' and not PYTHONPATH):
PYTHONPATH = []
else:
PYTHONPATH = PYTHONPATH.split(os.pathsep)
pic = getattr(sys, 'path_importer_cache', {})
stdpath = sys.path[len(PYTHONPATH):]
mydir = os.path.dirname(__file__)
for item in stdpath:
if item == mydir or not item:
continue # skip if current dir. on Windows, or my own directory
importer = pic.get(item)
if importer is not None:
loader = importer.find_module('site')
if loader is not None:
# This should actually reload the current module
loader.load_module('site')
break
else:
try:
import imp # Avoid import loop in Python 3
stream, path, descr = imp.find_module('site', [item])
except ImportError:
continue
if stream is None:
continue
try:
# This should actually reload the current module
imp.load_module('site', stream, path, descr)
finally:
stream.close()
break
else:
raise ImportError("Couldn't find the real 'site' module")
# 2.2 comp
known_paths = dict([(
makepath(item)[1], 1) for item in sys.path]) # noqa
oldpos = getattr(sys, '__egginsert', 0) # save old insertion position
sys.__egginsert = 0 # and reset the current one
for item in PYTHONPATH:
addsitedir(item) # noqa
sys.__egginsert += oldpos # restore effective old position
d, nd = makepath(stdpath[0]) # noqa
insert_at = None
new_path = []
for item in sys.path:
p, np = makepath(item) # noqa
if np == nd and insert_at is None:
# We've hit the first 'system' path entry, so added entries go here
insert_at = len(new_path)
if np in known_paths or insert_at is None:
new_path.append(item)
else:
# new path after the insert point, back-insert it
new_path.insert(insert_at, item)
insert_at += 1
sys.path[:] = new_path
if __name__ == 'site':
__boot()
del __boot
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment