diff options
author | Nirbheek Chauhan <nirbheek@centricular.com> | 2023-11-15 15:18:55 +0000 |
---|---|---|
committer | Nirbheek Chauhan <nirbheek@centricular.com> | 2023-11-15 16:47:39 +0000 |
commit | 55648bf7d7e7fa280fdb6e47de31bbec4bab0dc9 (patch) | |
tree | cd6d7a0a714d94e1e7d3da4ccda363b7af437d35 | |
parent | cfd8987210f2da44b6f3753431faa734d42c65ed (diff) |
cerbero: Port from imp module to importlib module
imp module was removed in Python 3.12
We no longer need the site.py patch from setuptools because we use
a venv now.
Part-of: <https://gitlab.freedesktop.org/gstreamer/cerbero/-/merge_requests/1301>
-rw-r--r-- | cerbero/bootstrap/build_tools.py | 29 | ||||
-rw-r--r-- | cerbero/bootstrap/site-patch.py | 78 | ||||
-rw-r--r-- | cerbero/build/cookbook.py | 5 | ||||
-rw-r--r-- | cerbero/packages/packagesstore.py | 5 | ||||
-rw-r--r-- | cerbero/utils/__init__.py | 9 |
5 files changed, 14 insertions, 112 deletions
diff --git a/cerbero/bootstrap/build_tools.py b/cerbero/bootstrap/build_tools.py index 31489a9d..f9659be5 100644 --- a/cerbero/bootstrap/build_tools.py +++ b/cerbero/bootstrap/build_tools.py @@ -106,30 +106,6 @@ 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 = stv.__version__.split('.', 1) - if len(version) < 1 or int(version[0]) < 49: - return - - # 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', 'posix_prefix', 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 setup_venv(self): # Python relies on symlinks to work on macOS. # See e.g. @@ -149,10 +125,7 @@ class BuildTools (BootstrapperBase, Fetch): os.rmdir(scriptsdir) async def start(self, jobs=0): - if sys.version_info >= (3, 11, 0): - self.setup_venv() - else: - self.insert_python_site() + self.setup_venv() # 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 deleted file mode 100644 index a8f412e8..00000000 --- a/cerbero/bootstrap/site-patch.py +++ /dev/null @@ -1,78 +0,0 @@ -# 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 diff --git a/cerbero/build/cookbook.py b/cerbero/build/cookbook.py index 819e43d9..7ce5249a 100644 --- a/cerbero/build/cookbook.py +++ b/cerbero/build/cookbook.py @@ -20,7 +20,6 @@ from collections import defaultdict import os import pickle import time -import imp import traceback from cerbero.config import USER_CONFIG_DIR, Platform, Architecture, Distro,\ @@ -28,7 +27,7 @@ from cerbero.config import USER_CONFIG_DIR, Platform, Architecture, Distro,\ from cerbero.build.build import BuildType from cerbero.build.source import SourceType from cerbero.errors import FatalError, RecipeNotFoundError, InvalidRecipeError -from cerbero.utils import _, shell, parse_file +from cerbero.utils import _, shell, parse_file, imp_load_source from cerbero.utils import messages as m from cerbero.utils.manifest import Manifest from cerbero.build import recipe as crecipe @@ -427,7 +426,7 @@ class CookBook (object): crecipe.Recipe._using_manifest_force_git = True m_path = os.path.join(repo, 'custom.py') if os.path.exists(m_path): - custom = imp.load_source('custom', m_path) + custom = imp_load_source('custom', m_path) for f in recipes_files: # Try to load recipes with the custom.py module located in the # recipes dir which can contain private classes and methods with diff --git a/cerbero/packages/packagesstore.py b/cerbero/packages/packagesstore.py index 3e79de3a..d2002aa3 100644 --- a/cerbero/packages/packagesstore.py +++ b/cerbero/packages/packagesstore.py @@ -17,7 +17,6 @@ # Boston, MA 02111-1307, USA. import os -import imp import traceback from collections import defaultdict @@ -26,7 +25,7 @@ from cerbero.config import Platform, Architecture, Distro, DistroVersion,\ License from cerbero.packages import package, PackageType from cerbero.errors import FatalError, PackageNotFoundError -from cerbero.utils import _, shell, remove_list_duplicates, parse_file +from cerbero.utils import _, shell, remove_list_duplicates, parse_file, imp_load_source from cerbero.utils import messages as m @@ -165,7 +164,7 @@ class PackagesStore (object): custom = None m_path = os.path.join(repo, 'custom.py') if os.path.exists(m_path): - custom = imp.load_source('custom', m_path) + custom = imp_load_source('custom', m_path) except Exception as ex: # import traceback # traceback.print_exc() diff --git a/cerbero/utils/__init__.py b/cerbero/utils/__init__.py index ebf92c58..d5725dcd 100644 --- a/cerbero/utils/__init__.py +++ b/cerbero/utils/__init__.py @@ -24,6 +24,7 @@ import shlex import shutil import pathlib import argparse +import importlib try: import sysconfig except: @@ -658,6 +659,14 @@ def detect_qt6(platform, arch, is_universal): return None return qmake6_path +def imp_load_source(modname, fname): + loader = importlib.machinery.SourceFileLoader(modname, fname) + spec = importlib.util.spec_from_file_location(modname, fname, loader=loader) + module = importlib.util.module_from_spec(spec) + sys.modules[module.__name__] = module + loader.exec_module(module) + return module + # asyncio.Semaphore classes set their working event loop internally on # creation, so we need to ensure the proper loop has already been set by then. # This is especially important if we create global semaphores that are |