diff options
author | cinap_lenrek <cinap_lenrek@localhost> | 2011-05-03 11:25:13 +0000 |
---|---|---|
committer | cinap_lenrek <cinap_lenrek@localhost> | 2011-05-03 11:25:13 +0000 |
commit | 458120dd40db6b4df55a4e96b650e16798ef06a0 (patch) | |
tree | 8f82685be24fef97e715c6f5ca4c68d34d5074ee /sys/src/cmd/python/Tools/versioncheck/pyversioncheck.py | |
parent | 3a742c699f6806c1145aea5149bf15de15a0afd7 (diff) |
add hg and python
Diffstat (limited to 'sys/src/cmd/python/Tools/versioncheck/pyversioncheck.py')
-rw-r--r-- | sys/src/cmd/python/Tools/versioncheck/pyversioncheck.py | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/sys/src/cmd/python/Tools/versioncheck/pyversioncheck.py b/sys/src/cmd/python/Tools/versioncheck/pyversioncheck.py new file mode 100644 index 000000000..144665383 --- /dev/null +++ b/sys/src/cmd/python/Tools/versioncheck/pyversioncheck.py @@ -0,0 +1,98 @@ +"""pyversioncheck - Module to help with checking versions""" +import types +import rfc822 +import urllib +import sys + +# Verbose options +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 + +# 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: + return ok + if ok < 0: + print '%s: No correctly formatted current version file found'%(package) + elif ok == 1: + print '%s: up-to-date (version %s)'%(package, version) + else: + print '%s: version %s installed, version %s found:' % \ + (package, version, newversion) + if verbose > VERBOSE_SILENT: + while 1: + line = fp.readline() + if not line: break + sys.stdout.write('\t'+line) + return ok + +def checkonly(package, url, version, verbose=0): + if verbose >= VERBOSE_EACHFILE: + print '%s:'%package + if type(url) == types.StringType: + ok, newversion, fp = _check1version(package, url, version, verbose) + else: + for u in url: + ok, newversion, fp = _check1version(package, u, version, verbose) + if ok >= 0 and verbose < VERBOSE_CHECKALL: + break + return ok, newversion, fp + +def _check1version(package, url, version, verbose=0): + if verbose >= VERBOSE_EACHFILE: + print ' Checking %s'%url + try: + fp = urllib.urlopen(url) + except IOError, arg: + if verbose >= VERBOSE_EACHFILE: + print ' Cannot open:', arg + return -1, None, None + msg = rfc822.Message(fp, seekable=0) + newversion = msg.getheader('current-version') + if not newversion: + if verbose >= VERBOSE_EACHFILE: + print ' No "Current-Version:" header in URL or URL not found' + return -1, None, None + version = version.lower().strip() + newversion = newversion.lower().strip() + if version == newversion: + if verbose >= VERBOSE_EACHFILE: + print ' Version identical (%s)'%newversion + return 1, version, fp + else: + if verbose >= VERBOSE_EACHFILE: + print ' Versions different (installed: %s, new: %s)'% \ + (version, newversion) + return 0, newversion, fp + + +def _test(): + print '--- TEST VERBOSE=1' + print '--- Testing existing and identical version file' + versioncheck('VersionTestPackage', _TESTDIR+'Version10.txt', '1.0', verbose=1) + print '--- Testing existing package with new version' + versioncheck('VersionTestPackage', _TESTDIR+'Version11.txt', '1.0', verbose=1) + print '--- Testing package with non-existing version file' + versioncheck('VersionTestPackage', _TESTDIR+'nonexistent.txt', '1.0', verbose=1) + print '--- Test package with 2 locations, first non-existing second ok' + versfiles = [_TESTDIR+'nonexistent.txt', _TESTDIR+'Version10.txt'] + versioncheck('VersionTestPackage', versfiles, '1.0', verbose=1) + print '--- TEST VERBOSE=2' + print '--- Testing existing and identical version file' + versioncheck('VersionTestPackage', _TESTDIR+'Version10.txt', '1.0', verbose=2) + print '--- Testing existing package with new version' + versioncheck('VersionTestPackage', _TESTDIR+'Version11.txt', '1.0', verbose=2) + print '--- Testing package with non-existing version file' + versioncheck('VersionTestPackage', _TESTDIR+'nonexistent.txt', '1.0', verbose=2) + print '--- Test package with 2 locations, first non-existing second ok' + versfiles = [_TESTDIR+'nonexistent.txt', _TESTDIR+'Version10.txt'] + versioncheck('VersionTestPackage', versfiles, '1.0', verbose=2) + +if __name__ == '__main__': + _test() |