blob: 47d1ece0d0169ab49d466d03f8d41a096d4c1fb4 [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', '//...')"])
64# Uncomment this for easier debugging of a single package
65# targets = ['//core/net:onos-core-net']
Brian O'Connor9c2c8232016-11-08 17:13:14 -080066
67# Find all tests associated with onos_jar rules
Ray Milkey32963cf2018-11-21 09:31:51 -080068# FIXME we may want to insert kind('java_test', testsof...)
69# output = runCmd([BUCK, 'query', '--json', "testsof('%s')"] + targets)
70# test_map = json.loads(output[0])
Brian O'Connor9c2c8232016-11-08 17:13:14 -080071
72# Flatten the values in the test target map
Ray Milkey32963cf2018-11-21 09:31:51 -080073# test_targets = [t for ts in test_map.values() for t in ts]
74# print test_targets
Brian O'Connor9c2c8232016-11-08 17:13:14 -080075
76# Build run tests
Ray Milkey32963cf2018-11-21 09:31:51 -080077# print runCmd([BUCK, 'test', '--no-cache', '--code-coverage', '--no-results-cache'] + test_targets)
Brian O'Connor9c2c8232016-11-08 17:13:14 -080078
79# Build the sonar rules for each target
Ray Milkey32963cf2018-11-21 09:31:51 -080080# sonar_files = runCmd([BUCK, 'build', '--show-output'] + ['%s-sonar' % t for t in (targets + test_targets)])
81# sonar_files = dict([i.split(' ') for i in sonar_files[1:]]) # drop the first line; it's boilerplate
82# print sonar_files
Brian O'Connor9c2c8232016-11-08 17:13:14 -080083
84
85def write_module(target, out):
Ray Milkey32963cf2018-11-21 09:31:51 -080086 if target in black_list:
87 return
88 path, module_name = splitTarget(target)
89 out.write('%s.sonar.projectBaseDir=%s\n' % (module_name, path))
90 out.write('%(name)s.sonar.projectName=%(name)s\n' % {'name': module_name})
91 query = 'labels(srcs, "%s-native")' % target
92 sources = runCmd(['bazel', 'query', query])
93 sources = [file for file in sources if "package-info" not in file]
94 sources_csl = ",".join(sources).replace("//", ONOS_ROOT + "/").replace(":", "/")
95 out.write('%s.sonar.sources=%s\n' % (module_name, sources_csl))
Brian O'Connor9c2c8232016-11-08 17:13:14 -080096
Ray Milkey32963cf2018-11-21 09:31:51 -080097 # tests = test_map[target] if target in test_map else []
Brian O'Connor9c2c8232016-11-08 17:13:14 -080098
Ray Milkey32963cf2018-11-21 09:31:51 -080099 # module_targets = [target] + tests
100 # for property in [sonar_files[t+'-sonar'] for t in module_targets]:
101 # print property
102 # with open(property, 'r') as f:
103 # for line in f.readlines():
104 # out.write('%s.%s' % (module_name, line))
Brian O'Connor9c2c8232016-11-08 17:13:14 -0800105
Ray Milkey32963cf2018-11-21 09:31:51 -0800106
107# if tests:
108# rmtree(path + '/surefire-reports', ignore_errors=True)
109# rmtree('surefire-reports', ignore_errors=True)
110# runCmd([BUCK, 'test',
111# '--no-cache', '--no-results-cache',
112# '--code-coverage',
113# '--no-results-cache',
114# '--surefire-xml', 'surefire-reports'
115# ] + tests)
116# copy('buck-out/gen/jacoco/jacoco.exec', path)
117# #write jacoco.exec path to out; not needed.. this is the default
118# copytree('surefire-reports', path + '/surefire-reports')
119# rmtree('surefire-reports')
Brian O'Connor9c2c8232016-11-08 17:13:14 -0800120
121# Write the sonar properties file
Brian O'Connor9c2c8232016-11-08 17:13:14 -0800122with open(FILE_NAME, 'w') as out:
Ray Milkey32963cf2018-11-21 09:31:51 -0800123 out.write(ROOT_TEMPLATE % {
124 'name': 'onos',
125 'key': 'org.onosproject:onos',
126 'version': ONOS_VERSION,
127 'jacoco': '%s/buck-out/gen/jacoco/jacoco.exec' % ONOS_ROOT,
128 'modules': ','.join([splitTarget(t)[1] for t in targets])
129 })
130 for target in targets:
131 print target
132 write_module(target, out)