version.py 3.1 KB

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