|
14 | 14 | # See the License for the specific language governing permissions and |
15 | 15 | # limitations under the License. |
16 | 16 |
|
| 17 | +import argparse |
| 18 | +import os |
| 19 | +import pathlib |
17 | 20 | import subprocess |
18 | | -import sys |
19 | | -import warnings |
20 | | - |
21 | | -count = 0 |
22 | | -commit = '' |
23 | | -dirty = '' |
24 | | - |
25 | | -try : |
26 | | - output = subprocess.check_output(['git', 'describe', '--dirty']) |
27 | | - (version, *rest) = output.decode('utf-8').strip().split('-') |
28 | | - (major, minor, patch) = version.strip('v').split('.') |
29 | | - |
30 | | - # first case: this is a dirty tagged release, only dirty flag |
31 | | - if len(rest) == 1 : |
32 | | - assert rest[0] == 'dirty' |
33 | | - dirty = 'dirty' |
34 | | - # second case: this is a committed post tag release |
35 | | - elif len(rest) == 2 : |
36 | | - count = rest[0] |
37 | | - commit = rest[1] |
38 | | - # third case: this is a dirty, committed post tag release |
39 | | - elif len(rest) == 3 : |
40 | | - assert rest[2] == 'dirty' |
41 | | - count = rest[0] |
42 | | - commit = rest[1] |
43 | | - dirty = rest[2] |
44 | | - |
45 | | - print('{}.{}.{}'.format(major, minor, count)) |
46 | | -except Exception as e : |
47 | | - warnings.warn('failed to compute version, using default') |
48 | | - print('0.0.0') |
| 21 | + |
| 22 | +pdo_source_root=pathlib.Path(__file__).parent.parent |
| 23 | +version_file = pdo_source_root / 'VERSION' |
| 24 | + |
| 25 | +parser = argparse.ArgumentParser() |
| 26 | + |
| 27 | +parser.add_argument( |
| 28 | + '--version-file', '-f', |
| 29 | + help=f'File where version information is stored (default: {version_file})', |
| 30 | + type=str) |
| 31 | + |
| 32 | +options = parser.parse_args() |
| 33 | + |
| 34 | +if options.version_file : |
| 35 | + version_file = pathlib.Path(options.version_file) |
| 36 | + pdo_source_root = version_file.parent |
| 37 | + |
| 38 | +# the version file is a tab separated list of version numbers and git commit hashes in reverse |
| 39 | +# order (newest is at the top of the file) |
| 40 | +with open(version_file, 'r') as vf : |
| 41 | + (version, commit, *rest) = vf.readline().strip().split('\t') |
| 42 | + |
| 43 | +# the version is of the form x.y.z, there may be an optional 'v' at the beginning of the version |
| 44 | +# string |
| 45 | +(major, minor, patch) = version.strip('v').split('.') |
| 46 | + |
| 47 | +# compute the number of commits since the tagged version was |
| 48 | +# committed to the repository |
| 49 | +command = ['git', 'rev-list', commit + '...HEAD', '--count'] |
| 50 | +output = subprocess.run(command, cwd=pdo_source_root, capture_output=True, text=True) |
| 51 | +count = output.stdout.strip() |
| 52 | + |
| 53 | +# the actual patch version number is the recorded patch number added to the number of commits |
| 54 | +# since the version was committed |
| 55 | +print('{}.{}.{}'.format(major, minor, int(patch) + int(count))) |
0 commit comments