source: rtems-docs/common/version.py @ 3605600

5
Last change on this file since 3605600 was d721375, checked in by Chris Johns <chrisj@…>, on 02/12/19 at 23:50:50

waf: Fix version.py to support older versions of git.

  • Property mode set to 100644
File size: 5.3 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(ctx, date):
73    ''' Format is YYYY-MM-DD'''
74    import datetime
75    ds = date.split('-')
76    if len(ds) != 3:
77        ctx.fatal('invalid date format from git: %s' % (date))
78    try:
79        year = int(ds[0])
80    except:
81        ctx.fatal('invalid date format from git, converting year: %s' % (date))
82    try:
83        month = int(ds[1])
84    except:
85        ctx.fatal('invalid date format from git, converting month: %s' % (date))
86    try:
87        day = int(ds[2])
88    except:
89        ctx.fatal('invalid date format from git, converting day: %s' % (date))
90    try:
91        when = datetime.date(year, month, day)
92    except:
93        ctx.fatal('invalid date format from git: %s' % (date))
94    if day == 3:
95        s = 'rd'
96    elif day == 11 or day == 12:
97        s = 'th'
98    elif day % 10 == 1:
99        s = 'st'
100    elif day % 10 == 2:
101        s = 'nd'
102    else:
103        s = 'th'
104    s = when.strftime('%%d%s %%B %%Y' % (s))
105    if day < 10:
106        s = s[1:]
107    return s
108
109def get(ctx, rtems_major_version):
110    global _version
111    global _date
112    global _released
113    version = _version
114    date = _date
115    released = _released
116    if _version == 'invalid':
117        version = rtems_major_version
118        date = _date
119        released = False
120        #
121        # Is there a VERSION file for a release or deployed source.
122        #
123        vc = 'VERSION'
124        if os.path.exists(vc):
125            try:
126                import configparser
127            except ImportError:
128                import ConfigParser as configparser
129            v = configparser.SafeConfigParser()
130            try:
131                v.read(vc)
132            except Exception as e:
133                ctx.fatal('Invalid version config format: %s: %s' % (vc, e))
134            try:
135                version = v.get('version', 'revision')
136                date = v.get('version', 'date')
137            except Exception as e:
138                ctx.fatal('Invalid version file: %s: %s' % (vc, e))
139            if not 'not_released' in version:
140                _released = True
141        else:
142            #
143            # Get date and version from Git
144            #
145            if ctx.exec_command([ctx.env.GIT[0], 'diff-index', '--quiet', 'HEAD']) == 0:
146                modified = ''
147            else:
148                modified = '-modified'
149            out = ctx.cmd_and_log([ctx.env.GIT[0], 'log', '-1',
150                                   '--format=%h,%cd', '--date=short'],
151                                  quiet = True)
152            f = out.strip('\n').split(',')
153            version = version + '.' + f[0] + modified
154            date = _pretty_day(ctx, f[1])
155        _version = version
156        _date = date
157        _release = released
158    return version, date, released
159
160def string():
161    return '%s (%s)' % (_version, _date)
162
163def version():
164    return _version
165
166def date():
167    return _date
168
169def released():
170    return _released
Note: See TracBrowser for help on using the repository browser.