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 deprecation warning due to pkg_resources #2259

Merged
merged 3 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions ctapipe/core/provenance.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@
from os.path import abspath
from pathlib import Path

import pkg_resources
import psutil
from astropy.time import Time
from pkg_resources import get_distribution

import ctapipe

from .support import Singleton

if sys.version_info < (3, 9):
from importlib_metadata import distributions, version
else:
from importlib.metadata import distributions, version

log = logging.getLogger(__name__)

__all__ = ["Provenance"]
Expand All @@ -52,7 +55,7 @@ def get_module_version(name):
return module.__version__
except AttributeError:
try:
return get_distribution(name).version
return version(name)
except Exception:
return "unknown"
except ImportError:
Expand Down Expand Up @@ -286,8 +289,8 @@ def provenance(self):

def _get_python_packages():
return [
{"name": p.project_name, "version": p.version, "path": p.module_path}
for p in sorted(pkg_resources.working_set, key=lambda p: p.project_name)
{"name": p.name, "version": p.version}
for p in sorted(distributions(), key=lambda d: d.name)
]


Expand Down
9 changes: 6 additions & 3 deletions ctapipe/tools/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
import os
import sys

from pkg_resources import resource_filename

from ..core import Provenance, get_module_version
from ..core.plugins import detect_and_import_plugins
from ..utils import datasets
from .utils import get_parser

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

__all__ = ["info"]

# TODO: this list should be global (or generated at install time)
Expand Down Expand Up @@ -189,7 +192,7 @@ def _info_resources():
all_resources = sorted(datasets.find_all_matching_datasets(r"\w.*"))
home = os.path.expanduser("~")
try:
resource_dir = resource_filename("ctapipe_resources", "")
resource_dir = files("ctapipe_resources")
except ImportError:
resource_dir = None

Expand Down
19 changes: 14 additions & 5 deletions ctapipe/tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@
"""Utils to create scripts and command-line tools"""
import argparse
import importlib
import sys
from collections import OrderedDict

if sys.version_info < (3, 10):
from importlib_metadata import distribution
else:
from importlib.metadata import distribution

__all__ = [
"ArgparseFormatter",
"get_parser",
Expand Down Expand Up @@ -38,9 +44,11 @@ def get_installed_tools():
TODO: not sure if this will be useful ... maybe to check if the list
of installed packages matches the available scripts somehow?
"""
from pkg_resources import get_entry_map

console_tools = get_entry_map("ctapipe")["console_scripts"]
console_tools = {
ep.name: ep.value
for ep in distribution("ctapipe").entry_points
if ep.group == "console_scripts"
}
return console_tools


Expand All @@ -49,8 +57,9 @@ def get_all_descriptions():
tools = get_installed_tools()

descriptions = OrderedDict()
for name, info in tools.items():
module = importlib.import_module(info.module_name)
for name, value in tools.items():
module_name, attr = value.split(":")
module = importlib.import_module(module_name)
if hasattr(module, "__doc__") and module.__doc__ is not None:
try:
descrip = module.__doc__
Expand Down
19 changes: 12 additions & 7 deletions ctapipe/utils/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import yaml
from astropy.table import Table
from pkg_resources import resource_listdir
from requests.exceptions import HTTPError

from .download import download_file_cached, get_cache_path
Expand All @@ -31,8 +30,11 @@
logger = logging.getLogger(__name__)

__all__ = [
"get_dataset_path", "find_in_path", "find_all_matching_datasets",
"get_default_url", "DEFAULT_URL"
"get_dataset_path",
"find_in_path",
"find_all_matching_datasets",
"get_default_url",
"DEFAULT_URL",
]


Expand Down Expand Up @@ -66,7 +68,10 @@ def get_searchpath_dirs(searchpath=None, url=None):


def find_all_matching_datasets(
pattern, searchpath=None, regexp_group=None, url=None,
pattern,
searchpath=None,
regexp_group=None,
url=None,
):
"""
Returns a list of resource names (or substrings) matching the given
Expand Down Expand Up @@ -111,13 +116,13 @@ def find_all_matching_datasets(

# then check resources module
if has_resources:
for resource in resource_listdir("ctapipe_resources", ""):
match = re.match(pattern, resource)
for resource in files("ctapipe_resources").iterdir():
match = re.match(pattern, resource.name)
if match:
if regexp_group is not None:
results.add(match.group(regexp_group))
else:
results.add(Path(resource))
results.add(resource)

return list(results)

Expand Down
2 changes: 0 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ exclude = '''

[tool.pytest.ini_options]
filterwarnings = [
"error::DeprecationWarning",
"error::PendingDeprecationWarning",
"error::astropy.utils.exceptions.AstropyDeprecationWarning",
"error::ctapipe.utils.deprecation.CTAPipeDeprecationWarning",
]
Expand Down