source: sasview/sansguiframe/src/sans/guiframe/version.py @ 8c347a6

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 8c347a6 was 8c347a6, checked in by Gervaise Alina <gervyh@…>, 13 years ago

moving guiframe under sansguiframe

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