source: rtems-source-builder/source-builder/sb/version.py @ cfed165

Last change on this file since cfed165 was cfed165, checked in by Chris Johns <chrisj@…>, on 09/29/22 at 08:57:16

sb/version: Set top from external package

  • Property mode set to 100644
File size: 8.0 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# Notes:
69#
70#  This module uses os.apth for paths and assumes all paths are in the host
71#  format.
72#
73
74from __future__ import print_function
75
76import os
77import sys
78
79from . import error
80from . import git
81from . import path
82
83#
84# Default to an internal string.
85#
86_version = '6'
87_revision = 'not_released'
88_version_str = '%s.%s' % (_version, _revision)
89_released = False
90_git = False
91_is_loaded = False
92_top_dir = None
93
94def _top():
95    if _top_dir is None:
96        top = path.dirname(sys.argv[0])
97    else:
98        top = _top_dir
99    if len(top) == 0:
100        top = '.'
101    return top
102
103def _load_released_version_config():
104    '''Local worker to load a configuration file.'''
105    top = _top()
106    for ver in [path.join(top, 'VERSION'),
107                path.join('..', 'VERSION')]:
108        if path.exists(path.join(ver)):
109            try:
110                import configparser
111            except ImportError:
112                import ConfigParser as configparser
113            v = configparser.SafeConfigParser()
114            try:
115                v.read(path.host(ver))
116            except Exception as e:
117                raise error.general('Invalid version config format: %s: %s' % (ver,
118                                                                               e))
119            return ver, v
120    return None, None
121
122def _load_released_version():
123    '''Load the release data if present. If not found the package is not released.
124
125    A release can be made by adding a file called `VERSION` to the top level
126    directory of a package. This is useful for user deploying a package and
127    making custom releases.
128
129    The RTEMS project reserves the `rtems-version.ini` file for it's
130    releases. This is the base release and should not be touched by users
131    deploying a package.
132
133    '''
134    global _version
135    global _revision
136    global _released
137    global _version_str
138    global _is_loaded
139
140    if not _is_loaded:
141        vc, v = _load_released_version_config()
142        if v is not None:
143            try:
144                ver_str = v.get('version', 'revision')
145            except Exception as e:
146                raise error.general('Invalid version file: %s: %s' % (vc, e))
147            ver_split = ver_str.split('.', 1)
148            if len(ver_split) < 2:
149                raise error.general('Invalid version release value: %s: %s' % (vc,
150                                                                               ver_str))
151            ver = ver_split[0]
152            rev = ver_split[1]
153            try:
154                _version = int(ver)
155            except:
156                raise error.general('Invalid version config value: %s: %s' % (vc,
157                                                                              ver))
158            _revision = rev
159            if 'not_released' not in ver:
160                _released = True
161            _version_str = ver_str
162            _is_loaded = True
163    return _released
164
165def _load_git_version():
166    global _version
167    global _revision
168    global _git
169    global _version_str
170    global _is_loaded
171
172    if not _is_loaded:
173        repo = git.repo(_top())
174        if repo.valid():
175            head = repo.head()
176            if repo.dirty():
177                modified = 'modified'
178                revision_sep = '-'
179                sep = ' '
180            else:
181                modified = ''
182                revision_sep = ''
183                sep = ''
184            _revision = '%s%s%s' % (head[0:12], revision_sep, modified)
185            _version_str = '%s (%s%s%s)' % (_version, head[0:12], sep, modified)
186            _git = True
187            _is_loaded = True
188    return _git
189
190def set_top(top):
191    global _top_dir
192    _top_dir = top
193
194def load_release_settings(section, error = False):
195    vc, v = _load_released_version_config()
196    items = []
197    if v is not None:
198        try:
199            items = v.items(section)
200        except Exception as e:
201            if not isinstance(error, bool):
202                error(e)
203            elif error:
204                raise error.general('Invalid config section: %s: %s: %s' % (vc,
205                                                                            section,
206                                                                            e))
207    return items
208
209def load_release_setting(section, option, raw = False, error = False):
210    vc, v = _load_released_version_config()
211    value = None
212    if v is not None:
213        try:
214            value = v.get(section, option, raw = raw)
215        except Exception as e:
216            if not isinstance(error, bool):
217                error(e)
218            elif error:
219                raise error.general('Invalid config section: %s: %s: %s.%s' % (vc,
220                                                                               section,
221                                                                               option,
222                                                                               e))
223    return value
224
225def released():
226    return _load_released_version()
227
228def version_control():
229    return _load_git_version()
230
231def string():
232    _load_released_version()
233    _load_git_version()
234    return _version_str
235
236def version():
237    _load_released_version()
238    _load_git_version()
239    return _version
240
241def revision():
242    _load_released_version()
243    _load_git_version()
244    return _revision
245
246if __name__ == '__main__':
247    print('Version: %s' % (str(version())))
248    print('Revision: %s' % (str(revision())))
249    print('String: %s' % (string()))
250    if version() == 'undefined':
251        raise Exception('version is undefined')
Note: See TracBrowser for help on using the repository browser.