| 1 |
|
| 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 |
|
| 12 |
packageName = 'openmsx' |
| 13 |
|
| 14 |
|
| 15 |
packageVersionNumber = '21.0' |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
androidReleaseVersionCode=21 |
| 22 |
|
| 23 |
|
| 24 |
packageVersionSuffix = '' |
| 25 |
packageVersion = packageVersionNumber + packageVersionSuffix |
| 26 |
|
| 27 |
|
| 28 |
releaseFlag = False |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
releaseYear = '2025' |
| 34 |
|
| 35 |
def _extractRevisionFromStdout(log, command, regex): |
| 36 |
text = captureStdout(log, command) |
| 37 |
if text is None: |
| 38 |
|
| 39 |
return None |
| 40 |
|
| 41 |
lines = text.split('\n') |
| 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 |
| 63 |
|
| 64 |
def extractRevision(): |
| 65 |
global _cachedRevision |
| 66 |
if _cachedRevision is not False: |
| 67 |
return _cachedRevision |
| 68 |
if releaseFlag: |
| 69 |
|
| 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 |
|
| 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) |