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 2.4 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. from __future__ import unicode_literals
  2. import datetime
  3. import os
  4. import subprocess
  5. from django.utils.lru_cache import lru_cache
  6. def get_version(version=None):
  7. "Returns a PEP 386-compliant version number from VERSION."
  8. version = get_complete_version(version)
  9. # Now build the two parts of the version number:
  10. # main = X.Y[.Z]
  11. # sub = .devN - for pre-alpha releases
  12. # | {a|b|c}N - for alpha, beta and rc releases
  13. main = get_main_version(version)
  14. sub = ''
  15. if version[3] == 'alpha' and version[4] == 0:
  16. git_changeset = get_git_changeset()
  17. if git_changeset:
  18. sub = '.dev%s' % git_changeset
  19. elif version[3] != 'final':
  20. mapping = {'alpha': 'a', 'beta': 'b', 'rc': 'c'}
  21. sub = mapping[version[3]] + str(version[4])
  22. return str(main + sub)
  23. def get_main_version(version=None):
  24. "Returns main version (X.Y[.Z]) from VERSION."
  25. version = get_complete_version(version)
  26. parts = 2 if version[2] == 0 else 3
  27. return '.'.join(str(x) for x in version[:parts])
  28. def get_complete_version(version=None):
  29. """Returns a tuple of the django version. If version argument is non-empty,
  30. then checks for correctness of the tuple provided.
  31. """
  32. if version is None:
  33. from django import VERSION as version
  34. else:
  35. assert len(version) == 5
  36. assert version[3] in ('alpha', 'beta', 'rc', 'final')
  37. return version
  38. def get_docs_version(version=None):
  39. version = get_complete_version(version)
  40. if version[3] != 'final':
  41. return 'dev'
  42. else:
  43. return '%d.%d' % version[:2]
  44. @lru_cache()
  45. def get_git_changeset():
  46. """Returns a numeric identifier of the latest git changeset.
  47. The result is the UTC timestamp of the changeset in YYYYMMDDHHMMSS format.
  48. This value isn't guaranteed to be unique, but collisions are very unlikely,
  49. so it's sufficient for generating the development version numbers.
  50. """
  51. repo_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  52. git_log = subprocess.Popen('git log --pretty=format:%ct --quiet -1 HEAD',
  53. stdout=subprocess.PIPE, stderr=subprocess.PIPE,
  54. shell=True, cwd=repo_dir, universal_newlines=True)
  55. timestamp = git_log.communicate()[0]
  56. try:
  57. timestamp = datetime.datetime.utcfromtimestamp(int(timestamp))
  58. except ValueError:
  59. return None
  60. return timestamp.strftime('%Y%m%d%H%M%S')