blob: 8826555bddf3f39686f3712483ca405686bc05bd [file] [log] [blame]
Guido van Rossum52910371997-12-23 18:43:55 +00001"""pyversioncheck - Module to help with checking versions"""
Guido van Rossum52910371997-12-23 18:43:55 +00002import rfc822
3import urllib
4import sys
Guido van Rossum52910371997-12-23 18:43:55 +00005
6# Verbose options
Martin v. Löwis23b44a32003-10-24 20:09:23 +00007VERBOSE_SILENT=0 # Single-line reports per package
8VERBOSE_NORMAL=1 # Single-line reports per package, more info if outdated
9VERBOSE_EACHFILE=2 # Report on each URL checked
10VERBOSE_CHECKALL=3 # Check each URL for each package
Guido van Rossum52910371997-12-23 18:43:55 +000011
12# Test directory
13## urllib bug: _TESTDIR="ftp://ftp.cwi.nl/pub/jack/python/versiontestdir/"
14_TESTDIR="http://www.cwi.nl/~jack/versiontestdir/"
15
16def versioncheck(package, url, version, verbose=0):
17 ok, newversion, fp = checkonly(package, url, version, verbose)
18 if verbose > VERBOSE_NORMAL:
Guido van Rossume4e41061998-04-06 14:20:27 +000019 return ok
Guido van Rossum52910371997-12-23 18:43:55 +000020 if ok < 0:
Collin Winter6afaeb72007-08-03 17:06:41 +000021 print('%s: No correctly formatted current version file found'%(package))
Guido van Rossum52910371997-12-23 18:43:55 +000022 elif ok == 1:
Collin Winter6afaeb72007-08-03 17:06:41 +000023 print('%s: up-to-date (version %s)'%(package, version))
Guido van Rossum52910371997-12-23 18:43:55 +000024 else:
Collin Winter6afaeb72007-08-03 17:06:41 +000025 print('%s: version %s installed, version %s found:' % \
26 (package, version, newversion))
Guido van Rossume4e41061998-04-06 14:20:27 +000027 if verbose > VERBOSE_SILENT:
28 while 1:
29 line = fp.readline()
30 if not line: break
31 sys.stdout.write('\t'+line)
Guido van Rossum52910371997-12-23 18:43:55 +000032 return ok
Guido van Rossume4e41061998-04-06 14:20:27 +000033
Guido van Rossum52910371997-12-23 18:43:55 +000034def checkonly(package, url, version, verbose=0):
35 if verbose >= VERBOSE_EACHFILE:
Collin Winter6afaeb72007-08-03 17:06:41 +000036 print('%s:'%package)
Guido van Rossum13257902007-06-07 23:15:56 +000037 if isinstance(url, str):
Guido van Rossume4e41061998-04-06 14:20:27 +000038 ok, newversion, fp = _check1version(package, url, version, verbose)
Guido van Rossum52910371997-12-23 18:43:55 +000039 else:
Guido van Rossume4e41061998-04-06 14:20:27 +000040 for u in url:
41 ok, newversion, fp = _check1version(package, u, version, verbose)
42 if ok >= 0 and verbose < VERBOSE_CHECKALL:
43 break
Guido van Rossum52910371997-12-23 18:43:55 +000044 return ok, newversion, fp
Guido van Rossume4e41061998-04-06 14:20:27 +000045
Guido van Rossum52910371997-12-23 18:43:55 +000046def _check1version(package, url, version, verbose=0):
47 if verbose >= VERBOSE_EACHFILE:
Collin Winter6afaeb72007-08-03 17:06:41 +000048 print(' Checking %s'%url)
Guido van Rossum52910371997-12-23 18:43:55 +000049 try:
Guido van Rossume4e41061998-04-06 14:20:27 +000050 fp = urllib.urlopen(url)
Guido van Rossumb940e112007-01-10 16:19:56 +000051 except IOError as arg:
Guido van Rossume4e41061998-04-06 14:20:27 +000052 if verbose >= VERBOSE_EACHFILE:
Collin Winter6afaeb72007-08-03 17:06:41 +000053 print(' Cannot open:', arg)
Guido van Rossume4e41061998-04-06 14:20:27 +000054 return -1, None, None
Guido van Rossum52910371997-12-23 18:43:55 +000055 msg = rfc822.Message(fp, seekable=0)
Barry Warsaw820c1202008-06-12 04:06:45 +000056 newversion = msg.get('current-version')
Guido van Rossum52910371997-12-23 18:43:55 +000057 if not newversion:
Guido van Rossume4e41061998-04-06 14:20:27 +000058 if verbose >= VERBOSE_EACHFILE:
Collin Winter6afaeb72007-08-03 17:06:41 +000059 print(' No "Current-Version:" header in URL or URL not found')
Guido van Rossume4e41061998-04-06 14:20:27 +000060 return -1, None, None
Walter Dörwaldaaab30e2002-09-11 20:36:02 +000061 version = version.lower().strip()
62 newversion = newversion.lower().strip()
Guido van Rossum52910371997-12-23 18:43:55 +000063 if version == newversion:
Guido van Rossume4e41061998-04-06 14:20:27 +000064 if verbose >= VERBOSE_EACHFILE:
Collin Winter6afaeb72007-08-03 17:06:41 +000065 print(' Version identical (%s)'%newversion)
Guido van Rossume4e41061998-04-06 14:20:27 +000066 return 1, version, fp
Guido van Rossum52910371997-12-23 18:43:55 +000067 else:
Guido van Rossume4e41061998-04-06 14:20:27 +000068 if verbose >= VERBOSE_EACHFILE:
Collin Winter6afaeb72007-08-03 17:06:41 +000069 print(' Versions different (installed: %s, new: %s)'% \
70 (version, newversion))
Guido van Rossume4e41061998-04-06 14:20:27 +000071 return 0, newversion, fp
72
73
Guido van Rossum52910371997-12-23 18:43:55 +000074def _test():
Collin Winter6afaeb72007-08-03 17:06:41 +000075 print('--- TEST VERBOSE=1')
76 print('--- Testing existing and identical version file')
Guido van Rossum52910371997-12-23 18:43:55 +000077 versioncheck('VersionTestPackage', _TESTDIR+'Version10.txt', '1.0', verbose=1)
Collin Winter6afaeb72007-08-03 17:06:41 +000078 print('--- Testing existing package with new version')
Guido van Rossum52910371997-12-23 18:43:55 +000079 versioncheck('VersionTestPackage', _TESTDIR+'Version11.txt', '1.0', verbose=1)
Collin Winter6afaeb72007-08-03 17:06:41 +000080 print('--- Testing package with non-existing version file')
Guido van Rossum52910371997-12-23 18:43:55 +000081 versioncheck('VersionTestPackage', _TESTDIR+'nonexistent.txt', '1.0', verbose=1)
Collin Winter6afaeb72007-08-03 17:06:41 +000082 print('--- Test package with 2 locations, first non-existing second ok')
Guido van Rossum52910371997-12-23 18:43:55 +000083 versfiles = [_TESTDIR+'nonexistent.txt', _TESTDIR+'Version10.txt']
84 versioncheck('VersionTestPackage', versfiles, '1.0', verbose=1)
Collin Winter6afaeb72007-08-03 17:06:41 +000085 print('--- TEST VERBOSE=2')
86 print('--- Testing existing and identical version file')
Guido van Rossum52910371997-12-23 18:43:55 +000087 versioncheck('VersionTestPackage', _TESTDIR+'Version10.txt', '1.0', verbose=2)
Collin Winter6afaeb72007-08-03 17:06:41 +000088 print('--- Testing existing package with new version')
Guido van Rossum52910371997-12-23 18:43:55 +000089 versioncheck('VersionTestPackage', _TESTDIR+'Version11.txt', '1.0', verbose=2)
Collin Winter6afaeb72007-08-03 17:06:41 +000090 print('--- Testing package with non-existing version file')
Guido van Rossum52910371997-12-23 18:43:55 +000091 versioncheck('VersionTestPackage', _TESTDIR+'nonexistent.txt', '1.0', verbose=2)
Collin Winter6afaeb72007-08-03 17:06:41 +000092 print('--- Test package with 2 locations, first non-existing second ok')
Guido van Rossum52910371997-12-23 18:43:55 +000093 versfiles = [_TESTDIR+'nonexistent.txt', _TESTDIR+'Version10.txt']
94 versioncheck('VersionTestPackage', versfiles, '1.0', verbose=2)
95
96if __name__ == '__main__':
Guido van Rossume4e41061998-04-06 14:20:27 +000097 _test()