blob: baa788805152c6569a943c8d4f4d78213d8420dd [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
Brian O'Connor9c2c8232016-11-08 17:13:14 -080030
31sonar.modules=%(modules)s
32
33'''
34
Ray Milkey926e6bd2018-11-21 16:35:55 -080035black_list = ["//protocols/grpc:grpc-core-repkg",
36 "//apps/openstacktelemetry:grpc-core-repkg",
37 "//web/gui2:_onos-gui2-base-jar"]
Brian O'Connor9c2c8232016-11-08 17:13:14 -080038
39# Change to $ONOS_ROOT
Ray Milkey32963cf2018-11-21 09:31:51 -080040ONOS_ROOT = os.environ['ONOS_ROOT']
Brian O'Connor9c2c8232016-11-08 17:13:14 -080041if ONOS_ROOT:
Ray Milkey32963cf2018-11-21 09:31:51 -080042 os.chdir(ONOS_ROOT)
43
Brian O'Connor9c2c8232016-11-08 17:13:14 -080044
45def splitTarget(target):
Ray Milkey32963cf2018-11-21 09:31:51 -080046 path, module = target.split(':', 2)
47 path = path.replace('//', '', 1)
48 return path, module
49
Brian O'Connor9c2c8232016-11-08 17:13:14 -080050
51def runCmd(cmd):
Ray Milkey32963cf2018-11-21 09:31:51 -080052 output = check_output(cmd).rstrip()
53 return output.split('\n') if output else []
Brian O'Connor9c2c8232016-11-08 17:13:14 -080054
Brian O'Connor9c2c8232016-11-08 17:13:14 -080055
Ray Milkey32963cf2018-11-21 09:31:51 -080056# build ONOS
57runCmd(["bazel", "build", "onos"])
Brian O'Connor9c2c8232016-11-08 17:13:14 -080058
Ray Milkey32963cf2018-11-21 09:31:51 -080059# Find all onos OSGi jar file rules
60targets = runCmd(["bazel", "query", "kind('_bnd', '//...')"])
Ray Milkey81df92a2018-11-21 13:51:16 -080061targets = [target for target in targets if not target in black_list]
Ray Milkey32963cf2018-11-21 09:31:51 -080062# Uncomment this for easier debugging of a single package
63# targets = ['//core/net:onos-core-net']
Brian O'Connor9c2c8232016-11-08 17:13:14 -080064
65# Find all tests associated with onos_jar rules
Ray Milkey32963cf2018-11-21 09:31:51 -080066# FIXME we may want to insert kind('java_test', testsof...)
67# output = runCmd([BUCK, 'query', '--json', "testsof('%s')"] + targets)
68# test_map = json.loads(output[0])
Brian O'Connor9c2c8232016-11-08 17:13:14 -080069
70# Flatten the values in the test target map
Ray Milkey32963cf2018-11-21 09:31:51 -080071# test_targets = [t for ts in test_map.values() for t in ts]
72# print test_targets
Brian O'Connor9c2c8232016-11-08 17:13:14 -080073
74# Build run tests
Ray Milkey32963cf2018-11-21 09:31:51 -080075# print runCmd([BUCK, 'test', '--no-cache', '--code-coverage', '--no-results-cache'] + test_targets)
Brian O'Connor9c2c8232016-11-08 17:13:14 -080076
77# Build the sonar rules for each target
Ray Milkey32963cf2018-11-21 09:31:51 -080078# sonar_files = runCmd([BUCK, 'build', '--show-output'] + ['%s-sonar' % t for t in (targets + test_targets)])
79# sonar_files = dict([i.split(' ') for i in sonar_files[1:]]) # drop the first line; it's boilerplate
80# print sonar_files
Brian O'Connor9c2c8232016-11-08 17:13:14 -080081
82
83def write_module(target, out):
Ray Milkey32963cf2018-11-21 09:31:51 -080084 path, module_name = splitTarget(target)
85 out.write('%s.sonar.projectBaseDir=%s\n' % (module_name, path))
86 out.write('%(name)s.sonar.projectName=%(name)s\n' % {'name': module_name})
87 query = 'labels(srcs, "%s-native")' % target
88 sources = runCmd(['bazel', 'query', query])
Ray Milkey926e6bd2018-11-21 16:35:55 -080089 sources = [file for file in sources if "package-info" not in file and ".java" in file]
90 print sources
Ray Milkey32963cf2018-11-21 09:31:51 -080091 sources_csl = ",".join(sources).replace("//", ONOS_ROOT + "/").replace(":", "/")
92 out.write('%s.sonar.sources=%s\n' % (module_name, sources_csl))
Brian O'Connor9c2c8232016-11-08 17:13:14 -080093
Ray Milkey32963cf2018-11-21 09:31:51 -080094 # tests = test_map[target] if target in test_map else []
Brian O'Connor9c2c8232016-11-08 17:13:14 -080095
Ray Milkey32963cf2018-11-21 09:31:51 -080096 # module_targets = [target] + tests
97 # for property in [sonar_files[t+'-sonar'] for t in module_targets]:
98 # print property
99 # with open(property, 'r') as f:
100 # for line in f.readlines():
101 # out.write('%s.%s' % (module_name, line))
Brian O'Connor9c2c8232016-11-08 17:13:14 -0800102
Ray Milkey32963cf2018-11-21 09:31:51 -0800103
104# if tests:
105# rmtree(path + '/surefire-reports', ignore_errors=True)
106# rmtree('surefire-reports', ignore_errors=True)
107# runCmd([BUCK, 'test',
108# '--no-cache', '--no-results-cache',
109# '--code-coverage',
110# '--no-results-cache',
111# '--surefire-xml', 'surefire-reports'
112# ] + tests)
113# copy('buck-out/gen/jacoco/jacoco.exec', path)
114# #write jacoco.exec path to out; not needed.. this is the default
115# copytree('surefire-reports', path + '/surefire-reports')
116# rmtree('surefire-reports')
Brian O'Connor9c2c8232016-11-08 17:13:14 -0800117
118# Write the sonar properties file
Brian O'Connor9c2c8232016-11-08 17:13:14 -0800119with open(FILE_NAME, 'w') as out:
Ray Milkey32963cf2018-11-21 09:31:51 -0800120 out.write(ROOT_TEMPLATE % {
121 'name': 'onos',
122 'key': 'org.onosproject:onos',
123 'version': ONOS_VERSION,
Ray Milkey926e6bd2018-11-21 16:35:55 -0800124 #'jacoco': '%s/buck-out/gen/jacoco/jacoco.exec' % ONOS_ROOT,
Ray Milkey32963cf2018-11-21 09:31:51 -0800125 'modules': ','.join([splitTarget(t)[1] for t in targets])
126 })
127 for target in targets:
128 print target
129 write_module(target, out)