Skip to content

Commit 2c8d03e

Browse files
committed
Fix switch --version
1 parent e48abb4 commit 2c8d03e

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

switch_model/utilities/__init__.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -882,18 +882,43 @@ def query_yes_no(question, default="yes"):
882882
sys.stdout.write("Please respond with 'yes' or 'no' "
883883
"(or 'y' or 'n').\n")
884884

885+
def catch_exceptions(func, *args, should_catch=True, warning_msg=None, **kwargs):
886+
"""Catches exceptions thrown by function."""
887+
if not should_catch:
888+
return func(*args, **kwargs)
889+
890+
try:
891+
return func(*args, **kwargs)
892+
except:
893+
if warning_msg is not None:
894+
warnings.warn(warning_msg)
895+
885896

886897
def run_command(command):
887898
return subprocess.check_output(command.split(" "), cwd=os.path.dirname(__file__)).strip().decode("UTF-8")
888899

900+
901+
def get_git_branch(warning_msg="Failed to get Git Branch."):
902+
return catch_exceptions(
903+
run_command,
904+
"git rev-parse --abbrev-ref HEAD",
905+
warning_msg=warning_msg
906+
)
907+
908+
def get_git_commit(warning_msg="Failed to get Git Commit Hash."):
909+
return catch_exceptions(
910+
run_command,
911+
"git rev-parse HEAD",
912+
warning_msg=warning_msg
913+
)
914+
889915
def add_git_info():
890-
try:
891-
commit_num = run_command("git rev-parse HEAD")
892-
branch = run_command("git rev-parse --abbrev-ref HEAD")
916+
commit_num = get_git_commit()
917+
branch = get_git_branch()
918+
if commit_num is not None:
893919
add_info("Git Commit", commit_num, section=ResultsInfoSection.GENERAL)
920+
if branch is not None:
894921
add_info("Git Branch", branch, section=ResultsInfoSection.GENERAL)
895-
except:
896-
warnings.warn("Failed to get Git Branch or Commit Hash for info.txt.")
897922

898923
def get_module_list(args=None, include_solve_module=True):
899924
# parse module options

0 commit comments

Comments
 (0)