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

Commit fceeb83

Browse files
committed
Use subprocess.DEVNULL helper.
1 parent c4f198e commit fceeb83

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

synapse/util/versionstring.py

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,36 +46,37 @@ def get_version_string(module: ModuleType) -> str:
4646
version_string = module.__version__ # type: ignore[attr-defined]
4747

4848
try:
49-
with open(os.devnull, "w") as null:
50-
cwd = os.path.dirname(os.path.abspath(module.__file__))
51-
52-
def _run_git_command(prefix: str, *params: str) -> str:
53-
try:
54-
result = (
55-
subprocess.check_output(["git", *params], stderr=null, cwd=cwd)
56-
.strip()
57-
.decode("ascii")
49+
cwd = os.path.dirname(os.path.abspath(module.__file__))
50+
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
5856
)
59-
return prefix + result
60-
except (subprocess.CalledProcessError, FileNotFoundError):
61-
return ""
62-
63-
git_branch = _run_git_command("b=", "rev-parse", "--abbrev-ref", "HEAD")
64-
git_tag = _run_git_command("t=", "describe", "--exact-match")
65-
git_commit = _run_git_command("", "rev-parse", "--short", "HEAD")
66-
67-
dirty_string = "-this_is_a_dirty_checkout"
68-
is_dirty = _run_git_command(
69-
"", "describe", "--dirty=" + dirty_string
70-
).endswith(dirty_string)
71-
git_dirty = "dirty" if is_dirty else ""
72-
73-
if git_branch or git_tag or git_commit or git_dirty:
74-
git_version = ",".join(
75-
s for s in (git_branch, git_tag, git_commit, git_dirty) if s
57+
.strip()
58+
.decode("ascii")
7659
)
77-
78-
version_string = f"{version_string} ({git_version})"
60+
return prefix + result
61+
except (subprocess.CalledProcessError, FileNotFoundError):
62+
return ""
63+
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 ""
73+
74+
if git_branch or git_tag or git_commit or git_dirty:
75+
git_version = ",".join(
76+
s for s in (git_branch, git_tag, git_commit, git_dirty) if s
77+
)
78+
79+
version_string = f"{version_string} ({git_version})"
7980
except Exception as e:
8081
logger.info("Failed to check for git repository: %s", e)
8182

0 commit comments

Comments
 (0)