Blame SOURCES/configlib.sh

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