version.py 3.6 KB

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