- Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Copy pathpyversioncheck.py
98 lines (90 loc) · 3.96 KB
/
pyversioncheck.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""pyversioncheck - Module to help with checking versions"""
importtypes
importrfc822
importurllib
importsys
# 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/"
defversioncheck(package, url, version, verbose=0):
ok, newversion, fp=checkonly(package, url, version, verbose)
ifverbose>VERBOSE_NORMAL:
returnok
ifok<0:
print'%s: No correctly formatted current version file found'%(package)
elifok==1:
print'%s: up-to-date (version %s)'%(package, version)
else:
print'%s: version %s installed, version %s found:'% \
(package, version, newversion)
ifverbose>VERBOSE_SILENT:
while1:
line=fp.readline()
ifnotline: break
sys.stdout.write('\t'+line)
returnok
defcheckonly(package, url, version, verbose=0):
ifverbose>=VERBOSE_EACHFILE:
print'%s:'%package
iftype(url) ==types.StringType:
ok, newversion, fp=_check1version(package, url, version, verbose)
else:
foruinurl:
ok, newversion, fp=_check1version(package, u, version, verbose)
ifok>=0andverbose<VERBOSE_CHECKALL:
break
returnok, newversion, fp
def_check1version(package, url, version, verbose=0):
ifverbose>=VERBOSE_EACHFILE:
print' Checking %s'%url
try:
fp=urllib.urlopen(url)
exceptIOError, arg:
ifverbose>=VERBOSE_EACHFILE:
print' Cannot open:', arg
return-1, None, None
msg=rfc822.Message(fp, seekable=0)
newversion=msg.getheader('current-version')
ifnotnewversion:
ifverbose>=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()
ifversion==newversion:
ifverbose>=VERBOSE_EACHFILE:
print' Version identical (%s)'%newversion
return1, version, fp
else:
ifverbose>=VERBOSE_EACHFILE:
print' Versions different (installed: %s, new: %s)'% \
(version, newversion)
return0, 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()