Skip to content

Commit 3042700

Browse files
committed
remove use of deprecated pkg-resources
1 parent 80348ed commit 3042700

File tree

5 files changed

+33
-21
lines changed

5 files changed

+33
-21
lines changed

cwltest/argparser.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
"""Command line argument parsing for cwltest."""
22
import argparse
33
import sys
4-
5-
import pkg_resources
4+
from importlib.metadata import PackageNotFoundError, version
65

76
from cwltest import DEFAULT_TIMEOUT
87

@@ -107,10 +106,10 @@ def arg_parser() -> argparse.ArgumentParser:
107106
help="Create JSON badges and store them in this directory.",
108107
)
109108

110-
if pkg := pkg_resources.require("cwltest"):
111-
ver = f"{sys.argv[0]} {pkg[0].version}"
112-
else:
113-
ver = "{} {}".format(sys.argv[0], "unknown version")
114-
parser.add_argument("--version", action="version", version=ver)
109+
try:
110+
ver = version("cwltest")
111+
except PackageNotFoundError:
112+
ver = "unknown version"
113+
parser.add_argument("--version", action="version", version=f"{sys.argv[0]} {ver}")
115114

116115
return parser

cwltest/utils.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
)
2323

2424
import junit_xml
25-
import pkg_resources
2625
import ruamel.yaml.scanner
2726
import schema_salad.avro
2827
import schema_salad.ref_resolver
@@ -32,8 +31,15 @@
3231
from ruamel.yaml.scalarstring import ScalarString
3332
from schema_salad.exceptions import ValidationException
3433

34+
if sys.version_info >= (3, 9):
35+
from importlib.resources import as_file, files
36+
else:
37+
from importlib_resources import as_file, files
38+
3539
from cwltest import REQUIRED, UNSUPPORTED_FEATURE, logger, templock
3640

41+
__all__ = ["files", "as_file"]
42+
3743

3844
class CWLTestConfig:
3945
"""Store configuration values for cwltest."""
@@ -167,12 +173,11 @@ def load_and_validate_tests(path: str) -> Tuple[Any, Dict[str, Any]]:
167173
168174
This also processes $import directives.
169175
"""
170-
schema_resource = pkg_resources.resource_stream(__name__, "cwltest-schema.yml")
171-
cache: Optional[Dict[str, Union[str, Graph, bool]]] = {
172-
"https://w3id.org/cwl/cwltest/cwltest-schema.yml": schema_resource.read().decode(
173-
"utf-8"
174-
)
175-
}
176+
schema_resource = files("cwltest").joinpath("cwltest-schema.yml")
177+
with schema_resource.open("r", encoding="utf-8") as fp:
178+
cache: Optional[Dict[str, Union[str, Graph, bool]]] = {
179+
"https://w3id.org/cwl/cwltest/cwltest-schema.yml": fp.read()
180+
}
176181
(
177182
document_loader,
178183
avsc_names,

docs/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@
8080
html_static_path = ["_static"]
8181

8282

83-
from pkg_resources import get_distribution
83+
from importlib.metadata import version as metadata_version
8484

85-
release = get_distribution("cwltest").version
85+
release = metadata_version("cwltest")
8686
version = ".".join(release.split(".")[:2])
8787

8888
autoapi_dirs = ["../cwltest"]

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ schema-salad >= 5.0.20200220195218, < 9
22
junit-xml >= 1.8
33
pytest >= 7, < 8
44
defusedxml
5+
importlib_resources>=1.4;python_version<'3.9'

tests/util.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
"""Test functions."""
2+
import atexit
13
import os
24
import subprocess # nosec
5+
from contextlib import ExitStack
6+
from pathlib import Path
37
from typing import List, Tuple
48

5-
from pkg_resources import Requirement, ResolutionError, resource_filename
9+
from cwltest.utils import as_file, files
610

711

812
def get_data(filename: str) -> str:
@@ -12,12 +16,15 @@ def get_data(filename: str) -> str:
1216
# joining path
1317
filepath = None
1418
try:
15-
filepath = resource_filename(Requirement.parse("cwltest"), filename)
16-
except ResolutionError:
19+
file_manager = ExitStack()
20+
atexit.register(file_manager.close)
21+
traversable = files("cwltest") / filename
22+
filepath = file_manager.enter_context(as_file(traversable))
23+
except ModuleNotFoundError:
1724
pass
1825
if not filepath or not os.path.isfile(filepath):
19-
filepath = os.path.join(os.path.dirname(__file__), os.pardir, filename)
20-
return filepath
26+
filepath = Path(os.path.dirname(__file__)) / ".." / filename
27+
return str(filepath.resolve())
2128

2229

2330
def run_with_mock_cwl_runner(args: List[str]) -> Tuple[int, str, str]:

0 commit comments

Comments
 (0)