Skip to content

Commit

Permalink
Use importlib in Python >= 3.12, add Conan v1 deprecation message (#1…
Browse files Browse the repository at this point in the history
…4841)

* Use importlib in Python 3.12

* Add Conan 1 deprecation warning

* Move warning to only show in create/install/build
  • Loading branch information
AbrilRBS authored Sep 29, 2023
1 parent 1a16a54 commit 8149642
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
11 changes: 10 additions & 1 deletion conans/client/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import signal
import sys
import textwrap
from argparse import ArgumentError
from difflib import get_close_matches

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
15 changes: 12 additions & 3 deletions conans/client/loader.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import fnmatch
import imp
import inspect
import os
import re
Expand Down Expand Up @@ -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=""):
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 8149642

Please sign in to comment.