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 | 32963cf | 2018-11-21 09:31:51 -0800 | [diff] [blame^] | 15 | ONOS_VERSION = '2.0.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 | |
Ray Milkey | 32963cf | 2018-11-21 09:31:51 -0800 | [diff] [blame^] | 40 | black_list = ["//protocols/grpc:grpc-core-repkg", "//apps/openstacktelemetry:grpc-core-repkg"] |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 41 | |
| 42 | # Change to $ONOS_ROOT |
Ray Milkey | 32963cf | 2018-11-21 09:31:51 -0800 | [diff] [blame^] | 43 | ONOS_ROOT = os.environ['ONOS_ROOT'] |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 44 | if ONOS_ROOT: |
Ray Milkey | 32963cf | 2018-11-21 09:31:51 -0800 | [diff] [blame^] | 45 | os.chdir(ONOS_ROOT) |
| 46 | |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 47 | |
| 48 | def splitTarget(target): |
Ray Milkey | 32963cf | 2018-11-21 09:31:51 -0800 | [diff] [blame^] | 49 | path, module = target.split(':', 2) |
| 50 | path = path.replace('//', '', 1) |
| 51 | return path, module |
| 52 | |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 53 | |
| 54 | def runCmd(cmd): |
Ray Milkey | 32963cf | 2018-11-21 09:31:51 -0800 | [diff] [blame^] | 55 | output = check_output(cmd).rstrip() |
| 56 | return output.split('\n') if output else [] |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 57 | |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 58 | |
Ray Milkey | 32963cf | 2018-11-21 09:31:51 -0800 | [diff] [blame^] | 59 | # build ONOS |
| 60 | runCmd(["bazel", "build", "onos"]) |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 61 | |
Ray Milkey | 32963cf | 2018-11-21 09:31:51 -0800 | [diff] [blame^] | 62 | # Find all onos OSGi jar file rules |
| 63 | targets = runCmd(["bazel", "query", "kind('_bnd', '//...')"]) |
| 64 | # Uncomment this for easier debugging of a single package |
| 65 | # targets = ['//core/net:onos-core-net'] |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 66 | |
| 67 | # Find all tests associated with onos_jar rules |
Ray Milkey | 32963cf | 2018-11-21 09:31:51 -0800 | [diff] [blame^] | 68 | # FIXME we may want to insert kind('java_test', testsof...) |
| 69 | # output = runCmd([BUCK, 'query', '--json', "testsof('%s')"] + targets) |
| 70 | # test_map = json.loads(output[0]) |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 71 | |
| 72 | # Flatten the values in the test target map |
Ray Milkey | 32963cf | 2018-11-21 09:31:51 -0800 | [diff] [blame^] | 73 | # test_targets = [t for ts in test_map.values() for t in ts] |
| 74 | # print test_targets |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 75 | |
| 76 | # Build run tests |
Ray Milkey | 32963cf | 2018-11-21 09:31:51 -0800 | [diff] [blame^] | 77 | # print runCmd([BUCK, 'test', '--no-cache', '--code-coverage', '--no-results-cache'] + test_targets) |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 78 | |
| 79 | # Build the sonar rules for each target |
Ray Milkey | 32963cf | 2018-11-21 09:31:51 -0800 | [diff] [blame^] | 80 | # sonar_files = runCmd([BUCK, 'build', '--show-output'] + ['%s-sonar' % t for t in (targets + test_targets)]) |
| 81 | # sonar_files = dict([i.split(' ') for i in sonar_files[1:]]) # drop the first line; it's boilerplate |
| 82 | # print sonar_files |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 83 | |
| 84 | |
| 85 | def write_module(target, out): |
Ray Milkey | 32963cf | 2018-11-21 09:31:51 -0800 | [diff] [blame^] | 86 | if target in black_list: |
| 87 | return |
| 88 | path, module_name = splitTarget(target) |
| 89 | out.write('%s.sonar.projectBaseDir=%s\n' % (module_name, path)) |
| 90 | out.write('%(name)s.sonar.projectName=%(name)s\n' % {'name': module_name}) |
| 91 | query = 'labels(srcs, "%s-native")' % target |
| 92 | sources = runCmd(['bazel', 'query', query]) |
| 93 | sources = [file for file in sources if "package-info" not in file] |
| 94 | sources_csl = ",".join(sources).replace("//", ONOS_ROOT + "/").replace(":", "/") |
| 95 | out.write('%s.sonar.sources=%s\n' % (module_name, sources_csl)) |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 96 | |
Ray Milkey | 32963cf | 2018-11-21 09:31:51 -0800 | [diff] [blame^] | 97 | # tests = test_map[target] if target in test_map else [] |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 98 | |
Ray Milkey | 32963cf | 2018-11-21 09:31:51 -0800 | [diff] [blame^] | 99 | # module_targets = [target] + tests |
| 100 | # for property in [sonar_files[t+'-sonar'] for t in module_targets]: |
| 101 | # print property |
| 102 | # with open(property, 'r') as f: |
| 103 | # for line in f.readlines(): |
| 104 | # out.write('%s.%s' % (module_name, line)) |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 105 | |
Ray Milkey | 32963cf | 2018-11-21 09:31:51 -0800 | [diff] [blame^] | 106 | |
| 107 | # if tests: |
| 108 | # rmtree(path + '/surefire-reports', ignore_errors=True) |
| 109 | # rmtree('surefire-reports', ignore_errors=True) |
| 110 | # runCmd([BUCK, 'test', |
| 111 | # '--no-cache', '--no-results-cache', |
| 112 | # '--code-coverage', |
| 113 | # '--no-results-cache', |
| 114 | # '--surefire-xml', 'surefire-reports' |
| 115 | # ] + tests) |
| 116 | # copy('buck-out/gen/jacoco/jacoco.exec', path) |
| 117 | # #write jacoco.exec path to out; not needed.. this is the default |
| 118 | # copytree('surefire-reports', path + '/surefire-reports') |
| 119 | # rmtree('surefire-reports') |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 120 | |
| 121 | # Write the sonar properties file |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 122 | with open(FILE_NAME, 'w') as out: |
Ray Milkey | 32963cf | 2018-11-21 09:31:51 -0800 | [diff] [blame^] | 123 | out.write(ROOT_TEMPLATE % { |
| 124 | 'name': 'onos', |
| 125 | 'key': 'org.onosproject:onos', |
| 126 | 'version': ONOS_VERSION, |
| 127 | 'jacoco': '%s/buck-out/gen/jacoco/jacoco.exec' % ONOS_ROOT, |
| 128 | 'modules': ','.join([splitTarget(t)[1] for t in targets]) |
| 129 | }) |
| 130 | for target in targets: |
| 131 | print target |
| 132 | write_module(target, out) |