-
Notifications
You must be signed in to change notification settings - Fork 0
/
report_errors.py
167 lines (131 loc) · 6.56 KB
/
report_errors.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
'''
Copyright 2021 Flexera Software LLC
See LICENSE.TXT for full license text
SPDX-License-Identifier: MIT
Author : sgeary
Created On : Mon Apr 12 2021
File : report_errors.py
'''
import logging
import os
import base64
import _version
logger = logging.getLogger(__name__)
#--------------------------------------------------------------------------------#
def create_error_report(reportData):
logger.info("Entering create_error_report")
# Dict to hold the complete list of reports
reports = {}
htmlFile = generate_error_report(reportData)
reports["viewable"] = htmlFile
reports["allFormats"] = [htmlFile]
logger.info("Exiting create_report_artifacts")
return reports
#------------------------------------------------------------------#
def generate_error_report(reportData):
logger.info(" Entering generate_error_report")
scriptDirectory = os.path.dirname(os.path.realpath(__file__))
cssFile = os.path.join(scriptDirectory, "report_branding/css/revenera_common.css")
logoImageFile = os.path.join(scriptDirectory, "report_branding/images/logo_reversed.svg")
iconFile = os.path.join(scriptDirectory, "report_branding/images/favicon-revenera.ico")
reportName = reportData["reportName"]
reportFileNameBase = reportData["reportFileNameBase"]
errorMsg = reportData["errorMsg"]
reportTimeStamp = reportData["reportTimeStamp"]
#########################################################
# Encode the image files
encodedLogoImage = encodeImage(logoImageFile)
encodedfaviconImage = encodeImage(iconFile)
htmlFile = reportFileNameBase + ".html"
#---------------------------------------------------------------------------------------------------
# Create a simple HTML file to display
#---------------------------------------------------------------------------------------------------
try:
html_ptr = open(htmlFile,"w")
except:
logger.error("Failed to open htmlfile %s:" %htmlFile)
raise
html_ptr.write("<html>\n")
html_ptr.write(" <head>\n")
html_ptr.write(" <!-- Required meta tags --> \n")
html_ptr.write(" <meta charset='utf-8'> \n")
html_ptr.write(" <meta name='viewport' content='width=device-width, initial-scale=1, shrink-to-fit=no'> \n")
html_ptr.write('''
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.1/css/bootstrap.min.css" integrity="sha384-VCmXjywReHh4PwowAiWNagnWcLhlEJLA5buUprzK8rxFgeH0kww/aWY76TfkUoSX" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.css">
''')
html_ptr.write(" <style>\n")
# Add the contents of the css file to the head block
try:
f_ptr = open(cssFile)
logger.debug("Adding css file details")
for line in f_ptr:
html_ptr.write(" %s" %line)
f_ptr.close()
except:
logger.error("Unable to open %s" %cssFile)
print("Unable to open %s" %cssFile)
html_ptr.write(" </style>\n")
html_ptr.write(" <link rel='icon' type='image/png' href='data:image/png;base64, {}'>\n".format(encodedfaviconImage.decode('utf-8')))
html_ptr.write(" <title>%s</title>\n" %(reportName))
html_ptr.write(" </head>\n")
html_ptr.write("<body>\n")
html_ptr.write("<div class=\"container-fluid\">\n")
#---------------------------------------------------------------------------------------------------
# Report Header
#---------------------------------------------------------------------------------------------------
html_ptr.write("<!-- BEGIN HEADER -->\n")
html_ptr.write("<div class='header'>\n")
html_ptr.write(" <div class='logo'>\n")
html_ptr.write(" <img src='data:image/svg+xml;base64,{}' style='width: 400px;'>\n".format(encodedLogoImage.decode('utf-8')))
html_ptr.write(" </div>\n")
html_ptr.write("<div class='report-title'>%s</div>\n" %reportName)
html_ptr.write("</div>\n")
html_ptr.write("<!-- END HEADER -->\n")
#---------------------------------------------------------------------------------------------------
# Body of Report
#---------------------------------------------------------------------------------------------------
html_ptr.write("<!-- BEGIN BODY -->\n")
html_ptr.write("<div class='jumbotron'>\n")
html_ptr.write("<h1 class='display-4'>Error creating report!</h1>\n")
for message in errorMsg:
html_ptr.write("<p class='lead'>%s</p>\n" %message)
html_ptr.write("</div>\n")
html_ptr.write("<!-- END BODY -->\n")
#---------------------------------------------------------------------------------------------------
# Report Footer
#---------------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------
# Report Footer
#---------------------------------------------------------------------------------------------------
html_ptr.write("<!-- BEGIN FOOTER -->\n")
html_ptr.write("<div class='report-footer'>\n")
html_ptr.write(" <div style='float:right'>Generated on %s</div>\n" %reportTimeStamp)
html_ptr.write("<br>\n")
html_ptr.write(" <div style='float:right'>Report Version: %s</div>\n" %_version.__version__)
html_ptr.write("</div>\n")
html_ptr.write("<!-- END FOOTER -->\n")
html_ptr.write("</div>\n")
#---------------------------------------------------------------------------------------------------
# Add javascript
#---------------------------------------------------------------------------------------------------
html_ptr.write('''
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
''')
html_ptr.write("</body>\n")
html_ptr.write("</html>\n")
html_ptr.close()
logger.info(" Exiting generate_error_report")
return htmlFile
####################################################################
def encodeImage(imageFile):
#############################################
# Create base64 variable for branding image
try:
with open(imageFile,"rb") as image:
logger.debug("Encoding image: %s" %imageFile)
encodedImage = base64.b64encode(image.read())
return encodedImage
except:
logger.error("Unable to open %s" %imageFile)
raise