summaryrefslogtreecommitdiff
path: root/tools/read-version
blob: e6167a2c7720b82bf93ed3547bce90e19bbe373e (plain)
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
#!/usr/bin/python
# vi: ts=4 expandtab

import os
import sys
import re

from distutils import version as ver

possible_topdir = os.path.normpath(os.path.join(os.path.abspath(
        sys.argv[0]), os.pardir, os.pardir))
if os.path.exists(os.path.join(possible_topdir, "cloudinit", "__init__.py")):
    sys.path.insert(0, possible_topdir)

from cloudinit import version as cver

def parse_versions(fn):
    with open(fn, 'r') as fh:
        lines = fh.read().splitlines()
    versions = []
    for line in lines:
        line = line.strip()
        if line.startswith("-") or not line:
            continue
        if not re.match(r"[\d]", line):
            continue
        line = line.strip(":")
        if (re.match(r"^[\d+]\.[\d+]\.[\d+]$", line) or
            re.match(r"^[\d+]\.[\d+]$", line)):
            versions.append(line)
    return versions

def find_changelog(args):
    p_files = []
    if args:
        p_files.append(args[0])
    p_files.append(os.path.join(os.pardir, "ChangeLog"))
    p_files.append(os.path.join(os.getcwd(), 'ChangeLog'))
    found = None
    for fn in p_files:
        if os.path.isfile(fn):
            found = fn
            break
    return found


if __name__ == '__main__':
    run_args = sys.argv[1:]
    fn = find_changelog(run_args)
    if not fn:
        sys.stderr.write("'ChangeLog' file not found!\n")
        sys.exit(1)
    else:
        versions = parse_versions(fn)
        if not versions:
            sys.stderr.write("No versions found in %s!\n" % (fn))
            sys.exit(1)
        else:
            # Check that the code version is the same 
            # as the version we found!
            ch_ver = versions[0].strip()
            code_ver = cver.version()
            ch_ver_obj = ver.StrictVersion(ch_ver)
            if ch_ver_obj != code_ver:
                sys.stderr.write(("Code version %s does not match"
                                  " changelog version %s\n") % 
                                  (code_ver, ch_ver_obj))
                sys.exit(1)
            sys.stdout.write(ch_ver)
            sys.exit(0)