Blame SOURCES/configlib.sh

8f5e58
# Copyright (C) 2017, Red Hat, Inc.
8f5e58
#
8f5e58
# Core configuration file library.
8f5e58
8f5e58
# Configurations are determined by sha values.  The way to determine is by
8f5e58
# the special text:
8f5e58
# $FILE_COMMENT_TYPE -*- cfg-sha: $SHA256 -*-
8f5e58
8f5e58
export LC_ALL=C
8f5e58
8f5e58
# check required binaries
8f5e58
__check_reqd_binaries() {
8f5e58
    local BIN __binaries=("egrep" "sort" "sha256sum" "sed")
8f5e58
    for BIN in $__binaries; do
8f5e58
        if ! type -P $BIN >/dev/null 2>&1; then
8f5e58
            echo "Binary $BIN not found.  Please install."
8f5e58
            exit 1
8f5e58
        fi
8f5e58
    done
8f5e58
}
8f5e58
8f5e58
# Calculates a sha from a file
8f5e58
# The algorithm for generating a sha from a config is thus:
8f5e58
#
8f5e58
# 1. Remove all comment lines and blank lines
8f5e58
# 2. Sort the content
8f5e58
# 3. generate the sha-256 sum
8f5e58
#
8f5e58
# From a script perspective, this means:
8f5e58
#   egrep -v ^\# %file% | egrep -v ^$ | sort -u | sha256sum
8f5e58
#
8f5e58
# Params:
8f5e58
#  $1 = output variable
8f5e58
#  $2 = file to use to calculate the shasum
8f5e58
#  $3 = file comment type (defaults to # if unspecified)
8f5e58
calc_sha() {
8f5e58
    __check_reqd_binaries
8f5e58
8f5e58
    if [ "$1" == "" ]; then
8f5e58
        echo "Please pass in a storage variable."
8f5e58
        return 1
8f5e58
    fi
8f5e58
8f5e58
    local __resultvar=$1
8f5e58
    __retval=1
8f5e58
    shift
8f5e58
8f5e58
    local __file=$1
8f5e58
    local cmnt=${2:-#}
8f5e58
8f5e58
    if [ -f "$__file" ]; then
8f5e58
        local __shasum=$(egrep -v ^"$cmnt" "$__file" | egrep -v ^$ | sort -u | sha256sum -t | cut -d" " -f1)
8f5e58
        eval $__resultvar="'$__shasum'"
8f5e58
        __retval=0
8f5e58
    fi
8f5e58
    return $__retval
8f5e58
}
8f5e58
8f5e58
# Retrieves a sha stored in a file
8f5e58
# Param:
8f5e58
#  $1 = output variable
8f5e58
#  $2 = file to use to calculate the shasum
8f5e58
#  $3 = file comment type (defaults to # if unspecified)
8f5e58
retr_sha() {
8f5e58
    __check_reqd_binaries
8f5e58
8f5e58
    if [ "$1" == "" ]; then
8f5e58
        echo "Please pass in a storage variable."
8f5e58
        return 1
8f5e58
    fi
8f5e58
8f5e58
    local __resultvar=$1
8f5e58
    __retval=1
8f5e58
    shift
8f5e58
8f5e58
    local __file=$1
8f5e58
    local cmnt=${2:-#}
8f5e58
8f5e58
    if [ -f "$__file" ]; then
8f5e58
        if grep -q "$cmnt -\*- cfg-sha:" "$__file"; then
8f5e58
            local __shasum=$(grep "$cmnt -\*- cfg-sha:" "$__file" | sed -e "s@$cmnt -\*- cfg-sha: @@" | cut -d" " -f1)
8f5e58
            eval $__resultvar="'$__shasum'"
8f5e58
            __retval=0
8f5e58
        fi
8f5e58
    fi
8f5e58
    return $__retval
8f5e58
}
8f5e58
8f5e58
8f5e58
# Set a config value
8f5e58
# set_conf dpdk_build_tree parameter value
8f5e58
# dpdk_build_tree is the directory where the .config lives
8f5e58
# parameter is the config parameter
8f5e58
# value is the value to set for the config parameter
8f5e58
set_conf() {
8f5e58
    c="$1/.config"
8f5e58
    shift
8f5e58
8f5e58
    if grep -q "$1" "$c"; then
8f5e58
        sed -i "s:^$1=.*$:$1=$2:g" $c
8f5e58
    else
8f5e58
        echo $1=$2 >> "$c"
8f5e58
    fi
8f5e58
}