From fceeb83b6e5efc8be66356f7e6235335638a8b07 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Wed, 1 Dec 2021 11:52:44 -0500 Subject: [PATCH] Use subprocess.DEVNULL helper. --- synapse/util/versionstring.py | 57 ++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/synapse/util/versionstring.py b/synapse/util/versionstring.py index 7b52d9622bf1..c144ff62c1fa 100644 --- a/synapse/util/versionstring.py +++ b/synapse/util/versionstring.py @@ -46,36 +46,37 @@ def get_version_string(module: ModuleType) -> str: version_string = module.__version__ # type: ignore[attr-defined] try: - with open(os.devnull, "w") as null: - cwd = os.path.dirname(os.path.abspath(module.__file__)) - - def _run_git_command(prefix: str, *params: str) -> str: - try: - result = ( - subprocess.check_output(["git", *params], stderr=null, cwd=cwd) - .strip() - .decode("ascii") + cwd = os.path.dirname(os.path.abspath(module.__file__)) + + def _run_git_command(prefix: str, *params: str) -> str: + try: + result = ( + subprocess.check_output( + ["git", *params], stderr=subprocess.DEVNULL, cwd=cwd ) - return prefix + result - except (subprocess.CalledProcessError, FileNotFoundError): - return "" - - git_branch = _run_git_command("b=", "rev-parse", "--abbrev-ref", "HEAD") - git_tag = _run_git_command("t=", "describe", "--exact-match") - git_commit = _run_git_command("", "rev-parse", "--short", "HEAD") - - dirty_string = "-this_is_a_dirty_checkout" - is_dirty = _run_git_command( - "", "describe", "--dirty=" + dirty_string - ).endswith(dirty_string) - git_dirty = "dirty" if is_dirty else "" - - if git_branch or git_tag or git_commit or git_dirty: - git_version = ",".join( - s for s in (git_branch, git_tag, git_commit, git_dirty) if s + .strip() + .decode("ascii") ) - - version_string = f"{version_string} ({git_version})" + return prefix + result + except (subprocess.CalledProcessError, FileNotFoundError): + return "" + + git_branch = _run_git_command("b=", "rev-parse", "--abbrev-ref", "HEAD") + git_tag = _run_git_command("t=", "describe", "--exact-match") + git_commit = _run_git_command("", "rev-parse", "--short", "HEAD") + + dirty_string = "-this_is_a_dirty_checkout" + is_dirty = _run_git_command("", "describe", "--dirty=" + dirty_string).endswith( + dirty_string + ) + git_dirty = "dirty" if is_dirty else "" + + if git_branch or git_tag or git_commit or git_dirty: + git_version = ",".join( + s for s in (git_branch, git_tag, git_commit, git_dirty) if s + ) + + version_string = f"{version_string} ({git_version})" except Exception as e: logger.info("Failed to check for git repository: %s", e)