aboutsummaryrefslogtreecommitdiff
path: root/version.py
blob: 03a03e8fb47161eeda3745cedddd556856393e05 (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
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  version.py
#
#  Copyright 2014 Neil Williams <codehelp@debian.org>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#


import subprocess
import os


# pylint: disable=superfluous-parens,too-many-locals


def version_tag():
    """
    Parses the git status to determine if this is a git tag
    or a developer commit and builds a version string combining
    the two. If there is no git directory, relies on this being
    a directory created from the tarball created by setup.py when
    it uses this script and retrieves the original version string
    from that.
    :return: a version string based on the tag and short hash
    """
    if not os.path.exists("./.git/"):
        base = os.path.basename(os.getcwd())
        name_list = ['grep', 'name=', 'setup.py']
        name_data = subprocess.check_output(name_list).strip().decode('utf-8')
        name_data = name_data.replace("name=\'", '')
        name_data = name_data.replace("\',", '')
        return base.replace("%s-" % name_data, '')
    tag_list = [
        'git', 'for-each-ref', '--sort=taggerdate', '--format',
        "'%(refname)'", 'refs/tags',
    ]
    hash_list = ['git', 'log', '-n', '1']
    tag_hash_list = ['git', 'rev-list']
    clone_data = subprocess.check_output(hash_list).strip().decode('utf-8')
    commits = clone_data.split('\n')
    clone_hash = commits[0].replace('commit ', '')[:8]
    tag_data = subprocess.check_output(tag_list).strip().decode('utf-8')
    tags = tag_data.split('\n')
    if len(tags) < 2:
        return clone_hash
    tag_line = str(tags[len(tags) - 1]).replace('\'', '').strip()
    tag_name = tag_line.split("/")[2]
    tag_hash_list.append(tag_name)
    tag_hash = subprocess.check_output(tag_hash_list).strip().decode('utf-8')
    tags = tag_hash.split('\n')
    tag_hash = tags[0][:8]
    if tag_hash == clone_hash:
        return tag_name
    else:
        # tag, month end and release are now out of sync.
        # use the rev-list count to always ensure that we are building
        # a newer version to cope with date changes at month end.
        # use short git hash for reference.
        dev_stamp = ['git', 'rev-list', '--count', 'HEAD']
        dev_count = subprocess.check_output(dev_stamp).strip().decode('utf-8')
        dev_short = ['git', 'rev-parse', '--short', 'HEAD']
        dev_hash = subprocess.check_output(dev_short).strip().decode('utf-8')
        return "%s+%s.%s" % (tag_name, dev_count, dev_hash)


def main():
    print(version_tag())
    return 0

if __name__ == '__main__':

    main()