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
introduced
by
Loading history...
|
|||
25 | import platform |
||
0 ignored issues
–
show
|
|||
26 | import signal |
||
27 | import sys |
||
0 ignored issues
–
show
|
|||
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
|
|||
37 | except ImportError: |
||
38 | print('psutil library not found. Glances cannot start.') |
||
0 ignored issues
–
show
|
|||
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
|
|||
44 | from glances.main import GlancesMain |
||
0 ignored issues
–
show
|
|||
45 | from glances.globals import WINDOWS |
||
46 | from glances.timer import Counter |
||
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
|
|||
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
|
|||
56 | sys.exit(1) |
||
57 | |||
58 | # Check psutil version |
||
59 | psutil_min_version = (5, 3, 0) |
||
0 ignored issues
–
show
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
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
|
|||
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 | try: |
||
74 | mode.end() |
||
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 |
||
91 | |||
92 | start_duration = Counter() |
||
93 | |||
94 | if core.is_standalone(): |
||
95 | from glances.standalone import GlancesStandalone as GlancesMode |
||
96 | elif core.is_client(): |
||
97 | if core.is_client_browser(): |
||
98 | from glances.client_browser import GlancesClientBrowser as GlancesMode |
||
99 | else: |
||
100 | from glances.client import GlancesClient as GlancesMode |
||
101 | elif core.is_server(): |
||
102 | from glances.server import GlancesServer as GlancesMode |
||
103 | elif core.is_webserver(): |
||
104 | from glances.webserver import GlancesWebServer as GlancesMode |
||
105 | |||
106 | # Init the mode |
||
107 | logger.info("Start {} mode".format(GlancesMode.__name__)) |
||
108 | mode = GlancesMode(config=config, args=args) |
||
109 | |||
110 | # Start the main loop |
||
111 | logger.debug("Glances started in {} seconds".format(start_duration.get())) |
||
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__)) |
||
129 | logger.info('{} {} and psutil {} detected'.format( |
||
0 ignored issues
–
show
|
|||
130 | platform.python_implementation(), |
||
131 | platform.python_version(), |
||
132 | psutil_version)) |
||
133 | |||
134 | # Share global var |
||
135 | global core |
||
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 |