66"""
77from __future__ import print_function
88
9+ import functools
910import os , types , sys , argparse , time , datetime , traceback , subprocess , platform
1011import 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+
955976def 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+
963994def 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
9731003def get_module_list (args = None , include_solve_module = True ):
0 commit comments