summary refs log tree commit diff
path: root/synapse/util/versionstring.py
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2019-07-22 13:15:08 +0100
committerErik Johnston <erik@matrix.org>2019-07-22 13:15:08 +0100
commit5ea773c50568088b30e15728b65480d0335fe14e (patch)
treefe446b85e05d4eee6fabce0a5f12988014f29e90 /synapse/util/versionstring.py
parentDemo uses deprecated cli option (#5725) (diff)
downloadsynapse-5ea773c50568088b30e15728b65480d0335fe14e.tar.xz
Cache get_version_string.
The version of a module isn't going to change over the lifetime of the
process (assuming no funky hot reloading is going on, which it isn't),
so let's just cache the result to avoid spawning lots of git
subprocesses.

Fixes #5672.
Diffstat (limited to 'synapse/util/versionstring.py')
-rw-r--r--synapse/util/versionstring.py23
1 files changed, 21 insertions, 2 deletions
diff --git a/synapse/util/versionstring.py b/synapse/util/versionstring.py
index a4d9a462f7..fa404b9d75 100644
--- a/synapse/util/versionstring.py
+++ b/synapse/util/versionstring.py
@@ -22,6 +22,23 @@ logger = logging.getLogger(__name__)
 
 
 def get_version_string(module):
+    """Given a module calculate a git-aware version string for it.
+
+    If called on a module not in a git checkout will return `__verison__`.
+
+    Args:
+        module (module)
+
+    Returns:
+        str
+    """
+
+    cached_version = getattr(module, "_synapse_version_string_cache", None)
+    if cached_version:
+        return cached_version
+
+    version_string = module.__version__
+
     try:
         null = open(os.devnull, "w")
         cwd = os.path.dirname(os.path.abspath(module.__file__))
@@ -80,8 +97,10 @@ def get_version_string(module):
                 s for s in (git_branch, git_tag, git_commit, git_dirty) if s
             )
 
-            return "%s (%s)" % (module.__version__, git_version)
+            version_string = "%s (%s)" % (module.__version__, git_version)
     except Exception as e:
         logger.info("Failed to check for git repository: %s", e)
 
-    return module.__version__
+    module._synapse_version_string_cache = version_string
+
+    return version_string