-
Notifications
You must be signed in to change notification settings - Fork 2
/
log_change.py
347 lines (264 loc) · 10.2 KB
/
log_change.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
'''
This is an interactive script that generates a changelog entry for any changes
implemented to real time RRFS runs. It asks the user a variety of RRFS-specific
questions, interrogates the UFS SRW App and its 1st-level Externals (submodules)
to determine if they are consistent with the code on disk, and generates a log
file with relevant tracking information.
Note: It is important to continue to use this logging script, even for changes
that occur outside the clone, even though those changes will not be captured in
the git information for the log file.
The log file will be appended to those in /misc/whome/rtrr/RRFS/
Requirements:
Python 3.6+
Conda Environment:
module use /contrib/miniconda3/modulefiles
module load miniconda3
module load regional_workflow
Usage:
python log_change.py -h
'''
#pylint: disable=invalid-name
import argparse
from collections import OrderedDict
from configparser import ConfigParser
import datetime as dt
import os
import shutil
import stat
import subprocess
import sys
import tempfile
import time
LOGFILE_LOC = '/misc/whome/rtrr/RRFS/'
DOMAINS = ['CONUS', 'AK', 'NA3km', 'NA13km', 'RTMA', 'all']
class cd:
'''Context manager for changing the current working directory'''
def __init__(self, newPath):
self.newPath = os.path.expanduser(newPath)
def __enter__(self):
self.savedPath = os.getcwd() #pylint: disable=attribute-defined-outside-init
os.chdir(self.newPath)
def __exit__(self, etype, value, traceback):
os.chdir(self.savedPath)
def get_repo_info():
''' Get the hashes that are currently checked out in each repository. Report
on differences between hashes checked out and those in Externals.cfg, as
well as differences that exist on disk. '''
repo_info = OrderedDict()
repos = load_externals()
repos['ufs-srweather-app'] = {'local_path': './'}
for repo, config in repos.items():
if repo != 'externals_description':
with cd(config.get('local_path', './')):
summary, code_hash = get_summary()
config_hash = config.get('hash')
repo_info[repo] = {
'summary': summary,
'hash': code_hash,
'diffs': get_local_changes(),
'hash_diffs': "N/A" if config_hash is None else
not code_hash.startswith(config_hash),
}
return repo_info
def get_local_changes():
''' Get differences that are not in the repo. '''
pipe = subprocess.Popen("git diff", shell=True, stdout=subprocess.PIPE)
ret = pipe.communicate()[0].decode('utf-8')
return ret if ret else None
def get_summary():
''' Return the results of a git summary, and the hash. '''
pipe = subprocess.Popen("git show --summary --decorate", shell=True, stdout=subprocess.PIPE)
summary = pipe.communicate()[0].decode('utf-8')
return summary, summary.split()[1]
def get_user_info():
''' Return a dictionary of user-supplied information. '''
print('Please provide the following information: ')
user_questions = OrderedDict({
'name':
{'question': "Your name: \n"},
'changes':
{'question': "What changed? \n"},
'components':
{'question': "What components were affected? (jobs, scripts, " \
"configuration layer, model configuration, etc.) \n"},
'first_cycle':
{'question': "First cycle in effect (YYYYMMDDHH): \n",
'check': isdate,
},
'comparison':
{'question': "Which runs should this be compared to? \n"},
'domains':
{'question': f"Domains affected ({DOMAINS}): \n",
'check': isdomain,
},
'rebuild':
{'question': "Did you rebuild? (Y/N)\n",
'check': isbool,
},
'reconfigure':
{'question': "Did you reconfigure? (Y/N)\n",
'check': isbool,
},
'inrepo':
{'question': "Are your changes in the repo? (Y/N)\n",
'check': isbool,
},
})
user_info = OrderedDict()
for info, gather in user_questions.items():
while not user_info.get(info):
tmp = input(gather['question'])
check = gather.get('check')
if check:
if not check(tmp):
tmp = None
user_info[info] = tmp
return user_info
def isbool(string):
''' Returns a bool after checking whether the input string meets the
requirements for a yes/no answer. '''
while True:
if len(string) < 1:
string = input('Please enter a Y/N response! \n')
else:
break
return string.lower()[0] in ['y', 'n']
def isdate(string):
''' Returns a bool after checking whether the input string meets the
requirements for a date string: YYYYMMDDHH. '''
if len(string) != 10:
print(f'Must enter cycle in YYYYMMDDHH format!')
return False
return True
def isdomain(string):
''' Returns a bool after checking whether the input string meets the
requirements for a domain, or list of domains '''
string_list = [i.strip("[]\"', ") for i in string.split(',')]
domains = [d.lower() for d in DOMAINS]
for s in string_list:
if s.lower() not in domains:
print(f'{s} is not a valid domain')
return False
return True
def load_externals(ext_path="Externals.cfg"):
''' Use configparser to load the Externals.cfg info. Return the dict'''
config = ConfigParser()
config.read(ext_path)
return {i: {i[0]: i[1] for i in config.items(i)} for i in config.sections()}
def log_message():
''' Recording a log message for printing or writing to file '''
# Save the original standard out location
original = sys.stdout
# Interact with user
user_info = get_user_info()
repos = get_repo_info()
# Print the results of the user input to a temporary file
tmp_file = tempfile.NamedTemporaryFile(mode='w', delete=False, dir='.')
print(f'Saving to a temp file: {tmp_file.name}')
# Print once to file, and once to screen
for fn in [tmp_file, original]:
sys.stdout = fn
print('*'*80)
print(f'Logging a change at {dt.datetime.now().strftime("%c")}: ')
print('*'*80)
print_dict(user_info)
print_dict(repos, sep='*')
return tmp_file
def logit(logfile, tmpfile):
''' Cat the temporary file (filename) at the beginning of the main log file.
'''
lock_file = f'{logfile}._lock'
while True:
if not os.path.exists(lock_file):
# Open the lock file
fd = open(lock_file, 'w')
try:
# Create a logfile backup just in case. Make it a hidden file.
# Won't remove this one in the script in case something goes
# wrong.
path, fname = os.path.dirname(logfile), os.path.basename(logfile)
shutil.copy(logfile, os.path.join(path, f".{fname}._bk"))
# Write the contents of the logfile to the tempfile for reverse
# cronological order.
with open(logfile, 'r') as log:
for line in log:
tmpfile.write(line)
tmpfile.close()
# Rename tempfile to logfile
shutil.move(tmpfile.name, logfile)
# Set open read permissions
os.chmod(logfile,
stat.S_IWUSR | stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH,
)
except:
print('Something went wrong writing the logfile')
raise
finally:
# Close and remove the lock file
fd.close()
if os.path.exists(lock_file):
os.remove(lock_file)
break
# Wait before trying to obtain the lock on the file
time.sleep(5)
#pylint: disable=inconsistent-return-statements
def print_dict(d, depth=0, sep=None):
''' Recurse through dict entries to print them '''
if not isinstance(d, dict):
return d
for key, value in d.items():
if isinstance(value, dict):
sep_len = 80 - len(key) - 2
emphasis = sep * sep_len if sep is not None else ''
print(f'{key}: {emphasis}')
print_dict(value, depth+2)
else:
print(f'{" "*depth}{key}: {value}')
#pylint: enable=inconsistent-return-statements
def parse_args():
''' Parse the command line arguments (cla) '''
parser = argparse.ArgumentParser(description='Change log generator')
# Positional argument
parser.add_argument(
'dev_name',
choices=[f'RRFS_dev{n}' for n in range(1, 5)],
help='The type of graphics to create.',
)
return parser.parse_args()
def main(cla):
''' Interact with user to get necessary information. '''
happy = False
while not happy:
message_file = log_message()
while True:
print('~~~~~~~~ END OF MESSAGE ~~~~~~~~')
answer = input("Are you happy with the log message? (Y/N) ")
if not answer:
print(f'Please enter a Y/N response. ')
elif answer.lower()[0] == 'y':
logfile = os.path.join(LOGFILE_LOC, f'{cla.dev_name}.log')
print(f'Adding your log message to {logfile}')
logit(logfile, message_file)
message_file.close()
happy = True
break
elif answer.lower()[0] == 'n':
kill = input("Type retry to enter new info: ")
if not kill:
message_file.close()
sys.exit()
elif kill.lower()[0] == "r":
print()
print('~'*80)
print(f'Clearing input and starting new log')
print('~'*80)
print()
break
else:
message_file.close()
sys.exit()
else:
print(f'Please enter a Y/N response. ')
if __name__ == '__main__':
CLA = parse_args()
main(CLA)