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