versionstring.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Copyright 2016 OpenMarket Ltd
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import logging
  17. import os
  18. import subprocess
  19. logger = logging.getLogger(__name__)
  20. def get_version_string(module):
  21. """Given a module calculate a git-aware version string for it.
  22. If called on a module not in a git checkout will return `__verison__`.
  23. Args:
  24. module (module)
  25. Returns:
  26. str
  27. """
  28. cached_version = getattr(module, "_synapse_version_string_cache", None)
  29. if cached_version:
  30. return cached_version
  31. version_string = module.__version__
  32. try:
  33. null = open(os.devnull, "w")
  34. cwd = os.path.dirname(os.path.abspath(module.__file__))
  35. try:
  36. git_branch = (
  37. subprocess.check_output(
  38. ["git", "rev-parse", "--abbrev-ref", "HEAD"], stderr=null, cwd=cwd
  39. )
  40. .strip()
  41. .decode("ascii")
  42. )
  43. git_branch = "b=" + git_branch
  44. except (subprocess.CalledProcessError, FileNotFoundError):
  45. # FileNotFoundError can arise when git is not installed
  46. git_branch = ""
  47. try:
  48. git_tag = (
  49. subprocess.check_output(
  50. ["git", "describe", "--exact-match"], stderr=null, cwd=cwd
  51. )
  52. .strip()
  53. .decode("ascii")
  54. )
  55. git_tag = "t=" + git_tag
  56. except (subprocess.CalledProcessError, FileNotFoundError):
  57. git_tag = ""
  58. try:
  59. git_commit = (
  60. subprocess.check_output(
  61. ["git", "rev-parse", "--short", "HEAD"], stderr=null, cwd=cwd
  62. )
  63. .strip()
  64. .decode("ascii")
  65. )
  66. except (subprocess.CalledProcessError, FileNotFoundError):
  67. git_commit = ""
  68. try:
  69. dirty_string = "-this_is_a_dirty_checkout"
  70. is_dirty = (
  71. subprocess.check_output(
  72. ["git", "describe", "--dirty=" + dirty_string], stderr=null, cwd=cwd
  73. )
  74. .strip()
  75. .decode("ascii")
  76. .endswith(dirty_string)
  77. )
  78. git_dirty = "dirty" if is_dirty else ""
  79. except (subprocess.CalledProcessError, FileNotFoundError):
  80. git_dirty = ""
  81. if git_branch or git_tag or git_commit or git_dirty:
  82. git_version = ",".join(
  83. s for s in (git_branch, git_tag, git_commit, git_dirty) if s
  84. )
  85. version_string = "%s (%s)" % (module.__version__, git_version)
  86. except Exception as e:
  87. logger.info("Failed to check for git repository: %s", e)
  88. module._synapse_version_string_cache = version_string
  89. return version_string