Completed
Push — master ( a1acfe...a11662 )
by Nicolas
01:53 queued 19s
created

end()   A

Complexity

Conditions 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
dl 0
loc 7
rs 9.4285
c 3
b 0
f 0
1
# -*- coding: utf-8 -*-
2
#
3
# This file is part of Glances.
4
#
5
# Copyright (C) 2017 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__ = '2.9.0'
31
__author__ = 'Nicolas Hennion <[email protected]>'
32
__license__ = 'LGPLv3'
33
34
# Import psutil
35
try:
36
    from psutil import __version__ as psutil_version
37
except ImportError:
38
    print('PSutil library not found. Glances cannot start.')
39
    sys.exit(1)
40
41
# Import Glances libs
42
# Note: others Glances libs will be imported optionally
43
from glances.logger import logger
44
from glances.main import GlancesMain
45
from glances.globals import WINDOWS
46
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.")
52
53
# Check Python version
54
if sys.version_info < (2, 7) or (3, 0) <= sys.version_info < (3, 3):
55
    print('Glances requires at least Python 2.7 or 3.3 to run.')
56
    sys.exit(1)
57
58
# Check PSutil version
59
psutil_min_version = (2, 0, 0)
60
psutil_version_info = tuple([int(num) for num in psutil_version.split('.')])
61
if psutil_version_info < psutil_min_version:
62
    print('PSutil 2.0 or higher is needed. Glances cannot start.')
63
    sys.exit(1)
64
65
66
def __signal_handler(signal, frame):
67
    """Callback for CTRL-C."""
68
    end()
69
70
71
def end():
72
    """Stop Glances."""
73
    mode.end()
74
    logger.info("Glances stopped with CTRL-C")
75
76
    # The end...
77
    sys.exit(0)
78
79
80
def start(config, args):
81
    """Start Glances"""
82
83
    # Load mode
84
    global mode
85
86
    if core.is_standalone():
87
        from glances.standalone import GlancesStandalone as GlancesMode
88
    elif core.is_client():
89
        if core.is_client_browser():
90
            from glances.client_browser import GlancesClientBrowser as GlancesMode
91
        else:
92
            from glances.client import GlancesClient as GlancesMode
93
    elif core.is_server():
94
        from glances.server import GlancesServer as GlancesMode
95
    elif core.is_webserver():
96
        from glances.webserver import GlancesWebServer as GlancesMode
97
98
    # Init the mode
99
    logger.info("Start {} mode".format(GlancesMode.__name__))
100
    mode = GlancesMode(config=config, args=args)
101
102
    # Start the main loop
103
    mode.serve_forever()
104
105
    # Shutdown
106
    mode.end()
107
108
109
def main():
110
    """Main entry point for Glances.
111
112
    Select the mode (standalone, client or server)
113
    Run it...
114
    """
115
    # Log Glances and PSutil version
116
    logger.info('Start Glances {}'.format(__version__))
117
    logger.info('{} {} and PSutil {} detected'.format(
118
        platform.python_implementation(),
119
        platform.python_version(),
120
        psutil_version))
121
122
    # Share global var
123
    global core
124
125
    # Create the Glances main instance
126
    core = GlancesMain()
127
    config = core.get_config()
128
    args = core.get_args()
129
130
    # Catch the CTRL-C signal
131
    signal.signal(signal.SIGINT, __signal_handler)
132
133
    # Glances can be ran in standalone, client or server mode
134
    start(config=config, args=args)
135