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