summaryrefslogtreecommitdiffstats
path: root/source-builder/sb/version.py
blob: 34fb4217b675676951ee11acd399264ace7428cf (plain) (blame)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#
# RTEMS Tools Project (http://www.rtems.org/)
# Copyright 2010-2016 Chris Johns (chrisj@rtems.org)
# All rights reserved.
#
# This file is part of the RTEMS Tools package in 'rtems-tools'.
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

#
# To release the RSB create a git archive and then add a suitable VERSION file
# to the top directory.
#

from __future__ import print_function

import sys

import download
import error
import git
import path
import sources

#
# Default to an internal string.
#
_version = '5'
_revision = 'not_released'
_version_str = '%s.%s' % (_version, _revision)
_released = False
_git = False

def _top():
    top = path.dirname(sys.argv[0])
    if len(top) == 0:
        top = '.'
    return top

def _load_released_version_config():
    top = _top()
    for ver in [top, '..']:
        if path.exists(path.join(ver, 'VERSION')):
            try:
                import configparser
            except ImportError:
                import ConfigParser as configparser
            v = configparser.SafeConfigParser()
            try:
                v.read(path.join(ver, 'VERSION'))
            except:
                raise error.general('Invalid VERSION file')
            return v
    return None

def _load_released_version():
    global _released
    global _version_str
    v = _load_released_version_config()
    if v is not None:
        try:
            _version_str = v.get('version', 'release')
        except:
            raise error.general('Invalid VERSION file')
        _released = True
    return _released

def _load_git_version():
    global _git
    global _version_str
    repo = git.repo(_top())
    if repo.valid():
        head = repo.head()
        if repo.dirty():
            modified = ' modified'
        else:
            modified = ''
        _version_str = '%s (%s%s)' % (_version, head[0:12], modified)
        _git = True
    return _git

def released():
    return _load_released_version()

def version_control():
    return _load_git_version()

def str():
    if not _released and not _git:
        if not _load_released_version():
            _load_git_version()
    return _version_str

def load_release_settings(macros):
    def setting_error(msg):
        raise error.general(msg)

    if released():
        v = _load_released_version_config()
        if v is not None:
            try:
                hashes = v.items('hashes')
            except:
                hashes = []
            for hash in hashes:
                hs = hash[1].split()
                if len(hs) != 2:
                    raise error.general('invalid release hash in VERSION')
                sources.hash((hs[0], hash[0], hs[1]), macros, setting_error)
            try:
                release_path = v.get('version', 'release_path', raw = True)
                release_path = ','.join([rp.strip() for rp in release_path.split(',')])
            except:
                release_path = None
            download.set_release_path(release_path, macros)

def version():
    return _version

if __name__ == '__main__':
    print('Version: %s' % (str()))