Skip to content

Commit 4e8ec90

Browse files
committed
Update all pkg_resources references to use importlib
Automated, slop transformation.
1 parent 7cf6277 commit 4e8ec90

7 files changed

Lines changed: 43 additions & 37 deletions

File tree

src/dlstbx/cli/run_health_checks.py

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

33
import argparse
44
import concurrent.futures
5+
import importlib.metadata
56
import logging
67

7-
import pkg_resources
88
import zocalo.configuration
99

1010
import dlstbx.health_checks as hc
@@ -15,7 +15,8 @@
1515

1616
def run():
1717
check_functions = {
18-
e.name: e.load for e in pkg_resources.iter_entry_points("zocalo.health_checks")
18+
e.name: e.load
19+
for e in importlib.metadata.entry_points(group="zocalo.health_checks")
1920
}
2021

2122
parser = argparse.ArgumentParser(

src/dlstbx/cli/wrap.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77

88
import argparse
99
import faulthandler
10+
import importlib.metadata
1011
import json
1112
import logging
1213
import signal
1314
import sys
1415

15-
import pkg_resources
1616
import workflows.recipe.wrapper
1717
import workflows.services.common_service
1818
import workflows.transport
@@ -63,7 +63,7 @@ def run():
6363
zc.activate()
6464

6565
known_wrappers = {
66-
e.name: e.load for e in pkg_resources.iter_entry_points("zocalo.wrappers")
66+
e.name: e.load for e in importlib.metadata.entry_points(group="zocalo.wrappers")
6767
}
6868

6969
# Set up parser

src/dlstbx/mimas/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
import dataclasses
44
import enum
55
import functools
6+
import importlib.metadata
67
import numbers
78
from typing import Any, Callable, List, Optional, Tuple, Union
89

910
import gemmi
10-
import pkg_resources
1111
import zocalo.configuration
1212

1313
from dlstbx.mimas.specification import BaseSpecification
@@ -411,7 +411,7 @@ def inner_wrapper(
411411
def _get_handlers() -> dict[str, Callable]:
412412
return {
413413
e.name: e.load()
414-
for e in pkg_resources.iter_entry_points("zocalo.mimas.handlers")
414+
for e in importlib.metadata.entry_points(group="zocalo.mimas.handlers")
415415
}
416416

417417

src/dlstbx/requirements.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
# command line options are passed through to the conda install process
55
from __future__ import annotations
66

7+
import importlib.metadata
78
import json
89
import os
910
import pathlib
1011
import subprocess
1112
import sys
1213
from typing import Optional
1314

14-
import pkg_resources
15+
from packaging.requirements import Requirement
1516

1617
import dlstbx
1718

@@ -81,17 +82,15 @@ def check():
8182
conda_environment = {
8283
package["name"]: package["version"] for package in json.loads(conda_list)
8384
}
84-
requirements = [
85-
(spec, pkg_resources.Requirement.parse(spec)) for spec in sorted(conda_required)
86-
]
85+
requirements = [(spec, Requirement(spec)) for spec in sorted(conda_required)]
8786

8887
# Now we should have an unduplicated set of requirements
8988
action_list = []
9089
for original_spec, requirement in requirements:
9190
# Check if package is installed in development mode
9291
try:
93-
currentversion = pkg_resources.require(requirement.name)[0].version
94-
except Exception:
92+
currentversion = importlib.metadata.version(requirement.name)
93+
except importlib.metadata.PackageNotFoundError:
9594
pass
9695
else:
9796
location = None
@@ -101,13 +100,13 @@ def check():
101100
with open(egg_link) as fh:
102101
location = fh.readline().strip()
103102
break
104-
if location and currentversion in requirement:
103+
if location and currentversion in requirement.specifier:
105104
print(
106105
"requires conda package %s, has %s as developer installation"
107106
% (requirement, currentversion)
108107
)
109108
continue
110-
elif location and currentversion not in requirement:
109+
elif location and currentversion not in requirement.specifier:
111110
_notice(
112111
" WARNING: Can not update package {package} automatically.",
113112
"",
@@ -125,7 +124,7 @@ def check():
125124

126125
# Check if package is installed with conda
127126
if requirement.name in conda_environment:
128-
if conda_environment[requirement.name] in requirement:
127+
if conda_environment[requirement.name] in requirement.specifier:
129128
print(
130129
"requires conda package %s, has %s"
131130
% (requirement, conda_environment[requirement.name])

src/dlstbx/services/cluster.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
import datetime
44
import errno
55
import getpass
6+
import importlib.metadata
67
import json
78
import logging
89
import math
910
import os
1011
import pathlib
1112
from typing import Optional
1213

13-
import pkg_resources
1414
import pydantic
1515
import requests
1616
import workflows.recipe
@@ -159,8 +159,8 @@ def initializing(self):
159159

160160
self.schedulers = {
161161
f.name: f.load()
162-
for f in pkg_resources.iter_entry_points(
163-
"zocalo.services.cluster.schedulers"
162+
for f in importlib.metadata.entry_points(
163+
group="zocalo.services.cluster.schedulers"
164164
)
165165
}
166166
self.log.debug(f"Supported schedulers: {', '.join(self.schedulers.keys())}")

src/dlstbx/services/images.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
from __future__ import annotations
22

33
import errno
4+
import importlib.metadata
45
import logging
56
import os
67
import re
78
import time
89
from typing import Any, Callable, Dict, NamedTuple, Protocol
910

1011
import PIL.Image
11-
import pkg_resources
1212
import procrunner
1313
import workflows.recipe
1414
from workflows.services.common_service import CommonService
@@ -53,7 +53,9 @@ def initializing(self):
5353
self.log.info("Image service starting")
5454
self.image_functions: dict[str, Callable] = {
5555
e.name: e.load()
56-
for e in pkg_resources.iter_entry_points("zocalo.services.images.plugins")
56+
for e in importlib.metadata.entry_points(
57+
group="zocalo.services.images.plugins"
58+
)
5759
}
5860
workflows.recipe.wrap_subscribe(
5961
self._transport,

tests/conftest.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# pytest configuration file
22
from __future__ import annotations
33

4+
import importlib.metadata
45
import os
56
from typing import List
7+
from unittest.mock import patch
68

79
import ispyb.sqlalchemy
8-
import pkg_resources
910
import pytest
1011
from sqlalchemy import create_engine
1112
from sqlalchemy.orm import sessionmaker
@@ -85,20 +86,23 @@ def handle_i99(scenario: mimas.MimasScenario, **kwargs) -> List[mimas.Invocation
8586

8687
@pytest.fixture
8788
def with_dummy_plugins():
88-
# Get the current distribution and entry map
89-
dist = pkg_resources.get_distribution("dlstbx")
90-
entry_map = pkg_resources.get_entry_map("dlstbx", group="zocalo.mimas.handlers")
91-
92-
# Create the fake entry point definitions and add the mapping
93-
entry_map["i99"] = pkg_resources.EntryPoint.parse(
94-
f"i99 = {__name__}:handle_i99", dist=dist
95-
)
96-
entry_map["i99_rotation"] = pkg_resources.EntryPoint.parse(
97-
f"i99_rotation = {__name__}:handle_i99_rotation", dist=dist
98-
)
99-
mimas._get_handlers.cache_clear()
100-
yield
101-
# cleanup
102-
del entry_map["i99"]
103-
del entry_map["i99_rotation"]
89+
real_entry_points = importlib.metadata.entry_points(group="zocalo.mimas.handlers")
90+
fake_entry_points = [
91+
importlib.metadata.EntryPoint(
92+
name="i99", value=f"{__name__}:handle_i99", group="zocalo.mimas.handlers"
93+
),
94+
importlib.metadata.EntryPoint(
95+
name="i99_rotation",
96+
value=f"{__name__}:handle_i99_rotation",
97+
group="zocalo.mimas.handlers",
98+
),
99+
]
100+
combined = list(real_entry_points) + fake_entry_points
101+
102+
with patch(
103+
"dlstbx.mimas.importlib.metadata.entry_points",
104+
return_value=combined,
105+
):
106+
mimas._get_handlers.cache_clear()
107+
yield
104108
mimas._get_handlers.cache_clear()

0 commit comments

Comments
 (0)