Skip to content

Commit 77bdcaf

Browse files
authored
Merge pull request #77 from staadecker/fix_switch_version
Fix switch --version
2 parents e48abb4 + e0646f7 commit 77bdcaf

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

switch_model/__main__.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,10 @@ 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
44-
print(f"Switch git branch {get_git_branch()}")
45-
except:
46-
pass
42+
from switch_model.utilities import get_git_branch
43+
branch = get_git_branch()
44+
if branch is not None:
45+
print(f"Switch Git branch: {branch}")
4746
return 0
4847
if cmd == "solve":
4948
from switch_model.solve import main

switch_model/utilities/__init__.py

Lines changed: 36 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

@@ -883,17 +884,47 @@ def query_yes_no(question, default="yes"):
883884
"(or 'y' or 'n').\n")
884885

885886

887+
def catch_exceptions(warning_msg=None, should_catch=True):
888+
"""Decorator that catches exceptions."""
889+
890+
def decorator(func):
891+
@functools.wraps(func)
892+
def wrapper(*args, **kwargs):
893+
if not should_catch:
894+
return func(*args, **kwargs)
895+
896+
try:
897+
return func(*args, **kwargs)
898+
except:
899+
if warning_msg is not None:
900+
warnings.warn(warning_msg)
901+
902+
return wrapper
903+
904+
return decorator
905+
906+
886907
def run_command(command):
887908
return subprocess.check_output(command.split(" "), cwd=os.path.dirname(__file__)).strip().decode("UTF-8")
888909

910+
911+
@catch_exceptions("Failed to get Git Branch.")
912+
def get_git_branch():
913+
return run_command("git rev-parse --abbrev-ref HEAD")
914+
915+
916+
@catch_exceptions("Failed to get Git Commit Hash.")
917+
def get_git_commit():
918+
return run_command("git rev-parse HEAD")
919+
920+
889921
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")
922+
commit_num = get_git_commit()
923+
branch = get_git_branch()
924+
if commit_num is not None:
893925
add_info("Git Commit", commit_num, section=ResultsInfoSection.GENERAL)
926+
if branch is not None:
894927
add_info("Git Branch", branch, section=ResultsInfoSection.GENERAL)
895-
except:
896-
warnings.warn("Failed to get Git Branch or Commit Hash for info.txt.")
897928

898929
def get_module_list(args=None, include_solve_module=True):
899930
# parse module options

0 commit comments

Comments
 (0)