Blame SOURCES/configlib.sh

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