Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 7b62791

Browse files
authoredDec 1, 2021
Clean-up get_version_string (#11468)
1 parent 153194c commit 7b62791

File tree

2 files changed

+26
-57
lines changed

2 files changed

+26
-57
lines changed
 

‎changelog.d/11468.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Refactor `get_version_string` to fix-up types and duplicated code.

‎synapse/util/versionstring.py

+25-57
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Copyright 2016 OpenMarket Ltd
2+
# Copyright 2021 The Matrix.org Foundation C.I.C.
23
#
34
# Licensed under the Apache License, Version 2.0 (the "License");
45
# you may not use this file except in compliance with the License.
@@ -29,10 +30,11 @@ def get_version_string(module: ModuleType) -> str:
2930
If called on a module not in a git checkout will return `__version__`.
3031
3132
Args:
32-
module (module)
33+
module: The module to check the version of. Must declare a __version__
34+
attribute.
3335
3436
Returns:
35-
str
37+
The module version (as a string).
3638
"""
3739

3840
cached_version = version_cache.get(module)
@@ -44,71 +46,37 @@ def get_version_string(module: ModuleType) -> str:
4446
version_string = module.__version__ # type: ignore[attr-defined]
4547

4648
try:
47-
null = open(os.devnull, "w")
4849
cwd = os.path.dirname(os.path.abspath(module.__file__))
4950

50-
try:
51-
git_branch = (
52-
subprocess.check_output(
53-
["git", "rev-parse", "--abbrev-ref", "HEAD"], stderr=null, cwd=cwd
51+
def _run_git_command(prefix: str, *params: str) -> str:
52+
try:
53+
result = (
54+
subprocess.check_output(
55+
["git", *params], stderr=subprocess.DEVNULL, cwd=cwd
56+
)
57+
.strip()
58+
.decode("ascii")
5459
)
55-
.strip()
56-
.decode("ascii")
57-
)
58-
git_branch = "b=" + git_branch
59-
except (subprocess.CalledProcessError, FileNotFoundError):
60-
# FileNotFoundError can arise when git is not installed
61-
git_branch = ""
62-
63-
try:
64-
git_tag = (
65-
subprocess.check_output(
66-
["git", "describe", "--exact-match"], stderr=null, cwd=cwd
67-
)
68-
.strip()
69-
.decode("ascii")
70-
)
71-
git_tag = "t=" + git_tag
72-
except (subprocess.CalledProcessError, FileNotFoundError):
73-
git_tag = ""
74-
75-
try:
76-
git_commit = (
77-
subprocess.check_output(
78-
["git", "rev-parse", "--short", "HEAD"], stderr=null, cwd=cwd
79-
)
80-
.strip()
81-
.decode("ascii")
82-
)
83-
except (subprocess.CalledProcessError, FileNotFoundError):
84-
git_commit = ""
85-
86-
try:
87-
dirty_string = "-this_is_a_dirty_checkout"
88-
is_dirty = (
89-
subprocess.check_output(
90-
["git", "describe", "--dirty=" + dirty_string], stderr=null, cwd=cwd
91-
)
92-
.strip()
93-
.decode("ascii")
94-
.endswith(dirty_string)
95-
)
60+
return prefix + result
61+
except (subprocess.CalledProcessError, FileNotFoundError):
62+
return ""
9663

97-
git_dirty = "dirty" if is_dirty else ""
98-
except (subprocess.CalledProcessError, FileNotFoundError):
99-
git_dirty = ""
64+
git_branch = _run_git_command("b=", "rev-parse", "--abbrev-ref", "HEAD")
65+
git_tag = _run_git_command("t=", "describe", "--exact-match")
66+
git_commit = _run_git_command("", "rev-parse", "--short", "HEAD")
67+
68+
dirty_string = "-this_is_a_dirty_checkout"
69+
is_dirty = _run_git_command("", "describe", "--dirty=" + dirty_string).endswith(
70+
dirty_string
71+
)
72+
git_dirty = "dirty" if is_dirty else ""
10073

10174
if git_branch or git_tag or git_commit or git_dirty:
10275
git_version = ",".join(
10376
s for s in (git_branch, git_tag, git_commit, git_dirty) if s
10477
)
10578

106-
version_string = "%s (%s)" % (
107-
# If the __version__ attribute doesn't exist, we'll have failed
108-
# loudly above.
109-
module.__version__, # type: ignore[attr-defined]
110-
git_version,
111-
)
79+
version_string = f"{version_string} ({git_version})"
11280
except Exception as e:
11381
logger.info("Failed to check for git repository: %s", e)
11482

0 commit comments

Comments
 (0)
This repository has been archived.