forked from ufs-community/ufs-weather-model
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_log_warnings_remarks.py
More file actions
158 lines (125 loc) · 5.51 KB
/
check_log_warnings_remarks.py
File metadata and controls
158 lines (125 loc) · 5.51 KB
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
import requests
from mdutils.mdutils import MdUtils
import os, sys
import json
import re
import logging
class APICall():
"""A GitHub API call"""
def __init__(self, endpoint='', num_commits=1):
self.token = os.environ.get('GITHUB_TOKEN')
self.base_url = os.environ.get('BASE_URL')
self.endpoint = endpoint
self.url = f"{self.base_url}/{self.endpoint}" #Could use a path join?
self.num_commits = num_commits
self.header = {
"Accept": "application/vnd.github.v3+json",
"Authorization": f"Bearer {self.token}",
"X-GitHub-Api-Version": "2022-11-28",
"Accept": "application/vnd.github.raw"
}
class Log():
"""A Regression Test log file."""
def __init__(self, machine):
"""Create the log file object for a specific machine."""
self.machine = machine.lower()
self.text_per_log = []
def call_API(self, endpoint):
"""Call the GitHub API to get information about the log file."""
api_call = APICall(endpoint)
response = requests.get(api_call.url, headers=api_call.header)
if response.status_code != 200:
logging.warning(response)
print(response)
sys.exit(1)
response = json.loads(response.text)
return response
def _get_commits(self):
"""Get PR head and base commits. Structure of response:
response = [{"head": {"sha": "a1b2c3d..."}, "base": {"sha": "b2c3d4e..."}}]
See GitHub documentation for https://docs.github.com/en/rest/commits/commits?apiVersion=2022-11-28#list-commits
"""
response = self.call_API(f"pulls/{os.environ.get('PR_NUM')}")
self.pr_head_commit = response['head']['sha']
self.pr_base_commit = response['base']['sha']
def _fetch_log_text(self, commit):
"""For each commit of a log, extract the log text."""
try:
api_call = APICall(f"contents/tests/logs/RegressionTests_{self.machine}.log")
url = api_call.url + (f"?ref={commit}") #Could use a path join?
r = requests.get(url, headers=api_call.header)
return r.text
except:
logging.error("An appropriate commit(s) was not provided. Call _get_commits() first.")
def _get_test_data(self, log_instance):
"""For each instance of a log at a given commit, extract runtime and memory data from the log text
Args:
log_instance: Log text for a given commit
Returns:
tests_for_log_instance: A dictionary of tests (keys) with a tuple of warnings and remarks as the value for each test
"""
tests_for_log_instance = {}
pattern = r"COMPILE \'(.*)\' \[\d+:\d+, \d+:\d+\] \( (\d+) warnings (\d+) remarks \)"
log_instance = log_instance.splitlines()
for line in log_instance:
test_match = re.search(pattern, line)
if test_match:
test_name, warnings, remarks = test_match.groups()
tests_for_log_instance[test_name] = (int(warnings), int(remarks))
return tests_for_log_instance
def _get_pr_data(self, commit):
"""Extract warnings/remarks data for a particular commit.
Returns:
log_data: A dictionary of tests as the key with a tuple of (warnings, remarks) as the value
"""
try:
log_text = self._fetch_log_text(commit)
log_data = self._get_test_data(log_text)
return log_data
except:
logging.error(f"No commit found for the ref {commit}")
sys.exit(1)
def compare_results(self, pr_log, base_log):
"""Compare warnings/remarks for PR head and base commits to determine whether warnings/remarks have increased."""
increases = {'warnings': {}, 'remarks': {}}
for test in pr_log:
# Check warnings
if pr_log[test][0] > base_log[test][0]:
increases['warnings'].update({test: pr_log[test][0] - base_log[test][0]})
# Check remarks
if pr_log[test][1] > base_log[test][1]:
increases['remarks'].update({test: pr_log[test][1] - base_log[test][1]})
return increases
def print_html_results(dict):
"""Print the comparison results in HTML."""
pr_num = os.environ.get('PR_NUM')
mdFile = MdUtils(file_name='summary.md', title=f'Increased Warnings/Remarks for PR #{pr_num}')
for machine, results in dict.items():
for category in results.keys():
if results[category]:
mdFile.write(f"\n<h3>{machine.upper()}</h3>\n")
unordered_list = [f"**{category.title()}:**", []]
for test, value in dict[machine][category].items():
unordered_list[1].append(f"{test}: {value}")
mdFile.new_list(unordered_list, marked_with='*')
return mdFile.get_md_text()
def main():
"""For each machine, create a log object, get current PR data, and determine
which tests increase warnings and/or remarks on each machine."""
machines = os.environ.get('MACHINES').split()
# For each machine, tests where warnings and/or remarks increase
increased_warnings_remarks = {}
for machine in machines:
log = Log(machine)
log._get_commits()
log.pr_log_data = log._get_pr_data(log.pr_head_commit)
log.base_log_data = log._get_pr_data(log.pr_base_commit)
increased_warnings_remarks[machine] = log.compare_results(log.pr_log_data, log.base_log_data)
results = print_html_results(increased_warnings_remarks)
if len(results) > 81: # Length of HTML header
print(results)
sys.exit(1)
else:
sys.exit(0)
if __name__ == "__main__": # pragma: no coverage
main()