__init__.py 4.2 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) 2021 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 31
# Version should start and end with a numerical char
# See https://packaging.python.org/specifications/core-metadata/#version
N
nicolargo 已提交
32
__version__ = '3.2.0'
N
Nicolargo 已提交
33
__author__ = 'Nicolas Hennion <nicolas@nicolargo.com>'
A
Alessio Sergi 已提交
34
__license__ = 'LGPLv3'
N
Nicolargo 已提交
35

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

N
Nicolas Hennion 已提交
43
# Import Glances libs
44
# Note: others Glances libs will be imported optionally
45 46
from glances.logger import logger
from glances.main import GlancesMain
47
from glances.timer import Counter
48
# Check locale
49 50 51
try:
    locale.setlocale(locale.LC_ALL, '')
except locale.Error:
52
    print("Warning: Unable to set locale. Expect encoding problems.")
53

54
# Check Python version
A
Alessio Sergi 已提交
55 56
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 已提交
57
    sys.exit(1)
58

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

A
flake8  
Alessio Sergi 已提交
66

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


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

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

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


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

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

93 94
    start_duration = Counter()

95
    if core.is_standalone():
96
        from glances.standalone import GlancesStandalone as GlancesMode
97
    elif core.is_client():
98 99 100 101 102 103 104 105
        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 已提交
106

107 108 109
    # Init the mode
    logger.info("Start {} mode".format(GlancesMode.__name__))
    mode = GlancesMode(config=config, args=args)
110

111
    # Start the main loop
112
    logger.debug("Glances started in {} seconds".format(start_duration.get()))
N
nicolargo 已提交
113
    if args.stdout_issue:
114
        # Serve once for issue/test mode
N
nicolargo 已提交
115 116 117 118
        mode.serve_issue()
    else:
        # Serve forever
        mode.serve_forever()
119

120 121
    # Shutdown
    mode.end()
N
nicolargo 已提交
122 123 124 125 126 127 128 129


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

    Select the mode (standalone, client or server)
    Run it...
    """
130 131 132
    # Catch the CTRL-C signal
    signal.signal(signal.SIGINT, __signal_handler)

A
Alessio Sergi 已提交
133
    # Log Glances and psutil version
N
nicolargo 已提交
134
    logger.info('Start Glances {}'.format(__version__))
A
Alessio Sergi 已提交
135
    logger.info('{} {} and psutil {} detected'.format(
N
nicolargo 已提交
136 137 138 139 140 141 142 143
        platform.python_implementation(),
        platform.python_version(),
        psutil_version))

    # Share global var
    global core

    # Create the Glances main instance
144 145
    # Glances options from the command line are readed first (in __init__)
    # then the options from the config file (in parse_args)
N
nicolargo 已提交
146 147 148
    core = GlancesMain()

    # Glances can be ran in standalone, client or server mode
149
    start(config=core.get_config(), args= core.get_args())