Cleanups to sonar coverage generation

- Jar files with no coverage data should still count
- Extract the ONOS version string from the bazel variables file

Change-Id: Id35cf5b78f821457dc301e3bcc1318fcaf2d71a1
diff --git a/tools/build/onos-prepare-sonar b/tools/build/onos-prepare-sonar
index ba7a615..1520d9c 100755
--- a/tools/build/onos-prepare-sonar
+++ b/tools/build/onos-prepare-sonar
@@ -14,7 +14,7 @@
 from shutil import copy, rmtree
 from subprocess import check_output, STDOUT, CalledProcessError
 
-ONOS_VERSION = '2.0.0-SNAPSHOT'
+ONOS_VERSION = ''
 
 GENFILES = 'bazel-genfiles'
 BIN = 'bazel-bin'
@@ -179,8 +179,8 @@
         raise exc
 
     if not os.path.exists('/tmp/jacoco.exec'):
-        # No coverage data was produced, not much to do
-        return
+        # No coverage data was produced, make an empty one
+        open('/tmp/jacoco.exec', 'a').close()
 
     # Filter out non-Java files
     sources = \
@@ -243,8 +243,21 @@
     capture_classes(module_name, path)
 
 
+'''
+    Extracts the ONOS version string from the bazel variables.
+'''
+
+
+def _extract_onos_version():
+    bazel_vars_path = os.getcwd() + "/tools/build/bazel/variables.bzl"
+    with open(bazel_vars_path, 'r') as bazel_vars_file:
+        for bazel_var in bazel_vars_file.readlines():
+            if "ONOS_VERSION" in bazel_var:
+                return bazel_var.replace(" ", "").replace('ONOS_VERSION="', '').replace('"', '')
+
+
 def _main():
-    global ONOS_ROOT, targets
+    global ONOS_ROOT, ONOS_VERSION, targets
     debug = False
     if len(sys.argv) > 1:
         debug = True
@@ -254,6 +267,9 @@
     if ONOS_ROOT:
         os.chdir(ONOS_ROOT)
 
+    # extract the ONOS version string
+    ONOS_VERSION = _extract_onos_version()
+
     # build ONOS
     run_command(["bazel", "build", "onos"])