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

Commit

Permalink
Use subprocess.DEVNULL helper.
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep committed Dec 1, 2021
1 parent c4f198e commit fceeb83
Showing 1 changed file with 29 additions and 28 deletions.
57 changes: 29 additions & 28 deletions synapse/util/versionstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit fceeb83

Please sign in to comment.