From 7875330078e4336161f0c0d169bc5f3e43b1a397 Mon Sep 17 00:00:00 2001 From: Matthew Waters Date: Thu, 20 Aug 2020 15:02:52 +1000 Subject: 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: https://gitlab.freedesktop.org/gstreamer/cerbero/-/issues/307 Part-of: --- cerbero/bootstrap/build_tools.py | 33 +++++++++++++++++ cerbero/bootstrap/site-patch.py | 78 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 cerbero/bootstrap/site-patch.py diff --git a/cerbero/bootstrap/build_tools.py b/cerbero/bootstrap/build_tools.py index 6c852a81..47af4455 100644 --- a/cerbero/bootstrap/build_tools.py +++ b/cerbero/bootstrap/build_tools.py @@ -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): @@ -109,7 +113,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() diff --git a/cerbero/bootstrap/site-patch.py b/cerbero/bootstrap/site-patch.py new file mode 100644 index 00000000..a8f412e8 --- /dev/null +++ b/cerbero/bootstrap/site-patch.py @@ -0,0 +1,78 @@ +# 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 -- cgit v1.2.3