Skip to content

Commit dd4bd4f

Browse files
committed
Add git branch + commit info to log print statements
1 parent be4a6e2 commit dd4bd4f

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

utils/blech_utils.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,28 @@ def __init__(self, data_dir):
5656
self.data_dir = data_dir
5757
self.tee = Tee(data_dir)
5858
self.load_graph()
59+
self.get_git_info()
5960
self.check_graph()
6061

62+
def get_git_info(self):
63+
"""
64+
Get branch and commit info, and print
65+
If not in git repo, print warning
66+
"""
67+
if not hasattr(self, 'blech_clust_dir'):
68+
print('Run load_graph() first')
69+
exit()
70+
pwd = os.getcwd()
71+
# Change to blech_clust directory
72+
os.chdir(self.blech_clust_dir)
73+
git_branch = os.popen('git rev-parse --abbrev-ref HEAD').read().strip()
74+
git_commit = os.popen('git rev-parse HEAD').read().strip()
75+
if git_branch == '' or git_commit == '':
76+
print('Not in git repository, please clone blech_clust rather than downloading zip')
77+
self.git_str = f'Git branch: {git_branch}\nGit commit: {git_commit}'
78+
# change back to original directory
79+
os.chdir(pwd)
80+
6181
def load_graph(self):
6282
"""
6383
Load computation graph from file, if file is present
@@ -144,6 +164,8 @@ def write_to_log(self, script_path, type = 'attempted'):
144164
type = 'attempted' : script was attempted
145165
type = 'completed' : script was completed
146166
"""
167+
if not hasattr(self, 'git_str'):
168+
raise ValueError('Run get_git_info() first')
147169
self.log_path = os.path.join(self.data_dir, 'execution_log.json')
148170
current_datetime = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
149171
if os.path.exists(self.log_path):
@@ -157,6 +179,7 @@ def write_to_log(self, script_path, type = 'attempted'):
157179
log_dict['attempted'][script_path] = current_datetime
158180
print('============================================================')
159181
print(f'Attempting {os.path.basename(script_path)}, started at {current_datetime}')
182+
print(self.git_str)
160183
print('============================================================')
161184
elif type == 'completed':
162185
if 'completed' not in log_dict.keys():

0 commit comments

Comments
 (0)