-
Notifications
You must be signed in to change notification settings - Fork 6
/
getFudgeVersion.py
85 lines (60 loc) · 3.1 KB
/
getFudgeVersion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# <<BEGIN-copyright>>
# Copyright 2022, Lawrence Livermore National Security, LLC.
# See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: BSD-3-Clause
# <<END-copyright>>
import re
import os
import sys
import subprocess
versionOutputFile = os.path.join('fudge', 'fudgeVersion.py')
def runShellCommand(_shellCommand, returnSTDOUT=False):
process = subprocess.Popen(_shellCommand, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = [output.decode('utf-8') for output in process.communicate()]
if process.returncode and stderr:
print(stderr)
if returnSTDOUT:
return stdout
def defaultVersionNumber():
if not os.path.isfile(versionOutputFile):
output = ['FUDGE_MAJORVERSION = 6', 'FUDGE_MINORVERSION = 7', 'FUDGE_RELEASECANDIDATE = \'\'', 'FUDGE_POSTRELEASE = \'\'', 'FUDGE_REPOIDENTIFIER = \'\'']
with open(versionOutputFile, 'w') as fileObject:
fileObject.write('\n'.join(output))
def getVersionNumber():
if os.path.isdir('.git'):
remoteURL = runShellCommand(['git', 'config', '--get', 'remote.origin.url'], True).split('\n')[0]
if 'zgitlab.llnl.gov' in remoteURL:
gitDescribe = runShellCommand(['git', 'describe', '--abbrev=40', '--match', 'fudge[0-9].[0-9]*'], True)
# see https://www.python.org/dev/peps/pep-0440/
regexGitDescribe = re.compile(r'^fudge(?P<major>\d+).(?P<minor>\d+)(?:-(?P<releaseCondidate>rc\d+)(-(?P<postRelease>\d+)-(?P<repoIdentifier>.+))?)?$')
regexMatch = regexGitDescribe.match(gitDescribe)
if regexMatch:
regexMatch = regexMatch.groupdict()
if not regexMatch["repoIdentifier"]:
gitDescribe = runShellCommand(['git', 'rev-parse', 'HEAD'], True)
repoIdentifier = f'g{gitDescribe[:8]}'
else:
repoIdentifier = regexMatch["repoIdentifier"]
output = [f'FUDGE_MAJORVERSION = {regexMatch["major"]}',
f'FUDGE_MINORVERSION = {regexMatch["minor"]}',
f'FUDGE_RELEASECANDIDATE = \'{regexMatch["releaseCondidate"] if regexMatch.get("releaseCondidate") else ""}\'',
f'FUDGE_POSTRELEASE = \'{regexMatch["postRelease"] if regexMatch.get("postRelease") else ""}\'',
f'FUDGE_REPOIDENTIFIER = \'{repoIdentifier}\'']
with open(versionOutputFile, 'w') as fileObject:
fileObject.write('\n'.join(output))
else:
defaultVersionNumber()
else:
defaultVersionNumber()
else:
defaultVersionNumber()
assert os.path.isfile(versionOutputFile)
sys.path.append('fudge')
import fudgeVersion as versionModule
versionString = f'{versionModule.FUDGE_MAJORVERSION}.{versionModule.FUDGE_MINORVERSION}'
if versionModule.FUDGE_RELEASECANDIDATE != '':
versionString += f'{versionModule.FUDGE_RELEASECANDIDATE}post{versionModule.FUDGE_POSTRELEASE}'
return versionString
if __name__ == '__main__':
getVersionNumber()