|
6 | 6 | """ |
7 | 7 | from __future__ import print_function |
8 | 8 |
|
| 9 | +import functools |
9 | 10 | import os, types, sys, argparse, time, datetime, traceback, subprocess, platform |
10 | 11 | import warnings |
11 | 12 |
|
@@ -883,17 +884,47 @@ def query_yes_no(question, default="yes"): |
883 | 884 | "(or 'y' or 'n').\n") |
884 | 885 |
|
885 | 886 |
|
| 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 | + |
886 | 907 | def run_command(command): |
887 | 908 | return subprocess.check_output(command.split(" "), cwd=os.path.dirname(__file__)).strip().decode("UTF-8") |
888 | 909 |
|
| 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 | + |
889 | 921 | 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: |
893 | 925 | add_info("Git Commit", commit_num, section=ResultsInfoSection.GENERAL) |
| 926 | + if branch is not None: |
894 | 927 | add_info("Git Branch", branch, section=ResultsInfoSection.GENERAL) |
895 | | - except: |
896 | | - warnings.warn("Failed to get Git Branch or Commit Hash for info.txt.") |
897 | 928 |
|
898 | 929 | def get_module_list(args=None, include_solve_module=True): |
899 | 930 | # parse module options |
|
0 commit comments