summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Waters <matthew@centricular.com>2020-08-20 15:02:52 +1000
committerMatthew Waters <matthew@centricular.com>2020-10-30 19:42:17 +1100
commit08c0440ac8d3b6f2a2f79d306a5311c0e0c5f251 (patch)
tree636f83220fe209c1e8b284aa9b6e6be4ebae85ea
parente3ce1b6dd4d6d5f65b9eaef1e5c46e865e89a9a2 (diff)
build-tools: copy the removed site.py from setuptools1.16
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: <https://gitlab.freedesktop.org/gstreamer/cerbero/-/merge_requests/655>
-rw-r--r--cerbero/bootstrap/build_tools.py33
-rw-r--r--cerbero/bootstrap/site-patch.py78
2 files changed, 111 insertions, 0 deletions
diff --git a/cerbero/bootstrap/build_tools.py b/cerbero/bootstrap/build_tools.py
index 293cf656..838297da 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, Platform, DistroVersion
from cerbero.bootstrap import BootstrapperBase
@@ -24,8 +26,10 @@ from cerbero.build.oven import Oven
from cerbero.build.cookbook import CookBook
from cerbero.commands.fetch import Fetch
from cerbero.utils import _
+from cerbero.utils import messages as m
from cerbero.errors import FatalError, ConfigurationError
+from pathlib import PurePath
class BuildTools (BootstrapperBase, Fetch):
@@ -101,9 +105,38 @@ 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):
self._setup_env()
+ self.insert_python_site()
oven = Oven(self.recipes, self.cookbook)
+
oven.start_cooking()
self.config.do_setup_env()
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