Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pkgresources import iso6 #7869

Closed
wants to merge 15 commits into from
7 changes: 7 additions & 0 deletions changelogs/unreleased/fix-pkgresources-import.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
description: Fixed dropped import from pkg_resources
change-type: patch
sections:
bugfix: "Addressed breaking change in setuptools (core Python library)"
upgrade-note: "If you had previously constrained `setuptools<71` in your project's `requirements.txt`, you may now drop the constraint"
destination-branches:
- iso6
40 changes: 21 additions & 19 deletions src/inmanta/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,26 @@
from pkg_resources import DistInfoDistribution, Distribution, Requirement

import inmanta.module
import packaging.requirements
from inmanta import const
from inmanta.ast import CompilerException
from inmanta.server.bootloader import InmantaBootloader
from inmanta.stable_api import stable_api
from inmanta.util import strtobool
from packaging import version

InvalidRequirement: tuple[Exception]
try:
from typing import TYPE_CHECKING
# older versions of pkg_resources raise this exception, newer versions don't have extern and raise the native one
import pkg_resources.extern.packaging.requirements

InvalidRequirement = (
packaging.requirements.InvalidRequirement,
pkg_resources.extern.packaging.requirements.InvalidRequirement,
)
except ImportError:
TYPE_CHECKING = False
InvalidRequirement = (packaging.requirements.InvalidRequirement,)

if TYPE_CHECKING:
from packaging.requirements import InvalidRequirement
else:
from pkg_resources.extern.packaging.requirements import InvalidRequirement

LOGGER = logging.getLogger(__name__)
LOGGER_PIP = logging.getLogger("inmanta.pip") # Use this logger to log pip commands or data related to pip commands.
Expand Down Expand Up @@ -807,19 +811,17 @@ def _gen_content_requirements_file(cls, requirements_list: Sequence[str]) -> str
extras = None
try:
# this will fail if an url is supplied
parsed_req = list(pkg_resources.parse_requirements(req_spec))
if len(parsed_req) > 0:
item = parsed_req[0]
if hasattr(item, "name"):
name = item.name
elif hasattr(item, "unsafe_name"):
name = item.unsafe_name
version = item.specs
marker = item.marker
if hasattr(item, "url"):
url = item.url
if hasattr(item, "extras") and len(item.extras) > 0:
extras = sorted(item.extras)
parsed_req = Requirement.parse(req_spec)
if hasattr(parsed_req, "name"):
name = parsed_req.name
elif hasattr(parsed_req, "unsafe_name"):
name = parsed_req.unsafe_name
version = parsed_req.specs
marker = parsed_req.marker
if hasattr(parsed_req, "url"):
url = parsed_req.url
if hasattr(parsed_req, "extras") and len(parsed_req.extras) > 0:
extras = sorted(parsed_req.extras)
except InvalidRequirement:
url = req_spec

Expand Down
9 changes: 2 additions & 7 deletions src/inmanta/moduletool.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from configparser import ConfigParser
from functools import total_ordering
from re import Pattern
from typing import TYPE_CHECKING, Any, Dict, List, Optional
from typing import Any, Dict, List, Optional

import click
import more_itertools
Expand Down Expand Up @@ -76,11 +76,6 @@
from inmanta.stable_api import stable_api
from packaging.version import Version

if TYPE_CHECKING:
from packaging.requirements import InvalidRequirement
else:
from pkg_resources.extern.packaging.requirements import InvalidRequirement

LOGGER = logging.getLogger(__name__)


Expand Down Expand Up @@ -812,7 +807,7 @@ def add(self, module_req: str, v1: bool = False, v2: bool = False, override: boo
raise CLIException("Current working directory doesn't contain an Inmanta module or project", exitcode=1)
try:
module_requirement = InmantaModuleRequirement.parse(module_req)
except InvalidRequirement:
except env.InvalidRequirement:
raise CLIException(f"'{module_req}' is not a valid requirement", exitcode=1)
if not override and module_like.has_module_requirement(module_requirement.key):
raise CLIException(
Expand Down