Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 1 | #!/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 | |
| 8 | import json |
| 9 | import os |
| 10 | |
| 11 | from shutil import copy, copytree, rmtree |
| 12 | from subprocess import call, check_call, check_output |
| 13 | |
Ray Milkey | 924c0e3 | 2016-11-18 13:47:14 -0800 | [diff] [blame] | 14 | # FIXME pull the version from the Buck version file |
Ray Milkey | f14cc6d | 2017-06-28 09:07:17 -0700 | [diff] [blame^] | 15 | ONOS_VERSION='1.11.0-SNAPSHOT' |
Ray Milkey | 924c0e3 | 2016-11-18 13:47:14 -0800 | [diff] [blame] | 16 | |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 17 | # SonarQube property file name and template |
| 18 | FILE_NAME = 'sonar-project.properties' |
| 19 | ROOT_TEMPLATE = '''# Auto-generated properties file |
| 20 | sonar.projectKey=%(key)s |
| 21 | sonar.projectName=%(name)s |
| 22 | sonar.projectVersion=%(version)s |
Ray Milkey | 924c0e3 | 2016-11-18 13:47:14 -0800 | [diff] [blame] | 23 | |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 24 | #sonar.sources=src |
| 25 | sonar.sourceEncoding=UTF-8 |
| 26 | sonar.java.target = 1.8 |
| 27 | sonar.java.source = 1.8 |
| 28 | sonar.language=java |
| 29 | |
| 30 | sonar.junit.reportsPath = surefire-reports |
| 31 | # global jacoco |
| 32 | #sonar.jacoco.reportPath = %(jacoco)s |
| 33 | # local jacoco |
| 34 | sonar.jacoco.reportPath = jacoco.exec |
| 35 | |
| 36 | sonar.modules=%(modules)s |
| 37 | |
| 38 | ''' |
| 39 | |
| 40 | BUCK = 'onos-buck' |
| 41 | |
| 42 | # Change to $ONOS_ROOT |
| 43 | ONOS_ROOT = os.environ[ 'ONOS_ROOT' ] |
| 44 | if ONOS_ROOT: |
| 45 | os.chdir( ONOS_ROOT ) |
| 46 | |
| 47 | def splitTarget(target): |
| 48 | path, module = target.split(':', 2) |
| 49 | path = path.replace('//', '', 1) |
| 50 | return path, module |
| 51 | |
| 52 | def 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 |
| 60 | targets = runCmd([BUCK, 'query', "kind('onos_jar', '//...')"]) |
| 61 | #targets = ['//core/net:onos-core-net'] |
| 62 | print targets |
| 63 | non_osgi_targets = ['%s#non-osgi' % t for t in targets] |
| 64 | |
| 65 | # Build all targets to fill buck-out/bin |
| 66 | print 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...) |
| 70 | output = runCmd([BUCK, 'query', '--json', "testsof('%s')"] + targets) |
| 71 | test_map = json.loads(output[0]) |
| 72 | |
| 73 | # Flatten the values in the test target map |
| 74 | test_targets = [t for ts in test_map.values() for t in ts] |
| 75 | print 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 |
| 81 | 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 |
| 84 | |
| 85 | |
| 86 | def 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'Connor | 0de619b | 2016-11-12 20:11:21 -0800 | [diff] [blame] | 103 | runCmd([BUCK, 'test', |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 104 | '--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'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 115 | with open(FILE_NAME, 'w') as out: |
| 116 | out.write(ROOT_TEMPLATE % { |
| 117 | 'name': 'onos', |
| 118 | 'key': 'org.onosproject:onos', |
Ray Milkey | 924c0e3 | 2016-11-18 13:47:14 -0800 | [diff] [blame] | 119 | 'version': ONOS_VERSION, |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 120 | '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) |