blob: 0b1016878c49ead2d4ec113696c907f20f6eb329 [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
14# SonarQube property file name and template
15FILE_NAME = 'sonar-project.properties'
16ROOT_TEMPLATE = '''# Auto-generated properties file
17sonar.projectKey=%(key)s
18sonar.projectName=%(name)s
19sonar.projectVersion=%(version)s
20
21#sonar.sources=src
22sonar.sourceEncoding=UTF-8
23sonar.java.target = 1.8
24sonar.java.source = 1.8
25sonar.language=java
26
27sonar.junit.reportsPath = surefire-reports
28# global jacoco
29#sonar.jacoco.reportPath = %(jacoco)s
30# local jacoco
31sonar.jacoco.reportPath = jacoco.exec
32
33sonar.modules=%(modules)s
34
35'''
36
37BUCK = 'onos-buck'
38
39# Change to $ONOS_ROOT
40ONOS_ROOT = os.environ[ 'ONOS_ROOT' ]
41if ONOS_ROOT:
42 os.chdir( ONOS_ROOT )
43
44def splitTarget(target):
45 path, module = target.split(':', 2)
46 path = path.replace('//', '', 1)
47 return path, module
48
49def runCmd(cmd):
50 output = check_output( cmd ).rstrip()
51 return output.split('\n') if output else []
52
53# TODO do we need to start with a clean slate?
54#runCmd([BUCK, 'clean'])
55
56# Find all onos_jar rules
57targets = runCmd([BUCK, 'query', "kind('onos_jar', '//...')"])
58#targets = ['//core/net:onos-core-net']
59print targets
60non_osgi_targets = ['%s#non-osgi' % t for t in targets]
61
62# Build all targets to fill buck-out/bin
63print runCmd([BUCK, 'build', '--no-cache'] + targets + non_osgi_targets)
64
65# Find all tests associated with onos_jar rules
66#FIXME we may want to insert kind('java_test', testsof...)
67output = runCmd([BUCK, 'query', '--json', "testsof('%s')"] + targets)
68test_map = json.loads(output[0])
69
70# Flatten the values in the test target map
71test_targets = [t for ts in test_map.values() for t in ts]
72print test_targets
73
74# Build run tests
75#print runCmd([BUCK, 'test', '--no-cache', '--code-coverage', '--no-results-cache'] + test_targets)
76
77# Build the sonar rules for each target
78sonar_files = runCmd([BUCK, 'build', '--show-output'] + ['%s-sonar' % t for t in (targets + test_targets)])
79sonar_files = dict([i.split(' ') for i in sonar_files[1:]]) # drop the first line; it's boilerplate
80print sonar_files
81
82
83def write_module(target, out):
84 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
88 tests = test_map[target] if target in test_map else []
89
90 module_targets = [target] + tests
91 for property in [sonar_files[t+'-sonar'] for t in module_targets]:
92 print property
93 with open(property, 'r') as f:
94 for line in f.readlines():
95 out.write('%s.%s' % (module_name, line))
96
97 if tests:
98 rmtree(path + '/surefire-reports', ignore_errors=True)
99 rmtree('surefire-reports', ignore_errors=True)
Brian O'Connor0de619b2016-11-12 20:11:21 -0800100 runCmd([BUCK, 'test',
Brian O'Connor9c2c8232016-11-08 17:13:14 -0800101 '--no-cache', '--no-results-cache',
102 '--code-coverage',
103 '--no-results-cache',
104 '--surefire-xml', 'surefire-reports'
105 ] + tests)
106 copy('buck-out/gen/jacoco/jacoco.exec', path)
107 #write jacoco.exec path to out; not needed.. this is the default
108 copytree('surefire-reports', path + '/surefire-reports')
109 rmtree('surefire-reports')
110
111# Write the sonar properties file
112# FIXME pull the version from the Buck version file
113with open(FILE_NAME, 'w') as out:
114 out.write(ROOT_TEMPLATE % {
115 'name': 'onos',
116 'key': 'org.onosproject:onos',
117 'version': '1.8.0-SNAPSHOT',
118 'jacoco': '%s/buck-out/gen/jacoco/jacoco.exec' % ONOS_ROOT,
119 'modules': ','.join([splitTarget(t)[1] for t in targets])
120 })
121 for target in targets:
122 print target
123 write_module(target, out)