source: sasview/guiframe/version.py @ a3149c5

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 a3149c5 was 32c0841, checked in by Gervaise Alina <gervyh@…>, 14 years ago

working on pylint

  • Property mode set to 100644
File size: 4.8 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
[32c0841]13import sys
14import time
15import subprocess
16import os
[52070a1]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    """
[d955bf19]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.
[52070a1]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        """
[d955bf19]43        Start the sub-process used to read the version URL
[52070a1]44        """
[32c0841]45        self._process = subprocess.Popen([sys.executable, __file__,
46                                          '-g', '-u%s' % version_url])
[52070a1]47        self._t_0 = time.time()
48       
49    def is_complete(self):
50        """
[d955bf19]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.
[52070a1]55        """
[32c0841]56        if(time.time() - self._t_0 < MAX_WAIT_TIME):
[52070a1]57            if self._process.poll() is not None:
58                return True
59            return False
60        else:
61            self._process.kill()
62        return True
63   
64    def get_version(self):
65        """
[d955bf19]66        Returns the last version number that was read from the server.
[52070a1]67        """
68        try:
69            f = open(VERSION_FILE, 'r')
70            return f.read()
71        except:
72            return '0.0.0'
73
74class VersionThread(Thread):
75    """
[d955bf19]76    Thread used to start the process of reading the current version of an
77    application from the deployment server.
78   
79    The VersionChecker is user in a Thread to allow the main application
80    to continue dealing with UI requests from the user. The main application
81    provides a call-back method for when the version number is obtained.
[52070a1]82    """
83    def __init__ (self, url, call_back=None, baggage=None):
84        Thread.__init__(self)
85        self._url = url
86        self._call_back = call_back
87        self._baggage = baggage
88     
89    def run(self):
90        """
[d955bf19]91        Execute the process of reading the current application version number.
[52070a1]92        """
93        checker = VersionChecker(self._url)
94        while(not checker.is_complete()):
95            time.sleep(1)
96        self._call_back(checker.get_version(), self._baggage)
97           
98def write_version(version, filename=VERSION_FILE):
99    """
[d955bf19]100    Store the version number
101    This could be put into a DB if the application has one.
[52070a1]102    """
103    f = open(filename, 'w')
104    f.write(version)
105    f.close()
106       
107def _get_version_from_server(url):
108    """
[d955bf19]109    Method executed in the independent process used to read the
110    current version number from the server.
111   
112    :param url: URL to read the version number from
113   
[52070a1]114    """
[32c0841]115    import urllib
116    import re
[52070a1]117    try: 
118        h = urllib.urlopen(url)
119        for line in h.readlines():
120            version = line.strip()
[32c0841]121            if len(re.findall('\d+\.\d+\.\d+$', version)) > 0:
[52070a1]122                write_version(version)
123                return       
124        write_version('0.0.0')
125    except:
126        write_version('0.0.0')
127       
128if __name__ == "__main__": 
129    _get_version = False
130    _url = 'http://danse.chem.utk.edu/prview_version.php'
131   
132    opts, args = getopt.getopt(sys.argv[1:], "gu:", ["get", "url="])
133    for opt, arg in opts:
134        if opt in ("-u", "--url"):
135            _url = arg
136        elif opt in ("-g", "--get"):
137            _get_version = True
138           
139    if _get_version:
140        # Get the version number from the URL.
141        _get_version_from_server(_url)
142    else: 
143        # Test execution of the reading process
144        def _process(version, baggage=None):
145            print "Received:", version   
146        checker = VersionThread(_url, _process)
147        checker.start()
148       
Note: See TracBrowser for help on using the repository browser.