source: sasview/guiframe/version.py @ d1b43cd

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since d1b43cd was 52070a1, checked in by Mathieu Doucet <doucetm@…>, 15 years ago

gui_frame: updated the version checker to minimize visible effect on performance.

  • Property mode set to 100644
File size: 4.7 KB
Line 
1"""
2This software was developed by the University of Tennessee as part of the
3Distributed Data Analysis of Neutron Scattering Experiments (DANSE)
4project funded by the US National Science Foundation.
5
6See the license text in license.txt
7
8copyright 2010, University of Tennessee
9"""
10
11import sys, time
12import subprocess, os
13import getopt
14from threading import Thread
15
16## Maximum number of seconds to wait for an answer from the server
17MAX_WAIT_TIME = 20
18## Local storage file name
19VERSION_FILE  = '.current_version'
20
21class VersionChecker(object):
22    """
23        Class of objects used to obtain the current version of an application
24        from the deployment server.
25        A sub process is started to read the URL associated with the version number.
26        The version number is written on file locally before the reading process
27        ends, then read when the version number is requested.   
28       
29        The reading of the URL is put in a separate process so that it doesn't
30        affect the performance of the application and can be managed and stopped at any time.
31    """
32    ## Process used to obtain the current application version from the server
33    _process = None
34    ## Start time of the process of obtaining a version number
35    _t_0 = None
36   
37    def __init__(self, version_url):
38        """
39            Start the sub-process used to read the version URL
40        """
41        self._process = subprocess.Popen([sys.executable, __file__, '-g', '-u%s' % version_url])
42        self._t_0 = time.time()
43       
44    def is_complete(self):
45        """
46            Method used to poll the reading process. The process will be killed
47            if the wait time is longer than a predefined maximum.
48            This method should always be called before get_version() to ensure
49            accuracy of the version number that is returned.
50        """
51        if(time.time()-self._t_0<MAX_WAIT_TIME):
52            if self._process.poll() is not None:
53                return True
54            return False
55        else:
56            self._process.kill()
57        return True
58   
59    def get_version(self):
60        """
61            Returns the last version number that was read from the server.
62        """
63        try:
64            f = open(VERSION_FILE, 'r')
65            return f.read()
66        except:
67            return '0.0.0'
68
69class VersionThread(Thread):
70    """
71       Thread used to start the process of reading the current version of an
72       application from the deployment server.
73       
74       The VersionChecker is user in a Thread to allow the main application
75       to continue dealing with UI requests from the user. The main application
76       provides a call-back method for when the version number is obtained.
77    """
78    def __init__ (self, url, call_back=None, baggage=None):
79        Thread.__init__(self)
80        self._url = url
81        self._call_back = call_back
82        self._baggage = baggage
83     
84    def run(self):
85        """
86            Execute the process of reading the current application version number.
87        """
88        checker = VersionChecker(self._url)
89        while(not checker.is_complete()):
90            time.sleep(1)
91        self._call_back(checker.get_version(), self._baggage)
92           
93def write_version(version, filename=VERSION_FILE):
94    """
95        Store the version number
96        This could be put into a DB if the application has one.
97    """
98    f = open(filename, 'w')
99    f.write(version)
100    f.close()
101       
102def _get_version_from_server(url):
103    """
104        Method executed in the independent process used to read the
105        current version number from the server.
106       
107        @param url: URL to read the version number from
108    """
109    import urllib, re
110    try: 
111        h = urllib.urlopen(url)
112        for line in h.readlines():
113            version = line.strip()
114            if len(re.findall('\d+\.\d+\.\d+$', version))>0:
115                write_version(version)
116                return       
117        write_version('0.0.0')
118    except:
119        write_version('0.0.0')
120       
121if __name__ == "__main__": 
122    _get_version = False
123    _url = 'http://danse.chem.utk.edu/prview_version.php'
124   
125    opts, args = getopt.getopt(sys.argv[1:], "gu:", ["get", "url="])
126    for opt, arg in opts:
127        if opt in ("-u", "--url"):
128            _url = arg
129        elif opt in ("-g", "--get"):
130            _get_version = True
131           
132    if _get_version:
133        # Get the version number from the URL.
134        _get_version_from_server(_url)
135    else: 
136        # Test execution of the reading process
137        def _process(version, baggage=None):
138            print "Received:", version   
139        checker = VersionThread(_url, _process)
140        checker.start()
141       
Note: See TracBrowser for help on using the repository browser.