1
2
3 """
4 Copyright 2009 Glencoe Software, Inc. All rights reserved.
5 Use is subject to license terms supplied in LICENSE.txt
6 """
7
8 from omero_version import omero_version
9
10 import platform
11 import logging
12 import urllib2
13 import urllib
14 import socket
15
17 """
18 Port of Java UpgradeCheck:
19 http://trac.openmicroscopy.org.uk/ome/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
47 DEFAULT_TIMEOUT = 3.0
48
49 - def __init__(self, agent, url = "http://upgrade.openmicroscopy.org.uk/", version = omero_version, timeout = DEFAULT_TIMEOUT):
50 """
51 ::
52 agent := Name of the agent which is accessing the registry. This will
53 be appended to "OMERO." in order to adhere to the registry
54 API.
55 url := Connection information for the upgrade check.
56 None or empty string disables check. Defaults to upgrade.openmicroscopy.org.uk
57 version := Version to check against the returned value.
58 Defaults to current version as specified in omero_version.py.
59 timeout := How long to wait for the HTTP GET in seconds (float).
60 The default timeout is 3 seconds.
61 """
62
63 self.log = logging.getLogger("omero.util.UpgradeCheck")
64
65 self.url = str(url)
66 self.version = str(version)
67 self.timeout = float(timeout)
68 self.agent = "OMERO." + str(agent)
69
70 self.upgradeUrl = None
71 self.exc = None
72
74 return self.upgradeUrl != None
75
77 return self.upgradeUrl
78
80 return self.exc != None
81
82
85
86 - def _set(self, results, e):
87 self.upgradeUrl = results
88 self.exc = e
89
91 """
92 If the {@link #url} has been set to null or the empty string, then no
93 upgrade check will be performed (silently). If however the string is an
94 invalid URL, a warning will be printed.
95
96 This method should <em>never</em> throw an exception.
97 """
98
99
100 if self.url == None or len(self.url) == 0:
101 return;
102
103 try:
104 params = {}
105 params["version"] = self.version
106 params["os.name"] = platform.system()
107 params["os.arch"] = platform.machine()
108 params["os.version"] = platform.version()
109 params["python.version"] = platform.python_version()
110 params["python.compiler"] = platform.python_compiler()
111 params["python.build"] = platform.python_build()
112 params = urllib.urlencode(params)
113
114 old_timeout = socket.getdefaulttimeout()
115 try:
116 socket.setdefaulttimeout(self.timeout)
117 full_url = "%s?%s" % (self.url, params)
118 request = urllib2.Request(full_url)
119 request.add_header('User-Agent', self.agent)
120 self.log.debug("Attempting to connect to %s" % full_url)
121 response = urllib2.urlopen(request)
122 result = response.read()
123 finally:
124 socket.setdefaulttimeout(old_timeout)
125
126 except Exception, e:
127 self.log.error(str(e), exc_info = 0)
128 self._set(None, e)
129 return
130
131 if len(result) == 0:
132 self.log.info("no update needed")
133 self._set(None, None)
134 else:
135 self.log.warn("UPGRADE AVAILABLE:" + result)
136 self._set(result, None)
137
138 if __name__ == "__main__":
139 logging.basicConfig(level=logging.INFO)
140 import doctest
141 doctest.testmod()
142