home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 April / PCWorld_2001-04_cd.bin / Software / TemaCD / webclean / !!!python!!! / BeOpen-Python-2.0.exe / PYVERSIONCHECK.PY < prev    next >
Encoding:
Python Source  |  2000-09-28  |  4.1 KB  |  102 lines

  1. """pyversioncheck - Module to help with checking versions"""
  2. import types
  3. import rfc822
  4. import urllib
  5. import sys
  6. import string
  7.  
  8. # Verbose options
  9. VERBOSE_SILENT=0    # Single-line reports per package
  10. VERBOSE_NORMAL=1    # Single-line reports per package, more info if outdated
  11. VERBOSE_EACHFILE=2    # Report on each URL checked
  12. VERBOSE_CHECKALL=3    # Check each URL for each package
  13.  
  14. # Test directory
  15. ## urllib bug: _TESTDIR="ftp://ftp.cwi.nl/pub/jack/python/versiontestdir/"
  16. _TESTDIR="http://www.cwi.nl/~jack/versiontestdir/"
  17.  
  18. def versioncheck(package, url, version, verbose=0):
  19.     ok, newversion, fp = checkonly(package, url, version, verbose)
  20.     if verbose > VERBOSE_NORMAL:
  21.         return ok
  22.     if ok < 0:
  23.         print '%s: No correctly formatted current version file found'%(package)
  24.     elif ok == 1:
  25.         print '%s: up-to-date (version %s)'%(package, version)
  26.     else:
  27.         print '%s: version %s installed, version %s found:' % \
  28.                         (package, version, newversion)
  29.         if verbose > VERBOSE_SILENT:
  30.             while 1:
  31.                 line = fp.readline()
  32.                 if not line: break
  33.                 sys.stdout.write('\t'+line)
  34.     return ok
  35.  
  36. def checkonly(package, url, version, verbose=0):
  37.     if verbose >= VERBOSE_EACHFILE:
  38.         print '%s:'%package
  39.     if type(url) == types.StringType:
  40.         ok, newversion, fp = _check1version(package, url, version, verbose)
  41.     else:
  42.         for u in url:
  43.             ok, newversion, fp = _check1version(package, u, version, verbose)
  44.             if ok >= 0 and verbose < VERBOSE_CHECKALL:
  45.                 break
  46.     return ok, newversion, fp
  47.  
  48. def _check1version(package, url, version, verbose=0):
  49.     if verbose >= VERBOSE_EACHFILE:
  50.         print '  Checking %s'%url
  51.     try:
  52.         fp = urllib.urlopen(url)
  53.     except IOError, arg:
  54.         if verbose >= VERBOSE_EACHFILE:
  55.             print '    Cannot open:', arg
  56.         return -1, None, None
  57.     msg = rfc822.Message(fp, seekable=0)
  58.     newversion = msg.getheader('current-version')
  59.     if not newversion:
  60.         if verbose >= VERBOSE_EACHFILE:
  61.             print '    No "Current-Version:" header in URL or URL not found'
  62.         return -1, None, None
  63.     version = string.strip(string.lower(version))
  64.     newversion = string.strip(string.lower(newversion))
  65.     if version == newversion:
  66.         if verbose >= VERBOSE_EACHFILE:
  67.             print '    Version identical (%s)'%newversion
  68.         return 1, version, fp
  69.     else:
  70.         if verbose >= VERBOSE_EACHFILE:
  71.             print '    Versions different (installed: %s, new: %s)'% \
  72.                         (version, newversion)
  73.         return 0, newversion, fp
  74.  
  75.  
  76. def _test():
  77.     print '--- TEST VERBOSE=1'
  78.     print '--- Testing existing and identical version file'
  79.     versioncheck('VersionTestPackage', _TESTDIR+'Version10.txt', '1.0', verbose=1)
  80.     print '--- Testing existing package with new version'
  81.     versioncheck('VersionTestPackage', _TESTDIR+'Version11.txt', '1.0', verbose=1)
  82.     print '--- Testing package with non-existing version file'
  83.     versioncheck('VersionTestPackage', _TESTDIR+'nonexistent.txt', '1.0', verbose=1)
  84.     print '--- Test package with 2 locations, first non-existing second ok'
  85.     versfiles = [_TESTDIR+'nonexistent.txt', _TESTDIR+'Version10.txt']
  86.     versioncheck('VersionTestPackage', versfiles, '1.0', verbose=1)
  87.     print '--- TEST VERBOSE=2'
  88.     print '--- Testing existing and identical version file'
  89.     versioncheck('VersionTestPackage', _TESTDIR+'Version10.txt', '1.0', verbose=2)
  90.     print '--- Testing existing package with new version'
  91.     versioncheck('VersionTestPackage', _TESTDIR+'Version11.txt', '1.0', verbose=2)
  92.     print '--- Testing package with non-existing version file'
  93.     versioncheck('VersionTestPackage', _TESTDIR+'nonexistent.txt', '1.0', verbose=2)
  94.     print '--- Test package with 2 locations, first non-existing second ok'
  95.     versfiles = [_TESTDIR+'nonexistent.txt', _TESTDIR+'Version10.txt']
  96.     versioncheck('VersionTestPackage', versfiles, '1.0', verbose=2)
  97.  
  98. if __name__ == '__main__':
  99.     _test()
  100.     
  101.     
  102.