Add various utility functions to remove code duplication 48/144148/5
authorDmitriy Nikiforov <d.nikiforov@partner.samsung.com>
Thu, 10 Aug 2017 19:31:50 +0000 (22:31 +0300)
committerDmitriy Nikiforov <d.nikiforov@partner.samsung.com>
Thu, 17 Aug 2017 12:12:00 +0000 (15:12 +0300)
Added functions:
 * get_device_arch
 * get_config_by_arch
 * get_profile_by_arch
 * install_deps
 * check_deps
 * check_and_install_deps

Change-Id: I2eeba01a28e48c3a773c9a89c56de2b53e00ecfd

infra/commands/build.sh
infra/commands/publish.sh
infra/tizen_fuzz.sh

index 7b7d8c4..e850bb5 100755 (executable)
@@ -63,12 +63,6 @@ TARGET_BUILD_SCRIPT="_target_build.sh"
 TARGET_SPEC='targetspec'
 LIBFUZZER_PKGS=('libFuzzer' 'fuzz-force-options')
 SRCS_DIR="${TIZEN_FUZZ_HOME}/sources"
-GBS_EMULATOR64_CONFIG="${CONFIG_DIR}/gbs_emulator64.conf"
-GBS_EMULATOR32_CONFIG="${CONFIG_DIR}/gbs_emulator32.conf"
-GBS_EMULATOR64_PROFILE="emulator64"
-GBS_EMULATOR32_PROFILE="emulator32"
-GBS_CONFIG="${GBS_EMULATOR64_CONFIG}"
-GBS_PROFILE="${GBS_EMULATOR64_PROFILE}"
 EXTRA_GBS_ARGS=('--clean' '--skip-srcrpm' '--clean-repos' '--include-all')
 CUSTOM_SPEC=""
 ARCH='x86_64'
@@ -119,21 +113,15 @@ if [[ -z ${1} ]]; then
     exit 1
 fi
 
-case ${ARCH} in
-    'x86_64')
-        GBS_CONFIG="${GBS_EMULATOR64_CONFIG}"
-        GBS_PROFILE="${GBS_EMULATOR64_PROFILE}"
-        ;;
-    'i586')
-        GBS_CONFIG="${GBS_EMULATOR32_CONFIG}"
-        GBS_PROFILE="${GBS_EMULATOR32_PROFILE}"
-        ;;
-    *)
-        echo "Error: Unsupported architecture '${ARCH}'."
-        print_help
-        exit 1
-        ;;
-esac
+GBS_CONFIG=$(get_config_by_arch "${ARCH}")
+GBS_PROFILE=$(get_profile_by_arch "${ARCH}")
+
+if [[ -z ${GBS_CONFIG} || -z ${GBS_PROFILE} ]]; then
+    echo "Error: Unsupported architecture '${ARCH}'."
+    print_help
+    exit 1
+fi
+
 
 if [[ -n ${CUSTOM_SPEC} && ! -f ${CUSTOM_SPEC} ]]; then
     echo "Error: ${CUSTOM_SPEC} file was not found."
index 3c079fa..6ac688c 100755 (executable)
@@ -86,8 +86,7 @@ fi
 # check the device architecture
 echo "Checking architecture..."
 
-declare -A archs=([x86_64]='x86_64' [i686]='i586')
-ARCH=${archs["$(sdb_shell_verbose uname -m)"]}
+ARCH=$(get_device_arch)
 if [[ -z ${ARCH} || ! -d ${TARGET_DIR}/${ARCH} ]]; then
     echo "Error: there is no built ${target_name} for ${ARCH} architecture."
     exit 1
index c3af87a..ee50b98 100755 (executable)
@@ -125,6 +125,113 @@ function sdb_shell {
     sdb_shell_verbose ${@} >/dev/null
 }
 
+# Checks the device architecture
+function get_device_arch {
+    declare -A archs=([x86_64]='x86_64' [i686]='i586')
+    echo "${archs[$(sdb_shell_verbose uname -m)]}"
+}
+
+# Gets gbs config file by architecture
+# Args:
+# 1) architecture
+function get_config_by_arch {
+    declare -A configs=([x86_64]="${CONFIG_DIR}/gbs_emulator64.conf"
+                        [i586]="${CONFIG_DIR}/gbs_emulator32.conf")
+    echo "${configs[$1]}"
+}
+
+# Gets gbs profile by architecture
+# Args:
+# 1) architecture
+function get_profile_by_arch {
+    declare -A profiles=([x86_64]='emulator64'
+                        [i586]='emulator32')
+    echo "${profiles[$1]}"
+}
+
+# Installs specified dependencies on device
+# Args:
+# 1)    repository: base or unified
+# 2-..) list of dependencies with prepended arch (e.g. x86_64/cpio)
+function install_deps {
+    local arch
+    local config
+    local url
+
+    arch=$(get_device_arch)
+    config=$(get_config_by_arch "${arch}")
+
+    if [[ -z ${config} ]]; then
+        echo "Error: unsupported architecture '${arch}'."
+        exit 1
+    fi
+
+    if [[ ${1} = "base" || ${1} = "unified" ]]; then
+        url=$(sed -En "s|url=(.*/tizen/${1}/.*)|\1|p" "${config}")
+    else
+        echo "Error: unsupported repository '${1}'"
+        exit 1
+    fi
+
+    shift
+    local tmp_dir=$(mktemp -d '/tmp/XXXX')
+    mkdir -p "${tmp_dir}"
+    pushd "${tmp_dir}"
+    for dependency in "${@}"; do
+        wget -q -nd -r -np -A "${dependency}-[0-9]*.rpm" "${url}"
+    done
+    popd
+
+    echo "Pushing following packages on device:"
+    ls -1A "${tmp_dir}"
+
+    "${SDB_CMD[@]}" push "${tmp_dir}" "/tmp" >/dev/null
+    sdb_shell rpm -ivh "/tmp/*.rpm"
+    sdb_shell rm -f "/tmp/*.rpm"
+
+    rm -rf "${tmp_dir}"
+}
+
+# Checks that all specified dependencies are installed on device and prints missing.
+# Args:
+# 1-..) list of dependencies to check
+function check_deps {
+    local found
+    for dependency in "${@}"; do
+        found=$("${SDB_CMD[@]}" shell rpm -qa '|' grep "${dependency}")
+        if [[ -z ${found} ]]; then
+            echo "${dependency}"
+        fi
+    done
+}
+
+# Checks and installs, if needed, specified dependencies from specified repo.
+# Args:
+# 1)    Whether missing dependencies should be installed (1) or not (0).
+# 2)    Repo: 'base' or 'unified'.
+# 3-..) List of packages to be installed.
+function check_and_install_deps {
+    local repo
+    local missing
+    local install
+    local deps
+
+    install="${1}"
+    repo="${2}"
+    missing=$(check_deps "${@:3}")
+    if [[ -n ${missing} ]]; then
+        if (( install == 1 )); then
+            deps=(${missing//$'\n'/ })
+            echo "Installing following packages: ${deps[*]}"
+            install_deps "${repo}" "${deps[@]}"
+        else
+            echo "Error: following dependencies are not installed on the device: ${deps[*]}"
+            echo "Use '--instal-deps' option to install them."
+            exit 1
+        fi
+    fi
+}
+
 # Exports all current environment
 function export_all {
     `echo "export" $((set -o posix ; set) | awk -F "=" 'BEGIN{ORS=" "}1 $1~/[a-zA-Z]/ {print $1}')`
@@ -135,6 +242,12 @@ function export_all {
     export -f remove_element
     export -f sdb_shell_verbose
     export -f sdb_shell
+    export -f get_device_arch
+    export -f get_config_by_arch
+    export -f get_profile_by_arch
+    export -f install_deps
+    export -f check_deps
+    export -f check_and_install_deps
     export -f export_all
 }