Test Failed
Push — develop ( d7cf39...faa4bd )
by Nicolas
04:34 queued 10s
created

glances/__init__.py (39 issues)

1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of Glances.
4
#
5
# Copyright (C) 2019 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__ = '3.1.3_BETA'
31
__author__ = 'Nicolas Hennion <[email protected]>'
32
__license__ = 'LGPLv3'
33
34
# Import psutil
35
try:
36
    from psutil import __version__ as psutil_version
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
37
except ImportError:
38
    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...
39
    sys.exit(1)
40
41
# Import Glances libs
42
# Note: others Glances libs will be imported optionally
43
from glances.logger import logger
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
44
from glances.main import GlancesMain
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
45
from glances.globals import WINDOWS
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
46
from glances.timer import Counter
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
47
# Check locale
48
try:
49
    locale.setlocale(locale.LC_ALL, '')
50
except locale.Error:
51
    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...
52
53
# Check Python version
54
if sys.version_info < (2, 7) or (3, 0) <= sys.version_info < (3, 4):
55
    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...
56
    sys.exit(1)
57
58
# Check psutil version
59
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...
60
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...
61
if psutil_version_info < psutil_min_version:
62
    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...
63
    sys.exit(1)
64
65
66
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...
67
    """Callback for CTRL-C."""
68
    end()
69
70
71
def end():
72
    """Stop Glances."""
73
    try:
74
        mode.end()
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable mode does not seem to be defined.
Loading history...
75
    except NameError:
76
        # NameError: name 'mode' is not defined in case of interrupt shortly...
77
        # ...after starting the server mode (issue #1175)
78
        pass
79
80
    logger.info("Glances stopped (keypressed: CTRL-C)")
81
82
    # The end...
83
    sys.exit(0)
84
85
86
def start(config, args):
87
    """Start Glances."""
88
89
    # Load mode
90
    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...
91
92
    start_duration = Counter()
93
94
    if core.is_standalone():
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable core does not seem to be defined.
Loading history...
95
        from glances.standalone import GlancesStandalone as GlancesMode
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
96
    elif core.is_client():
97
        if core.is_client_browser():
98
            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...
99
        else:
100
            from glances.client import GlancesClient as GlancesMode
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
101
    elif core.is_server():
102
        from glances.server import GlancesServer as GlancesMode
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
103
    elif core.is_webserver():
104
        from glances.webserver import GlancesWebServer as GlancesMode
0 ignored issues
show
import missing from __future__ import absolute_import
Loading history...
105
106
    # Init the mode
107
    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...
108
    mode = GlancesMode(config=config, args=args)
109
110
    # Start the main loop
111
    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...
112
    mode.serve_forever()
113
114
    # Shutdown
115
    mode.end()
116
117
118
def main():
119
    """Main entry point for Glances.
120
121
    Select the mode (standalone, client or server)
122
    Run it...
123
    """
124
    # Catch the CTRL-C signal
125
    signal.signal(signal.SIGINT, __signal_handler)
126
127
    # Log Glances and psutil version
128
    logger.info('Start Glances {}'.format(__version__))
0 ignored issues
show
Use formatting in logging functions and pass the parameters as arguments
Loading history...
129
    logger.info('{} {} and psutil {} detected'.format(
0 ignored issues
show
Use formatting in logging functions and pass the parameters as arguments
Loading history...
130
        platform.python_implementation(),
131
        platform.python_version(),
132
        psutil_version))
133
134
    # Share global var
135
    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...
136
137
    # Create the Glances main instance
138
    core = GlancesMain()
139
    config = core.get_config()
140
    args = core.get_args()
141
142
    # Glances can be ran in standalone, client or server mode
143
    start(config=config, args=args)
144