summaryrefslogtreecommitdiffstats
path: root/common/version.py
blob: 9beb3e76527166216c71251b51c806d98d668c8f (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#
# RTEMS Documentation Project (http://www.rtems.org/)
# Copyright 2019 Chris Johns (chrisj@rtems.org)
# Copyright (C) 2019 embedded brains GmbH
# All rights reserved.
#
# This file is part of the RTEMS Documentation package in 'rtems-docs'.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

#
# Releasing RTEMS Documentation
# -----------------------------
#
# The VERSION file format is:
#
#  The format is INI. The file requires a `[version`] section and a `revision`
#  and 'date' values:
#
#   [version]
#   revision = <version-string>
#   date = <date-string>
#
#  The `<version-string>` has the `version` and `revision` delimited by a
#  single `.`. An example file is:
#
#   [version]
#   revision = 5.0.not_released
#   date = 1st February 2019
#
#  where the `version` is `5` and the revision is `0` and the package is not
#  released. The label `not_released` is reversed to mean the package is not
#  released. A revision string can contain extra characters after the
#  `revision` number for example `5.0-rc1` or is deploying a package
#  `5.0-nasa-cfs`
#
# User deployment:
#
#  Create a git archive and then add a suitable VERSION file to the top
#  directory of the package.
#

from __future__ import print_function

import os.path

_version = 'invalid'
_date = 'unknown date'
_released = False

def _pretty_day(ctx, date):
    ''' Format is YYYY-MM-DD'''
    import datetime
    ds = date.split('-')
    if len(ds) != 3:
        ctx.fatal('invalid date format from git: %s' % (date))
    try:
        year = int(ds[0])
    except:
        ctx.fatal('invalid date format from git, converting year: %s' % (date))
    try:
        month = int(ds[1])
    except:
        ctx.fatal('invalid date format from git, converting month: %s' % (date))
    try:
        day = int(ds[2])
    except:
        ctx.fatal('invalid date format from git, converting day: %s' % (date))
    try:
        when = datetime.date(year, month, day)
    except:
        ctx.fatal('invalid date format from git: %s' % (date))
    if day == 3:
        s = 'rd'
    elif day == 11 or day == 12:
        s = 'th'
    elif day % 10 == 1:
        s = 'st'
    elif day % 10 == 2:
        s = 'nd'
    else:
        s = 'th'
    s = when.strftime('%%d%s %%B %%Y' % (s))
    if day < 10:
        s = s[1:]
    return s

def get(ctx, rtems_major_version):
    global _version
    global _date
    global _released
    version = _version
    date = _date
    released = _released
    if _version == 'invalid':
        version = rtems_major_version
        date = _date
        released = False
        #
        # Is there a VERSION file for a release or deployed source.
        #
        vc = 'VERSION'
        if os.path.exists(vc):
            try:
                import configparser
            except ImportError:
                import ConfigParser as configparser
            v = configparser.SafeConfigParser()
            try:
                v.read(vc)
            except Exception as e:
                ctx.fatal('Invalid version config format: %s: %s' % (vc, e))
            try:
                version = v.get('version', 'revision')
                date = v.get('version', 'date')
            except Exception as e:
                ctx.fatal('Invalid version file: %s: %s' % (vc, e))
            if not 'not_released' in version:
                _released = True
        else:
            #
            # Get date and version from Git
            #
            if ctx.exec_command([ctx.env.GIT[0], 'diff-index', '--quiet', 'HEAD']) == 0:
                modified = ''
            else:
                modified = '-modified'
            out = ctx.cmd_and_log([ctx.env.GIT[0], 'log', '-1',
                                   '--format=%h,%cd', '--date=short'],
                                  quiet = True)
            f = out.strip('\n').split(',')
            version = version + '.' + f[0] + modified
            date = _pretty_day(ctx, f[1])
        _version = version
        _date = date
        _release = released
    return version, date, released

def string():
    return '%s (%s)' % (_version, _date)

def version():
    return _version

def date():
    return _date

def released():
    return _released