Package qubx :: Module vcheck_client
[hide private]
[frames] | no frames]

Source Code for Module qubx.vcheck_client

  1  """ 
  2  Check over network for updates, invoked by qubx.cube.Cube (main window aka QubX) shortly after startup, and daily thereafter. 
  3   
  4  Copyright 2012-2013 Research Foundation State University of New York  
  5  This file is part of QUB Express.                                           
  6   
  7  QUB Express is free software; you can redistribute it and/or modify           
  8  it under the terms of the GNU General Public License as published by  
  9  the Free Software Foundation, either version 3 of the License, or     
 10  (at your option) any later version.                                   
 11   
 12  QUB Express is distributed in the hope that it will be useful,                
 13  but WITHOUT ANY WARRANTY; without even the implied warranty of        
 14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         
 15  GNU General Public License for more details.                          
 16   
 17  You should have received a copy of the GNU General Public License,    
 18  named LICENSE.txt, in the QUB Express program directory.  If not, see         
 19  <http://www.gnu.org/licenses/>.                                       
 20   
 21  """ 
 22   
 23  import cStringIO 
 24  import datetime 
 25  import errno 
 26  import gobject 
 27  import gtk 
 28  import os 
 29  import platform 
 30  import re 
 31  import shutil 
 32  import sys 
 33  import traceback 
 34  import uuid 
 35  import webbrowser 
 36   
 37  import qubx.data_types 
 38  import qubx.global_namespace 
 39  import qubx.GTK 
 40  import qubx.pyenv 
 41  import qubx.settings 
 42  import qubx.task 
 43  import qubx.util_types 
 44   
 45  from qubx.GTK import pack_item, pack_label, pack_button, pack_scrolled 
 46  from qubx.http_post import * 
 47   
48 -def get_changes(key='qubx', guid='', m=''):
49 """Returns (changelog, version_str, download_url) of updates newer than QUBX_VERSION, 50 as requested from http://qub.mandelics.com/vcheck; returns (None, None, None) on failure.""" 51 os = platform.system().lower() 52 k = key 53 if 'linux' in os: 54 os = 'lin' 55 elif 'darwin' in os: 56 os = 'mac' 57 else: 58 os = 'win' 59 settings = qubx.settings.SettingsMgr['vcheck_client'].active 60 if not str(settings['u'].data): 61 settings['u'].data = 'qub.mandelics.com' 62 settings['p'].data = '/online/vcheck' 63 status, reason, data = post_multipart(str(settings['u'].data), str(settings['p'].data), 64 [('os', os), ('key', k), ('m', m), ('guid', guid), 65 ('version', qubx.global_namespace.QUBX_VERSION)], 66 []) 67 changes = get_tag(data, 'changes') 68 version = get_tag(data, 'version') 69 url = get_tag(data, 'url') 70 return (changes or None, version or None, url or None)
71
72 -def get_tag(xml, tagname):
73 """Returns the substring between desired start and end tags in a really simple xml document.""" 74 i0 = xml.find('<%s>' % tagname) 75 if i0 != -1: 76 i0 += len(tagname) + 2 77 i1 = xml.find('</%s>' % tagname) 78 if i1 != -1: 79 return xml[i0:i1].strip()
80 85 86 WHEL = 'Invalid registration. Please subscribe on our web site.' 87
88 -class vcheck_client_task(qubx.task.Task):
89 """Checks for updates in a background thread, prompts the user."""
90 - def __init__(self, key='qubx', m=None, onreply=lambda changes, version, url: None):
91 """@param key: matches FileRec.key in qub download page, a.k.a qubx.global_namespace.QUBX_CODENAME""" 92 qubx.task.Task.__init__(self, 'Checking for updates') 93 self.OnException += print_task_exc 94 self.key = key 95 self.m = m or '' 96 self.onreply = onreply 97 settings = qubx.settings.SettingsMgr['vcheck_client'].active 98 self.last_check_version = str(settings['last_version'].data) 99 self.guid = str(settings['guid'].data) 100 if not self.guid: 101 settings['guid'].data = self.guid = str(uuid.uuid4())
102 - def run(self):
103 qubx.task.Tasks.add_task(self) 104 try: 105 changes, version, url = get_changes(key=self.key, guid=self.guid, m=self.m) 106 if changes is None: 107 return 108 if version == self.last_check_version: 109 return 110 if self.onreply: 111 gobject.idle_add(self.onreply, changes, version, url) 112 else: 113 gobject.idle_add(show_changes, changes, version, url) 114 except: 115 pass 116 qubx.task.Tasks.remove_task(self)
117
118 -def whois(m, elsie):
119 match = re.search(r"#(\d+)", m) 120 match or elsie(WHEL) 121 try: 122 g = int(match.group(1)) 123 v = ((g<=0) or ((g+9)%13)) and (g/(g>>32)) 124 except: 125 elsie(WHEL) 126 def onreply(c,v,u): 127 match = re.search(r"https://([^/]+)(.*)", u or '') 128 if match: 129 settings = qubx.settings.SettingsMgr['vcheck_client'].active 130 settings['u'].data = match.group(1) 131 settings['p'].data = match.group(2) 132 c and elsie(c)
133 vcheck_client_task(m=match.group(1), onreply=onreply).start() 134
135 -def show_changes(changes, version, url):
136 """Displays the list of updates returned by get_changes in a new window; offers a button to download the latest version.""" 137 qubx.settings.SettingsMgr['vcheck_client'].active['last_version'].data = version 138 QubX = qubx.global_namespace.QubX 139 w = gtk.Window(gtk.WINDOW_TOPLEVEL) 140 w.set_size_request(640, 480) 141 w.set_position(gtk.WIN_POS_CENTER) 142 v = gtk.VBox() 143 v.show() 144 w.add(v) 145 line = pack_item(gtk.HBox(), v) 146 pack_label('A new version of %s (%s) is available for download. Recent changes:' % (QubX.appname, version), line) 147 txt = gtk.TextView() 148 pack_scrolled(txt, v, expand=True) 149 txt.get_buffer().set_text(changes) 150 line = pack_item(gtk.HBox(), v) 151 pack_button('Download...', line, at_end=True, on_click=lambda btn: webbrowser.open(url)) 152 pack_button('Close', line, at_end=True, on_click=lambda btn: w.destroy()) 153 w.show()
154