Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5291037

Browse files
committed
Adding Jack Jansen's version checking utility.
1 parent 7f96291 commit 5291037

4 files changed

Lines changed: 212 additions & 0 deletions

File tree

Tools/versioncheck/README

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
This is versioncheck 1.0, a first stab at automatic checking of versions of
2+
Python extension packages installed on your system.
3+
4+
The basic idea is that each package contains a _checkversion.py
5+
somewhere, probably at the root level of the package. In addition, each
6+
package maintainer makes a file available on the net, through ftp or
7+
http, which contains the version number of the most recent distribution
8+
and some readable text explaining the differences with previous
9+
versions, where to download the package, etc.
10+
11+
The checkversions.py script walks through the installed Python tree (or
12+
through a tree of choice), and runs each _checkversion.py script. These
13+
scripts retrieve the current-version file over the net, compares version
14+
numbers and tells the user about new versions of packages available.
15+
16+
A boilerplate for the _checkversion.py file can be found here. Replace
17+
package name, version and the URL of the version-check file and put it in
18+
your distribution. In stead of a single URL you can also specify a list
19+
of URLs. Each of these will be checked in order until one is available,
20+
this is handy for distributions that live in multiple places. Put the
21+
primary distribution site (the most up-to-date site) before others.
22+
The script is executed with execfile(), not imported, and the current
23+
directory is the checkversion directory, so be careful with globals,
24+
importing, etc.
25+
26+
The version-check file consists of an rfc822-style header followed by
27+
plaintext. The only header field checked currently is
28+
'Current-Version:', which should contain te current version and is
29+
matched against the string contained in the _checkversion.py script.
30+
The rest of the file is human-readable text and presented to the user if
31+
there is a version mismatch. It should contain at the very least a URL
32+
of either the current distribution or a webpage describing it.
33+
34+
Pycheckversion.py is the module that does the actual checking of versions.
35+
It should be fine where it is, it is imported by checkversion before anything
36+
else is done, but if imports fail you may want to move it to somewhere
37+
along sys.path.
38+
39+
Jack Jansen, CWI, 23-Dec-97.
40+
41+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""This file (which is sourced, not imported) checks the version of the
2+
"versioncheck" package. It is also an example of how to format your own
3+
_checkversion.py file"""
4+
5+
import pyversioncheck
6+
7+
_PACKAGE="versioncheck"
8+
_VERSION="1.0"
9+
_URL="http://www.cwi.nl/~jack/versioncheck/curversion.txt"
10+
11+
try:
12+
_myverbose=VERBOSE
13+
except NameError:
14+
_myverbose=1
15+
16+
pyversioncheck.versioncheck(_PACKAGE, _URL, _VERSION, verbose=_myverbose)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""Checkversions - recursively search a directory (default: sys.prefix)
2+
for _checkversion.py files, and run each of them. This will tell you of
3+
new versions available for any packages you have installed."""
4+
5+
import os
6+
import getopt
7+
import sys
8+
import pyversioncheck
9+
10+
CHECKNAME="_checkversion.py"
11+
12+
VERBOSE=1
13+
14+
USAGE="""Usage: checkversions [-v verboselevel] [dir ...]
15+
Recursively examine a tree (default: sys.prefix) and for each package
16+
with a _checkversion.py file compare the installed version against the current
17+
version.
18+
19+
Values for verboselevel:
20+
0 - Minimal output, one line per package
21+
1 - Also print descriptions for outdated packages (default)
22+
2 - Print information on each URL checked
23+
3 - Check every URL for packages with multiple locations"""
24+
25+
def check1dir(dummy, dir, files):
26+
if CHECKNAME in files:
27+
fullname = os.path.join(dir, CHECKNAME)
28+
try:
29+
execfile(fullname)
30+
except:
31+
print '** Exception in', fullname
32+
33+
def walk1tree(tree):
34+
os.path.walk(tree, check1dir, None)
35+
36+
def main():
37+
global VERBOSE
38+
try:
39+
options, arguments = getopt.getopt(sys.argv[1:], 'v:')
40+
except getopt.error:
41+
print USAGE
42+
sys.exit(1)
43+
for o, a in options:
44+
if o == '-v':
45+
VERBOSE = string.atoi(a)
46+
if not arguments:
47+
arguments = [sys.prefix]
48+
for dir in arguments:
49+
walk1tree(dir)
50+
51+
if __name__ == '__main__':
52+
main()
53+
54+
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
"""pyversioncheck - Module to help with checking versions"""
2+
import Types
3+
import rfc822
4+
import urllib
5+
import sys
6+
import string
7+
8+
# Verbose options
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
13+
14+
# Test directory
15+
## urllib bug: _TESTDIR="ftp://ftp.cwi.nl/pub/jack/python/versiontestdir/"
16+
_TESTDIR="http://www.cwi.nl/~jack/versiontestdir/"
17+
18+
def versioncheck(package, url, version, verbose=0):
19+
ok, newversion, fp = checkonly(package, url, version, verbose)
20+
if verbose > VERBOSE_NORMAL:
21+
return ok
22+
if ok < 0:
23+
print '%s: No correctly formatted current version file found'%(package)
24+
elif ok == 1:
25+
print '%s: up-to-date (version %s)'%(package, version)
26+
else:
27+
print '%s: version %s installed, version %s found:' % \
28+
(package, version, newversion)
29+
if verbose > VERBOSE_SILENT:
30+
while 1:
31+
line = fp.readline()
32+
if not line: break
33+
sys.stdout.write('\t'+line)
34+
return ok
35+
36+
def checkonly(package, url, version, verbose=0):
37+
if verbose >= VERBOSE_EACHFILE:
38+
print '%s:'%package
39+
if type(url) == Types.StringType:
40+
ok, newversion, fp = _check1version(package, url, version, verbose)
41+
else:
42+
for u in url:
43+
ok, newversion, fp = _check1version(package, u, version, verbose)
44+
if ok >= 0 and verbose < VERBOSE_CHECKALL:
45+
break
46+
return ok, newversion, fp
47+
48+
def _check1version(package, url, version, verbose=0):
49+
if verbose >= VERBOSE_EACHFILE:
50+
print ' Checking %s'%url
51+
try:
52+
fp = urllib.urlopen(url)
53+
except IOError, arg:
54+
if verbose >= VERBOSE_EACHFILE:
55+
print ' Cannot open:', arg
56+
return -1, None, None
57+
msg = rfc822.Message(fp, seekable=0)
58+
newversion = msg.getheader('current-version')
59+
if not newversion:
60+
if verbose >= VERBOSE_EACHFILE:
61+
print ' No "Current-Version:" header in URL or URL not found'
62+
return -1, None, None
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
68+
return 1, version, fp
69+
else:
70+
if verbose >= VERBOSE_EACHFILE:
71+
print ' Versions different (installed: %s, new: %s)'% \
72+
(version, newversion)
73+
return 0, newversion, fp
74+
75+
76+
def _test():
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)
97+
98+
if __name__ == '__main__':
99+
_test()
100+
101+

0 commit comments

Comments
 (0)