-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvcast_exec.py
436 lines (353 loc) · 19.4 KB
/
vcast_exec.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#
# The MIT License
#
# Copyright 2020 Vector Informatik, GmbH.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
import os, subprocess,argparse, glob, sys, shutil
from managewait import ManageWait
import cobertura
import generate_lcov
import patch_rgw_directory as rgw
try:
import generate_results
except:
try:
import importlib
generate_results = importlib.import_module("generate-results")
except:
vc_script = os.path.join(os.environ['WORKSPACE'], "vc_scripts", "generate-results.py")
import imp
generate_results = imp.load_source("generate_results", vc_script)
try:
import vector.apps.parallel.parallel_build_execute as parallel_build_execute
except:
import prevcast_parallel_build_execute as parallel_build_execute
from vcast_utils import checkVectorCASTVersion, dump
import generate_sonarqube_testresults
class VectorCASTExecute(object):
def __init__(self, args):
# setup default values
self.azure = args.azure
self.gitlab = args.gitlab
self.print_exc = args.print_exc
self.print_exc = args.print_exc
self.timing = args.timing
self.jobs = args.jobs
self.sonarqube = args.sonarqube
self.junit = args.junit
self.cobertura = args.cobertura
self.cobertura_extended = args.cobertura_extended
self.metrics = args.metrics
self.fullstatus = args.fullstatus
self.aggregate = args.aggregate
self.pclp_output_html = args.pclp_output_html
self.pclp_input = args.pclp_input
self.html_base_dir = args.html_base_dir
self.use_cte = args.use_cte
if args.exit_with_failed_count == 'not present':
self.useJunitFailCountPct = False
self.junit_percent_to_fail = 0
elif args.exit_with_failed_count == '(default 0)':
self.useJunitFailCountPct = True
self.junit_percent_to_fail = 0
else:
self.useJunitFailCountPct = True
self.junit_percent_to_fail = int(args.exit_with_failed_count)
self.failed_count = 0
if args.output_dir:
self.xml_data_dir = os.path.join(args.output_dir, 'xml_data')
if not os.path.exists(self.xml_data_dir):
os.makedirs(self.xml_data_dir)
else:
self.xml_data_dir = "xml_data"
if args.build and not args.build_execute:
self.build_execute = "build"
self.vcast_action = "--vcast_action " + self.build_execute
elif args.build_execute:
self.build_execute = "build-execute"
self.vcast_action = "--vcast_action " + self.build_execute
else:
self.build_execute = ""
self.vcast_action = ""
self.source_root = args.source_root
self.verbose = args.verbose
self.FullMP = args.ManageProject
self.mpName = os.path.basename(args.ManageProject)[:-4]
if args.ci:
self.useCI = " --use_ci "
self.ci = " --ci "
else:
self.useCI = ""
self.ci = ""
if args.incremental:
self.useCBT = " --incremental "
else:
self.useCBT = ""
self.useLevelEnv = False
self.environment = None
self.level = None
self.compiler = None
self.testsuite = None
self.reportsName = ""
self.env_option = ""
self.level_option = ""
self.needIndexHtml = False
# if a manage level was specified...
if args.level:
self.useLevelEnv = True
self.level = args.level
# try level being Compiler/TestSuite
try:
self.compiler, self.testsuite = args.level.split("/")
self.reportsName = "_" + self.compiler + "_" + self.testsuite
except:
# just use the compiler name
self.compiler = args.level
self.reportsName = "_" + self.compiler
self.level_option = "--level " + args.level + " "
# if an environment was specified
if args.environment:
# afix the proper settings for commands later and report names
self.useLevelEnv = True
self.environment = args.environment
self.env_option = "--environment " + args.environment + " "
self.reportsName += "_" + self.environment
if self.useLevelEnv:
self.build_log_name = self.reportsName + "_build.log"
else:
self.build_log_name = self.mpName + "_build.log"
self.manageWait = ManageWait(self.verbose, "", 30, 1, self.FullMP, self.ci)
self.cleanup("junit", "test_results_")
self.cleanup("cobertura", "coverage_results_")
self.cleanup("sonarqube", "test_results_")
self.cleanup("pclp", "gl-code-quality-report.json")
self.cleanup(".", self.mpName + "_aggregate_report.html")
self.cleanup(".", self.mpName + "_metrics_report.html")
def cleanup(self, dirName, fname):
for file in glob.glob(os.path.join(self.xml_data_dir, dirName, fname + "*.*")):
try:
os.remove(file);
except:
print("Error removing file after failed to remove directory: " + file)
try:
shutil.rmtree(os.path.join(self.xml_data_dir , dirName))
except:
pass
def generateIndexHtml(self):
if not checkVectorCASTVersion(21):
print("Cannot create index.html. Please upgrade VectorCAST")
else:
print("Creating index.html")
try:
prj_dir = os.environ['CI_PROJECT_DIR'].replace("\\","/") + "/"
except:
prj_dir = os.getcwd().replace("\\","/") + "/"
tempHtmlReportList = glob.glob("*.html")
tempHtmlReportList += glob.glob(os.path.join(args.html_base_dir, "*.html"))
htmlReportList = []
for report in tempHtmlReportList:
if "index.html" not in report:
report = report.replace("\\","/")
report = report.replace(prj_dir,"")
htmlReportList.append(report)
from create_index_html import create_index_html
create_index_html(self.FullMP)
def runJunitMetrics(self):
print("Creating JUnit Metrics")
generate_results.verbose = self.verbose
generate_results.print_exc = self.print_exc
generate_results.timing = self.timing
if checkVectorCASTVersion(21, quiet=True):
self.useStartLine = True
else:
self.useStartLine = False
self.failed_count, self.passed_count = generate_results.buildReports(
FullManageProjectName = self.FullMP,
level =self.level,
envName = self.environment,
generate_individual_reports = True,
timing = self.timing,
cbtDict = None,
use_archive_extract = False,
report_only_failures = False,
no_full_report = False,
use_ci = self.ci,
xml_data_dir = self.xml_data_dir,
useStartLine = self.useStartLine)
# calculate the failed percentage
if (self.failed_count + self.passed_count > 0):
self.failed_pct = 100 * self.failed_count/ (self.failed_count + self.passed_count)
else:
self.failed_pct = 0
# if the failed percentage is less that the specified limit (default = 0)
# clear the failed count
if self.useJunitFailCountPct and self.failed_pct < self.junit_percent_to_fail:
self.failed_count = 0
self.needIndexHtml = True
def runLcovMetrics(self):
if not checkVectorCASTVersion(21):
print("XXX Cannot create LCOV metrics. Please upgrade VectorCAST\n")
else:
print("Creating LCOV Metrics")
generate_lcov.generateCoverageResults(self.FullMP, self.xml_data_dir, verbose = self.verbose, source_root = self.source_root)
def runCoberturaMetrics(self):
if not checkVectorCASTVersion(21):
print("Cannot create Cobertura metrics. Please upgrade VectorCAST")
else:
if self.cobertura_extended:
print("Creating Extended Cobertura Metrics")
else:
print("Creating Cobertura Metrics")
cobertura.generateCoverageResults(self.FullMP, self.azure, self.xml_data_dir, verbose = self.verbose,
extended=self.cobertura_extended, source_root = self.source_root)
def runSonarQubeMetrics(self):
if not checkVectorCASTVersion(21):
print("Cannot create SonarQube metrics. Please upgrade VectorCAST")
else:
print("Creating SonarQube Metrics")
generate_sonarqube_testresults.run(self.FullMP, self.xml_data_dir)
def runPcLintPlusMetrics(self):
print("Creating PC-lint Plus Metrics")
import generate_pclp_reports
os.makedirs(os.path.join(self.xml_data_dir,"pclp"))
report_name = os.path.join(self.xml_data_dir,"pclp","gl-code-quality-report.json")
print("PC-lint Plus Metrics file: " + report_name)
generate_pclp_reports.generate_reports(self.pclp_input, output_gitlab = report_name)
if args.pclp_output_html:
if not checkVectorCASTVersion(21):
print("Cannot create PC-Lint Plus HTML report. Please upgrade VectorCAST")
else:
print("Creating PC-lint Plus Findings")
import generate_pclp_reports
generate_pclp_reports.generate_html_report(self.FullMP, self.pclp_input, self.pclp_output_html)
def runReports(self):
if self.aggregate:
self.manageWait.exec_manage_command ("--create-report=aggregate --output=" + self.mpName + "_aggregate_report.html")
self.needIndexHtml = True
if self.metrics:
self.manageWait.exec_manage_command ("--create-report=metrics --output=" + self.mpName + "_metrics_report.html")
self.needIndexHtml = True
if self.fullstatus:
self.manageWait.exec_manage_command ("--full-status=" + self.mpName + "_full_status_report.html")
self.needIndexHtml = True
def exportRgw(self):
rgw.updateReqRepo(VC_Manage_Project=self.FullMP, VC_Workspace=os.getcwd() , top_level=False)
self.manageWait.exec_manage_command ("--clicast-args rgw export")
def runExec(self):
self.manageWait.exec_manage_command ("--status")
self.manageWait.exec_manage_command ("--force --release-locks")
self.manageWait.exec_manage_command ("--config VCAST_CUSTOM_REPORT_FORMAT=HTML")
if self.useLevelEnv:
output = "--output " + self.mpName + self.reportsName + "_rebuild.html"
else:
output = ""
if self.jobs != "1" and checkVectorCASTVersion(20, True):
# setup project for parallel execution
self.manageWait.exec_manage_command ("--config VCAST_DEPENDENCY_CACHE_DIR=./vcqik")
# should work for pre-vcast parallel_build_execute or vcast parallel_build_execute
pstr = "--project " + self.FullMP
jstr = "--jobs="+str(self.jobs)
cstr = "" if (self.compiler == None) else "--compiler="+self.compiler
tstr = "" if (self.testsuite == None) else "--testsuite="+self.testsuite
cbtStr = self.useCBT
ciStr = self.useCI
vbStr = "--verbose" if (self.verbose) else ""
# filter out the blank ones
callList = []
for s in [pstr, jstr, cstr, tstr, cbtStr, ciStr, vbStr, self.vcast_action]:
if s != "":
s = s.strip()
callList.append(s)
callStr = " ".join(callList)
parallel_build_execute.parallel_build_execute(callStr)
else:
cmd = "--" + self.build_execute + " " + self.useCBT + self.level_option + self.env_option + output
build_log = self.manageWait.exec_manage_command (cmd)
open(self.build_log_name,"w").write(build_log)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('ManageProject', help='VectorCAST Project Name')
actionGroup = parser.add_argument_group('Script Actions', 'Options for the main tasks')
actionGroup.add_argument('--build-execute', help='Builds and exeuctes the VectorCAST Project', action="store_true", default = False)
parser_specify = actionGroup.add_mutually_exclusive_group()
parser_specify.add_argument('--build', help='Only builds the VectorCAST Project', action="store_true", default = False)
parser_specify.add_argument('--incremental', help='Use Change Based Testing (Cannot be used with --build)', action="store_true", default = False)
metricsGroup = parser.add_argument_group('Metrics Options', 'Options generating metrics')
metricsGroup.add_argument('--output_dir', help='Set the base directory of the xml_data directory. Default is the workspace directory', default = None)
metricsGroup.add_argument('--source_root', help='Set the absolute path for the source file in coverage reporting', default = "")
metricsGroup.add_argument("--html_base_dir", help='Set the base directory of the html_reports directory. The default is the workspace directory', default = "html_reports")
metricsGroup.add_argument('--cobertura', help='Generate coverage results in Cobertura xml format', action="store_true", default = False)
metricsGroup.add_argument('--cobertura_extended', help='Generate coverage results in extended Cobertura xml format', action="store_true", default = False)
metricsGroup.add_argument('--lcov', help='Generate coverage results in an LCOV format', action="store_true", default = False)
metricsGroup.add_argument('--junit', help='Generate test results in Junit xml format', action="store_true", default = False)
metricsGroup.add_argument('--export_rgw', help='Export RGW data', action="store_true", default = False)
metricsGroup.add_argument('--junit_use_cte_for_classname', help=argparse.SUPPRESS, action="store_true", dest="use_cte")
metricsGroup.add_argument('--sonarqube', help='Generate test results in SonarQube Generic test execution report format (CppUnit)', action="store_true", default = False)
metricsGroup.add_argument('--pclp_input', help='Generate static analysis results from PC-lint Plus XML file to generic static analysis format (codequality)', action="store", default = None)
metricsGroup.add_argument('--pclp_output_html', help='Generate static analysis results from PC-lint Plus XML file to an HTML output', action="store", default = "pclp_findings.html")
metricsGroup.add_argument('--exit_with_failed_count', help='Returns failed test case count as script exit. Set a value to indicate a percentage above which the job will be marked as failed',
nargs='?', default='not present', const='(default 0)')
reportGroup = parser.add_argument_group('Report Selection', 'VectorCAST Manage reports that can be generated')
reportGroup.add_argument('--aggregate', help='Generate aggregate coverage report VectorCAST Project', action="store_true", default = False)
reportGroup.add_argument('--metrics', help='Generate metrics reports for VectorCAST Project', action="store_true", default = False)
reportGroup.add_argument('--fullstatus', help='Generate full status reports for VectorCAST Project', action="store_true", default = False)
beGroup = parser.add_argument_group('Build/Execution Options', 'Options that effect build/execute operation')
beGroup.add_argument('--jobs', help='Number of concurrent jobs (default = 1)', default="1")
beGroup.add_argument('--ci', help='Use Continuous Integration Licenses', action="store_true", default = False)
beGroup.add_argument('-l', '--level', help='Environment Name if only doing single environment. Should be in the form of compiler/testsuite', default=None)
beGroup.add_argument('-e', '--environment', help='Environment Name if only doing single environment.', default=None)
parser_specify = beGroup.add_mutually_exclusive_group()
parser_specify.add_argument('--gitlab', help='Build using GitLab CI (default)', action="store_true", default = True)
parser_specify.add_argument('--azure', help='Build using Azure DevOps', action="store_true", default = False)
actionGroup = parser.add_argument_group('Script Debug ', 'Options used for debugging the script')
actionGroup.add_argument('--print_exc', help='Prints exceptions', action="store_true", default = False)
actionGroup.add_argument('--timing', help='Prints timing information for metrics generation', action="store_true", default = False)
actionGroup.add_argument('-v', '--verbose', help='Enable verbose output', action="store_true", default = False)
args = parser.parse_args()
if args.ci:
os.environ['VCAST_USE_CI_LICENSES'] = "1"
os.environ['VCAST_MANAGE_PROJECT_DIRECTORY'] = os.path.abspath(args.ManageProject).rsplit(".",1)[0]
if not os.path.isfile(args.ManageProject):
print ("Manage project (.vcm file) provided does not exist: " + args.ManageProject)
print ("exiting...")
sys.exit(-1)
vcExec = VectorCASTExecute(args)
if args.build_execute or args.build:
vcExec.runExec()
if args.junit or vcExec.useJunitFailCountPct:
vcExec.runJunitMetrics()
if args.cobertura or args.cobertura_extended:
vcExec.runCoberturaMetrics()
if args.lcov:
vcExec.runLcovMetrics()
if args.sonarqube:
vcExec.runSonarQubeMetrics()
if args.pclp_input:
vcExec.runPcLintPlusMetrics()
if args.aggregate or args.metrics or args.fullstatus:
vcExec.runReports()
if vcExec.useJunitFailCountPct:
print("--exit_with_failed_count=" + args.exit_with_failed_count + " specified. Fail Percent = " + str(round(vcExec.failed_pct,0)) + "% Return code: ", str(vcExec.failed_count))
sys.exit(vcExec.failed_count)
if vcExec.needIndexHtml:
vcExec.generateIndexHtml()
if args.export_rgw:
vcExec.exportRgw()