source: sasview/guiframe/version.py @ 20f00bed

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 20f00bed was d955bf19, checked in by Gervaise Alina <gervyh@…>, 14 years ago

working on documentation

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