Blame SOURCES/configlib.sh

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