-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvcast_exec.py
More file actions
812 lines (651 loc) · 33.7 KB
/
Copy pathvcast_exec.py
File metadata and controls
812 lines (651 loc) · 33.7 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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
#
# 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 patch_rgw_directory as rgw
try:
import generate_results
except:
try:
import importlib
generate_results = importlib.import_module("generate-results")
except:
import imp
script_dir = os.path.dirname(os.path.abspath(__file__))
vc_script = os.path.join(script_dir, "generate-results.py")
generate_results = imp.load_source("generate_results", vc_script)
if sys.version_info[0] < 3:
python_path_updates = os.path.join(os.environ['VECTORCAST_DIR'], "DATA", "python")
sys.path.append(python_path_updates)
try:
import parallel_build_execute
except:
import prevcast_parallel_build_execute as parallel_build_execute
from vcast_utils import checkVectorCASTVersion, dump, getVectorCASTEncoding
from check_build_log import check_build_log
import shlex, platform
from pathlib import Path
import cobertura
from enum import Enum
class CITool(Enum):
JENKINS = "Jenkins"
GITLAB = "GitLab CI"
AZURE = "Azure Pipelines"
GITHUB = "GitHub Actions"
CIRCLECI = "CircleCI"
TRAVIS = "Travis CI"
BITBUCKET = "Bitbucket Pipelines"
TEAMCITY = "TeamCity"
BAMBOO = "Bamboo"
UNKNOWN = "Unknown CI/CD"
def displayVersion():
versionInfo = "Version Unknown"
# Get absolute path to directory containing the current script
script_dir = os.path.dirname(os.path.abspath(__file__))
# Build path to VERSION.txt
version_path = os.path.join(script_dir, 'VERSION.txt')
if os.path.exists(version_path):
with open(version_path,"rb") as fd:
versionInfo = fd.read().decode(self.encFmt, "replace")
print("vc_scripts_submodule Version: " + versionInfo)
else:
print("Can't Read Version of Jenkins Integration. See console log.")
class VectorCASTExecute(object):
def detect_ci_tool(self):
if "JENKINS_URL" in os.environ:
self.ciTool = CITool.JENKINS
elif "GITLAB_CI" in os.environ or self.gitlab:
self.ciTool = CITool.GITLAB
elif "AZURE_PIPELINES" in os.environ or "BUILD_SOURCEVERSION" in os.environ or self.azure:
self.ciTool = CITool.AZURE
elif "GITHUB_ACTIONS" in os.environ:
self.ciTool = CITool.GITHUB
elif "CIRCLECI" in os.environ:
self.ciTool = CITool.CIRCLECI
elif "TRAVIS" in os.environ:
self.ciTool = CITool.TRAVIS
elif "BITBUCKET_BUILD_NUMBER" in os.environ:
self.ciTool = CITool.BITBUCKET
elif "TEAMCITY_VERSION" in os.environ:
self.ciTool = CITool.TEAMCITY
elif "BAMBOO_BUILDNUMBER" in os.environ:
self.ciTool = CITool.BAMBOO
else:
self.ciTool = CITool.UNKNOWN
def __init__(self, args):
self.azure = args.azure
self.gitlab = args.gitlab
self.detect_ci_tool()
# setup default values
self.azure = args.azure
self.gitlab = args.gitlab
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
self.noIndex = args.noindex
if args.exit_with_failed_comp:
self.complexityThreshold = int(args.exit_with_failed_comp)
self.complexityCheck = True
else:
self.complexityThreshold = 100000
self.complexityCheck = False
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
self.passed_count = 0
if args.output_dir:
self.output_dir = args.output_dir
else:
self.output_dir = ""
self.xml_data_dir = "xml_data"
self.xml_data_dir = "xml_data"
if not os.path.exists(self.xml_data_dir):
os.makedirs(self.xml_data_dir)
self.covToDisplay = args.covToDisplay
if args.build and not args.build_execute:
self.build_execute = "--build"
self.vcast_action = "--vcast_action build"
elif args.build_execute:
self.build_execute = "--build-execute"
self.vcast_action = "--vcast_action 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]
self.encFmt = getVectorCASTEncoding()
if args.ci:
self.useCI = "--use_ci"
self.ci = "--ci"
else:
self.useCI = ""
self.ci = ""
self.importedResults = args.importedResults
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(
verbose = self.verbose,
command_line = "",
wait_time = 30,
wait_loops= 1,
mpName = self.FullMP,
useCI = 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")
self.cleanup(".", "*.log")
tmp_xml_dir = self.xml_data_dir
self.xml_data_dir = ""
self.cleanup("coverage")
self.cleanup("reports")
self.cleanup("test-results")
self.xml_data_dir = tmp_xml_dir
def cleanup(self, dirName, fname = ""):
if fname == "":
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 import_env_from_setup(self, setup_path: str) -> None:
print("Running setup script {setup_path}")
setup = Path(setup_path)
if platform.system().lower().startswith("win"):
# call setup_env.bat in cmd.exe and dump env via `set`
cmd = f'call {setup} && set'
p = subprocess.run(["cmd", "/c", cmd], capture_output=True, text=True)
else:
# source setup_env.sh in bash and dump env via `env`
# (setup script must be source-able)
bash_cmd = f'source {shlex.quote(str(setup))} >/dev/null 2>&1; env'
p = subprocess.run(["bash", "-lc", bash_cmd], capture_output=True, text=True)
if p.returncode != 0:
print (f"Setup script failed ({setup_path}) -> errno: {p.returncode}:\n{p.stderr}")
raise RuntimeError(f"Setup script failed ({setup_path}):\n{p.stderr}")
for line in p.stdout.splitlines():
if "=" not in line:
continue
k, v = line.split("=", 1)
os.environ[k] = v
def copyHtmlFiles(self):
source_dir = Path(".")
dest_dir = Path(self.html_base_dir)
if os.path.exists(dest_dir):
shutil.rmtree(dest_dir)
patterns = [
"*.html",
"html_reports/**/*",
"management/**/*",
"xml_data/**/*.html",
]
for pattern in patterns:
for src in glob.glob(str(source_dir / pattern), recursive=True):
src_path = Path(src)
# Skip directories; copy files only
if src_path.is_dir():
continue
# Preserve relative directory structure
rel_path = src_path.relative_to(source_dir)
dst_path = dest_dir / rel_path
# Make destination subdirectories if needed
dst_path.parent.mkdir(parents=True, exist_ok=True)
# Copy file metadata too
shutil.copy2(src_path, dst_path)
def copyXmlData(self):
source_dir = Path(".")
dest_dir = Path(self.output_dir)
patterns = [
"xml_data/**/*",
]
for pattern in patterns:
for src in glob.glob(str(source_dir / pattern), recursive=True):
src_path = Path(src)
# Skip directories; copy files only
if src_path.is_dir():
continue
# Preserve relative directory structure
rel_path = src_path.relative_to(source_dir)
dst_path = dest_dir / rel_path
# Make destination subdirectories if needed
dst_path.parent.mkdir(parents=True, exist_ok=True)
# Copy file metadata too
shutil.copy2(src_path, dst_path)
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('xml_data', "*.html"))
#tempHtmlReportList += glob.glob(os.path.join('html_reports', "*.html"))
tempHtmlReportList += glob.glob(os.path.join(self.xml_data_dir, "*.html"))
tempHtmlReportList += glob.glob(os.path.join(self.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, self.ciTool == CITool.GITLAB)
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 = True,
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")
import generate_lcov
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,
covToDisplay = self.covToDisplay)
def runSonarQubeMetrics(self):
if not checkVectorCASTVersion(21):
print("Cannot create SonarQube metrics. Please upgrade VectorCAST")
else:
print("Creating SonarQube Metrics")
import generate_sonarqube_testresults
generate_sonarqube_testresults.run(self.FullMP, self.xml_data_dir)
def runPcLintPlusMetrics(self):
if not checkVectorCASTVersion(21):
print("Cannot create PC-Lint Plus HTML report. Please upgrade VectorCAST")
else:
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 self.pclp_output_html:
print("Creating PC-lint Plus Findings")
generate_pclp_reports.generate_html_report(self.FullMP, self.pclp_input, self.pclp_output_html)
self.needIndexHtml = True
def runReports(self):
if self.aggregate:
agg_rpt_name = self.mpName + "_aggregate_report.html"
print("Creating Aggregate Coverage Report")
if os.path.exists(agg_rpt_name):
os.remove(agg_rpt_name)
self.manageWait.exec_manage_command ("--create-report=aggregate --output=" + agg_rpt_name)
self.needIndexHtml = True
if self.metrics:
met_rpt_name = self.mpName + "_metrics_report.html"
print("Creating Metrics Report")
if os.path.exists(met_rpt_name):
os.remove(met_rpt_name)
self.manageWait.exec_manage_command ("--create-report=metrics --output=" + met_rpt_name)
self.needIndexHtml = True
if self.fullstatus:
fs_rpt_name =self.mpName + "_full_status_report.html"
if os.path.exists(fs_rpt_name):
os.remove(fs_rpt_name)
print("Creating Full Status Report")
self.manageWait.exec_manage_command ("--full-status=" + fs_rpt_name)
self.needIndexHtml = True
def reportCreate(self, report_type, desc):
from vector.apps.DataAPI.vcproject_api import VCProjectApi
from vector.apps.DataAPI.cover_api import CoverApi
with VCProjectApi(self.FullMP) as vcproj:
forCover = {"FULL_REPORT": "AGGREGATE_REPORT",
"MANAGEMENT_REPORT": "COVER_MANAGEMENT_REPORT"}
for env in vcproj.Environment.all():
if not env.is_active:
continue
self.needIndexHtml = True
if "MANAGEMENT_REPORT" in report_type:
report_name = env.compiler.name + "_" + env.testsuite.name + "_" + env.name + "_management_report.html"
report_name = os.path.join("management",report_name)
level = env.level.name
print("Creating {} HTML report for {} in {}".format(desc, env.name, report_name))
else:
report_name = env.compiler.name + "_" + env.testsuite.name + "_" + env.name + "_full_report.html"
report_name = os.path.join("management",report_name)
print("Creating {} HTML report for {} in {}".format(desc, env.name, report_name))
if env.api:
if isinstance(env.api, CoverApi):
env.api.report(report_type=forCover[report_type], formats=["HTML"], output_file=report_name)
else:
env.api.report(report_type=report_type, formats=["HTML"], output_file=report_name)
else:
print(f"{env.name} has a null env.api {env.api}")
return
def generateTestCaseMgtRpt(self):
if not os.path.exists("management"):
os.makedirs("management")
else:
for file in glob.glob(os.path.join("management","*_management_report.html")):
os.remove(file)
if checkVectorCASTVersion(21):
print("Creating Test Case Management HTML report")
self.reportCreate(
report_type = "MANAGEMENT_REPORT",
desc = "Test Case Management"
)
else:
print("Cannot create Test Case Management HTML report. Please upgrade VectorCAST")
def generateUtFullReport(self):
if not os.path.exists("management"):
os.makedirs("management")
else:
for file in glob.glob(os.path.join("management","*_full_report.html")):
os.remove(file)
if checkVectorCASTVersion(21):
print("Creating Unit Test Case Full Report")
self.reportCreate(
report_type = "FULL_REPORT",
desc = "Full Report"
)
else:
print("Cannot create Test Case Management HTML report. Please upgrade VectorCAST")
def exportRgw(self):
print("Creating RGW Exports")
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):
print("Building {} with {} {}".format(self.FullMP,self.build_execute, self.useCBT))
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.importedResults:
self.manageWait.exec_manage_command (f"--force --import-result={self.importedResults}")
self.manageWait.exec_manage_command ("--status")
if self.useLevelEnv:
output = "--output " + self.mpName + self.reportsName + "_rebuild.html"
else:
output = "--output " + self.mpName + "_rebuild.html"
if checkVectorCASTVersion(25, True):
useParallelManageCommand = True
else:
useParallelManageCommand = False
if self.jobs != "1" and checkVectorCASTVersion(20, True) and not useParallelManageCommand:
# 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)
print("Build/Execute in parallel using {}".format(" ".join(callList)))
parallel_build_execute.parallel_build_execute(callStr)
else:
if useParallelManageCommand:
jstr = "--jobs=" + str(self.jobs)
else:
jstr = ""
cmd = "{} {} {} {} {} {} {}".format(self.build_execute, self.level_option , self.useCBT, self.env_option, jstr, "--verbose", output)
print("Build/Execute in using manage command with options: {}".format(cmd))
build_log = self.manageWait.exec_manage_command (cmd)
if os.path.exists("command.log"):
shutil.copyfile('command.log', "complete_build.log")
with open(self.build_log_name,"wb") as fd:
fd.write(build_log.encode(self.encFmt, "replace"))
def getReturnCode(self):
msgs = []
complexityFailureCount = 0
if self.complexityCheck:
for key in cobertura.vgByFunction:
if cobertura.vgByFunction[key] > self.complexityThreshold:
file, func = key.split("::")
print (f"[ERROR] \n File : {file}\n Function: {func}\n Message : COMPLEXITY is greater than {self.complexityThreshold}")
complexityFailureCount += 1
if complexityFailureCount > 0:
msgs.append(f"{complexityFailureCount} complexity failures")
if args.check_build_log:
if check_build_log(self.build_log_name) == 2:
msgs.append(f"Build log error. See information above...")
if self.useJunitFailCountPct:
print(f"[ERROR] exit_with_failed_count={args.exit_with_failed_count} specified. Fail Percent = {round(self.failed_pct,0)}% Return code: {self.failed_count}")
msgs.append(f"Tests case failues greater than {args.exit_with_failed_count} specified")
if msgs:
return " ; ".join(msgs)
else:
return 0
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('ManageProject', nargs='?', help='VectorCAST Project Name', default="")
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)
actionGroup.add_argument("--setup", default="", help="Path to setup_env.bat/.sh (optional)")
actionGroup.add_argument('--use_imported_result', help='Use existing VCR file from repository for CBT via Imported Results', dest="importedResults", default = None)
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)')
metricsGroup.add_argument('--exit_with_failed_comp', help='Returns failed if any of the functions have a Complexity (Vg) > value.',
nargs='?', default=None)
metricsGroup.add_argument('--check_build_log', help='Checks build log for a list of error phrases. Returns failure if any are found.',
action="store_true", default = False)
metricsGroup.add_argument("--covToDisplay", type=str.lower, choices=["statement", "branch", "mcdc", "function", "functioncall"],
default="statement",help='Selects which coverage to display for coverage print. Default is "statement".',)
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)
reportGroup.add_argument('--utfull', help='Generate Full Reports for each VectorCAST environment in project', action="store_true", default = False)
reportGroup.add_argument('--tcmr', help='Generate Test Cases Management Reports for each VectorCAST environment in project', action="store_true", default = False)
reportGroup.add_argument('--noindex', help='Stops index.html report that ties all the other HTML reports together from being created', 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)
actionGroup.add_argument('--version', help='Displays the version information', action="store_true", default = False)
args = parser.parse_args()
if args.importedResults and not args.incremental:
print("[INFO] Calling conflict of --use_import_result and not --incremental")
print("[INFO] Calling it this way ignores --use_imported_result")
if args.exit_with_failed_comp and not args.cobertura and not args.cobertura_extended:
args.cobertura = True
if args.verbose:
import sys, shlex
print("argv:", shlex.join(sys.argv)) # py3.8+
if args.version:
displayVersion()
sys.exit(0)
# Conditional requirement check
if not args.version and not args.ManageProject:
parser.error("ManageProject is required unless --version is specified")
sys.exit(0)
if not args.ManageProject.endswith(".vcm"):
args.ManageProject += ".vcm"
if args.ManageProject and not os.path.isfile(args.ManageProject):
print ("Manage project (.vcm file) provided does not exist: " + args.ManageProject)
print ("exiting...")
sys.exit(-1)
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.setup:
vcExec.import_env_from_setup(args.setup)
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 args.tcmr:
vcExec.generateTestCaseMgtRpt()
if args.utfull:
vcExec.generateUtFullReport()
if vcExec.needIndexHtml and not vcExec.noIndex:
vcExec.generateIndexHtml()
if args.export_rgw:
vcExec.exportRgw()
if args.html_base_dir != "html_reports":
vcExec.copyHtmlFiles()
if args.output_dir:
vcExec.copyXmlData()
returnCode = vcExec.getReturnCode()
sys.exit(returnCode)