version.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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-debugger'
  9. # Version number.
  10. packageVersionNumber = '0.10.0'
  11. # Note: suffix should be empty or with dash, like "-rc1" or "-test1"
  12. packageVersionSuffix = ''
  13. packageVersion = packageVersionNumber + packageVersionSuffix
  14. # Is this a release version ("True") or development version ("False").
  15. releaseFlag = False
  16. def _extractRevisionFromStdout(log, command, regex):
  17. text = captureStdout(log, command)
  18. if text is None:
  19. # Error logging already done by captureStdout().
  20. return None
  21. # pylint 0.18.0 somehow thinks captureStdout() returns a list, not a string.
  22. lines = text.split('\n') # pylint: disable-msg=E1103
  23. for revision, in filterLines(lines, regex):
  24. print >> log, 'Revision number found by "%s": %s' % (command, revision)
  25. return revision
  26. else:
  27. print >> log, 'Revision number not found in "%s" output:' % command
  28. print >> log, text
  29. return None
  30. def extractGitRevision(log):
  31. return _extractRevisionFromStdout(
  32. log, 'git describe --dirty', r'\S+?-(\S+)$'
  33. )
  34. def extractNumberFromGitRevision(revisionStr):
  35. if revisionStr is None:
  36. return None
  37. return re.match(r'(\d+)+', revisionStr).group(0)
  38. _cachedRevision = False # because None is a valid result
  39. def extractRevision():
  40. global _cachedRevision
  41. if _cachedRevision is not False:
  42. return _cachedRevision
  43. if releaseFlag:
  44. # Not necessary, we do not append revision for a release build.
  45. return None
  46. if not isdir('derived'):
  47. makedirs('derived')
  48. log = open('derived/version.log', 'w')
  49. print >> log, 'Extracting revision info...'
  50. try:
  51. revision = extractGitRevision(log)
  52. print >> log, 'Revision string: %s' % revision
  53. print >> log, 'Revision number: %s' % extractNumberFromGitRevision(revision)
  54. finally:
  55. log.close()
  56. _cachedRevision = revision
  57. return revision
  58. def extractRevisionNumber():
  59. return int(extractNumberFromGitRevision(extractRevision()) or 1)
  60. def extractRevisionString():
  61. return extractRevision() or 'unknown'
  62. def getVersionedPackageName():
  63. if releaseFlag:
  64. return '%s-%s' % (packageName, packageVersion)
  65. else:
  66. return '%s-%s-%s' % (
  67. packageName, packageVersion, extractRevisionString()
  68. )