Merge "automated/linux: add support for WA result postprocessing with LISA"
diff --git a/automated/linux/aep-pre-post/lisa-postprocessing.yaml b/automated/linux/aep-pre-post/lisa-postprocessing.yaml
new file mode 100644
index 0000000..8dbc108
--- /dev/null
+++ b/automated/linux/aep-pre-post/lisa-postprocessing.yaml
@@ -0,0 +1,29 @@
+metadata:
+    format: Lava-Test Test Definition 1.0
+    name: lisa-postprocessing
+    description: "Post-processing of results generated by
+                  Workload Automation v3."
+    maintainer:
+        - lisa.nguyen@linaro.org
+        - milosz.wasilewski@linaro.org
+    os:
+        - ubuntu
+    devices:
+        - hi6220-hikey
+        - hi960-hikey
+        - juno
+
+params:
+    LISA_REPOSITORY: https://github.com/ARM-software/lisa
+    LISA_REF: master
+    # LISA_SCRIPT is a path relative to LISA top directory
+    LISA_SCRIPT: ipynb/wltests/sched-evaluation-full.py
+    SKIP_INSTALL: false
+
+run:
+    steps:
+        - cd ./automated/linux/aep-pre-post
+        - export LISA_SHELL_DISABLE_COLORS=true
+        - export TERM=xterm
+        - ./lisa.sh -r "${LISA_REPOSITORY}" -t "${LISA_REF}" -s "${LISA_SCRIPT}" -S "${SKIP_INSTALL}"
+        - ../../utils/send-to-lava.sh ./output/result.txt
diff --git a/automated/linux/aep-pre-post/lisa.sh b/automated/linux/aep-pre-post/lisa.sh
new file mode 100755
index 0000000..a592ec1
--- /dev/null
+++ b/automated/linux/aep-pre-post/lisa.sh
@@ -0,0 +1,57 @@
+#!/bin/bash -ex
+# shellcheck disable=SC1090
+
+TEST_DIR=$(dirname "$(realpath "$0")")
+OUTPUT="${TEST_DIR}/output"
+SKIP_INSTALL="false"
+LISA_REPOSITORY="https://github.com/ARM-software/lisa"
+LISA_REF="master"
+LISA_SCRIPT="ipynb/wltests/sched-evaluation-full.py"
+
+usage() {
+    echo "Usage: $0 [-t <lisa_repository_ref>] [-r <lisa_repository>] [-s <lisa_script>] [-S <skip_install>]" 1>&2
+    exit 1
+}
+
+while getopts ":t:r:s:S:" opt; do
+    case "${opt}" in
+        t) LISA_REF="${OPTARG}" ;;
+        r) LISA_REPOSITORY="${OPTARG}" ;;
+        s) LISA_SCRIPT="${OPTARG}" ;;
+        S) SKIP_INSTALL="${OPTARG}" ;;
+        *) usage ;;
+    esac
+done
+
+. "${TEST_DIR}/../../lib/sh-test-lib"
+
+! check_root && error_msg "Please run this test as root."
+cd "${TEST_DIR}"
+create_out_dir "${OUTPUT}"
+RESULT_FILE="${OUTPUT}/result.txt"
+export RESULT_FILE
+
+if [ "${SKIP_INSTALL}" = "true" ] || [ "${SKIP_INSTALL}" = "True" ]; then
+    info_msg "Dependency installation skipped"
+else
+    PKGS="build-essential autoconf automake libtool pkg-config trace-cmd sshpass kernelshark nmap net-tools tree libfreetype6-dev libpng12-dev python-pip python-dev python-tk"
+    install_deps "${PKGS}"
+    pip install --upgrade --quiet pip && hash -r
+    pip install --upgrade --quiet setuptools
+    pip install --upgrade --quiet matplotlib numpy nose Cython trappy bart-py devlib psutil wrapt scipy IPython
+    git clone "${LISA_REPOSITORY}" lisa
+    (
+    cd lisa
+    git checkout "${LISA_REF}"
+    )
+fi
+# TODO: check if lisa directory exists
+cd lisa
+. init_env
+lisa-update submodules
+python "${LISA_SCRIPT}"
+ls
+for FILE in *.csv
+do
+    python "${TEST_DIR}/postprocess_lisa_results.py" -f "${FILE}" -o "${RESULT_FILE}"
+done
diff --git a/automated/linux/aep-pre-post/postprocess_lisa_results.py b/automated/linux/aep-pre-post/postprocess_lisa_results.py
new file mode 100644
index 0000000..c0426c5
--- /dev/null
+++ b/automated/linux/aep-pre-post/postprocess_lisa_results.py
@@ -0,0 +1,48 @@
+import argparse
+import csv
+
+
+def main():
+    parser = argparse.ArgumentParser()
+    parser.add_argument("-f",
+                        "--file",
+                        help="CSV file for postprocessing",
+                        dest="source_filename")
+    parser.add_argument("-o",
+                        "--output-file",
+                        help="Results file",
+                        dest="results_filename")
+    args = parser.parse_args()
+
+    row_index = 0
+    headers = []
+    measurements = {}
+    with open(args.source_filename, "rb") as f:
+        reader = csv.reader(f)
+        for row in reader:
+            if row_index == 0:
+                headers = row
+            elif row_index > 0 and row_index <= 2:
+                item_index = 0
+                for item in row:
+                    if len(item) > 0:
+                        headers[item_index] = headers[item_index] + "_" + item
+                    item_index = item_index + 1
+            else:
+                # concatenate first 3 cells
+                name = row[0] + "_" + row[1] + "_" + row[2]
+                item_index = 3
+                for item in row[item_index:]:
+                    if item != "":
+                        measurement_name = name + "_" + headers[item_index]
+                        measurements.update({measurement_name: item})
+                    item_index = item_index + 1
+            row_index = row_index + 1
+    with open(args.results_filename, "a") as results:
+        for key, value in measurements.items():
+            # key is test name
+            # value is measurement to be recorded
+            results.write("%s pass %s _\r\n" % (key, value))
+
+if __name__ == "__main__":
+    main()