summaryrefslogtreecommitdiff
path: root/tools/read-version
blob: e585ab2e01c7dad931cec3055d9e9efb9d668d21 (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
71
72
73
74
75
76
77
#!/usr/bin/env python

import os
import json
import sys

if "avoid-pep8-E402-import-not-top-of-file":
    _tdir = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
    sys.path.insert(0, _tdir)
    from cloudinit import version as ci_version
    from cloudinit import util


use_long = '--long' in sys.argv or os.environ.get('CI_RV_LONG')
use_tags = '--tags' in sys.argv or os.environ.get('CI_RV_TAGS')
output_json = '--json' in sys.argv

src_version = ci_version.version_string()
version_long = None

if os.path.isdir(os.path.join(_tdir, ".git")):
    def fix_git_version(ver):
        ver = ver.strip()
        if "-" in ver:
            # change X.Y.Z-1023-gHASH to X.Y.Z+1023.ghash
            return "{0}+{1}.{2}".format(*ver.split("-"))
        return ver

    flags = []
    if use_tags:
        flags = ['--tags']
    cmd = ['git', 'describe'] + flags

    version = fix_git_version(util.subp(cmd)[0])

    if not version.startswith(src_version):
        sys.stderr.write("git describe version (%s) differs from "
                         "cloudinit.version (%s)\n" % (version, src_version))
        sys.exit(1)

    version_long = fix_git_version(util.subp(cmd + ["--long"])[0])
else:
    version = src_version
    try:
        version_long = ci_version.full_version_string()
    except ValueError:
        pass

# version is X.Y.Z[+xxx.gHASH]
# version_long is None or X.Y.Z+xxx.gHASH
release = version.partition("+")[0]
extra = None
commit = None
distance = None

if version_long:
    info = version_long.partition("+")[2]
    extra = "+" + info
    distance, commit = info.split(".")
    # remove the 'g' from gHASH
    commit = commit[1:]

data = {
    'release': release,
    'version': version,
    'version_long': version_long,
    'extra': extra,
    'commit': commit,
    'distance': distance,
}

if output_json:
    sys.stdout.write(json.dumps(data, indent=1) + "\n")
else:
    sys.stdout.write(release + "\n")

sys.exit(0)