##// END OF EJS Templates
update: sanitize version, and check for version parsing exc
marcink -
r2434:ee3535c6 default
parent child Browse files
Show More
@@ -1,71 +1,83 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2013-2017 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 21 import logging
22 22 import urllib2
23 from packaging import version
23 from packaging.version import Version
24 24
25 25 import rhodecode
26 26 from rhodecode.lib.ext_json import json
27 27 from rhodecode.model import BaseModel
28 28 from rhodecode.model.meta import Session
29 29 from rhodecode.model.settings import SettingsModel
30 30
31 31
32 32 log = logging.getLogger(__name__)
33 33
34 34
35 35 class UpdateModel(BaseModel):
36 36 UPDATE_SETTINGS_KEY = 'update_version'
37 37 UPDATE_URL_SETTINGS_KEY = 'rhodecode_update_url'
38 38
39 39 @staticmethod
40 40 def get_update_data(update_url):
41 41 """Return the JSON update data."""
42 42 ver = rhodecode.__version__
43 43 log.debug('Checking for upgrade on `%s` server', update_url)
44 44 opener = urllib2.build_opener()
45 45 opener.addheaders = [('User-agent', 'RhodeCode-SCM/%s' % ver)]
46 46 response = opener.open(update_url)
47 47 response_data = response.read()
48 48 data = json.loads(response_data)
49 49 log.debug('update server returned data')
50 50 return data
51 51
52 52 def get_update_url(self):
53 53 settings = SettingsModel().get_all_settings()
54 54 return settings.get(self.UPDATE_URL_SETTINGS_KEY)
55 55
56 56 def store_version(self, version):
57 57 log.debug('Storing version %s into settings', version)
58 58 setting = SettingsModel().create_or_update_setting(
59 59 self.UPDATE_SETTINGS_KEY, version)
60 60 Session().add(setting)
61 61 Session().commit()
62 62
63 63 def get_stored_version(self):
64 64 obj = SettingsModel().get_setting_by_name(self.UPDATE_SETTINGS_KEY)
65 65 if obj:
66 66 return obj.app_settings_value
67 67 return '0.0.0'
68 68
69 def _sanitize_version(self, version):
70 """
71 Cleanup our custom ver.
72 e.g 4.11.0_20171204_204825_CE_default_EE_default to 4.11.0
73 """
74 return version.split('_')[0]
75
69 76 def is_outdated(self, cur_version, latest_version=None):
70 77 latest_version = latest_version or self.get_stored_version()
71 return version.Version(latest_version) > version.Version(cur_version)
78 try:
79 cur_version = self._sanitize_version(cur_version)
80 return Version(latest_version) > Version(cur_version)
81 except Exception:
82 # could be invalid version, etc
83 return False
General Comments 0
You need to be logged in to leave comments. Login now