Test Failed
Push — master ( 4c6c3d...040528 )
by Nicolas
04:27
created

glances/__init__.py (2 issues)

Check for undefined variables.

Best Practice Comprehensibility Minor
1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of Glances.
4
#
5
# Copyright (C) 2020 Nicolargo <[email protected]>
6
#
7
# Glances is free software; you can redistribute it and/or modify
8
# it under the terms of the GNU Lesser General Public License as published by
9
# the Free Software Foundation, either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Glances is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU Lesser General Public License for more details.
16
#
17
# You should have received a copy of the GNU Lesser General Public License
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
#
20
21
"""Init the Glances software."""
22
23
# Import system libs
24
import locale
25
import platform
26
import signal
27
import sys
28
29
# Global name
30
# Version should start and end with a numerical char
31
# See https://packaging.python.org/specifications/core-metadata/#version
32
__version__ = '3.1.6.2'
33
__author__ = 'Nicolas Hennion <[email protected]>'
34
__license__ = 'LGPLv3'
35
36
# Import psutil
37
try:
38
    from psutil import __version__ as psutil_version
39
except ImportError:
40
    print('psutil library not found. Glances cannot start.')
41
    sys.exit(1)
42
43
# Import Glances libs
44
# Note: others Glances libs will be imported optionally
45
from glances.logger import logger
46
from glances.main import GlancesMain
47
from glances.globals import WINDOWS
48
from glances.timer import Counter
49
# Check locale
50
try:
51
    locale.setlocale(locale.LC_ALL, '')
52
except locale.Error:
53
    print("Warning: Unable to set locale. Expect encoding problems.")
54
55
# Check Python version
56
if sys.version_info < (2, 7) or (3, 0) <= sys.version_info < (3, 4):
57
    print('Glances requires at least Python 2.7 or 3.4 to run.')
58
    sys.exit(1)
59
60
# Check psutil version
61
psutil_min_version = (5, 3, 0)
62
psutil_version_info = tuple([int(num) for num in psutil_version.split('.')])
63
if psutil_version_info < psutil_min_version:
64
    print('psutil 5.3.0 or higher is needed. Glances cannot start.')
65
    sys.exit(1)
66
67
68
def __signal_handler(signal, frame):
69
    """Callback for CTRL-C."""
70
    end()
71
72
73
def end():
74
    """Stop Glances."""
75
    try:
76
        mode.end()
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable mode does not seem to be defined.
Loading history...
77
    except NameError:
78
        # NameError: name 'mode' is not defined in case of interrupt shortly...
79
        # ...after starting the server mode (issue #1175)
80
        pass
81
82
    logger.info("Glances stopped (keypressed: CTRL-C)")
83
84
    # The end...
85
    sys.exit(0)
86
87
88
def start(config, args):
89
    """Start Glances."""
90
91
    # Load mode
92
    global mode
93
94
    start_duration = Counter()
95
96
    if core.is_standalone():
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable core does not seem to be defined.
Loading history...
97
        from glances.standalone import GlancesStandalone as GlancesMode
98
    elif core.is_client():
99
        if core.is_client_browser():
100
            from glances.client_browser import GlancesClientBrowser as GlancesMode
101
        else:
102
            from glances.client import GlancesClient as GlancesMode
103
    elif core.is_server():
104
        from glances.server import GlancesServer as GlancesMode
105
    elif core.is_webserver():
106
        from glances.webserver import GlancesWebServer as GlancesMode
107
108
    # Init the mode
109
    logger.info("Start {} mode".format(GlancesMode.__name__))
110
    mode = GlancesMode(config=config, args=args)
111
112
    # Start the main loop
113
    logger.debug("Glances started in {} seconds".format(start_duration.get()))
114
    mode.serve_forever()
115
116
    # Shutdown
117
    mode.end()
118
119
120
def main():
121
    """Main entry point for Glances.
122
123
    Select the mode (standalone, client or server)
124
    Run it...
125
    """
126
    # Catch the CTRL-C signal
127
    signal.signal(signal.SIGINT, __signal_handler)
128
129
    # Log Glances and psutil version
130
    logger.info('Start Glances {}'.format(__version__))
131
    logger.info('{} {} and psutil {} detected'.format(
132
        platform.python_implementation(),
133
        platform.python_version(),
134
        psutil_version))
135
136
    # Share global var
137
    global core
138
139
    # Create the Glances main instance
140
    core = GlancesMain()
141
    config = core.get_config()
142
    args = core.get_args()
143
144
    # Glances can be ran in standalone, client or server mode
145
    start(config=config, args=args)
146