pyversioncheck.py 3.96 KB
Newer Older
1
"""pyversioncheck - Module to help with checking versions"""
Georg Brandl's avatar
Georg Brandl committed
2
import urllib.request
Georg Brandl's avatar
Georg Brandl committed
3
import email
4 5 6
import sys

# Verbose options
7 8 9 10
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
11 12 13 14 15 16 17 18

# 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:
19
        return ok
20
    if ok < 0:
21
        print('%s: No correctly formatted current version file found'%(package))
22
    elif ok == 1:
23
        print('%s: up-to-date (version %s)'%(package, version))
24
    else:
25 26
        print('%s: version %s installed, version %s found:' % \
                        (package, version, newversion))
27 28 29 30 31
        if verbose > VERBOSE_SILENT:
            while 1:
                line = fp.readline()
                if not line: break
                sys.stdout.write('\t'+line)
32
    return ok
33

34 35
def checkonly(package, url, version, verbose=0):
    if verbose >= VERBOSE_EACHFILE:
36
        print('%s:'%package)
37
    if isinstance(url, str):
38
        ok, newversion, fp = _check1version(package, url, version, verbose)
39
    else:
40 41 42 43
        for u in url:
            ok, newversion, fp = _check1version(package, u, version, verbose)
            if ok >= 0 and verbose < VERBOSE_CHECKALL:
                break
44
    return ok, newversion, fp
45

46 47
def _check1version(package, url, version, verbose=0):
    if verbose >= VERBOSE_EACHFILE:
48
        print('  Checking %s'%url)
49
    try:
Georg Brandl's avatar
Georg Brandl committed
50
        fp = urllib.request.urlopen(url)
51
    except IOError as arg:
52
        if verbose >= VERBOSE_EACHFILE:
53
            print('    Cannot open:', arg)
54
        return -1, None, None
Georg Brandl's avatar
Georg Brandl committed
55
    msg = email.message_from_file(fp)
56
    newversion = msg.get('current-version')
57
    if not newversion:
58
        if verbose >= VERBOSE_EACHFILE:
59
            print('    No "Current-Version:" header in URL or URL not found')
60
        return -1, None, None
61 62
    version = version.lower().strip()
    newversion = newversion.lower().strip()
63
    if version == newversion:
64
        if verbose >= VERBOSE_EACHFILE:
65
            print('    Version identical (%s)'%newversion)
66
        return 1, version, fp
67
    else:
68
        if verbose >= VERBOSE_EACHFILE:
69 70
            print('    Versions different (installed: %s, new: %s)'% \
                        (version, newversion))
71 72 73
        return 0, newversion, fp


74
def _test():
75 76
    print('--- TEST VERBOSE=1')
    print('--- Testing existing and identical version file')
77
    versioncheck('VersionTestPackage', _TESTDIR+'Version10.txt', '1.0', verbose=1)
78
    print('--- Testing existing package with new version')
79
    versioncheck('VersionTestPackage', _TESTDIR+'Version11.txt', '1.0', verbose=1)
80
    print('--- Testing package with non-existing version file')
81
    versioncheck('VersionTestPackage', _TESTDIR+'nonexistent.txt', '1.0', verbose=1)
82
    print('--- Test package with 2 locations, first non-existing second ok')
83 84
    versfiles = [_TESTDIR+'nonexistent.txt', _TESTDIR+'Version10.txt']
    versioncheck('VersionTestPackage', versfiles, '1.0', verbose=1)
85 86
    print('--- TEST VERBOSE=2')
    print('--- Testing existing and identical version file')
87
    versioncheck('VersionTestPackage', _TESTDIR+'Version10.txt', '1.0', verbose=2)
88
    print('--- Testing existing package with new version')
89
    versioncheck('VersionTestPackage', _TESTDIR+'Version11.txt', '1.0', verbose=2)
90
    print('--- Testing package with non-existing version file')
91
    versioncheck('VersionTestPackage', _TESTDIR+'nonexistent.txt', '1.0', verbose=2)
92
    print('--- Test package with 2 locations, first non-existing second ok')
93 94 95 96
    versfiles = [_TESTDIR+'nonexistent.txt', _TESTDIR+'Version10.txt']
    versioncheck('VersionTestPackage', versfiles, '1.0', verbose=2)

if __name__ == '__main__':
97
    _test()