Skip to content

Commit

Permalink
remove use of deprecated pkg-resources
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-c committed Dec 8, 2023
1 parent 80348ed commit 3042700
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 21 deletions.
13 changes: 6 additions & 7 deletions cwltest/argparser.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
"""Command line argument parsing for cwltest."""
import argparse
import sys

import pkg_resources
from importlib.metadata import PackageNotFoundError, version

from cwltest import DEFAULT_TIMEOUT

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

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

return parser
19 changes: 12 additions & 7 deletions cwltest/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
)

import junit_xml
import pkg_resources
import ruamel.yaml.scanner
import schema_salad.avro
import schema_salad.ref_resolver
Expand All @@ -32,8 +31,15 @@
from ruamel.yaml.scalarstring import ScalarString
from schema_salad.exceptions import ValidationException

if sys.version_info >= (3, 9):
from importlib.resources import as_file, files
else:
from importlib_resources import as_file, files

from cwltest import REQUIRED, UNSUPPORTED_FEATURE, logger, templock

__all__ = ["files", "as_file"]


class CWLTestConfig:
"""Store configuration values for cwltest."""
Expand Down Expand Up @@ -167,12 +173,11 @@ def load_and_validate_tests(path: str) -> Tuple[Any, Dict[str, Any]]:
This also processes $import directives.
"""
schema_resource = pkg_resources.resource_stream(__name__, "cwltest-schema.yml")
cache: Optional[Dict[str, Union[str, Graph, bool]]] = {
"https://w3id.org/cwl/cwltest/cwltest-schema.yml": schema_resource.read().decode(
"utf-8"
)
}
schema_resource = files("cwltest").joinpath("cwltest-schema.yml")
with schema_resource.open("r", encoding="utf-8") as fp:
cache: Optional[Dict[str, Union[str, Graph, bool]]] = {
"https://w3id.org/cwl/cwltest/cwltest-schema.yml": fp.read()
}
(
document_loader,
avsc_names,
Expand Down
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@
html_static_path = ["_static"]


from pkg_resources import get_distribution
from importlib.metadata import version as metadata_version

release = get_distribution("cwltest").version
release = metadata_version("cwltest")
version = ".".join(release.split(".")[:2])

autoapi_dirs = ["../cwltest"]
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ schema-salad >= 5.0.20200220195218, < 9
junit-xml >= 1.8
pytest >= 7, < 8
defusedxml
importlib_resources>=1.4;python_version<'3.9'
17 changes: 12 additions & 5 deletions tests/util.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
"""Test functions."""
import atexit
import os
import subprocess # nosec
from contextlib import ExitStack
from pathlib import Path
from typing import List, Tuple

from pkg_resources import Requirement, ResolutionError, resource_filename
from cwltest.utils import as_file, files


def get_data(filename: str) -> str:
Expand All @@ -12,12 +16,15 @@ def get_data(filename: str) -> str:
# joining path
filepath = None
try:
filepath = resource_filename(Requirement.parse("cwltest"), filename)
except ResolutionError:
file_manager = ExitStack()
atexit.register(file_manager.close)
traversable = files("cwltest") / filename
filepath = file_manager.enter_context(as_file(traversable))
except ModuleNotFoundError:
pass
if not filepath or not os.path.isfile(filepath):
filepath = os.path.join(os.path.dirname(__file__), os.pardir, filename)
return filepath
filepath = Path(os.path.dirname(__file__)) / ".." / filename
return str(filepath.resolve())


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

0 comments on commit 3042700

Please sign in to comment.