source: rtems-tools/rtemstoolkit/version.py @ 36fde51

5
Last change on this file since 36fde51 was 3bd8def, checked in by Chris Johns <chrisj@…>, on 10/03/18 at 01:38:09

config: Consolidate the version information into a single configuration file

  • Property mode set to 100644
File size: 8.3 KB
Line 
1#
2# RTEMS Tools Project (http://www.rtems.org/)
3# Copyright 2010-2018 Chris Johns (chrisj@rtems.org)
4# All rights reserved.
5#
6# This file is part of the RTEMS Tools package in 'rtems-tools'.
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions are met:
10#
11# 1. Redistributions of source code must retain the above copyright notice,
12# this list of conditions and the following disclaimer.
13#
14# 2. Redistributions in binary form must reproduce the above copyright notice,
15# this list of conditions and the following disclaimer in the documentation
16# and/or other materials provided with the distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28# POSSIBILITY OF SUCH DAMAGE.
29#
30
31#
32# Releasing RTEMS Tools
33# ---------------------
34#
35# Format:
36#
37#  The format is INI. The file requires a `[version`] section and a `revision`
38#  option:
39#
40#   [version]
41#   revision = <version-string>
42#
43#  The `<version-string>` has the `version` and `revision` delimited by a
44#  single `.`. An example file is:
45#
46#   [version]
47#   revision = 5.0.not_released
48#
49#  where the `version` is `5` and the revision is `0` and the package is not
50#  released. The label `not_released` is reversed to mean the package is not
51#  released. A revision string can contain extra characters after the
52#  `revision` number for example `5.0-rc1` or is deploying a package
53#  `5.0-nasa-cfs`
54#
55#  Packages can optionally add specialised sections to a version configuration
56#  files. These can be accessed via the:
57#
58#   load_release_settings: Return the items in a section
59#   load_release_setting: Return an item from a section
60#
61# User deployment:
62#
63#  Create a git archive and then add a suitable VERSION file to the top
64#  directory of the package. The package assumes your python executable is
65#  location in `bin` directory which is one below the top of the package's
66#  install prefix.
67#
68# RTEMS Release:
69#
70#  Set the values in the `rtems-version.ini` file. This is a shared file so
71#  packages and encouraged to add specific settings to other configuration
72#  files.
73#
74# Notes:
75#
76#  This module uses os.apth for paths and assumes all paths are in the host
77#  format.
78#
79
80from __future__ import print_function
81
82import itertools
83import os
84import sys
85
86try:
87    import configparser
88except ImportError:
89    import ConfigParser as configparser
90
91#
92# Support to handle importing when installed in a package and as a unit test.
93# If there is a better way to let us know.
94#
95try:
96    from . import error
97    from . import git
98    from . import rtems
99except (ValueError, SystemError):
100    import error
101    import git
102    import path
103    import rtems
104
105#
106# Default to an internal string.
107#
108_version = 'undefined'
109_revision = 'not_released'
110_version_str = '%s.%s' % (_version, _revision)
111_released = False
112_git = False
113_is_loaded = False
114
115def _top():
116    top = os.path.dirname(sys.argv[0])
117    if len(top) == 0:
118        top = '.'
119    return top
120
121def _load_released_version_config():
122    '''Local worker to load a configuration file.'''
123    top = _top()
124    for ver in [os.path.join(top, 'VERSION'),
125                os.path.join('..', 'VERSION'),
126                rtems.configuration_file('rtems-version.ini')]:
127        if os.path.exists(os.path.join(ver)):
128            v = configparser.SafeConfigParser()
129            try:
130                v.read(ver)
131            except Exception as e:
132                raise error.general('Invalid version config format: %s: %s' % (ver,
133                                                                               e))
134            return ver, v
135    return None, None
136
137def _load_released_version():
138    '''Load the release data if present. If not found the package is not released.
139
140    A release can be made by adding a file called `VERSION` to the top level
141    directory of a package. This is useful for user deploying a package and
142    making custom releases.
143
144    The RTEMS project reserves the `rtems-version.ini` file for it's
145    releases. This is the base release and should not be touched by users
146    deploying a package.
147
148    '''
149    global _version
150    global _revision
151    global _released
152    global _version_str
153    global _is_loaded
154
155    if not _is_loaded:
156        vc, v = _load_released_version_config()
157        if v is not None:
158            try:
159                ver_str = v.get('version', 'revision')
160            except Exception as e:
161                raise error.general('Invalid version file: %s: %s' % (vc, e))
162            ver_split = ver_str.split('.')
163            if len(ver_split) < 2:
164                raise error.general('Invalid version release value: %s: %s' % (vc,
165                                                                               ver_str))
166            ver = ver_split[0]
167            rev = '.'.join(ver_split[1:])
168            try:
169                _version = int(ver)
170            except:
171                raise error.general('Invalid version config value: %s: %s' % (vc,
172                                                                              ver))
173            try:
174                _revision = int(''.join(itertools.takewhile(str.isdigit, rev)))
175            except Exception as e:
176                raise error.general('Invalid revision config value: %s: %s: %s' % (vc,
177                                                                                   rev,
178                                                                                   e))
179            if not 'not_released' in ver:
180                _released = True
181            _version_str = ver_str
182            _is_loaded = True
183    return _released
184
185def _load_git_version():
186    global _version
187    global _revision
188    global _git
189    global _version_str
190    repo = git.repo(_top())
191    if repo.valid():
192        head = repo.head()
193        if repo.dirty():
194            modified = 'modified'
195            sep = ' '
196        else:
197            modified = ''
198            sep = ''
199        _revision = '%s-%s' % (head[0:12], modified)
200        _version_str = '%s (%s%s%s)' % (_version, head[0:12], sep, modified)
201        _git = True
202    return _git
203
204def load_release_settings(section, error = False):
205    vc, v = _load_released_version_config()
206    items = []
207    if v is not None:
208        try:
209            items = v.items(section)
210        except Exception as e:
211            if not isinstance(error, bool):
212                error(e)
213            elif error:
214                raise error.general('Invalid config section: %s: %s: %s' % (vc,
215                                                                            section,
216                                                                            e))
217    return items
218
219def load_release_setting(section, option, raw = False, error = False):
220    vc, v = _load_released_version_config()
221    value = None
222    if v is not None:
223        try:
224            value = v.get(section, option, raw = raw)
225        except Exception as e:
226            if not isinstance(error, bool):
227                error(e)
228            elif error:
229                raise error.general('Invalid config section: %s: %s: %s.%s' % (vc,
230                                                                               section,
231                                                                               option,
232                                                                               e))
233    return value
234
235def released():
236    return _load_released_version()
237
238def version_control():
239    return _load_git_version()
240
241def string():
242    _load_released_version()
243    _load_git_version()
244    return _version_str
245
246def version():
247    _load_released_version()
248    _load_git_version()
249    return _version
250
251def revision():
252    _load_released_version()
253    _load_git_version()
254    return _revision
255
256if __name__ == '__main__':
257    print('Version: %s' % (str()))
Note: See TracBrowser for help on using the repository browser.