versionstring.py 3.3 KB

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