Brian O'Connor | 8c7b128 | 2016-11-06 06:15:12 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # This script generates the Sonar project properties file from the Buck rules |
| 4 | # To run sonar, run 'sonar-scanner' from $ONOS_ROOT |
| 5 | # |
| 6 | |
| 7 | import os |
| 8 | from subprocess import call, check_call, check_output |
| 9 | |
| 10 | # SonarQube property file name and template |
| 11 | FILE_NAME = 'sonar-project.properties' |
| 12 | ROOT_TEMPLATE = '''# Auto-generated properties file |
| 13 | sonar.projectKey=%(key)s |
| 14 | sonar.projectName=%(name)s |
| 15 | sonar.projectVersion=%(version)s |
| 16 | |
| 17 | sonar.sources=src |
| 18 | sonar.sourceEncoding=UTF-8 |
| 19 | sonar.language=java |
| 20 | |
| 21 | sonar.modules=%(modules)s |
| 22 | |
| 23 | ''' |
| 24 | |
| 25 | # Change to $ONOS_ROOT |
| 26 | ONOS_ROOT = os.environ[ 'ONOS_ROOT' ] |
| 27 | if ONOS_ROOT: |
| 28 | os.chdir( ONOS_ROOT ) |
| 29 | |
| 30 | # Final all onos_jar rules |
| 31 | cmd = ['onos-buck', 'query', "kind('onos_jar', deps(//tools/package:onos-package))"] |
| 32 | output = check_output( cmd ) |
| 33 | |
| 34 | modules = {} |
| 35 | for line in output.rstrip().split('\n'): |
| 36 | # Parse rule into path and name |
| 37 | path, name = line.split(':', 2) |
| 38 | path = path.replace('//', '', 1) |
| 39 | if path not in modules: |
| 40 | modules[path] = name |
| 41 | else: |
| 42 | print "Skipping %s" % line |
| 43 | |
| 44 | # Write the sonar properties file |
| 45 | # FIXME pull the version from the Buck version file |
| 46 | with open(FILE_NAME, 'w') as f: |
| 47 | f.write(ROOT_TEMPLATE % { |
| 48 | 'name': 'onos', |
| 49 | 'key': 'org.onosproject:onos', |
| 50 | 'version': '1.8.0-SNAPSHOT', |
| 51 | 'modules': ','.join( modules.values() ) |
| 52 | }) |
| 53 | for path, name in modules.iteritems(): |
| 54 | f.write('%s.sonar.projectBaseDir=%s\n' % ( name, path )) |
| 55 | ''' Extra options |
| 56 | module2.sonar.projectBaseDir=Module 2 |
| 57 | module2.sonar.sources=sources |
| 58 | module2.sonar.projectName=Module #2 |
| 59 | ''' |