/
info.py
117 lines (95 loc) · 2.92 KB
/
info.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# Licensed under a 3-clause BSD style license - see LICENSE.rst
import importlib
import logging
import os
import platform
import sys
import warnings
import click
from gammapy import __version__
log = logging.getLogger(__name__)
# Should be in sync with `docs/install/dependencies.rst`
GAMMAPY_DEPENDENCIES = [
# required
"numpy",
"scipy",
"astropy",
"regions",
"click",
"yaml",
# "pydantic", # has no __version__
# optional
"IPython",
# "jupyter", # has no __version__
"jupyterlab",
"matplotlib",
"pandas",
"healpy",
"iminuit",
"sherpa",
"naima",
"emcee",
"corner",
"ray",
]
GAMMAPY_ENV_VARIABLES = ["GAMMAPY_DATA"]
@click.command(name="info")
@click.option("--system/--no-system", default=True, help="Show system info")
@click.option("--version/--no-version", default=True, help="Show version")
@click.option(
"--dependencies/--no-dependencies", default=True, help="Show dependencies"
)
@click.option("--envvar/--no-envvar", default=True, help="Show environment variables")
def cli_info(system, version, dependencies, envvar):
"""Display information about Gammapy"""
if system:
info = get_info_system()
print_info(info=info, title="System")
if version:
info = get_info_version()
print_info(info=info, title="Gammapy package")
if dependencies:
info = get_info_dependencies()
print_info(info=info, title="Other packages")
if envvar:
info = get_info_envvar()
print_info(info=info, title="Gammapy environment variables")
def print_info(info, title):
"""Print Gammapy info."""
info_all = f"\n{title}:\n\n"
for key, value in info.items():
info_all += f"\t{key:22s} : {value:<10s} \n"
print(info_all)
def get_info_system():
"""Get info about user system"""
return {
"python_executable": sys.executable,
"python_version": platform.python_version(),
"machine": platform.machine(),
"system": platform.system(),
}
def get_info_version():
"""Get detailed info about Gammapy version."""
info = {"version": __version__}
try:
path = sys.modules["gammapy"].__path__[0]
except Exception:
path = "unknown"
info["path"] = path
return info
def get_info_dependencies():
"""Get info about Gammapy dependencies."""
info = {}
for name in GAMMAPY_DEPENDENCIES:
try:
with warnings.catch_warnings():
warnings.simplefilter("ignore")
module = importlib.import_module(name)
module_version = getattr(module, "__version__", "no version info found")
except ImportError:
module_version = "not installed"
info[name] = module_version
return info
def get_info_envvar():
"""Get info about Gammapy environment variables."""
return {name: os.environ.get(name, "not set") for name in GAMMAPY_ENV_VARIABLES}