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 | |
| 14 | # SonarQube property file name and template |
| 15 | FILE_NAME = 'sonar-project.properties' |
| 16 | ROOT_TEMPLATE = '''# Auto-generated properties file |
| 17 | sonar.projectKey=%(key)s |
| 18 | sonar.projectName=%(name)s |
| 19 | sonar.projectVersion=%(version)s |
| 20 | |
| 21 | #sonar.sources=src |
| 22 | sonar.sourceEncoding=UTF-8 |
| 23 | sonar.java.target = 1.8 |
| 24 | sonar.java.source = 1.8 |
| 25 | sonar.language=java |
| 26 | |
| 27 | sonar.junit.reportsPath = surefire-reports |
| 28 | # global jacoco |
| 29 | #sonar.jacoco.reportPath = %(jacoco)s |
| 30 | # local jacoco |
| 31 | sonar.jacoco.reportPath = jacoco.exec |
| 32 | |
| 33 | sonar.modules=%(modules)s |
| 34 | |
| 35 | ''' |
| 36 | |
| 37 | BUCK = 'onos-buck' |
| 38 | |
| 39 | # Change to $ONOS_ROOT |
| 40 | ONOS_ROOT = os.environ[ 'ONOS_ROOT' ] |
| 41 | if ONOS_ROOT: |
| 42 | os.chdir( ONOS_ROOT ) |
| 43 | |
| 44 | def splitTarget(target): |
| 45 | path, module = target.split(':', 2) |
| 46 | path = path.replace('//', '', 1) |
| 47 | return path, module |
| 48 | |
| 49 | def 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 |
| 57 | targets = runCmd([BUCK, 'query', "kind('onos_jar', '//...')"]) |
| 58 | #targets = ['//core/net:onos-core-net'] |
| 59 | print targets |
| 60 | non_osgi_targets = ['%s#non-osgi' % t for t in targets] |
| 61 | |
| 62 | # Build all targets to fill buck-out/bin |
| 63 | print 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...) |
| 67 | output = runCmd([BUCK, 'query', '--json', "testsof('%s')"] + targets) |
| 68 | test_map = json.loads(output[0]) |
| 69 | |
| 70 | # Flatten the values in the test target map |
| 71 | test_targets = [t for ts in test_map.values() for t in ts] |
| 72 | print 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 |
| 78 | 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 |
| 81 | |
| 82 | |
| 83 | def 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) |
| 100 | runCmd(['buck', |
| 101 | 'test', |
| 102 | '--no-cache', '--no-results-cache', |
| 103 | '--code-coverage', |
| 104 | '--no-results-cache', |
| 105 | '--surefire-xml', 'surefire-reports' |
| 106 | ] + tests) |
| 107 | copy('buck-out/gen/jacoco/jacoco.exec', path) |
| 108 | #write jacoco.exec path to out; not needed.. this is the default |
| 109 | copytree('surefire-reports', path + '/surefire-reports') |
| 110 | rmtree('surefire-reports') |
| 111 | |
| 112 | # Write the sonar properties file |
| 113 | # FIXME pull the version from the Buck version file |
| 114 | with open(FILE_NAME, 'w') as out: |
| 115 | out.write(ROOT_TEMPLATE % { |
| 116 | 'name': 'onos', |
| 117 | 'key': 'org.onosproject:onos', |
| 118 | 'version': '1.8.0-SNAPSHOT', |
| 119 | 'jacoco': '%s/buck-out/gen/jacoco/jacoco.exec' % ONOS_ROOT, |
| 120 | 'modules': ','.join([splitTarget(t)[1] for t in targets]) |
| 121 | }) |
| 122 | for target in targets: |
| 123 | print target |
| 124 | write_module(target, out) |