Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
Ray Milkey | 8705cce | 2019-01-14 14:05:48 -0800 | [diff] [blame] | 3 | """ |
| 4 | This script prepares this ONOS directory so that the Sonar Scanner can be run. |
| 5 | - Build ONOS |
| 6 | - Run coverage tests on a per module basis, stage surefire-reports and jacoco.exec |
| 7 | - Generate sonar-project.properties file |
| 8 | """ |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 9 | |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 10 | import os |
Ray Milkey | 8705cce | 2019-01-14 14:05:48 -0800 | [diff] [blame] | 11 | import sys |
| 12 | import fnmatch |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 13 | |
Ray Milkey | 8705cce | 2019-01-14 14:05:48 -0800 | [diff] [blame] | 14 | from shutil import copy, rmtree |
| 15 | from subprocess import check_output, STDOUT, CalledProcessError |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 16 | |
Ray Milkey | 32963cf | 2018-11-21 09:31:51 -0800 | [diff] [blame] | 17 | ONOS_VERSION = '2.0.0-SNAPSHOT' |
Ray Milkey | 924c0e3 | 2016-11-18 13:47:14 -0800 | [diff] [blame] | 18 | |
Ray Milkey | 8705cce | 2019-01-14 14:05:48 -0800 | [diff] [blame] | 19 | GENFILES = 'bazel-genfiles' |
Ray Milkey | fd51515 | 2019-01-24 22:02:39 -0800 | [diff] [blame] | 20 | BIN = 'bazel-bin' |
| 21 | |
Ray Milkey | 8705cce | 2019-01-14 14:05:48 -0800 | [diff] [blame] | 22 | SONAR_PROJECT = GENFILES + '/sonar-project' |
| 23 | SUREFIRE_REPORTS = 'surefire-reports' |
| 24 | SONAR_PROPERTIES_FILE_NAME = SONAR_PROJECT + '/sonar-project.properties' |
| 25 | |
| 26 | # Template for the sonar properties file |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 27 | ROOT_TEMPLATE = '''# Auto-generated properties file |
| 28 | sonar.projectKey=%(key)s |
| 29 | sonar.projectName=%(name)s |
| 30 | sonar.projectVersion=%(version)s |
Ray Milkey | 924c0e3 | 2016-11-18 13:47:14 -0800 | [diff] [blame] | 31 | |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 32 | sonar.sourceEncoding=UTF-8 |
| 33 | sonar.java.target = 1.8 |
| 34 | sonar.java.source = 1.8 |
| 35 | sonar.language=java |
| 36 | |
Ray Milkey | 8705cce | 2019-01-14 14:05:48 -0800 | [diff] [blame] | 37 | sonar.junit.reportsPath = surefire-reports |
| 38 | sonar.jacoco.reportPath = jacoco.exec |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 39 | |
| 40 | sonar.modules=%(modules)s |
| 41 | |
| 42 | ''' |
| 43 | |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 44 | |
Ray Milkey | 8705cce | 2019-01-14 14:05:48 -0800 | [diff] [blame] | 45 | def split_target(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 | |
Ray Milkey | 8705cce | 2019-01-14 14:05:48 -0800 | [diff] [blame] | 51 | def run_command(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 | 8705cce | 2019-01-14 14:05:48 -0800 | [diff] [blame] | 56 | def run_command_with_stderr(cmd): |
| 57 | output = check_output(cmd, stderr=STDOUT).rstrip() |
| 58 | return output.split('\n') if output else [] |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 59 | |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 60 | |
Ray Milkey | 8705cce | 2019-01-14 14:05:48 -0800 | [diff] [blame] | 61 | def make_dirs(path): |
| 62 | try: |
| 63 | os.makedirs(path) |
| 64 | except OSError: |
| 65 | pass |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 66 | |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 67 | |
Ray Milkey | 8705cce | 2019-01-14 14:05:48 -0800 | [diff] [blame] | 68 | def find_bazel_project(): |
| 69 | return os.path.basename(os.getcwd()) |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 70 | |
Ray Milkey | 8705cce | 2019-01-14 14:05:48 -0800 | [diff] [blame] | 71 | |
| 72 | def find_bazel_classes_directory(module_name, path): |
Ray Milkey | e6b5d9e | 2019-01-23 13:18:23 -0800 | [diff] [blame] | 73 | bazel_bin_marker = "bazel-bin:" |
| 74 | bazel_bin_dir = "" |
| 75 | info = run_command(["bazel", "info"]) |
| 76 | for bin_dir in info: |
| 77 | if bin_dir.startswith(bazel_bin_marker): |
| 78 | bazel_bin_dir = bin_dir.replace(bazel_bin_marker, "") |
| 79 | break |
| 80 | return bazel_bin_dir |
Ray Milkey | 8705cce | 2019-01-14 14:05:48 -0800 | [diff] [blame] | 81 | |
| 82 | |
| 83 | def capture_surefire_reports(module_path): |
| 84 | matches = [] |
| 85 | for root, dirnames, filenames in os.walk('bazel-testlogs/' + module_path + '/src/test/java'): |
| 86 | for filename in fnmatch.filter(filenames, '*.xml'): |
| 87 | source_path = os.path.join(root, filename) |
| 88 | matches.append(source_path) |
| 89 | |
| 90 | destination_path = \ |
| 91 | SONAR_PROJECT + "/" + module_path + "/" + SUREFIRE_REPORTS + "/TEST-" + \ |
| 92 | source_path.replace("bazel-testlogs/", "")\ |
| 93 | .replace("/test.xml", "test.xml")\ |
| 94 | .replace(module_path, "")\ |
| 95 | .replace("/src/test/java/", "")\ |
| 96 | .replace("/", ".") |
| 97 | make_dirs(path=os.path.dirname(destination_path)) |
| 98 | copy(source_path, destination_path) |
| 99 | |
| 100 | |
| 101 | def capture_jacoco(module_path): |
| 102 | source_path = '/tmp/jacoco.exec' |
| 103 | destination_path = \ |
| 104 | SONAR_PROJECT + "/" + module_path |
| 105 | make_dirs(path=os.path.dirname(destination_path)) |
| 106 | copy(source_path, destination_path) |
| 107 | |
| 108 | |
| 109 | def capture_sources(module_path): |
| 110 | source_path = module_path + '/src' |
| 111 | destination_path = SONAR_PROJECT + "/" + module_path + '/src' |
| 112 | os.symlink(os.getcwd() + '/' + source_path, destination_path) |
| 113 | |
| 114 | |
Ray Milkey | fd51515 | 2019-01-24 22:02:39 -0800 | [diff] [blame] | 115 | def capture_classes(module_name, path): |
| 116 | # module name: onos-core-net |
| 117 | # path: core/net |
| 118 | |
| 119 | base_project_dir = os.getcwd() + "/" + SONAR_PROJECT + "/" + path + "/" |
| 120 | base_bin_dir = os.getcwd() + "/" + BIN + "/" + path + "/lib" + module_name |
| 121 | jar_file_path = base_bin_dir + ".jar" |
| 122 | test_jar_file_path = base_bin_dir + "-tests.jar" |
| 123 | classes_directory = base_project_dir + "classes" |
| 124 | test_classes_directory = base_project_dir + "test-classes" |
| 125 | |
| 126 | make_dirs(classes_directory) |
| 127 | current_directory = os.getcwd() |
| 128 | os.chdir(classes_directory) |
| 129 | run_command(["jar", "xvf", jar_file_path]) |
| 130 | |
| 131 | make_dirs(test_classes_directory) |
| 132 | os.chdir(test_classes_directory) |
| 133 | run_command(["jar", "xvf", test_jar_file_path]) |
| 134 | |
| 135 | os.chdir(current_directory) |
| 136 | |
| 137 | |
Ray Milkey | 8705cce | 2019-01-14 14:05:48 -0800 | [diff] [blame] | 138 | """ |
| 139 | Writes out the properties for a given module and stages the files needed by the scanner. |
| 140 | """ |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 141 | |
| 142 | |
| 143 | def write_module(target, out): |
Ray Milkey | 8705cce | 2019-01-14 14:05:48 -0800 | [diff] [blame] | 144 | path, module_name = split_target(target) |
| 145 | query = 'labels(srcs, "%s-native")' % target |
Ray Milkey | fd51515 | 2019-01-24 22:02:39 -0800 | [diff] [blame] | 146 | tests_query = 'labels(srcs, "%s-tests")' % target |
Ray Milkey | 8705cce | 2019-01-14 14:05:48 -0800 | [diff] [blame] | 147 | |
| 148 | # get rid of previous data |
| 149 | try: |
| 150 | os.remove('/tmp/jacoco.exec') |
| 151 | except OSError: |
| 152 | pass |
| 153 | |
| 154 | try: |
| 155 | # Find all the test targets in this package |
| 156 | coverage_target_query = "attr(name, .*-coverage, tests(//" + path + ":*))" |
| 157 | coverage_targets_result = run_command(["bazel", "query", coverage_target_query]) |
| 158 | |
| 159 | # Find the test targets that are coverage targets |
| 160 | run_coverage_command = ['bazel', 'test'] |
| 161 | for coverage_target in coverage_targets_result: |
| 162 | run_coverage_command.append(str(coverage_target)) |
| 163 | except CalledProcessError: |
| 164 | print "Error querying test files for target " + target |
| 165 | return |
| 166 | |
| 167 | try: |
| 168 | # Use bazel to run all the coverage targets |
| 169 | run_coverage_command.append('--cache_test_results=no') |
| 170 | run_command(run_coverage_command) |
| 171 | |
Ray Milkey | fd51515 | 2019-01-24 22:02:39 -0800 | [diff] [blame] | 172 | # Find the source files used by the base library |
Ray Milkey | 8705cce | 2019-01-14 14:05:48 -0800 | [diff] [blame] | 173 | sources = run_command(['bazel', 'query', query]) |
Ray Milkey | fd51515 | 2019-01-24 22:02:39 -0800 | [diff] [blame] | 174 | |
| 175 | # Find the source files used by the tests |
| 176 | test_sources = run_command(['bazel', 'query', tests_query]) |
Ray Milkey | 8705cce | 2019-01-14 14:05:48 -0800 | [diff] [blame] | 177 | except CalledProcessError as exc: |
| 178 | print "Error running test files for target " + target |
| 179 | raise exc |
| 180 | |
| 181 | if not os.path.exists('/tmp/jacoco.exec'): |
| 182 | # No coverage data was produced, not much to do |
| 183 | return |
| 184 | |
| 185 | # Filter out non-Java files |
| 186 | sources = \ |
| 187 | [source_file for source_file in sources if "package-info" not in source_file and ".java" in source_file] |
Ray Milkey | fd51515 | 2019-01-24 22:02:39 -0800 | [diff] [blame] | 188 | test_sources = \ |
| 189 | [test_source_file for test_source_file in test_sources |
| 190 | if "package-info" not in test_source_file and ".java" in test_source_file] |
Ray Milkey | 8705cce | 2019-01-14 14:05:48 -0800 | [diff] [blame] | 191 | |
| 192 | # Adjust source file paths to be relative to the root |
| 193 | sources_filtered = [] |
| 194 | for source in sources: |
| 195 | sources_filtered.append(source.replace('//' + path + ':', "", 1)) |
| 196 | |
Ray Milkey | fd51515 | 2019-01-24 22:02:39 -0800 | [diff] [blame] | 197 | test_sources_filtered = [] |
| 198 | for test_source in test_sources: |
| 199 | test_sources_filtered.append(test_source.replace('//' + path + ':', "", 1)) |
| 200 | |
Ray Milkey | 8705cce | 2019-01-14 14:05:48 -0800 | [diff] [blame] | 201 | # create a CSL of all the source files for use in the properties file |
| 202 | sources_csl = ",".join(sources_filtered).replace("//", "").replace(":", "/") |
| 203 | |
Ray Milkey | fd51515 | 2019-01-24 22:02:39 -0800 | [diff] [blame] | 204 | # create a CSL of all the test source files for use in the properties file |
| 205 | test_sources_csl = ",".join(test_sources_filtered).replace("//", "").replace(":", "/") |
| 206 | |
Ray Milkey | 8705cce | 2019-01-14 14:05:48 -0800 | [diff] [blame] | 207 | # Write out the properties for this package |
Ray Milkey | 32963cf | 2018-11-21 09:31:51 -0800 | [diff] [blame] | 208 | out.write('%s.sonar.projectBaseDir=%s\n' % (module_name, path)) |
| 209 | out.write('%(name)s.sonar.projectName=%(name)s\n' % {'name': module_name}) |
Ray Milkey | 32963cf | 2018-11-21 09:31:51 -0800 | [diff] [blame] | 210 | out.write('%s.sonar.sources=%s\n' % (module_name, sources_csl)) |
Ray Milkey | fd51515 | 2019-01-24 22:02:39 -0800 | [diff] [blame] | 211 | out.write('%s.sonar.tests=%s\n' % (module_name, test_sources_csl)) |
| 212 | out.write('%s.sonar.java.binaries = classes\n' % module_name) |
| 213 | out.write('%s.sonar.java.test.binaries = test-classes\n' % module_name) |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 214 | |
Ray Milkey | 8705cce | 2019-01-14 14:05:48 -0800 | [diff] [blame] | 215 | # Get the dependencies for this package using bazel |
| 216 | deps_files = run_command_with_stderr(['bazel', 'build', '%s-tests-gen-deps' % target]) |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 217 | |
Ray Milkey | 8705cce | 2019-01-14 14:05:48 -0800 | [diff] [blame] | 218 | dep_file = "" |
| 219 | for source_file in deps_files: |
| 220 | if source_file.endswith(".txt"): |
| 221 | dep_file = source_file.strip() |
| 222 | |
| 223 | libraries = [] |
| 224 | with open(dep_file, 'r') as read_files: |
| 225 | lines = read_files.readline().split(',') |
| 226 | |
| 227 | external_base_path = os.path.realpath( |
| 228 | os.path.realpath("bazel-" + find_bazel_project() + "/external") + "/../../..") |
| 229 | for line in lines: |
| 230 | library_file_name = line |
| 231 | if line.startswith("external"): |
| 232 | library_file_name = external_base_path + "/" + library_file_name |
| 233 | else: |
| 234 | library_file_name = os.getcwd() + "/" + library_file_name |
| 235 | libraries.append(library_file_name) |
| 236 | |
| 237 | out.write('%s.sonar.java.libraries = %s\n' % (module_name, ",".join(libraries))) |
| 238 | |
| 239 | # Capture files needed by the scanner into the staging area |
| 240 | capture_surefire_reports(path) |
| 241 | capture_jacoco(path) |
| 242 | capture_sources(path) |
Ray Milkey | fd51515 | 2019-01-24 22:02:39 -0800 | [diff] [blame] | 243 | capture_classes(module_name, path) |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 244 | |
Ray Milkey | 32963cf | 2018-11-21 09:31:51 -0800 | [diff] [blame] | 245 | |
Ray Milkey | 8705cce | 2019-01-14 14:05:48 -0800 | [diff] [blame] | 246 | def _main(): |
| 247 | global ONOS_ROOT, targets |
| 248 | debug = False |
| 249 | if len(sys.argv) > 1: |
| 250 | debug = True |
Brian O'Connor | 9c2c823 | 2016-11-08 17:13:14 -0800 | [diff] [blame] | 251 | |
Ray Milkey | 8705cce | 2019-01-14 14:05:48 -0800 | [diff] [blame] | 252 | # Change to $ONOS_ROOT |
| 253 | ONOS_ROOT = os.environ['ONOS_ROOT'] |
| 254 | if ONOS_ROOT: |
| 255 | os.chdir(ONOS_ROOT) |
| 256 | |
| 257 | # build ONOS |
| 258 | run_command(["bazel", "build", "onos"]) |
| 259 | |
| 260 | # find the test targets to get coverage for |
| 261 | if debug: |
| 262 | # Use a predefined list of targets for debugging |
| 263 | targets = ['//core/net:onos-core-net', '//utils/misc:onlab-misc'] |
| 264 | else: |
| 265 | # Query all onos OSGi jar file rules with tests from bazel |
| 266 | targets = run_command(["bazel", "query", "attr('name', '.*-tests-gen', '//...')"]) |
| 267 | targets = [target.replace("-tests-gen-deps", "") for target in targets] |
| 268 | |
| 269 | # Filter out targets without any tests in them |
| 270 | targets_with_tests = [] |
| 271 | for target in targets: |
| 272 | colon = target.find(':') |
| 273 | base_target = target[0:colon] |
| 274 | target_query_result = run_command(['bazel', 'query', 'tests(' + base_target + ':*)']) |
| 275 | for result_line in target_query_result: |
| 276 | if "src/test" in result_line: |
| 277 | targets_with_tests.append(target) |
| 278 | break |
| 279 | targets = targets_with_tests |
| 280 | |
| 281 | # Clear out any old results |
| 282 | rmtree(SONAR_PROJECT, True) |
| 283 | |
| 284 | # make a directory for the new results |
| 285 | make_dirs(SONAR_PROJECT) |
| 286 | |
| 287 | # fill in the template for the sonar properties |
| 288 | sonar_parameters = { |
Ray Milkey | 32963cf | 2018-11-21 09:31:51 -0800 | [diff] [blame] | 289 | 'name': 'onos', |
| 290 | 'key': 'org.onosproject:onos', |
| 291 | 'version': ONOS_VERSION, |
Ray Milkey | 8705cce | 2019-01-14 14:05:48 -0800 | [diff] [blame] | 292 | 'jacoco': '/tmp/jacoco.exec', |
| 293 | 'reports': 'surefire-reports', |
| 294 | 'modules': ','.join([split_target(t)[1] for t in targets]) |
| 295 | } |
| 296 | if debug: |
| 297 | sonar_parameters["key"] = 'org.onosproject:onos-test-sonar' |
| 298 | sonar_parameters["name"] = 'onos-test-sonar' |
| 299 | |
| 300 | # Write the sonar properties file |
| 301 | with open(SONAR_PROPERTIES_FILE_NAME, 'w') as out: |
| 302 | out.write(ROOT_TEMPLATE % sonar_parameters) |
| 303 | for target in targets: |
| 304 | print "Processing coverage for target " + target |
| 305 | write_module(target, out) |
| 306 | |
| 307 | |
| 308 | if __name__ == "__main__": |
| 309 | _main() |