From 21c07d15c2aabbd1ba7dc61c69019eaaa5b0b46e Mon Sep 17 00:00:00 2001 From: Jakub Kaczmarzyk Date: Wed, 25 Jan 2023 16:06:12 -0500 Subject: [PATCH] handle 'git' not being installed (#90) Fixes #89 --- tests/test_all.py | 25 +++++++++++++++++++++++++ wsinfer/cli/infer.py | 24 ++++++++++++++---------- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/tests/test_all.py b/tests/test_all.py index 8b5bbab..faf8d1e 100644 --- a/tests/test_all.py +++ b/tests/test_all.py @@ -1,5 +1,6 @@ import json import math +import os from pathlib import Path import platform import subprocess @@ -963,3 +964,27 @@ def test_jit_compile(model_name: str, weights_name: str): "JIT-compiled model was SLOWER than original: " f"jit={time_yesjit:0.3f} vs nojit={time_nojit:0.3f}" ) + + +def test_issue_89(): + from wsinfer.cli.infer import _get_info_for_save + + w = get_model_weights("resnet34", "TCGA-BRCA-v1") + d = _get_info_for_save(w) + assert d + assert "git" in d["runtime"] + assert d["runtime"]["git"] + assert d["runtime"]["git"]["git_remote"] + assert d["runtime"]["git"]["git_branch"] + + # Test that _get_info_for_save does not fail if git is not found. + orig_path = os.environ["PATH"] + try: + os.environ["PATH"] = "" + w = get_model_weights("resnet34", "TCGA-BRCA-v1") + d = _get_info_for_save(w) + assert d + assert "git" in d["runtime"] + assert d["runtime"]["git"] is None + finally: + os.environ["PATH"] = orig_path # reset path diff --git a/wsinfer/cli/infer.py b/wsinfer/cli/infer.py index fd6eabb..a11b503 100644 --- a/wsinfer/cli/infer.py +++ b/wsinfer/cli/infer.py @@ -5,6 +5,7 @@ import os from pathlib import Path import platform +import shutil import subprocess import sys import typing @@ -121,17 +122,20 @@ def get_stdout(args) -> str: } # Test if we are in a git repo. If we are, then get git info. - cmd = subprocess.run( - "git branch".split(), - cwd=here, - stdout=subprocess.DEVNULL, - stderr=subprocess.DEVNULL, - ) - if cmd.returncode == 0: + git_program = shutil.which("git") + git_installed = git_program is not None + is_git_repo = False + if git_installed: + cmd = subprocess.run( + [str(git_program), "branch"], + cwd=here, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + ) + is_git_repo = cmd.returncode == 0 + git_info = None + if git_installed and is_git_repo: git_info = get_git_info() - else: - git_info = None - del cmd, here # For sanity. weights_file = weights.file if weights_file is None: