version.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # Copyright (c) Django Software Foundation and individual contributors.
  2. # All rights reserved.
  3. #
  4. # Redistribution and use in source and binary forms, with or without
  5. # modification, are permitted provided that the following conditions are met:
  6. #
  7. # 1. Redistributions of source code must retain the above copyright notice,
  8. # this list of conditions and the following disclaimer.
  9. #
  10. # 2. Redistributions in binary form must reproduce the above copyright
  11. # notice, this list of conditions and the following disclaimer in the
  12. # documentation and/or other materials provided with the distribution.
  13. #
  14. # 3. Neither the name of Django nor the names of its contributors may be
  15. # used to endorse or promote products derived from this software without
  16. # specific prior written permission.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  22. # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. # POSSIBILITY OF SUCH DAMAGE.
  29. #
  30. # The following code was copied from the Django project, and only lightly
  31. # modified. Please adhere to the above copyright and license for the code
  32. # in this file.
  33. # Note: Nothing is covered here because this file is imported before nose and
  34. # coverage take over.. and so its a false positive that nothing is covered.
  35. import datetime # pragma: nocover
  36. import os # pragma: nocover
  37. import subprocess # pragma: nocover
  38. from ..core.backend import VERSION # pragma: nocover
  39. def get_version(version=VERSION): # pragma: nocover
  40. "Returns a PEP 386-compliant version number from VERSION."
  41. assert len(version) == 5
  42. assert version[3] in ('alpha', 'beta', 'rc', 'final')
  43. # Now build the two parts of the version number:
  44. # main = X.Y[.Z]
  45. # sub = .devN - for pre-alpha releases
  46. # | {a|b|c}N - for alpha, beta and rc releases
  47. # We want to explicitly include all three version/release numbers
  48. # parts = 2 if version[2] == 0 else 3
  49. parts = 3
  50. main = '.'.join(str(x) for x in version[:parts])
  51. sub = ''
  52. if version[3] == 'alpha' and version[4] == 0:
  53. git_changeset = get_git_changeset()
  54. if git_changeset:
  55. sub = '.dev%s' % git_changeset
  56. elif version[3] != 'final':
  57. mapping = {'alpha': 'a', 'beta': 'b', 'rc': 'c'}
  58. sub = mapping[version[3]] + str(version[4])
  59. return main + sub
  60. def get_git_changeset(): # pragma: nocover
  61. """Returns a numeric identifier of the latest git changeset.
  62. The result is the UTC timestamp of the changeset in YYYYMMDDHHMMSS format.
  63. This value isn't guaranteed to be unique, but collisions are very
  64. unlikely, so it's sufficient for generating the development version
  65. numbers.
  66. """
  67. repo_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  68. git_log = subprocess.Popen('git log --pretty=format:%ct --quiet -1 HEAD',
  69. stdout=subprocess.PIPE, stderr=subprocess.PIPE,
  70. shell=True, cwd=repo_dir,
  71. universal_newlines=True)
  72. timestamp = git_log.communicate()[0]
  73. try:
  74. timestamp = datetime.datetime.utcfromtimestamp(int(timestamp))
  75. except ValueError: # pragma: nocover
  76. return None # pragma: nocover
  77. return timestamp.strftime('%Y%m%d%H%M%S')