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

glances/__init__.py (39 issues)

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
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
25
import platform
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
26
import signal
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
27
import sys
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
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
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
39
except ImportError:
40
    print('psutil library not found. Glances cannot start.')
0 ignored issues
show
Unused Code Coding Style introduced by
Unnecessary parens after u'print' keyword
Loading history...
print statement used
Loading history...
41
    sys.exit(1)
42
43
# Import Glances libs
44
# Note: others Glances libs will be imported optionally
45
from glances.logger import logger
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
46
from glances.main import GlancesMain
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
47
from glances.globals import WINDOWS
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
48
from glances.timer import Counter
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
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.")
0 ignored issues
show
Unused Code Coding Style introduced by
Unnecessary parens after u'print' keyword
Loading history...
print statement used
Loading history...
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.')
0 ignored issues
show
Unused Code Coding Style introduced by
Unnecessary parens after u'print' keyword
Loading history...
print statement used
Loading history...
58
    sys.exit(1)
59
60
# Check psutil version
61
psutil_min_version = (5, 3, 0)
0 ignored issues
show
Coding Style Naming introduced by
The name psutil_min_version does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
62
psutil_version_info = tuple([int(num) for num in psutil_version.split('.')])
0 ignored issues
show
Coding Style Naming introduced by
The name psutil_version_info does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
63
if psutil_version_info < psutil_min_version:
64
    print('psutil 5.3.0 or higher is needed. Glances cannot start.')
0 ignored issues
show
Unused Code Coding Style introduced by
Unnecessary parens after u'print' keyword
Loading history...
print statement used
Loading history...
65
    sys.exit(1)
66
67
68
def __signal_handler(signal, frame):
0 ignored issues
show
Comprehensibility Bug introduced by
signal is re-defining a name which is already available in the outer-scope (previously defined on line 26).

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...
The argument signal seems to be unused.
Loading history...
The argument frame seems to be unused.
Loading history...
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
0 ignored issues
show
Global variable 'mode' undefined at the module level
Loading history...
Coding Style Naming introduced by
The name mode does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
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
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
98
    elif core.is_client():
99
        if core.is_client_browser():
100
            from glances.client_browser import GlancesClientBrowser as GlancesMode
0 ignored issues
show
This line is too long as per the coding-style (82/80).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
import missing from __future__ import absolute_import
Loading history...
101
        else:
102
            from glances.client import GlancesClient as GlancesMode
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
103
    elif core.is_server():
104
        from glances.server import GlancesServer as GlancesMode
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
105
    elif core.is_webserver():
106
        from glances.webserver import GlancesWebServer as GlancesMode
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
107
108
    # Init the mode
109
    logger.info("Start {} mode".format(GlancesMode.__name__))
0 ignored issues
show
Use formatting in logging functions and pass the parameters as arguments
Loading history...
The variable GlancesMode does not seem to be defined for all execution paths.
Loading history...
110
    mode = GlancesMode(config=config, args=args)
111
112
    # Start the main loop
113
    logger.debug("Glances started in {} seconds".format(start_duration.get()))
0 ignored issues
show
Use formatting in logging functions and pass the parameters as arguments
Loading history...
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__))
0 ignored issues
show
Use formatting in logging functions and pass the parameters as arguments
Loading history...
131
    logger.info('{} {} and psutil {} detected'.format(
0 ignored issues
show
Use formatting in logging functions and pass the parameters as arguments
Loading history...
132
        platform.python_implementation(),
133
        platform.python_version(),
134
        psutil_version))
135
136
    # Share global var
137
    global core
0 ignored issues
show
Global variable 'core' undefined at the module level
Loading history...
Coding Style Naming introduced by
The name core does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
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