From 84492573963178a0815dfc699608a669f725f308 Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Tue, 31 Aug 2021 12:45:40 +0100 Subject: [PATCH] Remove dependency on setuptools Fixes: #3219 --- mypy.ini | 3 --- setup.cfg | 2 +- src/molecule/__init__.py | 11 ++++++----- src/molecule/driver/base.py | 4 +--- src/molecule/shell.py | 4 ++-- src/molecule/test/functional/conftest.py | 6 +++--- 6 files changed, 13 insertions(+), 17 deletions(-) diff --git a/mypy.ini b/mypy.ini index 99346acdd7..c10054b53b 100644 --- a/mypy.ini +++ b/mypy.ini @@ -31,9 +31,6 @@ ignore_missing_imports = True [mypy-ruamel] ignore_missing_imports = True -[mypy-setuptools] -ignore_missing_imports = True - [mypy-testinfra.*] ignore_missing_imports = True diff --git a/setup.cfg b/setup.cfg index 50e5234fd7..ba8548b8f4 100644 --- a/setup.cfg +++ b/setup.cfg @@ -73,6 +73,7 @@ install_requires = cookiecutter >= 1.7.3 # dependency issues in older versions dataclasses; python_version<"3.7" enrich >= 1.2.5 + importlib-metadata<2; python_version<"3.8" Jinja2 >= 2.11.3 packaging paramiko >= 2.5.0, < 3 @@ -80,7 +81,6 @@ install_requires = PyYAML >= 5.1, < 6 rich >= 9.5.1 subprocess-tee >= 0.3.2 - setuptools >= 42 # for pkg_resources # selinux python module is needed as least by ansible-docker/podman modules # and allows us of isolated (default) virtualenvs. It does not avoid need # to install the system selinux libraries but it will provide a clear diff --git a/src/molecule/__init__.py b/src/molecule/__init__.py index 99418861f8..6367ba673a 100644 --- a/src/molecule/__init__.py +++ b/src/molecule/__init__.py @@ -23,10 +23,11 @@ from __future__ import absolute_import, division, print_function __metaclass__ = type - try: - import pkg_resources + # py38+ + from importlib.metadata import version # type: ignore +except ImportError: + # py36-py37 + from importlib_metadata import version - __version__ = pkg_resources.get_distribution("molecule").version -except Exception: - __version__ = "unknown" +__version__ = version("molecule") diff --git a/src/molecule/driver/base.py b/src/molecule/driver/base.py index 0384b9b0a3..705175f9e0 100644 --- a/src/molecule/driver/base.py +++ b/src/molecule/driver/base.py @@ -24,8 +24,6 @@ from abc import ABCMeta, abstractmethod from typing import Dict -import pkg_resources - import molecule from molecule.status import Status @@ -45,7 +43,7 @@ def __init__(self, config=None): self._config = config self._path = os.path.abspath(os.path.dirname(inspect.getfile(self.__class__))) self.module = self.__module__.split(".", maxsplit=1)[0] - self.version = pkg_resources.get_distribution(self.module).version + self.version = molecule.version(self.module) @property @abstractmethod diff --git a/src/molecule/shell.py b/src/molecule/shell.py index 27c789f06e..b47d306c25 100644 --- a/src/molecule/shell.py +++ b/src/molecule/shell.py @@ -23,7 +23,7 @@ import sys import click -import pkg_resources +import packaging from ansible_compat.runtime import Runtime import molecule @@ -56,7 +56,7 @@ def print_version(ctx, param, value): if not value or ctx.resilient_parsing: return - v = pkg_resources.parse_version(molecule.__version__) + v = packaging.version.Version(molecule.__version__) color = "bright_yellow" if v.is_prerelease else "green" msg = f"molecule [{color}]{v}[/] using python [repr.number]{sys.version_info[0]}.{sys.version_info[1]}[/] \n" diff --git a/src/molecule/test/functional/conftest.py b/src/molecule/test/functional/conftest.py index d94104f783..51339ac706 100644 --- a/src/molecule/test/functional/conftest.py +++ b/src/molecule/test/functional/conftest.py @@ -26,11 +26,11 @@ from typing import Optional import pexpect -import pkg_resources import pytest from ansible_compat.runtime import Runtime from packaging.version import Version +import molecule from molecule import logger, util from molecule.test.conftest import change_dir_to, molecule_directory from molecule.text import strip_ansi_color @@ -47,8 +47,8 @@ @pytest.fixture(scope="session", autouse=True) def require_installed_package(): try: - pkg_resources.require("molecule") - except pkg_resources.DistributionNotFound as e: + molecule.version("molecule") + except Exception as e: pytest.fail( "Functional tests require molecule package to be installed: {}".format(e) )