aio-builder/lib/builder-utils.sh

362 lines
9.0 KiB
Bash
Raw Normal View History

#############################################
# void retrieve_file (src, dst)
#
# Description: retrieve file from an URL
2013-03-07 21:38:36 +01:00
#
# Parameter
# downloader: program to download the files
# src: source url to get file from
# dst: path to store file to
#
#############################################
function retrieve_file() {
2013-03-07 21:38:36 +01:00
local downloader=$1
local src=$2
local dst=$3
# Check, if the URL is a file URL starting with file://
if [ -f $dst ] && [ -z ${DIST_FORCE_DOWNLOAD} ]; then
echo " Info: File still cached/downloaded. To force a download, set DIST_FORCE_DOWNLOAD=1"
elif [[ $src == file://* ]]; then
2013-03-07 21:38:36 +01:00
fileurl=`echo $src | sed "s/^file:\/\///"`
cp $fileurl $dst 2>/dev/null
else
rm -f $dst
if [ "$downloader" = "wget" ]; then
2013-06-24 19:47:07 +02:00
wget --no-check-certificate --tries=1 -O $dst --timeout=5 -q --no-verbose "$src"
2013-03-07 21:38:36 +01:00
if [ "$?" == "1" ] ; then
rm $dst
fi
elif [ "$downloader" = "plowdown" ]; then
plowdown --max-retries=1 -o ${dst%/*} "$src"
elif [ "$downloader" = "aria2c" ]; then
#aria2c --seed-time=0 -d ${dst%/*} -o ${dst##*/} "$src"
aria2c --seed-time=0 --allow-overwrite=true -o ${dst##*/} "$src"
mv -f ${dst##*/} ${dst%/*}
else
2013-03-07 21:38:36 +01:00
fatal_error "Downloader not implemented: $downloader"
fi
fi
}
#############################################
# void extract_file (format, src, dst)
#
# Description: Extract a file
2013-03-07 21:38:36 +01:00
#
# Parameter
# format: compression format
# src: source file to be used
# dst: path to extract the file
#
#############################################
function process_file() {
2013-03-07 21:38:36 +01:00
local format=$1
local src=$2
local dst=$3
local option=$4
2013-03-07 21:38:36 +01:00
log_debug "Compression format: $format"
if [ "$format" = "cp" ]; then
cp $src $dst
elif [ "$format" = "7zip" ]; then
$CMD_7z x -y $option -o$dst $src
2012-01-31 20:53:55 +01:00
elif [ "$format" = "unzip" ]; then
$CMD_unzip $option -o $src -d $dst
elif [ "$format" = "unrar" ]; then
$CMD_unrar x -y $option $src $dst
2012-05-16 06:32:07 +02:00
elif [ "$format" = "targz" ]; then
$CMD_tar xzvf $option $src -C $dst
2012-05-16 06:32:07 +02:00
elif [ "$format" = "tarbz2" ]; then
$CMD_tar xjvf $option $src -C $dst
2012-10-30 16:22:30 +01:00
elif [ "$format" = "cab" ]; then
$CMD_cabextract $option -d $dst $src
2013-03-07 21:38:36 +01:00
else
fatal_error "Unknown compression format: $format"
fi
}
#############################################
# check if method is available and call it
#############################################
function call_entry_point() {
2013-03-07 21:38:36 +01:00
local _resultvar=$1
local func=$2
# Entry point
type $func &>/dev/null
if [ $? == 0 ] ; then
$func
eval $_resultvar="0"
else
eval $_resultvar="1"
fi
}
###################
# Fata Error
###################
fatal_error() {
2013-03-07 21:38:36 +01:00
echo "FATAL: $1"
exit 1
}
###################
# Check error
###################
builder_check_error() {
2013-03-07 21:38:36 +01:00
if [ "$?" == "1" ] ; then
fatal_error "$1"
fi
}
###################
# Logging Debug
###################
log_debug() {
2013-03-07 21:38:36 +01:00
local str=$1
if [ "$DEBUG_LEVEL" = "debug" ] || [ "$DEBUG_LEVEL" = "info" ] ; then
echo $str
fi
}
###################
# Logging Info
###################
log_info() {
2013-03-07 21:38:36 +01:00
local str=$1
if [ "$DEBUG_LEVEL" = "info" ] ; then
echo $str
fi
}
#####################
2013-03-07 21:38:36 +01:00
# void calc_release()
#
# Description:
# Calculate new release based on
# the latest one found in the repository.
2013-03-07 21:38:36 +01:00
#
# $RELEASE is set to the calculated release.
2013-03-07 21:38:36 +01:00
#
####################
function calc_release() {
2013-03-07 21:38:36 +01:00
# Find all revision files and sort them
2013-09-09 05:06:01 +02:00
local file_list=`mktemp /tmp/aio-builder.calc_release.XXXXXXXXXXX`
for cfg_file in `find ${AIO_REPOS_BASE_DIR} -name "${PN}-${VERSION}-${CREATOR_TAG}*.cfg" -print ` ; do
2013-03-07 21:38:36 +01:00
. ${cfg_file}
printf "%08d;$cfg_file\n" $REV_RELEASE >> ${file_list}
done
local oldest_cfg=`sort -n ${file_list} | cut -f 2 -d ";" | tail -1`
rm -f ${file_list}
if [ ! -f "${oldest_cfg}" ] ; then
echo "Warning: no cfg-file found for this product. RELEASE will be set to 1"
RELEASE=1
else
log_debug "calc_release() oldest_cfg: ${oldest_cfg}"
. ${oldest_cfg}
log_debug " latest release: $REV_RELEASE"
RELEASE=`expr ${REV_RELEASE} + 1 2> /dev/null`
builder_check_error "Cannot incrememnt REV_RELEASE from file ${oldest_cfg}"
fi
}
2013-03-07 21:38:36 +01:00
###################
# Write Package Ini file
#
# Create a ini file containing needed information for package uninstall
#
# Parameter
# file: file to create / modify
#
###################
write_ini_file() {
local ini_file=$1
2013-09-09 05:06:01 +02:00
local ini_section=$2
local ini_option=$3
local ini_value=$4
2013-03-08 14:15:35 +01:00
touch $ini_file
2013-09-09 05:06:01 +02:00
$CMD_inifile $(cygpath -pw "$ini_file") [$ini_section] $ini_option=$ini_value
}
###################
# Get Verison of File
#
# Copy file only if it is newer
#
# Parameter
# File: to process
# Var:
#
###################
# return 0 if program version is equal or greater than check version
check_version()
{
local version=$1 check=$2
local winner=$(echo -e "$version\n$check" | sed '/^$/d' | sort -Vr | head -1)
[[ "$winner" = "$version" ]] && return 0
return 1
}
###################
# Get Verison of File
#
# Copy file only if it is newer
#
# Parameter
# File: to process
# Var:
#
###################
get_file_ver() {
local getver_file=$2
local __resultvar=$1
# local myresult=`$CMD_showver $(cygpath -pw "$getver_file") | grep FileVersion: | rev | cut -d: -f1 | rev | tr -d ' '`
local myresult=`cscript //nologo $(cygpath -pw "$BASEDIR/libexec/VersionInfo.vbs") $(cygpath -pw "$getver_file") | tr -d '\r' | tr -d '\n'`
local mydate=`date -r $getver_file +%Y.%m.%d.%M`
if [ "x$myresult" = "x" ] ; then
echo "$getver_file -> $mydate"
eval $__resultvar="'$mydate'"
else
echo "$getver_file -> $myresult"
eval $__resultvar="'$myresult'"
fi
2013-09-09 05:06:01 +02:00
}
###################
# Copy Verison
#
# Copy file only if it is newer
#
# Parameter
# source: sourcefiel
# target: Targetfile
#
###################
copyver() {
local source_file=$1
local target_file=$2
local source_ver="0.0.0.0"
local target_ver="0.0.0.0"
2013-03-10 05:57:28 +01:00
echo "Source File: $source_file Target Version: $target_file"
if [ -f "$source_file" ] && [ -f "$target_file" ] ; then
get_file_ver source_ver $source_file
get_file_ver target_ver $target_file
if check_version $source_ver $target_ver ; then
echo "Source Verssion: $source_ver > Target Version: $target_ver"
echo "Copying File"
cp -a $source_file $target_file
else
echo "Source Verssion: $source_ver =< Target Version: $target_ver"
echo "Not Copying File"
fi
elif [ -f "$source_file" ] && [ ! -f "$target_file" ] ; then
get_file_ver source_ver $source_file
echo "Source Verssion: $source_ver"
echo "Copying File"
cp -a $source_file $target_file
else
echo "Nothing to copy"
fi
}
2013-03-07 21:38:36 +01:00
2013-03-07 22:43:46 +01:00
###################
2013-09-09 05:06:01 +02:00
# Tidy xml
2013-03-07 22:43:46 +01:00
#
2013-09-09 05:06:01 +02:00
# Tidy XML files (mainly Sereby package.xml)
2013-03-07 22:43:46 +01:00
#
# Parameter
2013-09-09 05:06:01 +02:00
# source: sourcefiel
# target: Targetfile
2013-03-07 22:43:46 +01:00
#
###################
2013-09-09 05:06:01 +02:00
tidy_xml() {
local source_file=$1
local target_file=$2
2013-03-07 22:43:46 +01:00
2013-09-09 05:06:01 +02:00
cat $source_file | $CMD_tidy -utf8 -xml -w 255 -i -c -q -asxml | sed -e 's!&gt;!>!g' -e 's!&lt;!<!g' > $target_file
2013-03-07 22:43:46 +01:00
}
#############################################
# void extract_file (format, src, dst)
#
# Description: Extract a file
#
# Parameter
# format: compression format
# src: source file to be used
# dst: path to extract the file
#
#############################################
function extract_file() {
local format=$1
local src=$2
local dst=$3
local option=$4
local src_dir=`dirname "$src"`
local src_file=`basename "$src"`
local dst_dir=`dirname "$dst"`
local dst_file=`basename "$dst"`
log_debug "Compression format: $format"
mkdir -p $dst
if [ "$format" = "msi-a" ]; then
mkdir /cygdrive/c/msi-adm
pushd $src_dir
echo $src_file
$CMD_msiexec /a "$src_file" $option TARGETDIR="c:\msi-adm"
popd
cp -r /cygdrive/c/msi-adm/* $dst
rm -rf /cygdrive/c/msi-adm
elif [ "$format" = "msp-a" ]; then
pushd $dst_dir
cp $src $dst_dir
$CMD_msiexec /p "$src_file" /a "$dst_file" SHORTFILENAMES=TRUE /qb
popd
elif [ "$format" = "msp-x" ]; then
$CMD_msix $(cygpath -pw "$src") /out $(cygpath -pw "$dst")
elif [ "$format" = "7zip" ]; then
$CMD_7z x -y $option -o$dst $src
elif [ "$format" = "unzip" ]; then
$CMD_unzip $option -o $src -d $dst
elif [ "$format" = "unrar" ]; then
$CMD_unrar x -y $option $(cygpath -pw "$src") $(cygpath -pw "$dst")
elif [ "$format" = "cab" ]; then
$CMD_cabextract $option -d $dst $src
elif [ "$format" = "cab-sfx" ]; then
$CMD_cabextract $option -d $dst $src
$CMD_xmlstarlet sel -N X="http://schemas.microsoft.com/wix/2008/Burn" -t -m "//X:Payload" -v "concat(@SourcePath,'-',@FilePath)" -n $dst/0 | tr '\\' '/' > $dst/rename.list
for i in `cat $dst/rename.list`
do
pushd $dst
local src_var=$(echo $i | cut -f1 -d-)
local dst_var=$(echo $i | cut -f2 -d-)
mkdir -p $(dirname $dst_var)
mv -v $src_var $dst_var
popd
done
elif [ "$format" = "inno" ]; then
$CMD_innounp -x -d"$(cygpath -pw "$dst")" "$(cygpath -pw "$src")"
else
fatal_error "Unknown compression format: $format"
fi
}