yunohost/data/helpers.d/package
2016-05-21 11:20:25 +02:00

102 lines
2.8 KiB
Text

# Check either a package is installed or not
#
# example: ynh_package_is_installed 'yunohost' && echo "ok"
#
# usage: ynh_package_is_installed name
# | arg: name - the package name to check
ynh_package_is_installed() {
dpkg-query -W -f '${Status}' "$1" 2>/dev/null \
| grep -c "ok installed" &>/dev/null
}
# Get the version of an installed package
#
# example: version=$(ynh_package_version 'yunohost')
#
# usage: ynh_package_version name
# | arg: name - the package name to get version
# | ret: the version or an empty string
ynh_package_version() {
if ynh_package_is_installed "$1"; then
dpkg-query -W -f '${Version}' "$1" 2>/dev/null
else
echo ''
fi
}
# APT wrapper for non-interactive operation
#
# usage: ynh_apt update
ynh_apt() {
DEBIAN_FRONTEND=noninteractive sudo apt-get -y -qq $@
}
# Update package index files
#
# usage: ynh_package_update
ynh_package_update() {
ynh_apt update
}
# Install package(s)
#
# usage: ynh_package_install name [name [...]]
# | arg: name - the package name to install
ynh_package_install() {
ynh_apt -o Dpkg::Options::=--force-confdef \
-o Dpkg::Options::=--force-confold install $@
}
# Build and install a package from an equivs control file
#
# example: generate an empty control file with `equivs-control`, adjust its
# content and use helper to build and install the package:
# ynh_package_install_from_equivs /path/to/controlfile
#
# usage: ynh_package_install_from_equivs controlfile
# | arg: controlfile - path of the equivs control file
ynh_package_install_from_equivs() {
controlfile=$1
# install equivs package as needed
ynh_package_is_installed 'equivs' \
|| ynh_package_install equivs
# retrieve package information
pkgname=$(grep '^Package: ' $controlfile | cut -d' ' -f 2)
pkgversion=$(grep '^Version: ' $controlfile | cut -d' ' -f 2)
[[ -z "$pkgname" || -z "$pkgversion" ]] \
&& echo "Invalid control file" && exit 1
# update packages cache
ynh_package_update
# build and install the package
TMPDIR=$(ynh_mkdir_tmp)
(cp "$controlfile" "${TMPDIR}/control" \
&& cd "$TMPDIR" \
&& equivs-build ./control 1>/dev/null \
&& sudo dpkg --force-depends \
-i "./${pkgname}_${pkgversion}_all.deb" 2>&1 \
&& ynh_package_install -f) \
&& ([[ -n "$TMPDIR" ]] && rm -rf $TMPDIR)
# check if the package is actually installed
ynh_package_is_installed "$pkgname"
}
# Remove package(s)
#
# usage: ynh_package_remove name [name [...]]
# | arg: name - the package name to remove
ynh_package_remove() {
ynh_apt remove $@
}
# Remove package(s) and their uneeded dependencies
#
# usage: ynh_package_autoremove name [name [...]]
# | arg: name - the package name to remove
ynh_package_autoremove() {
ynh_apt autoremove $@
}