Skip to content

Commit 7287d3c

Browse files
authored
Merge pull request #43 from mxstack/42
fixes #42: no pkg_resources on Python 3.12
2 parents 8b9c52d + 26ddb46 commit 7287d3c

File tree

13 files changed

+80
-55
lines changed

13 files changed

+80
-55
lines changed

.github/workflows/tests.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ jobs:
1414
steps:
1515
- uses: actions/checkout@v4
1616
- uses: actions/setup-python@v5
17+
with:
18+
python-version: "3.12"
1719
- uses: pre-commit/action@v3.0.0
1820
build:
1921
strategy:

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ build/
1010
constraints-mxdev.txt
1111
example/*-outfile.txt
1212
requirements-mxdev.txt
13-
venv/
13+
.venv/
1414
dist/

.pre-commit-config.yaml

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
---
22
repos:
33
- repo: https://github.com/psf/black.git
4-
rev: 22.3.0
4+
rev: 24.2.0
55
hooks:
66
- id: black
77
language_version: python3
88
exclude: ^(tests\/hooks-abort-render\/hooks|docs\/HelloCookieCutter1)
99

1010
- repo: https://github.com/pre-commit/mirrors-mypy
11-
rev: 'v0.960' # Use the sha / tag you want to point at
11+
rev: 'v1.9.0' # Use the sha / tag you want to point at
1212
hooks:
1313
- id: mypy
1414
additional_dependencies: [types-setuptools]
1515
- repo: https://github.com/PyCQA/doc8
16-
rev: 0.8.1
16+
rev: v1.1.1
1717
hooks:
1818
- id: doc8
1919
name: doc8
@@ -28,13 +28,8 @@ repos:
2828
# - id: flake8
2929
# additional_dependencies:
3030
# - flake8-docstrings
31-
# - repo: https://github.com/PyCQA/bandit
32-
# rev: 1.6.0
33-
# hooks:
34-
# - id: bandit
35-
# args: [--ini, .bandit]
3631
- repo: https://github.com/mgedmin/check-manifest
37-
rev: "0.48"
32+
rev: "0.49"
3833
hooks:
3934
- id: check-manifest
4035
# - repo: https://github.com/Lucas-C/pre-commit-hooks-safety

CHANGES.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
## 4.0.2 (unreleased)
44

5-
6-
- Nothing changed yet.
7-
5+
- Fix #42: deprecated use of `pkg_resoures` to load entry points and parse requirements.
6+
This enables mxdev to work on Python 3.12, where `pkg_resources` is no longer installed by default in virtual_envs.
7+
[jensens]
88

99
## 4.0.1 (2024-03-01)
1010

Makefile

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ VENV_CREATE?=true
8181
# target folder for the virtual environment. If `VENV_ENABLED` is `true` and
8282
# `VENV_CREATE` is false it is expected to point to an existing virtual
8383
# environment. If `VENV_ENABLED` is `false` it is ignored.
84-
# Default: venv
85-
VENV_FOLDER?=venv
84+
# Default: .venv
85+
VENV_FOLDER?=.venv
8686

8787
# mxdev to install in virtual environment.
8888
# Default: mxdev
@@ -222,8 +222,13 @@ endif
222222

223223
# Determine the executable path
224224
ifeq ("$(VENV_ENABLED)", "true")
225-
export PATH:=$(abspath $(VENV_FOLDER))/bin:$(PATH)
226225
export VIRTUAL_ENV=$(abspath $(VENV_FOLDER))
226+
ifeq ("$(OS)", "Windows_NT")
227+
VENV_EXECUTABLE_FOLDER=$(VIRTUAL_ENV)/Scripts
228+
else
229+
VENV_EXECUTABLE_FOLDER=$(VIRTUAL_ENV)/bin
230+
endif
231+
export PATH:=$(VENV_EXECUTABLE_FOLDER):$(PATH)
227232
MXENV_PYTHON=python
228233
else
229234
MXENV_PYTHON=$(PRIMARY_PYTHON)

pyproject.toml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,16 @@ classifiers = [
2121
"Programming Language :: Python :: 3.11",
2222
"Programming Language :: Python :: 3.12",
2323
]
24-
dependencies = []
24+
dependencies = ["packaging"]
2525
dynamic = ["readme"]
2626

2727
[project.optional-dependencies]
28-
mypy = [
29-
"types-setuptools",
30-
"types-pkg-resources",
31-
]
28+
mypy = []
3229
test = [
3330
"pytest",
3431
"pytest-cov",
3532
"pytest-mock",
3633
"httpretty",
37-
"types-setuptools",
3834
]
3935

4036
[project.urls]

src/mxdev/config.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from .including import read_with_included
22
from .logging import logger
3+
from packaging.requirements import Requirement
34

45
import os
5-
import pkg_resources
66
import typing
77

88

@@ -55,11 +55,11 @@ def __init__(
5555
self.overrides = {}
5656
for line in raw_overrides.split("\n"):
5757
try:
58-
parsed = pkg_resources.Requirement.parse(line)
58+
parsed = Requirement(line)
5959
except Exception:
6060
logger.error(f"Can not parse override: {line}")
6161
continue
62-
self.overrides[parsed.key] = line
62+
self.overrides[parsed.name] = line
6363

6464
raw_ignores = settings.get("ignores", "").strip()
6565
self.ignore_keys = []
@@ -68,7 +68,7 @@ def __init__(
6868
if line:
6969
self.ignore_keys.append(line)
7070

71-
def is_ns_member(name):
71+
def is_ns_member(name) -> bool:
7272
for hook in hooks:
7373
if name.startswith(hook.namespace):
7474
return True

src/mxdev/entry_points.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# this is a helper to load entrypoints with importlib, since pkg_resources
2+
# is deprecated. In Python 3.12 an API incompatible change was introduced,
3+
# so this code is that ugly now.
4+
from importlib.metadata import entry_points
5+
6+
7+
try:
8+
# do we have Python 3.12+?
9+
from importlib.metadata import EntryPoints # type: ignore # noqa: F401
10+
11+
HAS_IMPORTLIB_ENTRYPOINTS = True
12+
except ImportError:
13+
HAS_IMPORTLIB_ENTRYPOINTS = False
14+
15+
16+
def load_eps_by_group(group: str) -> list:
17+
if HAS_IMPORTLIB_ENTRYPOINTS:
18+
eps = entry_points(group=group) # type: ignore
19+
else:
20+
eps_base = entry_points()
21+
if group not in eps_base:
22+
return []
23+
eps = eps_base[group] # type: ignore
24+
# XXX: for some reasons entry points are loaded twice. not sure if this
25+
# is a glitch when installing with uv or something related to
26+
# importlib.metadata.entry_points
27+
return list(set(eps)) # type: ignore

src/mxdev/hooks.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
1+
from .entry_points import load_eps_by_group
12
from .state import State
2-
from pkg_resources import iter_entry_points
33

44
import typing
55

66

7+
try:
8+
# do we have Python 3.12+
9+
from importlib.metadata import EntryPoints # type: ignore # noqa: F401
10+
11+
HAS_IMPORTLIB_ENTRYPOINTS = True
12+
except ImportError:
13+
HAS_IMPORTLIB_ENTRYPOINTS = False
14+
15+
716
class Hook:
817
"""Entry point for hooking into mxdev."""
918

@@ -18,7 +27,7 @@ def write(self, state: State) -> None:
1827

1928

2029
def load_hooks() -> list:
21-
return [ep.load()() for ep in iter_entry_points("mxdev") if ep.name == "hook"]
30+
return [ep.load()() for ep in load_eps_by_group("mxdev") if ep.name == "hook"]
2231

2332

2433
def read_hooks(state: State, hooks: typing.List[Hook]) -> None:

src/mxdev/processing.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from .logging import logger
22
from .state import State
33
from .vcs.common import WorkingCopies
4+
from packaging.requirements import Requirement
45
from pathlib import Path
56
from urllib import parse
67
from urllib import request
78

8-
import pkg_resources
99
import typing
1010

1111

@@ -49,15 +49,15 @@ def process_line(
4949
variety="r",
5050
)
5151
try:
52-
parsed = pkg_resources.Requirement.parse(line)
52+
parsed = Requirement(line)
5353
except Exception:
5454
pass
5555
else:
56-
if parsed.key in package_keys:
56+
if parsed.name in package_keys:
5757
line = f"# {line.strip()} -> mxdev disabled (source)\n"
58-
if variety == "c" and parsed.key in override_keys:
58+
if variety == "c" and parsed.name in override_keys:
5959
line = f"# {line.strip()} -> mxdev disabled (override)\n"
60-
if variety == "c" and parsed.key in ignore_keys:
60+
if variety == "c" and parsed.name in ignore_keys:
6161
line = f"# {line.strip()} -> mxdev disabled (ignore)\n"
6262
if variety == "c":
6363
return [], [line]

0 commit comments

Comments
 (0)