Skip to content

Commit 3b7a534

Browse files
committed
Fix switch --version
1 parent a94d29a commit 3b7a534

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
@@ -951,6 +951,17 @@ def query_yes_no(question, default="yes"):
951951
else:
952952
sys.stdout.write("Please respond with 'yes' or 'no' " "(or 'y' or 'n').\n")
953953

954+
def catch_exceptions(func, *args, should_catch=True, warning_msg=None, **kwargs):
955+
"""Catches exceptions thrown by function."""
956+
if not should_catch:
957+
return func(*args, **kwargs)
958+
959+
try:
960+
return func(*args, **kwargs)
961+
except:
962+
if warning_msg is not None:
963+
warnings.warn(warning_msg)
964+
954965

955966
def run_command(command):
956967
return (
@@ -960,14 +971,28 @@ def run_command(command):
960971
)
961972

962973

974+
975+
def get_git_branch(warning_msg="Failed to get Git Branch."):
976+
return catch_exceptions(
977+
run_command,
978+
"git rev-parse --abbrev-ref HEAD",
979+
warning_msg=warning_msg
980+
)
981+
982+
def get_git_commit(warning_msg="Failed to get Git Commit Hash."):
983+
return catch_exceptions(
984+
run_command,
985+
"git rev-parse HEAD",
986+
warning_msg=warning_msg
987+
)
988+
963989
def add_git_info():
964-
try:
965-
commit_num = run_command("git rev-parse HEAD")
966-
branch = run_command("git rev-parse --abbrev-ref HEAD")
990+
commit_num = get_git_commit()
991+
branch = get_git_branch()
992+
if commit_num is not None:
967993
add_info("Git Commit", commit_num, section=ResultsInfoSection.GENERAL)
994+
if branch is not None:
968995
add_info("Git Branch", branch, section=ResultsInfoSection.GENERAL)
969-
except:
970-
warnings.warn("Failed to get Git Branch or Commit Hash for info.txt.")
971996

972997

973998
def get_module_list(args=None, include_solve_module=True):

0 commit comments

Comments
 (0)