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

Improve info tool dependencies option #2303

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion ctapipe/core/provenance.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def get_module_version(name):
try:
module = import_module(name)
return module.__version__
except AttributeError:
except (AttributeError, ModuleNotFoundError):
try:
return version(name)
except Exception:
Expand Down
61 changes: 31 additions & 30 deletions ctapipe/tools/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
""" print information about ctapipe and its command-line tools. """
import logging
import os
import re
import sys
from importlib.metadata import metadata, requires
from importlib.resources import files

from ..core import Provenance, get_module_version
Expand All @@ -12,27 +14,6 @@

__all__ = ["info"]

# TODO: this list should be global (or generated at install time)
_dependencies = sorted(
[
"astropy",
"matplotlib",
"numpy",
"traitlets",
"sklearn",
"scipy",
"numba",
"pytest",
"iminuit",
"tables",
"eventio",
]
)

_optional_dependencies = sorted(
["ctapipe_resources", "pytest", "graphviz", "matplotlib"]
)


def main(args=None):
parser = get_parser(info)
Expand Down Expand Up @@ -78,6 +59,12 @@ def main(args=None):
info(**vars(args))


def pretty_print_requires(package):
pack_name = re.split(";|=|>|<|@|~| ", package)[0]
entry = f"{pack_name} -- {get_module_version(pack_name)}"
return entry


def info(
version=False,
tools=False,
Expand Down Expand Up @@ -160,17 +147,32 @@ def _info_tools():

def _info_dependencies():
"""Print info about dependencies."""
print("\n*** ctapipe core dependencies ***\n")

for name in _dependencies:
version = get_module_version(name)
print(f"{name:>20s} -- {version}")
meta = metadata("ctapipe")
extras = [v for k, v in meta.items() if k == "Provides-Extra"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be:

In [10]: meta.get_all("Provides-Extra")
Out[10]: ['all', 'dev', 'docs', 'tests']


all_dependencies = set(requires("ctapipe"))

print("\n*** ctapipe optional dependencies ***\n")
optional_dependencies = {extra: [] for extra in extras}

for name in _optional_dependencies:
version = get_module_version(name)
print(f"{name:>20s} -- {version}")
required_dependencies = []
for package in all_dependencies:
if "extra" in package:
for extra in extras:
if extra in package:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is the same if twice here

optional_dependencies[extra].append(pretty_print_requires(package))
else:
required_dependencies.append(pretty_print_requires(package))

print("\n*** ctapipe core dependencies ***\n")

for package in required_dependencies:
print(package)

for extra in optional_dependencies:
print(f"\n*** ctapipe optional dependencies [{extra}] ***\n")
for package in optional_dependencies[extra]:
print(package)


def _info_resources():
Expand Down Expand Up @@ -215,7 +217,6 @@ def _info_system():
system_prov = prov.current_activity.provenance["system"]

for section in ["platform", "python"]:

print("\n====== ", section, " ======== \n")
sysinfo = system_prov[section]

Expand Down
78 changes: 78 additions & 0 deletions ctapipe/tools/tests/test_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""Test ctapipe-info functionality."""


def test_info_version(script_runner):

result = script_runner.run(
"ctapipe-info",
"--version",
)

assert result.success
assert result.stderr == ""


def test_info_tools(script_runner):

result = script_runner.run(
"ctapipe-info",
"--tools",
)

assert result.success
assert result.stderr == ""


def test_info_dependencies(script_runner):

result = script_runner.run(
"ctapipe-info",
"--dependencies",
)

assert result.success
assert result.stderr == ""


def test_info_system(script_runner):

result = script_runner.run(
"ctapipe-info",
"--system",
)

assert result.success
assert result.stderr == ""


def test_info_plugins(script_runner):

result = script_runner.run(
"ctapipe-info",
"--plugins",
)

assert result.success
assert result.stderr == ""


def test_info_eventsources(script_runner):

result = script_runner.run(
"ctapipe-info",
"--event-sources",
)

assert result.success
assert result.stderr == ""


def test_info_all(script_runner):

result = script_runner.run(
"ctapipe-info",
"--all",
)

assert result.success
assert result.stderr == ""
6 changes: 0 additions & 6 deletions ctapipe/tools/tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,6 @@ def test_display_dl1(tmp_path, dl1_image_file, dl1_parameters_file):
run_tool(DisplayDL1Calib(), ["--help-all"], raises=True)


def test_info():
from ctapipe.tools.info import info

info(show_all=True)


def test_fileinfo(tmp_path, dl1_image_file):
"""check we can run ctapipe-fileinfo and get results"""
import yaml
Expand Down
2 changes: 2 additions & 0 deletions docs/changes/2303.optimization.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Optimize ``ctapipe-info --dependencies`` by reading them from the package installation code.
Use ``pytest-console-scripts`` to test this tool.
1 change: 1 addition & 0 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dependencies:
- psutil
- pytables
- pytest
- pytest-console-scripts
- pytest-cov
- pytest-runner
- pytest-astropy-header
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ tests =
tomli
pytest_astropy_header
h5py

pytest-console-scripts

docs =
sphinx
Expand Down