-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_report.py
182 lines (142 loc) · 7.46 KB
/
create_report.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
'''
Copyright 2024 Flexera Software LLC
See LICENSE.TXT for full license text
SPDX-License-Identifier: MIT
Author : sgeary
Created On : Tue Mar 26 2024
File : create_report.py
'''
import sys, os, logging, argparse, json, re
from datetime import datetime
import _version
import report_errors, report_data, report_artifacts
import common.api.project.upload_reports
import common.report_archive
import common.api.system.release
###################################################################################
# Test the version of python to make sure it's at least the version the script
# was tested on, otherwise there could be unexpected results
if sys.version_info < (3, 6):
raise Exception("The current version of Python is less than 3.6 which is unsupported.\n Script created/tested against python version 3.6.8. ")
else:
pass
propertiesFile = "../server_properties.json" # Created by installer or manually
propertiesFile = logfileName = os.path.dirname(os.path.realpath(__file__)) + "/" + propertiesFile
logfileName = os.path.join(os.path.dirname(os.path.realpath(__file__)),"_apply_license_defaults_utility.log")
###################################################################################
# Set up logging handler to allow for different levels of logging to be capture
logging.basicConfig(format='%(asctime)s,%(msecs)-3d %(levelname)-8s [%(filename)-30s:%(lineno)-4d] %(message)s', datefmt='%Y-%m-%d:%H:%M:%S', filename=logfileName, filemode='w',level=logging.DEBUG)
logger = logging.getLogger(__name__)
logging.getLogger("urllib3").setLevel(logging.WARNING) # Disable logging for requests module
logging.getLogger("common").setLevel(logging.WARNING) # Disable logging for requests module
####################################################################################
# Create command line argument options
parser = argparse.ArgumentParser()
parser.add_argument('-pid', "--projectID", help="Project ID")
parser.add_argument("-rid", "--reportID", help="Report ID")
parser.add_argument("-authToken", "--authToken", help="Code Insight Authorization Token")
parser.add_argument("-baseURL", "--baseURL", help="Code Insight Core Server Protocol/Domain Name/Port. i.e. http://localhost:8888 or https://sca.codeinsight.com:8443")
parser.add_argument("-reportOpts", "--reportOptions", help="Options for report content")
#----------------------------------------------------------------------#
def main():
reportName = "Apply License Defaults Utility"
reportVersion = _version.__version__
logger.info("Creating %s - %s" %(reportName, reportVersion))
print("Creating %s - %s" %(reportName, reportVersion))
print(" Logfile: %s" %(logfileName))
#####################################################################################################
# Code Insight System Information
# Pull the base URL from the same file that the installer is creating
if os.path.exists(propertiesFile):
try:
file_ptr = open(propertiesFile, "r")
configData = json.load(file_ptr)
baseURL = configData["core.server.url"]
file_ptr.close()
logger.info("Using baseURL from properties file: %s" %propertiesFile)
except:
logger.error("Unable to open properties file: %s" %propertiesFile)
# Is there a self signed certificate to consider?
try:
certificatePath = configData["core.server.certificate"]
os.environ["REQUESTS_CA_BUNDLE"] = certificatePath
os.environ["SSL_CERT_FILE"] = certificatePath
logger.info("Self signed certificate added to env")
except:
logger.info("No self signed certificate in properties file")
else:
baseURL = "http://localhost:8888" # Required if the core.server.properties files is not used
logger.info("Using baseURL from create_report.py")
# See what if any arguments were provided
args = parser.parse_args()
projectID = args.projectID
reportID = args.reportID
reportOptions = args.reportOptions
authToken = args.authToken
fileNameTimeStamp = datetime.now().strftime("%Y%m%d-%H%M%S")
reportTimeStamp = datetime.strptime(fileNameTimeStamp, "%Y%m%d-%H%M%S").strftime("%B %d, %Y at %H:%M:%S")
# Based on how the shell pass the arguemnts clean up the options if on a linux system
if sys.platform.startswith('linux'):
reportOptions = reportOptions.replace('""', '"')[1:-1]
reportOptions = json.loads(reportOptions)
releaseDetails = common.api.system.release.get_release_details(baseURL, authToken)
releaseVersion = releaseDetails["fnci.release.name"].replace(" ", "")
logger.debug("Code Insight Release: %s" %releaseVersion)
logger.debug("Custom Report Provided Arguments:")
logger.debug(" projectID: %s" %projectID)
logger.debug(" reportID: %s" %reportID)
logger.debug(" reportOptions: %s" %reportOptions)
reportData = {}
reportData["primaryProjectID"] = projectID
reportData["reportName"] = reportName
reportData["reportVersion"] = reportVersion
reportData["reportOptions"] = reportOptions
reportData["releaseVersion"] = releaseVersion
reportData["fileNameTimeStamp"] = fileNameTimeStamp
reportData["reportTimeStamp"] = reportTimeStamp
if "error" in reportOptions.keys():
reportFileNameBase = reportName.replace(" ", "_") + "-Creation_Error-" + fileNameTimeStamp
reportData["error"] = reportOptions["error"]
reportData["reportName"] = reportName
reportData["reportFileNameBase"] = reportFileNameBase
reports = report_errors.create_error_report(reportData)
print(" *** ERROR *** Error found validating report options")
else:
print(" Collect data for %s" %reportName)
reportData = report_data.gather_data_for_report(baseURL, authToken, reportData)
if "error" in reportData:
logger.error("Error collecting report data: %s" %reportData["error"])
reportFileNameBase = reportName.replace(" ", "_") + "-Creation_Error-" + fileNameTimeStamp
reportData["reportName"] = reportName
reportData["reportFileNameBase"] = reportFileNameBase
reports = report_errors.create_error_report(reportData)
print(" Error report artifacts have been created")
else:
print(" Report data has been collected")
projectNameForFile = re.sub(r"[^a-zA-Z0-9]+", '-', reportData["topLevelProjectName"] ) # Remove special characters from project name for artifacts
reportFileNameBase = projectNameForFile + "-" + str(projectID) + "-" + reportName.replace(" ", "_") + "-" + fileNameTimeStamp
reportData["fileNameTimeStamp"] = fileNameTimeStamp
reportData["reportFileNameBase"] = reportFileNameBase
reports = report_artifacts.create_report_artifacts(reportData)
print(" Report artifacts have been created")
for report in reports["allFormats"]:
print(" - %s"%report)
#########################################################
# Create zip file to be uploaded to Code Insight
print(" Create report archive for upload")
uploadZipfile = common.report_archive.create_report_zipfile(reports, reportFileNameBase)
print(" Upload zip file creation completed")
common.api.project.upload_reports.upload_project_report_data(baseURL, projectID, reportID, authToken, uploadZipfile)
print(" Report uploaded to Code Insight")
#########################################################
# Remove the file since it has been uploaded to Code Insight
try:
os.remove(uploadZipfile)
except OSError:
logger.error("Error removing %s" %uploadZipfile)
print("Error removing %s" %uploadZipfile)
logger.info("Completed creating %s" %reportName)
print("Completed creating %s" %reportName)
#----------------------------------------------------------------------#
if __name__ == "__main__":
main()