version.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # Contains the openMSX version number and versioning related functions.
  2. from executils import captureStdout
  3. from makeutils import filterLines
  4. from io import open
  5. from os import makedirs
  6. from os.path import isdir
  7. import re
  8. # Name used for packaging.
  9. packageName = 'openmsx'
  10. # Version number.
  11. packageVersionNumber = '0.15.0'
  12. # Version code for Android must be an incremental number
  13. # Increase this number for each release build. For a dev build, the
  14. # version number is based on the git commit count but for a release
  15. # build, it must be hardcoded
  16. androidReleaseVersionCode=10
  17. # Note: suffix should be empty or with dash, like "-rc1" or "-test1"
  18. packageVersionSuffix = ''
  19. packageVersion = packageVersionNumber + packageVersionSuffix
  20. # Is this a release version ("True") or development version ("False").
  21. releaseFlag = False
  22. def _extractRevisionFromStdout(log, command, regex):
  23. text = captureStdout(log, command)
  24. if text is None:
  25. # Error logging already done by captureStdout().
  26. return None
  27. # pylint 0.18.0 somehow thinks captureStdout() returns a list, not a string.
  28. lines = text.split('\n') # pylint: disable-msg=E1103
  29. for revision, in filterLines(lines, regex):
  30. print('Revision number found by "%s": %s' % (command, revision), file=log)
  31. return revision
  32. else:
  33. print('Revision number not found in "%s" output:' % command, file=log)
  34. print(str(text), file=log)
  35. return None
  36. def extractGitRevision(log):
  37. return _extractRevisionFromStdout(
  38. log, 'git describe --dirty', r'\S+?-(\S+)$'
  39. )
  40. def extractNumberFromGitRevision(revisionStr):
  41. if revisionStr is None:
  42. return None
  43. if revisionStr == 'dirty':
  44. return None
  45. return re.match(r'(\d+)+', revisionStr).group(0)
  46. _cachedRevision = False # because None is a valid result
  47. def extractRevision():
  48. global _cachedRevision
  49. if _cachedRevision is not False:
  50. return _cachedRevision
  51. if releaseFlag:
  52. # Not necessary, we do not append revision for a release build.
  53. return None
  54. if not isdir('derived'):
  55. makedirs('derived')
  56. with open('derived/version.log', 'w', encoding='utf-8') as log:
  57. print('Extracting revision info...', file=log)
  58. revision = extractGitRevision(log)
  59. print('Revision string: %s' % revision, file=log)
  60. revisionNumber = extractNumberFromGitRevision(revision)
  61. print('Revision number: %s' % revisionNumber, file=log)
  62. _cachedRevision = revision
  63. return revision
  64. def extractRevisionNumber():
  65. return int(extractNumberFromGitRevision(extractRevision()) or 1)
  66. def extractRevisionString():
  67. return extractRevision() or 'unknown'
  68. def getVersionedPackageName():
  69. if releaseFlag:
  70. return '%s-%s' % (packageName, packageVersion)
  71. else:
  72. return '%s-%s-%s' % (
  73. packageName, packageVersion, extractRevisionString()
  74. )
  75. def countGitCommits():
  76. if not isdir('derived'):
  77. makedirs('derived')
  78. with open('derived/commitCountVersion.log', 'w', encoding='utf-8') as log:
  79. print('Extracting commit count...', file=log)
  80. commitCount = captureStdout(log, 'git rev-list HEAD --count')
  81. print('Commit count: %s' % commitCount, file=log)
  82. return commitCount
  83. def getAndroidVersionCode():
  84. if releaseFlag:
  85. return '%s' % (androidReleaseVersionCode)
  86. else:
  87. return '%s' % ( countGitCommits() )
  88. if __name__ == '__main__':
  89. print(packageVersionNumber)