Skip to content

Commit e1b87bd

Browse files
pesapstaadecker
authored andcommitted
Merge pull request #77 from staadecker/fix_switch_version
Fix switch --version
2 parents a94d29a + cdd7f28 commit e1b87bd

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

switch_model/__main__.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,11 @@ def main():
3939
del sys.argv[1]
4040
if cmd == "--version":
4141
print("Switch model version " + switch_model.__version__)
42-
try:
43-
from switch_model.utilities import get_git_branch
42+
from switch_model.utilities import get_git_branch
4443

45-
print(f"Switch git branch {get_git_branch()}")
46-
except:
47-
pass
44+
branch = get_git_branch()
45+
if branch is not None:
46+
print(f"Switch Git branch: {branch}")
4847
return 0
4948
if cmd == "solve":
5049
from switch_model.solve import main

switch_model/utilities/__init__.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77
from __future__ import print_function
88

9+
import functools
910
import os, types, sys, argparse, time, datetime, traceback, subprocess, platform
1011
import warnings
1112

@@ -952,6 +953,26 @@ def query_yes_no(question, default="yes"):
952953
sys.stdout.write("Please respond with 'yes' or 'no' " "(or 'y' or 'n').\n")
953954

954955

956+
def catch_exceptions(warning_msg=None, should_catch=True):
957+
"""Decorator that catches exceptions."""
958+
959+
def decorator(func):
960+
@functools.wraps(func)
961+
def wrapper(*args, **kwargs):
962+
if not should_catch:
963+
return func(*args, **kwargs)
964+
965+
try:
966+
return func(*args, **kwargs)
967+
except:
968+
if warning_msg is not None:
969+
warnings.warn(warning_msg)
970+
971+
return wrapper
972+
973+
return decorator
974+
975+
955976
def run_command(command):
956977
return (
957978
subprocess.check_output(command.split(" "), cwd=os.path.dirname(__file__))
@@ -960,14 +981,23 @@ def run_command(command):
960981
)
961982

962983

984+
@catch_exceptions("Failed to get Git Branch.")
985+
def get_git_branch():
986+
return run_command("git rev-parse --abbrev-ref HEAD")
987+
988+
989+
@catch_exceptions("Failed to get Git Commit Hash.")
990+
def get_git_commit():
991+
return run_command("git rev-parse HEAD")
992+
993+
963994
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")
995+
commit_num = get_git_commit()
996+
branch = get_git_branch()
997+
if commit_num is not None:
967998
add_info("Git Commit", commit_num, section=ResultsInfoSection.GENERAL)
999+
if branch is not None:
9681000
add_info("Git Branch", branch, section=ResultsInfoSection.GENERAL)
969-
except:
970-
warnings.warn("Failed to get Git Branch or Commit Hash for info.txt.")
9711001

9721002

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

0 commit comments

Comments
 (0)