source: rtems-docs/common/version.py @ 6f18d69

5
Last change on this file since 6f18d69 was 6f18d69, checked in by Chris Johns <chrisj@…>, on 02/06/19 at 21:46:53

Fix the version logic to make it more robust.

  • Property mode set to 100644
File size: 4.8 KB
Line 
1#
2# RTEMS Documentation Project (http://www.rtems.org/)
3# Copyright 2019 Chris Johns (chrisj@rtems.org)
4# Copyright (C) 2019 embedded brains GmbH
5# All rights reserved.
6#
7# This file is part of the RTEMS Documentation package in 'rtems-docs'.
8#
9# Redistribution and use in source and binary forms, with or without
10# modification, are permitted provided that the following conditions are met:
11#
12# 1. Redistributions of source code must retain the above copyright notice,
13# this list of conditions and the following disclaimer.
14#
15# 2. Redistributions in binary form must reproduce the above copyright notice,
16# this list of conditions and the following disclaimer in the documentation
17# and/or other materials provided with the distribution.
18#
19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29# POSSIBILITY OF SUCH DAMAGE.
30#
31
32#
33# Releasing RTEMS Documentation
34# -----------------------------
35#
36# The VERSION file format is:
37#
38#  The format is INI. The file requires a `[version`] section and a `revision`
39#  and 'date' values:
40#
41#   [version]
42#   revision = <version-string>
43#   date = <date-string>
44#
45#  The `<version-string>` has the `version` and `revision` delimited by a
46#  single `.`. An example file is:
47#
48#   [version]
49#   revision = 5.0.not_released
50#   date = 1st February 2019
51#
52#  where the `version` is `5` and the revision is `0` and the package is not
53#  released. The label `not_released` is reversed to mean the package is not
54#  released. A revision string can contain extra characters after the
55#  `revision` number for example `5.0-rc1` or is deploying a package
56#  `5.0-nasa-cfs`
57#
58# User deployment:
59#
60#  Create a git archive and then add a suitable VERSION file to the top
61#  directory of the package.
62#
63
64from __future__ import print_function
65
66import os.path
67
68_version = 'invalid'
69_date = 'unknown date'
70_released = False
71
72def _pretty_day(day):
73    if day == 3:
74        s = 'rd'
75    elif day == 11:
76        s = 'th'
77    elif day == 12:
78        s = 'th'
79    elif day == 13:
80        s = 'th'
81    elif day % 10 == 1:
82        s = 'st'
83    elif day % 10 == 2:
84        s = 'nd'
85    else:
86        s = 'th'
87    return str(day) + s
88
89def get(ctx, rtems_major_version):
90    global _version
91    global _date
92    global _released
93    version = _version
94    date = _date
95    released = _released
96    if _version == 'invalid':
97        version = rtems_major_version
98        date = _date
99        released = False
100        #
101        # Is there a VERSION file for a release or deployed source.
102        #
103        vc = 'VERSION'
104        if os.path.exists(vc):
105            try:
106                import configparser
107            except ImportError:
108                import ConfigParser as configparser
109            v = configparser.SafeConfigParser()
110            try:
111                v.read(vc)
112            except Exception as e:
113                ctx.fatal('Invalid version config format: %s: %s' % (vc, e))
114            try:
115                version = v.get('version', 'revision')
116                date = v.get('version', 'date')
117            except Exception as e:
118                ctx.fatal('Invalid version file: %s: %s' % (vc, e))
119            if not 'not_released' in version:
120                _released = True
121        else:
122            #
123            # Get date and version from Git
124            #
125            if ctx.exec_command(['git', 'diff-index', '--quiet', 'HEAD']) == 0:
126                modified = ''
127            else:
128                modified = '-modified'
129            try:
130                out = ctx.cmd_and_log(['git', 'log', '-1',
131                                       '--format=%h,%cd', '--date=format:%e,%B,%Y'],
132                                      quiet = True)
133                f = out.strip('\n').split(',')
134                version = version + '.' + f[0] + modified
135                date = _pretty_day(int(f[1])) + ' ' + f[2] + ' ' + f[3]
136            except waflib.Build.Errors.WafError:
137                date = 'unknown date'
138        _version = version
139        _date = date
140        _release = released
141    return version, date, released
142
143def string():
144    return '%s (%s)' % (_version, _date)
145
146def version():
147    return _version
148
149def date():
150    return _date
151
152def released():
153    return _released
Note: See TracBrowser for help on using the repository browser.