home *** CD-ROM | disk | FTP | other *** search
/ Chip 2006 June / CHIP 2006-06.2.iso / program / freeware / Democracy-0.8.2.exe / xulrunner / python / autoupdate.py < prev    next >
Encoding:
Python Source  |  2006-04-10  |  1.8 KB  |  50 lines

  1. from downloader import grabURL
  2. from threading import Thread, Lock
  3. import config
  4. import xml.dom.minidom
  5.  
  6. _lock = Lock()
  7.  
  8. # Pass in a connection to the frontend
  9. def setDelegate(newDelegate):
  10.     global delegate
  11.     delegate = newDelegate
  12.  
  13. def checkForUpdates(notifyIfUpToDate=False):
  14.     if _lock.acquire(False):
  15.         thread = Thread(target=lambda: _checkForUpdates(notifyIfUpToDate),
  16.                         name="upgrade notification")
  17.         thread.setDaemon(False)
  18.         thread.start()
  19.     
  20.  
  21. def _checkForUpdates(notifyIfUpToDate):
  22.     platform = config.get(config.APP_PLATFORM)
  23.     serial = int(config.get(config.APP_SERIAL))
  24.     info = grabURL(config.get(config.AUTOUPDATE_URL))
  25.     updated = False
  26.     if info is not None:
  27.         data = info['file-handle'].read()
  28.         info['file-handle'].close()
  29.         domObj = xml.dom.minidom.parseString(data)
  30.         versions = domObj.getElementsByTagNameNS("http://www.getdemocracy.com/versionfile/1.0","version")
  31.         for version in versions:
  32.             attributes = version.attributes
  33.             if ((attributes['platform'].value == platform) and
  34.                 (int(attributes['serial'].value)>serial)):
  35.                 ver = attributes['version'].value
  36.                 url = attributes['updateurl'].value
  37.                 text = ""
  38.                 for node in version.childNodes:
  39.                     if node.nodeType == node.TEXT_NODE:
  40.                         text = text + node.data
  41.                 print "DTV: new update '%s' available (have '%s')" % \
  42.                    (ver, config.get(config.APP_VERSION))
  43.                 delegate.updateAvailable(url)
  44.                 updated = True
  45.                 break
  46.         domObj.unlink()
  47.         if notifyIfUpToDate and not updated:
  48.             delegate.dtvIsUpToDate()
  49.     _lock.release()
  50.