1997-12-23 18:43:55 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								"""pyversioncheck - Module to help with checking versions"""
							 | 
						
					
						
							
								
									
										
										
										
											2008-06-23 11:45:20 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import urllib.request
							 | 
						
					
						
							
								
									
										
										
										
											2008-06-12 22:23:59 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								import email
							 | 
						
					
						
							
								
									
										
										
										
											1997-12-23 18:43:55 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								import sys
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								# Verbose options
							 | 
						
					
						
							
								
									
										
										
										
											2003-10-24 20:09:23 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								VERBOSE_SILENT=0        # Single-line reports per package
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								VERBOSE_NORMAL=1        # Single-line reports per package, more info if outdated
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								VERBOSE_EACHFILE=2      # Report on each URL checked
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								VERBOSE_CHECKALL=3      # Check each URL for each package
							 | 
						
					
						
							
								
									
										
										
										
											1997-12-23 18:43:55 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								# Test directory
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								## urllib bug: _TESTDIR="ftp://ftp.cwi.nl/pub/jack/python/versiontestdir/"
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								_TESTDIR="http://www.cwi.nl/~jack/versiontestdir/"
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								def versioncheck(package, url, version, verbose=0):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    ok, newversion, fp = checkonly(package, url, version, verbose)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    if verbose > VERBOSE_NORMAL:
							 | 
						
					
						
							
								
									
										
										
										
											1998-04-06 14:20:27 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        return ok
							 | 
						
					
						
							
								
									
										
										
										
											1997-12-23 18:43:55 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    if ok < 0:
							 | 
						
					
						
							
								
									
										
										
										
											2007-08-03 17:06:41 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        print('%s: No correctly formatted current version file found'%(package))
							 | 
						
					
						
							
								
									
										
										
										
											1997-12-23 18:43:55 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    elif ok == 1:
							 | 
						
					
						
							
								
									
										
										
										
											2007-08-03 17:06:41 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        print('%s: up-to-date (version %s)'%(package, version))
							 | 
						
					
						
							
								
									
										
										
										
											1997-12-23 18:43:55 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    else:
							 | 
						
					
						
							
								
									
										
										
										
											2007-08-03 17:06:41 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        print('%s: version %s installed, version %s found:' % \
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                        (package, version, newversion))
							 | 
						
					
						
							
								
									
										
										
										
											1998-04-06 14:20:27 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if verbose > VERBOSE_SILENT:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            while 1:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                line = fp.readline()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                if not line: break
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                sys.stdout.write('\t'+line)
							 | 
						
					
						
							
								
									
										
										
										
											1997-12-23 18:43:55 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    return ok
							 | 
						
					
						
							
								
									
										
										
										
											1998-04-06 14:20:27 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											1997-12-23 18:43:55 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								def checkonly(package, url, version, verbose=0):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    if verbose >= VERBOSE_EACHFILE:
							 | 
						
					
						
							
								
									
										
										
										
											2007-08-03 17:06:41 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        print('%s:'%package)
							 | 
						
					
						
							
								
									
										
										
										
											2007-06-07 23:15:56 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    if isinstance(url, str):
							 | 
						
					
						
							
								
									
										
										
										
											1998-04-06 14:20:27 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        ok, newversion, fp = _check1version(package, url, version, verbose)
							 | 
						
					
						
							
								
									
										
										
										
											1997-12-23 18:43:55 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    else:
							 | 
						
					
						
							
								
									
										
										
										
											1998-04-06 14:20:27 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        for u in url:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            ok, newversion, fp = _check1version(package, u, version, verbose)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								            if ok >= 0 and verbose < VERBOSE_CHECKALL:
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                break
							 | 
						
					
						
							
								
									
										
										
										
											1997-12-23 18:43:55 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    return ok, newversion, fp
							 | 
						
					
						
							
								
									
										
										
										
											1998-04-06 14:20:27 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											1997-12-23 18:43:55 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								def _check1version(package, url, version, verbose=0):
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    if verbose >= VERBOSE_EACHFILE:
							 | 
						
					
						
							
								
									
										
										
										
											2007-08-03 17:06:41 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        print('  Checking %s'%url)
							 | 
						
					
						
							
								
									
										
										
										
											1997-12-23 18:43:55 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    try:
							 | 
						
					
						
							
								
									
										
										
										
											2008-06-23 11:45:20 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        fp = urllib.request.urlopen(url)
							 | 
						
					
						
							
								
									
										
										
										
											2007-01-10 16:19:56 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    except IOError as arg:
							 | 
						
					
						
							
								
									
										
										
										
											1998-04-06 14:20:27 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if verbose >= VERBOSE_EACHFILE:
							 | 
						
					
						
							
								
									
										
										
										
											2007-08-03 17:06:41 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            print('    Cannot open:', arg)
							 | 
						
					
						
							
								
									
										
										
										
											1998-04-06 14:20:27 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        return -1, None, None
							 | 
						
					
						
							
								
									
										
										
										
											2008-06-12 22:23:59 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    msg = email.message_from_file(fp)
							 | 
						
					
						
							
								
									
										
										
										
											2008-06-12 04:06:45 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    newversion = msg.get('current-version')
							 | 
						
					
						
							
								
									
										
										
										
											1997-12-23 18:43:55 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    if not newversion:
							 | 
						
					
						
							
								
									
										
										
										
											1998-04-06 14:20:27 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if verbose >= VERBOSE_EACHFILE:
							 | 
						
					
						
							
								
									
										
										
										
											2007-08-03 17:06:41 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            print('    No "Current-Version:" header in URL or URL not found')
							 | 
						
					
						
							
								
									
										
										
										
											1998-04-06 14:20:27 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        return -1, None, None
							 | 
						
					
						
							
								
									
										
										
										
											2002-09-11 20:36:02 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    version = version.lower().strip()
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    newversion = newversion.lower().strip()
							 | 
						
					
						
							
								
									
										
										
										
											1997-12-23 18:43:55 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    if version == newversion:
							 | 
						
					
						
							
								
									
										
										
										
											1998-04-06 14:20:27 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if verbose >= VERBOSE_EACHFILE:
							 | 
						
					
						
							
								
									
										
										
										
											2007-08-03 17:06:41 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            print('    Version identical (%s)'%newversion)
							 | 
						
					
						
							
								
									
										
										
										
											1998-04-06 14:20:27 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        return 1, version, fp
							 | 
						
					
						
							
								
									
										
										
										
											1997-12-23 18:43:55 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    else:
							 | 
						
					
						
							
								
									
										
										
										
											1998-04-06 14:20:27 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        if verbose >= VERBOSE_EACHFILE:
							 | 
						
					
						
							
								
									
										
										
										
											2007-08-03 17:06:41 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								            print('    Versions different (installed: %s, new: %s)'% \
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								                        (version, newversion))
							 | 
						
					
						
							
								
									
										
										
										
											1998-04-06 14:20:27 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								        return 0, newversion, fp
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											1997-12-23 18:43:55 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								def _test():
							 | 
						
					
						
							
								
									
										
										
										
											2007-08-03 17:06:41 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    print('--- TEST VERBOSE=1')
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    print('--- Testing existing and identical version file')
							 | 
						
					
						
							
								
									
										
										
										
											1997-12-23 18:43:55 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    versioncheck('VersionTestPackage', _TESTDIR+'Version10.txt', '1.0', verbose=1)
							 | 
						
					
						
							
								
									
										
										
										
											2007-08-03 17:06:41 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    print('--- Testing existing package with new version')
							 | 
						
					
						
							
								
									
										
										
										
											1997-12-23 18:43:55 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    versioncheck('VersionTestPackage', _TESTDIR+'Version11.txt', '1.0', verbose=1)
							 | 
						
					
						
							
								
									
										
										
										
											2007-08-03 17:06:41 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    print('--- Testing package with non-existing version file')
							 | 
						
					
						
							
								
									
										
										
										
											1997-12-23 18:43:55 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    versioncheck('VersionTestPackage', _TESTDIR+'nonexistent.txt', '1.0', verbose=1)
							 | 
						
					
						
							
								
									
										
										
										
											2007-08-03 17:06:41 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    print('--- Test package with 2 locations, first non-existing second ok')
							 | 
						
					
						
							
								
									
										
										
										
											1997-12-23 18:43:55 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    versfiles = [_TESTDIR+'nonexistent.txt', _TESTDIR+'Version10.txt']
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    versioncheck('VersionTestPackage', versfiles, '1.0', verbose=1)
							 | 
						
					
						
							
								
									
										
										
										
											2007-08-03 17:06:41 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    print('--- TEST VERBOSE=2')
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    print('--- Testing existing and identical version file')
							 | 
						
					
						
							
								
									
										
										
										
											1997-12-23 18:43:55 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    versioncheck('VersionTestPackage', _TESTDIR+'Version10.txt', '1.0', verbose=2)
							 | 
						
					
						
							
								
									
										
										
										
											2007-08-03 17:06:41 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    print('--- Testing existing package with new version')
							 | 
						
					
						
							
								
									
										
										
										
											1997-12-23 18:43:55 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    versioncheck('VersionTestPackage', _TESTDIR+'Version11.txt', '1.0', verbose=2)
							 | 
						
					
						
							
								
									
										
										
										
											2007-08-03 17:06:41 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    print('--- Testing package with non-existing version file')
							 | 
						
					
						
							
								
									
										
										
										
											1997-12-23 18:43:55 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    versioncheck('VersionTestPackage', _TESTDIR+'nonexistent.txt', '1.0', verbose=2)
							 | 
						
					
						
							
								
									
										
										
										
											2007-08-03 17:06:41 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    print('--- Test package with 2 locations, first non-existing second ok')
							 | 
						
					
						
							
								
									
										
										
										
											1997-12-23 18:43:55 +00:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    versfiles = [_TESTDIR+'nonexistent.txt', _TESTDIR+'Version10.txt']
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    versioncheck('VersionTestPackage', versfiles, '1.0', verbose=2)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								if __name__ == '__main__':
							 | 
						
					
						
							
								
									
										
										
										
											1998-04-06 14:20:27 +00:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								    _test()
							 |