From 8149642064ebaef7ec84fd91a213dad4d9b7b6b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Rinc=C3=B3n=20Blanco?= Date: Sat, 30 Sep 2023 00:55:48 +0200 Subject: [PATCH] Use importlib in Python >= 3.12, add Conan v1 deprecation message (#14841) * Use importlib in Python 3.12 * Add Conan 1 deprecation warning * Move warning to only show in create/install/build --- conans/client/command.py | 11 ++++++++++- conans/client/loader.py | 15 ++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/conans/client/command.py b/conans/client/command.py index 5b548b805ea..0a7036104e0 100644 --- a/conans/client/command.py +++ b/conans/client/command.py @@ -4,6 +4,7 @@ import os import signal import sys +import textwrap from argparse import ArgumentError from difflib import get_close_matches @@ -318,6 +319,7 @@ def create(self, *args): that the package has been created correctly. Check 'conan test' command to know more about 'test_folder' project. """ + self._warn_conan_version() parser = argparse.ArgumentParser(description=self.create.__doc__, prog="conan create", formatter_class=SmartFormatter) @@ -469,6 +471,7 @@ def install(self, *args): the package is installed, Conan will write the files for the specified generators. """ + self._warn_conan_version() parser = argparse.ArgumentParser(description=self.install.__doc__, prog="conan install", formatter_class=SmartFormatter) @@ -859,7 +862,7 @@ def build(self, *args): using a build helper, like CMake(), the --package-folder will be configured as the destination folder for the install step. """ - + self._warn_conan_version() parser = argparse.ArgumentParser(description=self.build.__doc__, prog="conan build", formatter_class=SmartFormatter) @@ -2177,6 +2180,12 @@ def _warn_python_version(self): front=Color.BRIGHT_RED) self._out.writeln("*"*width, front=Color.BRIGHT_RED) + def _warn_conan_version(self): + width = 70 + self._out.writeln(textwrap.fill("Conan 1 is on a deprecation path, " + "please consider migrating to Conan 2", width), + front=Color.BRIGHT_YELLOW) + def run(self, *args): """HIDDEN: entry point for executing commands, dispatcher to class methods diff --git a/conans/client/loader.py b/conans/client/loader.py index 91650936495..7eae666bbcd 100644 --- a/conans/client/loader.py +++ b/conans/client/loader.py @@ -1,5 +1,4 @@ import fnmatch -import imp import inspect import os import re @@ -40,6 +39,9 @@ def __init__(self, runner, output, python_requires, generator_manager=None, pyre sys.modules["conans"].python_requires = python_requires self._cached_conanfile_classes = {} self._requester = requester + if sys.version_info.major >= 3 and sys.version_info.minor >= 12: + from importlib import invalidate_caches + invalidate_caches() def load_basic(self, conanfile_path, lock_python_requires=None, user=None, channel=None, display=""): @@ -439,8 +441,15 @@ def _parse_conanfile(conan_file_path): old_dont_write_bytecode = sys.dont_write_bytecode try: sys.dont_write_bytecode = True - # FIXME: imp is deprecated in favour of implib - loaded = imp.load_source(module_id, conan_file_path) + # imp is deprecated in favour of importlib, removed in 3.12 + if sys.version_info.major >= 3 and sys.version_info.minor >= 12: + from importlib import util as imp_util + spec = imp_util.spec_from_file_location(module_id, conan_file_path) + loaded = imp_util.module_from_spec(spec) + spec.loader.exec_module(loaded) + else: + import imp + loaded = imp.load_source(module_id, conan_file_path) sys.dont_write_bytecode = old_dont_write_bytecode except ImportError: version_txt = _get_required_conan_version_without_loading(conan_file_path)