Passed
Pull Request — master (#1929)
by
unknown
02:32
created

gammapy/scripts/info.py (2 issues)

1
# Licensed under a 3-clause BSD style license - see LICENSE.rst
2
from __future__ import absolute_import, division, print_function, unicode_literals
3
import os
4
import platform
5
import sys
6
import warnings
7
import logging
8
import importlib
9
from collections import OrderedDict
10
import click
0 ignored issues
show
Unable to import 'click'
Loading history...
11
from .. import version
12
13
log = logging.getLogger(__name__)
14
15
GAMMAPY_DEPENDENCIES = [
16
    "numpy",
17
    "scipy",
18
    "matplotlib",
19
    "cython",
20
    "astropy",
21
    "astropy_healpix",
22
    "reproject",
23
    "sherpa",
24
    "pytest",
25
    "sphinx",
26
    "healpy",
27
    "regions",
28
    "iminuit",
29
    "naima",
30
    "uncertainties",
31
]
32
33
GAMMAPY_ENV_VARIABLES = [
34
    "GAMMAPY_EXTRA",
35
    "GAMMA_CAT",
36
    "GAMMAPY_FERMI_LAT_DATA",
37
    "CTADATA",
38
]
39
40
41
@click.command(name="info")
42
@click.option("--system/--no-system", default=True, help="Show system info")
43
@click.option("--version/--no-version", default=True, help="Show version info")
44
@click.option(
45
    "--dependencies/--no-dependencies", default=True, help="Show dependencies info"
46
)
47
@click.option("--envvar/--no-envvar", default=True, help="Show environment variables")
48
def cli_info(system, version, dependencies, envvar):
0 ignored issues
show
Comprehensibility Bug introduced by
version is re-defining a name which is already available in the outer-scope (previously defined on line 11).

It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior:

param = 5

class Foo:
    def __init__(self, param):   # "param" would be flagged here
        self.param = param
Loading history...
49
    """Display information about Gammapy
50
    """
51
    if system:
52
        info = get_info_system()
53
        print_info(info=info, title="System")
54
55
    if version:
56
        info = get_info_version()
57
        print_info(info=info, title="Gammapy package")
58
59
    if dependencies:
60
        info = get_info_dependencies()
61
        print_info(info=info, title="Other packages")
62
63
    if envvar:
64
        info = get_info_envvar()
65
        print_info(info=info, title="Gammapy environment variables")
66
67
68
def print_info(info, title):
69
    """Print Gammapy info."""
70
    info_all = "\n{}:\n\n".format(title)
71
72
    for key, value in info.items():
73
        info_all += "\t{:22s} : {:<10s} \n".format(key, value)
74
75
    print(info_all)
76
77
78
def get_info_system():
79
    """Get info about user system"""
80
    info = OrderedDict()
81
    info["python_executable"] = sys.executable
82
    info["python_version"] = platform.python_version()
83
    info["machine"] = platform.machine()
84
    info["system"] = platform.system()
85
    return info
86
87
88
def get_info_version():
89
    """Get detailed info about Gammapy version."""
90
    info = OrderedDict()
91
    try:
92
        path = sys.modules["gammapy"].__path__[0]
93
    except:
94
        path = "unknown"
95
    info["path"] = path
96
    info["version"] = version.version
97
    if not version.release:
98
        info["githash"] = version.githash
99
    return info
100
101
102
def get_info_dependencies():
103
    """Get info about Gammapy dependencies."""
104
    info = OrderedDict()
105
    for name in GAMMAPY_DEPENDENCIES:
106
        try:
107
            with warnings.catch_warnings():
108
                warnings.simplefilter("ignore")
109
                module = importlib.import_module(name)
110
111
            module_version = getattr(module, "__version__", "no version info found")
112
        except ImportError:
113
            module_version = "not installed"
114
        info[name] = module_version
115
    return info
116
117
118
def get_info_envvar():
119
    """Get info about Gammapy environment variables."""
120
    info = OrderedDict()
121
    for name in GAMMAPY_ENV_VARIABLES:
122
        info[name] = os.environ.get(name, "not set")
123
    return info
124