code-coverage: merging capabilities enhanced to show only merged reports

- Now the merge.sh reports how many individual code coverage reports
were successfuly merged.
- Prepares merge.sh to be sourced rather than invoked.

Signed-off-by: Saul Romero <saul.romero@arm.com>
diff --git a/coverage-tool/coverage-reporting/merge.sh b/coverage-tool/coverage-reporting/merge.sh
index e0ec69c..635b1f2 100755
--- a/coverage-tool/coverage-reporting/merge.sh
+++ b/coverage-tool/coverage-reporting/merge.sh
@@ -12,8 +12,7 @@
 # DESCRIPTION: Wrapper to merge intermediate json files and LCOV trace .info
 # files.
 #==============================================================================
-
-set +x
+set -x
 #################################################################
 # Function to manipulate json objects.
 # The json object properties can be accessed through "." separated
@@ -159,8 +158,7 @@
   if [ "$_type" = '"http"' ];then
     fname="$where.$extension" # Same filename as folder
     rm $where/$fname &>/dev/null || true
-    wget -o error.log $_origin -O $where/$fname || (
-            cat error.log && exit -1)
+    wget -nv $_origin -O $where/$fname || return -1
     cloned_file="$(get_abs_path $where/$fname)"
   elif [ "$_type" = '"bundle"' ];then
     # Check file exists at origin, i.e. was unbundled before
@@ -171,14 +169,14 @@
   elif [ "$_type" = '"file"' ];then
     if [[ "$_origin" = http* ]]; then
         echo "$_origin looks like 'http' rather than 'file' please check..."
-        exit -1
+        return -1
     fi
     fname="$where.$extension" # Same filename as folder
     cp -f $_origin $where/$fname
     cloned_file="$(get_abs_path $where/$fname)"
   else
     echo "Error unsupported file type:$_type.... Aborting."
-    exit -1
+    return -1
   fi
   if [ "$_compression" = "tar.xz" ];then
     cd $where
@@ -188,6 +186,7 @@
     cd -
   fi
   eval "${var_name}=${cloned_file}"
+  return 0
 }
 
 #####################################################################
@@ -207,6 +206,8 @@
   local json_string="$(cat $merge_configuration_file)"
   local config_json_file=""
   local info_file=""
+
+  #printf "\tReading from JSON data:\n\t\t%s" $json_string
   # Get files array
   local nf=$(get_json_object "$json_string" "-files")
   # Init target folder
@@ -218,8 +219,10 @@
     pushd $input_folder > /dev/null
     _file=$(get_json_object "$json_string" "files.$f")
     # The name of the folder is the 'id' value
-    folder=$(get_json_object "$_file" "*id")
-    echo "Getting files from project '$folder' into '$input_folder'..."
+    id=$(get_json_object "$_file" "*id")
+    tf_config=$(get_json_object "$_file" "*tf-configuration" "N/A")
+    folder=$id
+    printf "Getting files from configuration '$tf_config', build '$folder' into '$input_folder'...\n"
     mkdir -p $folder
     bundles=$(get_json_object "$_file" "bundles" None)
     if [ "$bundles" != "None" ];then
@@ -229,10 +232,11 @@
         get_file "$(get_json_object "$bundles" "$n")" $folder
       done
     fi
-    # Download/copy files and save their locations
-    get_file "$(get_json_object "$_file" "config")" $folder config_json_file
-    get_file "$(get_json_object "$_file" "info")" $folder info_file
-    info_files+=($info_file)
+    # Download/copy files and save their locations only if all are found
+    get_file "$(get_json_object "$_file" "config")" $folder config_json_file && \
+    get_file "$(get_json_object "$_file" "info")" $folder info_file && \
+    info_files+=($info_file) && json_files+=($config_json_file) && \
+    list_of_merged_builds+=($id)
     popd > /dev/null
   done
 }
@@ -244,15 +248,17 @@
 #   merged_json_file: Location and name for merged json scm sources
 #   LOCAL_WORKSPACE: Local workspace folder with the source code files
 #   generate_local: Flag to generate local lcov reports
+#   info_files: Array of locations and names of info files
+#   json_files: Array of locations and names of json files
 # Arguments:
 #   1: Location where reside json and info files
 # Outputs:
 #   Merged coverage file
-#   Merged json file
+#   Merged json configuration file
 ################################################################
 merge_files() {
-  local input_folder="${1:-$LCOV_FOLDER}"
-# Merge info and json files
+# Merge info and json configuration files
+  printf "\tFound report files from %d code coverage folders to be merged...\n" ${#info_files[@]}
   local lc=" "
   if [ -n "$LOCAL_WORKSPACE" ];then
     # Translation from info workspaces into local workspace
@@ -265,8 +271,8 @@
   # Getting the path of the merge.py must reside at the same
   # path as the merge.sh
   python3 ${DIR}/merge.py \
-      $(find $input_folder -name "*.info" -exec echo "-a {}" \;) \
-      $(find $input_folder -name "*.json" -exec echo "-j {}" \;) \
+      ${info_files[@]/#/-a } \
+      ${json_files[@]/#/-j } \
       -o $merged_coverage_file \
       -m $merged_json_file \
       $lc
@@ -284,6 +290,7 @@
 #   Lcov report files for each info file
 ################################################################
 generate_local_reports() {
+  printf "\tCreating local code coverage reports...\n"
   for i in ${!info_files[@]};
   do
     local info_file=${info_files[$i]}
@@ -303,23 +310,10 @@
 #   Prints to stdout script usage
 ################################################################
 usage() {
-  clear
-  echo "Usage:"
-  echo "merge -h              Display this help message."
-  echo "-j <JSON filename>    JSON configuration file (info and intermediate json filenames to be merged)."
-  echo "[-l <Report path>]    Coverage reports directory. Defaults to ./Coverage"
-  echo "[-w <Workspace path>] Workspace directory for source code files."
-  echo "[-o <Info filename>]  Merged info file. Defaults to ./merged_coverage.info"
-  echo "[-m <JSON filename>]  JSON merged SCM sources. Defaults to ./merged_scm.json"
-  echo "[-c]                  Flag to download/copy the source files from the JSON merged SCM into the workspace directory."
-  echo "[-g]                  Flag to generate local reports for each info/json instance."
-  echo "$help_message"
-}
-
 help_message=$(cat <<EOF
 
 # The script merging the info files (code coverage) and json SCM sources
-# (intermediate layer) needs a JSON configuration file with the following
+# (intermediate layer) needs a minimum JSON configuration file with the following
 # properties:
 # files: array of objects that describe the type of file/project to be
 # merged.
@@ -331,6 +325,8 @@
 #   info:  Info file
 #       type: Type of storage for the file. (http or file)
 #       origin: Location (url or folder) of the file
+#   <metadata>: Metadata that can be used for print more information related to
+#               each project [Optional]
 # Example:
 { "files" : [
                 {
@@ -344,7 +340,8 @@
                         {
                             "type": "http",
                             "origin": "<URL of info file for project 1>"
-                        }
+                        },
+                    "metadata": ....
                 },
                 {
                     "id": "<project 2>",
@@ -357,7 +354,8 @@
                         {
                             "type": "http",
                             "origin": "<URL of info file for project 2>"
-                        }
+                        },
+                      "metadata": ....
                 },
                 .
                 .
@@ -366,8 +364,21 @@
 }
 EOF
 )
+  clear || true
+  echo "Usage:"
+  echo "merge -h              Display this help message."
+  echo "-j <JSON filename>    JSON configuration file (info and intermediate json filenames to be merged)."
+  echo "[-l <Report path>]    Coverage reports directory. Defaults to ./Coverage"
+  echo "[-w <Workspace path>] Workspace directory for source code files."
+  echo "[-o <Info filename>]  Merged info file. Defaults to ./merged_coverage.info"
+  echo "[-m <JSON filename>]  JSON merged SCM sources. Defaults to ./merged_scm.json"
+  echo "[-c]                  Flag to download/copy the source files from the JSON merged SCM into the workspace directory."
+  echo "[-g]                  Flag to generate local reports for each info/json instance."
+  echo "$help_message"
+}
 
-clear
+
+[ ${-/x} != ${-} ] && TRACING=true || TRACING=false
 LOCAL_WORKSPACE=""
 CLONE_SOURCES=false
 merge_configuration_file=""
@@ -377,7 +388,11 @@
 # File name for merge coverage info
 merged_coverage_file="./merged_coverage.info"
 merged_json_file="./merged_scm.json"
+# File name to pass variables to calling script
+variables_file="./variables.sh"
 info_files=() # Array of info files
+json_files=() # Array of configuration json files
+list_of_merged_builds=()
 while getopts ":hj:o:l:w:i:cm:g" opt; do
   case ${opt} in
     h )
@@ -385,7 +400,7 @@
       exit 0
       ;;
     w )
-      LOCAL_WORKSPACE=$(cd $OPTARG; pwd)
+      LOCAL_WORKSPACE=$(cd $OPTARG  &>/dev/null || true; pwd)
       ;;
     c )
       CLONE_SOURCES=true
@@ -405,6 +420,9 @@
     g )
       generate_local=true
       ;;
+    x )
+      variables_file=$OPTARG
+      ;;
     \? )
       echo "Invalid option: $OPTARG" 1>&2
       usage
@@ -440,9 +458,14 @@
     clone_repos $merged_json_file
 fi
 
-# Generate branch coverage report
+# Generate merged coverage report
+merged_status=true
 genhtml --branch-coverage $merged_coverage_file \
     --output-directory $LCOV_FOLDER
+if [ $? -ne 0 ];then
+    merged_status=false
+    echo "ERROR: Cannot merge coverage reports"
+fi
 
 if [ "$generate_local" = true ];then
   generate_local_reports