__init__.py 3.8 KB
Newer Older
N
Nicolas Hennion 已提交
1 2
# -*- coding: utf-8 -*-
#
3
# This file is part of Glances.
N
Nicolas Hennion 已提交
4
#
N
nicolargo 已提交
5
# Copyright (C) 2018 Nicolargo <nicolas@nicolargo.com>
N
Nicolas Hennion 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18
#
# Glances is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Glances is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
N
Nicolargo 已提交
19
#
A
Alessio Sergi 已提交
20

A
PEP 257  
Alessio Sergi 已提交
21
"""Init the Glances software."""
N
Nicolas Hennion 已提交
22

23
# Import system libs
24
import locale
A
Alessio Sergi 已提交
25
import platform
26
import signal
A
Alessio Sergi 已提交
27
import sys
28

N
Nicolargo 已提交
29
# Global name
30
__version__ = '3.0.1'
N
Nicolargo 已提交
31
__author__ = 'Nicolas Hennion <nicolas@nicolargo.com>'
A
Alessio Sergi 已提交
32
__license__ = 'LGPLv3'
N
Nicolargo 已提交
33

A
Alessio Sergi 已提交
34 35
# Import psutil
try:
36
    from psutil import __version__ as psutil_version
A
Alessio Sergi 已提交
37
except ImportError:
A
Alessio Sergi 已提交
38
    print('psutil library not found. Glances cannot start.')
A
Alessio Sergi 已提交
39 40
    sys.exit(1)

N
Nicolas Hennion 已提交
41
# Import Glances libs
42
# Note: others Glances libs will be imported optionally
43 44
from glances.logger import logger
from glances.main import GlancesMain
45
from glances.globals import WINDOWS
N
Nicolas Hennion 已提交
46

47
# Check locale
48 49 50
try:
    locale.setlocale(locale.LC_ALL, '')
except locale.Error:
51
    print("Warning: Unable to set locale. Expect encoding problems.")
52

53
# Check Python version
A
Alessio Sergi 已提交
54 55
if sys.version_info < (2, 7) or (3, 0) <= sys.version_info < (3, 4):
    print('Glances requires at least Python 2.7 or 3.4 to run.')
N
nicolargo 已提交
56
    sys.exit(1)
57

A
Alessio Sergi 已提交
58 59
# Check psutil version
psutil_min_version = (5, 3, 0)
60 61
psutil_version_info = tuple([int(num) for num in psutil_version.split('.')])
if psutil_version_info < psutil_min_version:
A
Alessio Sergi 已提交
62
    print('psutil 5.3.0 or higher is needed. Glances cannot start.')
N
Nicolas Hennion 已提交
63 64
    sys.exit(1)

A
flake8  
Alessio Sergi 已提交
65

66
def __signal_handler(signal, frame):
A
PEP 257  
Alessio Sergi 已提交
67
    """Callback for CTRL-C."""
68 69 70 71
    end()


def end():
A
PEP 257  
Alessio Sergi 已提交
72
    """Stop Glances."""
73 74 75 76 77 78 79
    try:
        mode.end()
    except NameError:
        # NameError: name 'mode' is not defined in case of interrupt shortly...
        # ...after starting the server mode (issue #1175)
        pass

80
    logger.info("Glances stopped (keypressed: CTRL-C)")
81 82 83 84 85

    # The end...
    sys.exit(0)


86
def start(config, args):
87
    """Start Glances."""
N
Nicolas Hennion 已提交
88

89 90
    # Load mode
    global mode
N
Nicolas Hennion 已提交
91

92
    if core.is_standalone():
93
        from glances.standalone import GlancesStandalone as GlancesMode
94
    elif core.is_client():
95 96 97 98 99 100 101 102
        if core.is_client_browser():
            from glances.client_browser import GlancesClientBrowser as GlancesMode
        else:
            from glances.client import GlancesClient as GlancesMode
    elif core.is_server():
        from glances.server import GlancesServer as GlancesMode
    elif core.is_webserver():
        from glances.webserver import GlancesWebServer as GlancesMode
N
Nicolas Hennion 已提交
103

104 105 106
    # Init the mode
    logger.info("Start {} mode".format(GlancesMode.__name__))
    mode = GlancesMode(config=config, args=args)
107

108 109
    # Start the main loop
    mode.serve_forever()
110

111 112
    # Shutdown
    mode.end()
N
nicolargo 已提交
113 114 115 116 117 118 119 120


def main():
    """Main entry point for Glances.

    Select the mode (standalone, client or server)
    Run it...
    """
121 122 123
    # Catch the CTRL-C signal
    signal.signal(signal.SIGINT, __signal_handler)

A
Alessio Sergi 已提交
124
    # Log Glances and psutil version
N
nicolargo 已提交
125
    logger.info('Start Glances {}'.format(__version__))
A
Alessio Sergi 已提交
126
    logger.info('{} {} and psutil {} detected'.format(
N
nicolargo 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139
        platform.python_implementation(),
        platform.python_version(),
        psutil_version))

    # Share global var
    global core

    # Create the Glances main instance
    core = GlancesMain()
    config = core.get_config()
    args = core.get_args()

    # Glances can be ran in standalone, client or server mode
140
    start(config=config, args=args)