1
2 """
3 Copyright 2009 Glencoe Software, Inc. All rights reserved.
4 Use is subject to license terms supplied in LICENSE.txt
5 """
6
7 from omero_version import omero_version
8
9 import exceptions
10 import platform
11 import logging
12 import urllib2
13 import urllib
14 import socket
15
17 """
18 Port of Java UpgradeCheck:
19 https://trac.openmicroscopy.org.uk/omero/browser/trunk/components/common/src/ome/system/UpgradeCheck.java
20
21 >>> from omero.util.upgrade_check import UpgradeCheck
22 >>> uc = UpgradeCheck("doctest")
23 >>> uc.run()
24 >>> uc.isUpgradeNeeded()
25 False
26 >>> uc.isExceptionThrown()
27 False
28 >>> uc = UpgradeCheck("doctest", version = "0.0.0")
29 >>> uc.run()
30 >>> uc.isUpgradeNeeded()
31 True
32 >>> uc.isExceptionThrown()
33 False
34 >>>
35 >>> uc = UpgradeCheck("doctest", url = "http://some-completely-unknown-host.abcd/")
36 >>> uc.run()
37 >>> uc.isUpgradeNeeded()
38 False
39 >>> uc.isExceptionThrown()
40 True
41 """
42
43
44
45
46 DEFAULT_TIMEOUT = 10 * 1000
47
49 """
50 ::
51 agent := Name of the agent which is accessing the registry. This will
52 be appended to "OMERO." in order to adhere to the registry
53 API.
54 url := Connection information for the upgrade check.
55 None or empty string disables check. Defaults to upgrade.openmicroscopy.org.uk
56 version := Version to check against the returned value.
57 Defaults to current version as specified in omero_version.py.
58 timeout := How long to wait for the HTTP GET
59 """
60
61 self.log = logging.getLogger("omero.util.UpgradeCheck")
62
63 self.url = str(url)
64 self.version = str(version)
65 self.timeout = int(timeout)
66 self.agent = "OMERO." + str(agent)
67
68 self.upgradeUrl = None
69 self.exc = None
70
72 return self.upgradeUrl != None
73
75 return self.upgradeUrl
76
78 return self.exc != None
79
80
83
84 - def _set(self, results, e):
85 self.upgradeUrl = results
86 self.exc = e
87
89 """
90 If the {@link #url} has been set to null or the empty string, then no
91 upgrade check will be performed (silently). If however the string is an
92 invalid URL, a warning will be printed.
93
94 This method should <em>never</em> throw an exception.
95 """
96
97
98 if self.url == None or len(self.url) == 0:
99 return;
100
101 try:
102 params = {}
103 params["version"] = self.version
104 params["os.name"] = platform.system()
105 params["os.arch"] = platform.machine()
106 params["os.version"] = platform.version()
107 params["python.version"] = platform.python_version()
108 params["python.compiler"] = platform.python_compiler()
109 params["python.build"] = platform.python_build()
110 params = urllib.urlencode(params)
111
112 old_timeout = socket.getdefaulttimeout()
113 try:
114 socket.setdefaulttimeout(self.timeout)
115 full_url = "%s?%s" % (self.url, params)
116 request = urllib2.Request(full_url)
117 request.add_header('User-Agent', self.agent)
118 self.log.debug("Attempting to connect to %s" % full_url)
119 response = urllib2.urlopen(request)
120 result = response.read()
121 finally:
122 socket.setdefaulttimeout(old_timeout)
123
124 except exceptions.Exception, e:
125 self.log.error(str(e), exc_info = 0)
126 self._set(None, e)
127 return
128
129 if len(result) == 0:
130 self.log.info("no update needed")
131 self._set(None, None)
132 else:
133 self.log.warn("UPGRADE AVAILABLE:" + result)
134 self._set(result, None)
135
136 if __name__ == "__main__":
137 logging.basicConfig(level=logging.INFO)
138 import doctest
139 doctest.testmod()
140