You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

version.py 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import datetime
  2. import functools
  3. import os
  4. import subprocess
  5. import sys
  6. from distutils.version import LooseVersion
  7. # Private, stable API for detecting the Python version. PYXY means "Python X.Y
  8. # or later". So that third-party apps can use these values, each constant
  9. # should remain as long as the oldest supported Django version supports that
  10. # Python version.
  11. PY36 = sys.version_info >= (3, 6)
  12. PY37 = sys.version_info >= (3, 7)
  13. PY38 = sys.version_info >= (3, 8)
  14. PY39 = sys.version_info >= (3, 9)
  15. def get_version(version=None):
  16. """Return a PEP 440-compliant version number from VERSION."""
  17. version = get_complete_version(version)
  18. # Now build the two parts of the version number:
  19. # main = X.Y[.Z]
  20. # sub = .devN - for pre-alpha releases
  21. # | {a|b|rc}N - for alpha, beta, and rc releases
  22. main = get_main_version(version)
  23. sub = ''
  24. if version[3] == 'alpha' and version[4] == 0:
  25. git_changeset = get_git_changeset()
  26. if git_changeset:
  27. sub = '.dev%s' % git_changeset
  28. elif version[3] != 'final':
  29. mapping = {'alpha': 'a', 'beta': 'b', 'rc': 'rc'}
  30. sub = mapping[version[3]] + str(version[4])
  31. return main + sub
  32. def get_main_version(version=None):
  33. """Return main version (X.Y[.Z]) from VERSION."""
  34. version = get_complete_version(version)
  35. parts = 2 if version[2] == 0 else 3
  36. return '.'.join(str(x) for x in version[:parts])
  37. def get_complete_version(version=None):
  38. """
  39. Return a tuple of the django version. If version argument is non-empty,
  40. check for correctness of the tuple provided.
  41. """
  42. if version is None:
  43. from django import VERSION as version
  44. else:
  45. assert len(version) == 5
  46. assert version[3] in ('alpha', 'beta', 'rc', 'final')
  47. return version
  48. def get_docs_version(version=None):
  49. version = get_complete_version(version)
  50. if version[3] != 'final':
  51. return 'dev'
  52. else:
  53. return '%d.%d' % version[:2]
  54. @functools.lru_cache()
  55. def get_git_changeset():
  56. """Return a numeric identifier of the latest git changeset.
  57. The result is the UTC timestamp of the changeset in YYYYMMDDHHMMSS format.
  58. This value isn't guaranteed to be unique, but collisions are very unlikely,
  59. so it's sufficient for generating the development version numbers.
  60. """
  61. repo_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  62. git_log = subprocess.Popen(
  63. 'git log --pretty=format:%ct --quiet -1 HEAD',
  64. stdout=subprocess.PIPE, stderr=subprocess.PIPE,
  65. shell=True, cwd=repo_dir, universal_newlines=True,
  66. )
  67. timestamp = git_log.communicate()[0]
  68. try:
  69. timestamp = datetime.datetime.utcfromtimestamp(int(timestamp))
  70. except ValueError:
  71. return None
  72. return timestamp.strftime('%Y%m%d%H%M%S')
  73. def get_version_tuple(version):
  74. """
  75. Return a tuple of version numbers (e.g. (1, 2, 3)) from the version
  76. string (e.g. '1.2.3').
  77. """
  78. loose_version = LooseVersion(version)
  79. version_numbers = []
  80. for item in loose_version.version:
  81. if not isinstance(item, int):
  82. break
  83. version_numbers.append(item)
  84. return tuple(version_numbers)