blob: 49bbfc8cc4b226cc6217066013639412a6a9192e [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
ONOS Jenkins User1733ccf2018-04-20 00:49:12 +000015ONOS_VERSION='1.13.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
40BUCK = 'onos-buck'
41
42# Change to $ONOS_ROOT
43ONOS_ROOT = os.environ[ 'ONOS_ROOT' ]
44if ONOS_ROOT:
45 os.chdir( ONOS_ROOT )
46
47def splitTarget(target):
48 path, module = target.split(':', 2)
49 path = path.replace('//', '', 1)
50 return path, module
51
52def runCmd(cmd):
53 output = check_output( cmd ).rstrip()
54 return output.split('\n') if output else []
55
56# TODO do we need to start with a clean slate?
57#runCmd([BUCK, 'clean'])
58
59# Find all onos_jar rules
60targets = runCmd([BUCK, 'query', "kind('onos_jar', '//...')"])
61#targets = ['//core/net:onos-core-net']
62print targets
63non_osgi_targets = ['%s#non-osgi' % t for t in targets]
64
65# Build all targets to fill buck-out/bin
66print runCmd([BUCK, 'build', '--no-cache'] + targets + non_osgi_targets)
67
68# Find all tests associated with onos_jar rules
69#FIXME we may want to insert kind('java_test', testsof...)
70output = runCmd([BUCK, 'query', '--json', "testsof('%s')"] + targets)
71test_map = json.loads(output[0])
72
73# Flatten the values in the test target map
74test_targets = [t for ts in test_map.values() for t in ts]
75print test_targets
76
77# Build run tests
78#print runCmd([BUCK, 'test', '--no-cache', '--code-coverage', '--no-results-cache'] + test_targets)
79
80# Build the sonar rules for each target
81sonar_files = runCmd([BUCK, 'build', '--show-output'] + ['%s-sonar' % t for t in (targets + test_targets)])
82sonar_files = dict([i.split(' ') for i in sonar_files[1:]]) # drop the first line; it's boilerplate
83print sonar_files
84
85
86def write_module(target, out):
87 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
91 tests = test_map[target] if target in test_map else []
92
93 module_targets = [target] + tests
94 for property in [sonar_files[t+'-sonar'] for t in module_targets]:
95 print property
96 with open(property, 'r') as f:
97 for line in f.readlines():
98 out.write('%s.%s' % (module_name, line))
99
100 if tests:
101 rmtree(path + '/surefire-reports', ignore_errors=True)
102 rmtree('surefire-reports', ignore_errors=True)
Brian O'Connor0de619b2016-11-12 20:11:21 -0800103 runCmd([BUCK, 'test',
Brian O'Connor9c2c8232016-11-08 17:13:14 -0800104 '--no-cache', '--no-results-cache',
105 '--code-coverage',
106 '--no-results-cache',
107 '--surefire-xml', 'surefire-reports'
108 ] + tests)
109 copy('buck-out/gen/jacoco/jacoco.exec', path)
110 #write jacoco.exec path to out; not needed.. this is the default
111 copytree('surefire-reports', path + '/surefire-reports')
112 rmtree('surefire-reports')
113
114# Write the sonar properties file
Brian O'Connor9c2c8232016-11-08 17:13:14 -0800115with open(FILE_NAME, 'w') as out:
116 out.write(ROOT_TEMPLATE % {
117 'name': 'onos',
118 'key': 'org.onosproject:onos',
Ray Milkey924c0e32016-11-18 13:47:14 -0800119 'version': ONOS_VERSION,
Brian O'Connor9c2c8232016-11-08 17:13:14 -0800120 'jacoco': '%s/buck-out/gen/jacoco/jacoco.exec' % ONOS_ROOT,
121 'modules': ','.join([splitTarget(t)[1] for t in targets])
122 })
123 for target in targets:
124 print target
125 write_module(target, out)