OXYGENHUB
◆ main openmsx / build / version.py
4.1 KiB · Python · last changed chore(): initial commit 1 month ago source blame history raw
1
# Contains the openMSX version number and versioning related functions.
2
3
from executils import captureStdout
4
from makeutils import filterLines
5
6
from io import open
7
from os import makedirs
8
from os.path import isdir
9
import re
10
11
# Name used for packaging.
12
packageName = 'openmsx'
13
14
# Version number.
15
packageVersionNumber = '21.0'
16
17
# Version code for Android must be an incremental number
18
# Increase this number for each release build. For a dev build, the
19
# version number is based on the git commit count but for a release
20
# build, it must be hardcoded
21
androidReleaseVersionCode=21
22
23
# Note: suffix should be empty or with dash, like "-rc2" or "-test1"
24
packageVersionSuffix = ''
25
packageVersion = packageVersionNumber + packageVersionSuffix
26
27
# Is this a release version ("True") or development version ("False").
28
releaseFlag = False
29
30
# Release year for copyright message. Make sure to update it when releasing.
31
# (Or just when a new year arrives?)
32
# Automatically taking current year doesn't seem to be correct.
33
releaseYear = '2025'
34
35
def _extractRevisionFromStdout(log, command, regex):
36
	text = captureStdout(log, command)
37
	if text is None:
38
		# Error logging already done by captureStdout().
39
		return None
40
	# pylint 0.18.0 somehow thinks captureStdout() returns a list, not a string.
41
	lines = text.split('\n') # pylint: disable-msg=E1103
42
	for revision, in filterLines(lines, regex):
43
		print('Revision number found by "%s": %s' % (command, revision), file=log)
44
		return revision
45
	else:
46
		print('Revision number not found in "%s" output:' % command, file=log)
47
		print(str(text), file=log)
48
		return None
49
50
def extractGitRevision(log):
51
	return _extractRevisionFromStdout(
52
		log, 'git describe --dirty', r'\S+?-(\S+)$'
53
		)
54
55
def extractNumberFromGitRevision(revisionStr):
56
	if revisionStr is None:
57
		return None
58
	if revisionStr == 'dirty':
59
		return None
60
	return re.match(r'(\d+)+', revisionStr).group(0)
61
62
_cachedRevision = False # because None is a valid result
63
64
def extractRevision():
65
	global _cachedRevision
66
	if _cachedRevision is not False:
67
		return _cachedRevision
68
	if releaseFlag:
69
		# Not necessary, we do not append revision for a release build.
70
		return None
71
	if not isdir('derived'):
72
		makedirs('derived')
73
	with open('derived/version.log', 'w', encoding='utf-8') as log:
74
		print('Extracting revision info...', file=log)
75
		revision = extractGitRevision(log)
76
		print('Revision string: %s' % revision, file=log)
77
		revisionNumber = extractNumberFromGitRevision(revision)
78
		print('Revision number: %s' % revisionNumber, file=log)
79
	_cachedRevision = revision
80
	return revision
81
82
def extractRevisionNumber():
83
	return int(extractNumberFromGitRevision(extractRevision()) or 0)
84
85
def extractRevisionString():
86
	return extractRevision() or 'unknown'
87
88
def getVersionTripleString():
89
	"""Version in "x.y.z" format."""
90
	return '%s.%d' % (packageVersionNumber, extractRevisionNumber())
91
92
def getDetailedVersion():
93
	if releaseFlag:
94
		return packageVersion
95
	else:
96
		return '%s-%s' % (packageVersion, extractRevisionString())
97
98
def getVersionedPackageName():
99
	return '%s-%s' % (packageName, getDetailedVersion())
100
101
def countGitCommits():
102
	if not isdir('derived'):
103
		makedirs('derived')
104
	with open('derived/commitCountVersion.log', 'w', encoding='utf-8') as log:
105
		print('Extracting commit count...', file=log)
106
		commitCount = captureStdout(log, 'git rev-list HEAD --count')
107
		print('Commit count: %s' % commitCount, file=log)
108
	return commitCount
109
110
def getAndroidVersionCode():
111
	if releaseFlag:
112
		return '%s' % (androidReleaseVersionCode)
113
	else:
114
		return '%s' % ( countGitCommits() )
115
116
def getCopyright():
117
	return f"Copyright 2001-{releaseYear}"
118
119
formatMap = dict(
120
	main=lambda: packageVersionNumber,
121
	plain=lambda: packageVersion,
122
	triple=getVersionTripleString,
123
	detailed=getDetailedVersion,
124
	)
125
126
if __name__ == '__main__':
127
	import sys
128
	badFormat = False
129
	for fmt in sys.argv[1:] or ['detailed']:
130
		try:
131
			formatter = formatMap[fmt]
132
		except KeyError:
133
			print('Unknown version format "%s"' % fmt, file=sys.stderr)
134
			badFormat = True
135
		else:
136
			print(formatter())
137
	if badFormat:
138
		print('Supported version formats:', ', '.join(sorted(formatMap.keys())),
139
			file=sys.stderr)
140
		sys.exit(2)