blob: a47282a22b99401770b68f07c51e5c8ed236e17b [file] [log] [blame]
Brian O'Connor9c2c8232016-11-08 17:13:14 -08001#!/usr/bin/env python
2
3# This script prepares this ONOS directory so that the Sonar Scanner can be run.
4# - Build ONOS
5# - Run tests on a per module basis, stage surefire-reports and jacoco.exec
6# - Generate sonar-project.properties file
7
8import json
9import os
10
11from shutil import copy, copytree, rmtree
12from subprocess import call, check_call, check_output
13
Ray Milkey924c0e32016-11-18 13:47:14 -080014# FIXME pull the version from the Buck version file
Ray Milkey32963cf2018-11-21 09:31:51 -080015ONOS_VERSION = '2.0.0-SNAPSHOT'
Ray Milkey924c0e32016-11-18 13:47:14 -080016
Brian O'Connor9c2c8232016-11-08 17:13:14 -080017# SonarQube property file name and template
18FILE_NAME = 'sonar-project.properties'
19ROOT_TEMPLATE = '''# Auto-generated properties file
20sonar.projectKey=%(key)s
21sonar.projectName=%(name)s
22sonar.projectVersion=%(version)s
Ray Milkey924c0e32016-11-18 13:47:14 -080023
Brian O'Connor9c2c8232016-11-08 17:13:14 -080024#sonar.sources=src
25sonar.sourceEncoding=UTF-8
26sonar.java.target = 1.8
27sonar.java.source = 1.8
28sonar.language=java
29
30sonar.junit.reportsPath = surefire-reports
31# global jacoco
32#sonar.jacoco.reportPath = %(jacoco)s
33# local jacoco
34sonar.jacoco.reportPath = jacoco.exec
35
36sonar.modules=%(modules)s
37
38'''
39
Ray Milkey32963cf2018-11-21 09:31:51 -080040black_list = ["//protocols/grpc:grpc-core-repkg", "//apps/openstacktelemetry:grpc-core-repkg"]
Brian O'Connor9c2c8232016-11-08 17:13:14 -080041
42# Change to $ONOS_ROOT
Ray Milkey32963cf2018-11-21 09:31:51 -080043ONOS_ROOT = os.environ['ONOS_ROOT']
Brian O'Connor9c2c8232016-11-08 17:13:14 -080044if ONOS_ROOT:
Ray Milkey32963cf2018-11-21 09:31:51 -080045 os.chdir(ONOS_ROOT)
46
Brian O'Connor9c2c8232016-11-08 17:13:14 -080047
48def splitTarget(target):
Ray Milkey32963cf2018-11-21 09:31:51 -080049 path, module = target.split(':', 2)
50 path = path.replace('//', '', 1)
51 return path, module
52
Brian O'Connor9c2c8232016-11-08 17:13:14 -080053
54def runCmd(cmd):
Ray Milkey32963cf2018-11-21 09:31:51 -080055 output = check_output(cmd).rstrip()
56 return output.split('\n') if output else []
Brian O'Connor9c2c8232016-11-08 17:13:14 -080057
Brian O'Connor9c2c8232016-11-08 17:13:14 -080058
Ray Milkey32963cf2018-11-21 09:31:51 -080059# build ONOS
60runCmd(["bazel", "build", "onos"])
Brian O'Connor9c2c8232016-11-08 17:13:14 -080061
Ray Milkey32963cf2018-11-21 09:31:51 -080062# Find all onos OSGi jar file rules
63targets = runCmd(["bazel", "query", "kind('_bnd', '//...')"])
Ray Milkey81df92a2018-11-21 13:51:16 -080064targets = [target for target in targets if not target in black_list]
Ray Milkey32963cf2018-11-21 09:31:51 -080065# Uncomment this for easier debugging of a single package
66# targets = ['//core/net:onos-core-net']
Brian O'Connor9c2c8232016-11-08 17:13:14 -080067
68# Find all tests associated with onos_jar rules
Ray Milkey32963cf2018-11-21 09:31:51 -080069# FIXME we may want to insert kind('java_test', testsof...)
70# output = runCmd([BUCK, 'query', '--json', "testsof('%s')"] + targets)
71# test_map = json.loads(output[0])
Brian O'Connor9c2c8232016-11-08 17:13:14 -080072
73# Flatten the values in the test target map
Ray Milkey32963cf2018-11-21 09:31:51 -080074# test_targets = [t for ts in test_map.values() for t in ts]
75# print test_targets
Brian O'Connor9c2c8232016-11-08 17:13:14 -080076
77# Build run tests
Ray Milkey32963cf2018-11-21 09:31:51 -080078# print runCmd([BUCK, 'test', '--no-cache', '--code-coverage', '--no-results-cache'] + test_targets)
Brian O'Connor9c2c8232016-11-08 17:13:14 -080079
80# Build the sonar rules for each target
Ray Milkey32963cf2018-11-21 09:31:51 -080081# sonar_files = runCmd([BUCK, 'build', '--show-output'] + ['%s-sonar' % t for t in (targets + test_targets)])
82# sonar_files = dict([i.split(' ') for i in sonar_files[1:]]) # drop the first line; it's boilerplate
83# print sonar_files
Brian O'Connor9c2c8232016-11-08 17:13:14 -080084
85
86def write_module(target, out):
Ray Milkey32963cf2018-11-21 09:31:51 -080087 path, module_name = splitTarget(target)
88 out.write('%s.sonar.projectBaseDir=%s\n' % (module_name, path))
89 out.write('%(name)s.sonar.projectName=%(name)s\n' % {'name': module_name})
90 query = 'labels(srcs, "%s-native")' % target
91 sources = runCmd(['bazel', 'query', query])
92 sources = [file for file in sources if "package-info" not in file]
93 sources_csl = ",".join(sources).replace("//", ONOS_ROOT + "/").replace(":", "/")
94 out.write('%s.sonar.sources=%s\n' % (module_name, sources_csl))
Brian O'Connor9c2c8232016-11-08 17:13:14 -080095
Ray Milkey32963cf2018-11-21 09:31:51 -080096 # tests = test_map[target] if target in test_map else []
Brian O'Connor9c2c8232016-11-08 17:13:14 -080097
Ray Milkey32963cf2018-11-21 09:31:51 -080098 # module_targets = [target] + tests
99 # for property in [sonar_files[t+'-sonar'] for t in module_targets]:
100 # print property
101 # with open(property, 'r') as f:
102 # for line in f.readlines():
103 # out.write('%s.%s' % (module_name, line))
Brian O'Connor9c2c8232016-11-08 17:13:14 -0800104
Ray Milkey32963cf2018-11-21 09:31:51 -0800105
106# if tests:
107# rmtree(path + '/surefire-reports', ignore_errors=True)
108# rmtree('surefire-reports', ignore_errors=True)
109# runCmd([BUCK, 'test',
110# '--no-cache', '--no-results-cache',
111# '--code-coverage',
112# '--no-results-cache',
113# '--surefire-xml', 'surefire-reports'
114# ] + tests)
115# copy('buck-out/gen/jacoco/jacoco.exec', path)
116# #write jacoco.exec path to out; not needed.. this is the default
117# copytree('surefire-reports', path + '/surefire-reports')
118# rmtree('surefire-reports')
Brian O'Connor9c2c8232016-11-08 17:13:14 -0800119
120# Write the sonar properties file
Brian O'Connor9c2c8232016-11-08 17:13:14 -0800121with open(FILE_NAME, 'w') as out:
Ray Milkey32963cf2018-11-21 09:31:51 -0800122 out.write(ROOT_TEMPLATE % {
123 'name': 'onos',
124 'key': 'org.onosproject:onos',
125 'version': ONOS_VERSION,
126 'jacoco': '%s/buck-out/gen/jacoco/jacoco.exec' % ONOS_ROOT,
127 'modules': ','.join([splitTarget(t)[1] for t in targets])
128 })
129 for target in targets:
130 print target
131 write_module(target, out)