Move all global utility function to infra/utils.sh 49/144149/4
authorDmitriy Nikiforov <d.nikiforov@partner.samsung.com>
Thu, 10 Aug 2017 19:39:21 +0000 (22:39 +0300)
committerDmitriy Nikiforov <d.nikiforov@partner.samsung.com>
Thu, 17 Aug 2017 12:12:04 +0000 (15:12 +0300)
Change-Id: I60982c957452812792f591d394f963d9c4a0dfad

infra/tizen_fuzz.sh
infra/utils.sh [new file with mode: 0644]

index ee50b98..35f6c0b 100755 (executable)
@@ -45,213 +45,6 @@ EOF
 }
 
 ########################################################
-#                  UTILITY FUNCTIONS
-########################################################
-
-# Joins array using the specified delimeter.
-# Args:
-# 1)    delimeter
-# 2-..) array to join
-function join {
-    local d=$1
-    shift
-    echo -n "$1"
-    shift
-    printf "%s" "${@/#/$d}"
-}
-
-# Silent pushd
-function pushd {
-    command pushd "$@" > /dev/null
-}
-
-# Silent popd
-function popd {
-    command popd "$@" > /dev/null
-}
-
-# Checks if verbose-mode is on and sets '-x' option
-function check_verbose {
-    if (( VERBOSE == 1 )); then
-        set -x
-    fi
-}
-
-# Removes element from the array
-# Args:
-# 1) element to remove
-# 2-..) array
-function remove_element {
-    remove=${1}
-    shift
-    new_array=()
-    for elem in ${@}; do
-        if [[ ${elem} != ${remove} ]]; then
-            new_array+=("${elem}")
-        fi
-    done
-
-    join ' ' ${new_array[@]}
-}
-
-# Workaround for 'sdb shell' inability to propagate error codes
-function sdb_shell_verbose {
-    set +e
-
-    error_str='SDB_ERROR'
-    cmd="${SDB_CMD[@]} shell ${@}"
-    output=$(command ${cmd} '||' 'echo' "${error_str}")
-
-    echo "${output}" | grep "${error_str}" >/dev/null
-    if [[ $? -eq 0 ]]; then
-        echo "Error: failed to execute command on device:" >&2
-        echo "${output}" | grep -v "${error_str}" >&2
-        exit 1
-    fi
-
-    if [[ -n ${output} ]]; then
-        # strip last '\r' character
-        printf "%s" "${output::(-1)}"
-    fi
-
-    set -e
-}
-
-# Silent version
-function sdb_shell {
-    # print the sdb command
-    echo "${SDB_CMD[@]} 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}')`
-    export -f join
-    export -f pushd
-    export -f popd
-    export -f check_verbose
-    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
-}
-
-########################################################
 #               GLOBAL OPTIONS PARSING
 ########################################################
 
@@ -288,6 +81,9 @@ fi
 #                SUBCOMMANDS PARSING
 ########################################################
 
+# source utility functions
+source "$(dirname ${0})/utils.sh"
+
 BUILD_ARTIFACTS_DIR="${TIZEN_FUZZ_HOME}/build"
 GBS_ROOT="${TIZEN_FUZZ_HOME}/GBS_ROOT"
 
diff --git a/infra/utils.sh b/infra/utils.sh
new file mode 100644 (file)
index 0000000..5a674a4
--- /dev/null
@@ -0,0 +1,206 @@
+########################################################
+#                  UTILITY FUNCTIONS
+########################################################
+
+# Joins array using the specified delimeter.
+# Args:
+# 1)    delimeter
+# 2-..) array to join
+function join {
+    local d=$1
+    shift
+    echo -n "$1"
+    shift
+    printf "%s" "${@/#/$d}"
+}
+
+# Silent pushd
+function pushd {
+    command pushd "$@" > /dev/null
+}
+
+# Silent popd
+function popd {
+    command popd "$@" > /dev/null
+}
+
+# Checks if verbose-mode is on and sets '-x' option
+function check_verbose {
+    if (( VERBOSE == 1 )); then
+        set -x
+    fi
+}
+
+# Removes element from the array
+# Args:
+# 1) element to remove
+# 2-..) array
+function remove_element {
+    remove=${1}
+    shift
+    new_array=()
+    for elem in ${@}; do
+        if [[ ${elem} != ${remove} ]]; then
+            new_array+=("${elem}")
+        fi
+    done
+
+    join ' ' ${new_array[@]}
+}
+
+# Workaround for 'sdb shell' inability to propagate error codes
+function sdb_shell_verbose {
+    set +e
+
+    error_str='SDB_ERROR'
+    cmd="${SDB_CMD[@]} shell ${@}"
+    output=$(command ${cmd} '||' 'echo' "${error_str}")
+
+    echo "${output}" | grep "${error_str}" >/dev/null
+    if [[ $? -eq 0 ]]; then
+        echo "Error: failed to execute command on device:" >&2
+        echo "${output}" | grep -v "${error_str}" >&2
+        exit 1
+    fi
+
+    if [[ -n ${output} ]]; then
+        # strip last '\r' character
+        printf "%s" "${output::(-1)}"
+    fi
+
+    set -e
+}
+
+# Silent version
+function sdb_shell {
+    # print the sdb command
+    echo "${SDB_CMD[@]} 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}')`
+    export -f join
+    export -f pushd
+    export -f popd
+    export -f check_verbose
+    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
+}