1 """pyversioncheck - Module to help with checking versions"""
8 VERBOSE_SILENT
=0 # Single-line reports per package
9 VERBOSE_NORMAL
=1 # Single-line reports per package, more info if outdated
10 VERBOSE_EACHFILE
=2 # Report on each URL checked
11 VERBOSE_CHECKALL
=3 # Check each URL for each package
14 ## urllib bug: _TESTDIR="ftp://ftp.cwi.nl/pub/jack/python/versiontestdir/"
15 _TESTDIR
="http://www.cwi.nl/~jack/versiontestdir/"
17 def versioncheck(package
, url
, version
, verbose
=0):
18 ok
, newversion
, fp
= checkonly(package
, url
, version
, verbose
)
19 if verbose
> VERBOSE_NORMAL
:
22 print '%s: No correctly formatted current version file found'%(package)
24 print '%s: up-to-date (version %s)'%(package
, version
)
26 print '%s: version %s installed, version %s found:' % \
27 (package
, version
, newversion
)
28 if verbose
> VERBOSE_SILENT
:
32 sys
.stdout
.write('\t'+line
)
35 def checkonly(package
, url
, version
, verbose
=0):
36 if verbose
>= VERBOSE_EACHFILE
:
38 if type(url
) == types
.StringType
:
39 ok
, newversion
, fp
= _check1version(package
, url
, version
, verbose
)
42 ok
, newversion
, fp
= _check1version(package
, u
, version
, verbose
)
43 if ok
>= 0 and verbose
< VERBOSE_CHECKALL
:
45 return ok
, newversion
, fp
47 def _check1version(package
, url
, version
, verbose
=0):
48 if verbose
>= VERBOSE_EACHFILE
:
49 print ' Checking %s'%url
51 fp
= urllib
.urlopen(url
)
53 if verbose
>= VERBOSE_EACHFILE
:
54 print ' Cannot open:', arg
56 msg
= rfc822
.Message(fp
, seekable
=0)
57 newversion
= msg
.getheader('current-version')
59 if verbose
>= VERBOSE_EACHFILE
:
60 print ' No "Current-Version:" header in URL or URL not found'
62 version
= version
.lower().strip()
63 newversion
= newversion
.lower().strip()
64 if version
== newversion
:
65 if verbose
>= VERBOSE_EACHFILE
:
66 print ' Version identical (%s)'%newversion
69 if verbose
>= VERBOSE_EACHFILE
:
70 print ' Versions different (installed: %s, new: %s)'% \
72 return 0, newversion
, fp
76 print '--- TEST VERBOSE=1'
77 print '--- Testing existing and identical version file'
78 versioncheck('VersionTestPackage', _TESTDIR
+'Version10.txt', '1.0', verbose
=1)
79 print '--- Testing existing package with new version'
80 versioncheck('VersionTestPackage', _TESTDIR
+'Version11.txt', '1.0', verbose
=1)
81 print '--- Testing package with non-existing version file'
82 versioncheck('VersionTestPackage', _TESTDIR
+'nonexistent.txt', '1.0', verbose
=1)
83 print '--- Test package with 2 locations, first non-existing second ok'
84 versfiles
= [_TESTDIR
+'nonexistent.txt', _TESTDIR
+'Version10.txt']
85 versioncheck('VersionTestPackage', versfiles
, '1.0', verbose
=1)
86 print '--- TEST VERBOSE=2'
87 print '--- Testing existing and identical version file'
88 versioncheck('VersionTestPackage', _TESTDIR
+'Version10.txt', '1.0', verbose
=2)
89 print '--- Testing existing package with new version'
90 versioncheck('VersionTestPackage', _TESTDIR
+'Version11.txt', '1.0', verbose
=2)
91 print '--- Testing package with non-existing version file'
92 versioncheck('VersionTestPackage', _TESTDIR
+'nonexistent.txt', '1.0', verbose
=2)
93 print '--- Test package with 2 locations, first non-existing second ok'
94 versfiles
= [_TESTDIR
+'nonexistent.txt', _TESTDIR
+'Version10.txt']
95 versioncheck('VersionTestPackage', versfiles
, '1.0', verbose
=2)
97 if __name__
== '__main__':