|
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
|
|
|
"""Manage Glances update.""" |
|
21
|
|
|
|
|
22
|
|
|
from datetime import datetime, timedelta |
|
23
|
|
|
from distutils.version import LooseVersion |
|
24
|
|
|
import threading |
|
25
|
|
|
import json |
|
26
|
|
|
import pickle |
|
27
|
|
|
import os |
|
28
|
|
|
try: |
|
29
|
|
|
import requests |
|
30
|
|
|
except ImportError: |
|
31
|
|
|
outdated_tag = False |
|
32
|
|
|
else: |
|
33
|
|
|
outdated_tag = True |
|
34
|
|
|
|
|
35
|
|
|
from glances import __version__ |
|
36
|
|
|
from glances.config import user_cache_dir |
|
37
|
|
|
from glances.globals import safe_makedirs |
|
38
|
|
|
from glances.logger import logger |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
class Outdated(object): |
|
42
|
|
|
|
|
43
|
|
|
""" |
|
44
|
|
|
This class aims at providing methods to warn the user when a new Glances |
|
45
|
|
|
version is available on the Pypi repository (https://pypi.python.org/pypi/Glances/). |
|
46
|
|
|
""" |
|
47
|
|
|
|
|
48
|
|
|
PYPI_API_URL = 'https://pypi.python.org/pypi/Glances/json' |
|
49
|
|
|
max_refresh_date = timedelta(days=7) |
|
50
|
|
|
|
|
51
|
|
|
def __init__(self, args, config): |
|
52
|
|
|
"""Init the Outdated class""" |
|
53
|
|
|
self.args = args |
|
54
|
|
|
self.config = config |
|
55
|
|
|
self.cache_dir = user_cache_dir() |
|
56
|
|
|
self.cache_file = os.path.join(self.cache_dir, 'glances-version.db') |
|
57
|
|
|
|
|
58
|
|
|
# Set default value... |
|
59
|
|
|
self.data = { |
|
60
|
|
|
u'installed_version': __version__, |
|
61
|
|
|
u'latest_version': '0.0', |
|
62
|
|
|
u'refresh_date': datetime.now() |
|
63
|
|
|
} |
|
64
|
|
|
# Read the configuration file |
|
65
|
|
|
self.load_config(config) |
|
66
|
|
|
logger.debug("Check Glances version up-to-date: {}".format(not self.args.disable_check_update)) |
|
67
|
|
|
|
|
68
|
|
|
# And update ! |
|
69
|
|
|
self.get_pypi_version() |
|
70
|
|
|
|
|
71
|
|
|
def load_config(self, config): |
|
72
|
|
|
"""Load outdated parameter in the global section of the configuration file.""" |
|
73
|
|
|
|
|
74
|
|
|
global_section = 'global' |
|
75
|
|
|
if (hasattr(config, 'has_section') and |
|
76
|
|
|
config.has_section(global_section)): |
|
77
|
|
|
self.args.disable_check_update = config.get_value(global_section, 'check_update').lower() == 'false' |
|
78
|
|
|
else: |
|
79
|
|
|
logger.debug("Cannot find section {} in the configuration file".format(global_section)) |
|
80
|
|
|
return False |
|
81
|
|
|
|
|
82
|
|
|
return True |
|
83
|
|
|
|
|
84
|
|
|
def installed_version(self): |
|
85
|
|
|
return self.data['installed_version'] |
|
86
|
|
|
|
|
87
|
|
|
def latest_version(self): |
|
88
|
|
|
return self.data['latest_version'] |
|
89
|
|
|
|
|
90
|
|
|
def refresh_date(self): |
|
91
|
|
|
return self.data['refresh_date'] |
|
92
|
|
|
|
|
93
|
|
|
def get_pypi_version(self): |
|
94
|
|
|
"""Wrapper to get the latest Pypi version (async) |
|
95
|
|
|
The data are stored in a cached file |
|
96
|
|
|
Only update online once a week |
|
97
|
|
|
""" |
|
98
|
|
|
if not outdated_tag or self.args.disable_check_update: |
|
99
|
|
|
return |
|
100
|
|
|
|
|
101
|
|
|
# If the cached file exist, read-it |
|
102
|
|
|
cached_data = self._load_cache() |
|
103
|
|
|
|
|
104
|
|
|
if cached_data == {}: |
|
105
|
|
|
# Update needed |
|
106
|
|
|
# Update and save the cache |
|
107
|
|
|
thread = threading.Thread(target=self._update_pypi_version) |
|
108
|
|
|
thread.start() |
|
109
|
|
|
else: |
|
110
|
|
|
# Update not needed |
|
111
|
|
|
self.data['latest_version'] = cached_data['latest_version'] |
|
112
|
|
|
logger.debug("Get the Glances version from the cache file") |
|
113
|
|
|
|
|
114
|
|
|
def is_outdated(self): |
|
115
|
|
|
"""Return True if a new version is available""" |
|
116
|
|
|
if self.args.disable_check_update: |
|
117
|
|
|
# Check is disabled by configuration |
|
118
|
|
|
return False |
|
119
|
|
|
|
|
120
|
|
|
if not outdated_tag: |
|
121
|
|
|
logger.debug("Python Request lib is not installed. Can not get last Glances version on Pypi.") |
|
122
|
|
|
return False |
|
123
|
|
|
|
|
124
|
|
|
logger.debug("Check Glances version (installed: {} / latest: {})".format(self.installed_version(), self.latest_version())) |
|
125
|
|
|
return LooseVersion(self.latest_version()) > LooseVersion(self.installed_version()) |
|
126
|
|
|
|
|
127
|
|
|
def _load_cache(self): |
|
128
|
|
|
"""Load cache file and return cached data""" |
|
129
|
|
|
# If the cached file exist, read-it |
|
130
|
|
|
cached_data = {} |
|
131
|
|
|
try: |
|
132
|
|
|
with open(self.cache_file, 'rb') as f: |
|
133
|
|
|
cached_data = pickle.load(f) |
|
134
|
|
|
except Exception as e: |
|
135
|
|
|
logger.debug("Cannot read the version cache file: {}".format(e)) |
|
136
|
|
|
else: |
|
137
|
|
|
logger.debug("Read the version cache file") |
|
138
|
|
|
if cached_data['installed_version'] != self.installed_version() or \ |
|
139
|
|
|
datetime.now() - cached_data['refresh_date'] > self.max_refresh_date: |
|
140
|
|
|
# Reset the cache if: |
|
141
|
|
|
# - the installed version is different |
|
142
|
|
|
# - the refresh_date is > max_refresh_date |
|
143
|
|
|
cached_data = {} |
|
144
|
|
|
return cached_data |
|
145
|
|
|
|
|
146
|
|
|
def _save_cache(self): |
|
147
|
|
|
"""Save data to the cache file.""" |
|
148
|
|
|
# Create the cache directory |
|
149
|
|
|
safe_makedirs(self.cache_dir) |
|
150
|
|
|
|
|
151
|
|
|
# Create/overwrite the cache file |
|
152
|
|
|
with open(self.cache_file, 'wb') as f: |
|
153
|
|
|
pickle.dump(self.data, f) |
|
154
|
|
|
|
|
155
|
|
|
def _update_pypi_version(self): |
|
156
|
|
|
"""Get the latest Pypi version (as a string) via the Restful JSON API""" |
|
157
|
|
|
# Get the Nginx status |
|
158
|
|
|
logger.debug("Get latest Glances version from the PyPI RESTful API ({})".format(self.PYPI_API_URL)) |
|
159
|
|
|
|
|
160
|
|
|
# Update the current time |
|
161
|
|
|
self.data[u'refresh_date'] = datetime.now() |
|
162
|
|
|
|
|
163
|
|
|
try: |
|
164
|
|
|
res = requests.get(self.PYPI_API_URL) |
|
165
|
|
|
except Exception as e: |
|
166
|
|
|
logger.debug("Cannot get the Glances version from the PyPI RESTful API ({})".format(e)) |
|
167
|
|
|
else: |
|
168
|
|
|
if res.ok: |
|
169
|
|
|
# Update data |
|
170
|
|
|
self.data[u'latest_version'] = json.loads(res.text)['info']['version'] |
|
171
|
|
|
logger.debug("Save Glances version to the cache file") |
|
172
|
|
|
else: |
|
173
|
|
|
logger.debug("Cannot get the Glances version from the PyPI RESTful API ({})".format(res.reason)) |
|
174
|
|
|
|
|
175
|
|
|
# Save result to the cache file |
|
176
|
|
|
# Note: also saved if the Glances Pypi version can not be grabed |
|
177
|
|
|
self._save_cache() |
|
178
|
|
|
|
|
179
|
|
|
return self.data |
|
180
|
|
|
|