1 """pyversioncheck - Module to help with checking versions"""
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
15 ## urllib bug: _TESTDIR="ftp://ftp.cwi.nl/pub/jack/python/versiontestdir/"
16 _TESTDIR
="http://www.cwi.nl/~jack/versiontestdir/"
18 def versioncheck(package
, url
, version
, verbose
=0):
19 ok
, newversion
, fp
= checkonly(package
, url
, version
, verbose
)
20 if verbose
> VERBOSE_NORMAL
:
23 print '%s: No correctly formatted current version file found'%(package)
25 print '%s: up-to-date (version %s)'%(package
, version
)
27 print '%s: version %s installed, version %s found:' % \
28 (package
, version
, newversion
)
29 if verbose
> VERBOSE_SILENT
:
33 sys
.stdout
.write('\t'+line
)
36 def checkonly(package
, url
, version
, verbose
=0):
37 if verbose
>= VERBOSE_EACHFILE
:
39 if type(url
) == types
.StringType
:
40 ok
, newversion
, fp
= _check1version(package
, url
, version
, verbose
)
43 ok
, newversion
, fp
= _check1version(package
, u
, version
, verbose
)
44 if ok
>= 0 and verbose
< VERBOSE_CHECKALL
:
46 return ok
, newversion
, fp
48 def _check1version(package
, url
, version
, verbose
=0):
49 if verbose
>= VERBOSE_EACHFILE
:
50 print ' Checking %s'%url
52 fp
= urllib
.urlopen(url
)
54 if verbose
>= VERBOSE_EACHFILE
:
55 print ' Cannot open:', arg
57 msg
= rfc822
.Message(fp
, seekable
=0)
58 newversion
= msg
.getheader('current-version')
60 if verbose
>= VERBOSE_EACHFILE
:
61 print ' No "Current-Version:" header in URL or URL not found'
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
70 if verbose
>= VERBOSE_EACHFILE
:
71 print ' Versions different (installed: %s, new: %s)'% \
73 return 0, newversion
, fp
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)
98 if __name__
== '__main__':