Skip to content

Commit

Permalink
handle 'git' not being installed (#90)
Browse files Browse the repository at this point in the history
Fixes #89
  • Loading branch information
kaczmarj authored Jan 25, 2023
1 parent 7d59361 commit 21c07d1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
25 changes: 25 additions & 0 deletions tests/test_all.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import math
import os
from pathlib import Path
import platform
import subprocess
Expand Down Expand Up @@ -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
24 changes: 14 additions & 10 deletions wsinfer/cli/infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
from pathlib import Path
import platform
import shutil
import subprocess
import sys
import typing
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 21c07d1

Please sign in to comment.