-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapplication_extension.py
More file actions
152 lines (110 loc) · 5.33 KB
/
application_extension.py
File metadata and controls
152 lines (110 loc) · 5.33 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
import cast_upgrade_1_5_20 # @UnusedImport @UnresolvedImport
from cast.application import ApplicationLevelExtension, CASTAIP # @UnresolvedImport
import glob
import logging
import xlsxwriter
import xlrd
import unanalysed
import os
import time
def main(application, report_path, version=None, previously_unanalysed=set()):
# Generate the Excel report and get the raw percentage
workbook = xlsxwriter.Workbook(report_path)
percentage, new_unanalysed_percentage = unanalysed.generate_report(application, workbook, version, previously_unanalysed)
workbook.close()
try:
# try Publish the Execution Report in CMS
report_name='Unanalyzed Code'
metric_name = 'NEW unanalysed files percentage'
metric='%.1f%%' % new_unanalysed_percentage
second_metric_name = 'Unanalysed files percentage'
second_metric='%.1f%%' % percentage
level='OK'
if (new_unanalysed_percentage > 10):
level='Warning'
# this import may fail in versions < 8.3
from cast.application import publish_report # @UnresolvedImport
publish_report(report_name, level, metric_name, metric, second_metric_name, second_metric, detail_report_path=report_path)
except:
pass # probably not in 8.3
# try to send report by email
mngt_application = application.get_application_configuration()
if mngt_application:
try:
address = mngt_application.get_email_to_send_reports()
if address:
logging.info('Sending report by mail to %s', address)
caip = CASTAIP.get_running_caip()
# Import the email modules we'll need
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
from email.mime.multipart import MIMEMultipart
# Open a plain text file for reading. For this example, assume that
# the text file contains only ASCII characters.
msg = MIMEMultipart()
msg['From'] = 'Unanalysed Code Report<' + caip.get_mail_from_address() + '>'
msg['To'] = address
msg['Subject'] = 'Unanalysed Code Report for %s' % application.get_name()
# message body
html = """\
<html>
<head></head>
<body>
<p>Percentage of unanalysed files: %s%%<br>
<p>Percentage of new unanalysed files: %s%%<br>
See attachement for full details.<br>
<a href="https://github.com/CAST-Extend/com.castsoftware.uc.checkanalysiscompleteness/blob/master/readme.md">Usage documentation</a>
</p>
</body>
</html>
""" % (percentage, new_unanalysed_percentage)
msg.attach(MIMEText(html, 'html'))
# attach report
part = MIMEBase('application', "octet-stream")
part.set_payload(open(report_path, "rb").read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="completeness_report.xlsx"')
msg.attach(part)
server = caip.get_mail_server()
server.send_message(msg)
server.quit()
logging.info('Mail sent')
except Exception as e:
logging.warning(e)
pass
def find_latest_report(folder):
"""
Return the latest resport in the given folder
"""
pathes = list(glob.glob(os.path.join(folder, "completeness_report_*.xlsx")))
pathes.sort()
if pathes:
return pathes[-1]
def load_previously_unanalysed_files(folder):
"""
Give the set of unanalysed files in the previous run
"""
path = find_latest_report(folder)
result = set()
if path:
try:
xl_workbook = xlrd.open_workbook(path)
xl_sheet = xl_workbook.sheet_by_name('Files Not Analyzed')
for row_idx in range(1, xl_sheet.nrows):
path = xl_sheet.cell(row_idx, 1)
result.add(path.value)
except:
pass
return result
class CheckApplication(ApplicationLevelExtension):
def end_application(self, application):
logging.info("##################################################################")
logging.info("Checking application completeness")
self.get_plugin().get_version()
report_path = os.path.join(self.get_plugin().intermediate, time.strftime("completeness_report_%Y%m%d_%H%M%S.xlsx"))
# make path usable directly in windows
report_path = report_path.replace('/', '\\')
main(application, report_path, previously_unanalysed=load_previously_unanalysed_files(self.get_plugin().intermediate))
logging.info("Generated report %s" % report_path)
logging.info("##################################################################")