epm-assure 3.55 KB
Newer Older
1
#!/bin/sh
2
#
3 4
# Copyright (C) 2013-2016  Etersoft
# Copyright (C) 2013-2016  Vitaly Lipatov <lav@etersoft.ru>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

__check_command_in_path()
{
22 23
    # with hack for sudo case
    ( PATH=$PATH:/sbin:/usr/sbin print_command_path "$1" )
24 25
}

26
# returns TRUE if package version is omitted, or package is not installed, or version is too old
27 28 29 30 31
__epm_need_update()
{
    local PACKAGE="$1"
    local PACKAGEVERSION="$2"

32
    [ -n "$PACKAGEVERSION" ] || return 0
33 34 35 36 37 38 39

    load_helper epm-query
    is_installed "$PACKAGE" || return 0

    load_helper epm-print
    # epm print version for package N
    local INSTALLEDVERSION=$(query_package_field "version" "$PACKAGE")
40 41
    # if needed >= installed, return 0
    [ "$(compare_version "$PACKAGEVERSION" "$INSTALLEDVERSION")" -gt 0 ] && return 0
42 43 44 45

    return 1
}

46
__epm_assure_checking()
47
{
48 49 50
    local CMD="$1"
    local PACKAGE="$2"
    local PACKAGEVERSION="$3"
51 52

    [ -n "$PACKAGEVERSION" ] && return 1
53

54
    if is_dirpath "$CMD" ; then
Vitaly Lipatov's avatar
Vitaly Lipatov committed
55
        # TODO: check for /usr/bin, /bin, /usr/sbin, /sbin
56
        if [ -e "$CMD" ] ; then
57
            if [ -n "$verbose" ] ; then
58
                info 'File or directory $CMD is already exists.'
59
                epm qf "$CMD" >&2
60
            fi
61
            return 0
62 63
        fi

Vitaly Lipatov's avatar
Vitaly Lipatov committed
64
        [ -n "$PACKAGE" ] || fatal "You need run with package name param when use with absolute path to non executable file"
65
        return 1
66 67
    fi

68
    if __check_command_in_path "$CMD" >/dev/null ; then
69 70
        if [ -n "$verbose" ] ; then
            local compath="$(__check_command_in_path "$1")"
71
            info 'Command $CMD is exists: $compath'
72
            epm qf "$compath" >&2
73
        fi
74
        return 0
75 76
    fi

77 78 79 80
    # at least check if the package is installed
    load_helper epm-query
    is_installed "$PACKAGE" && return 0

81 82 83 84 85 86 87 88 89
    return 1
}

# Do fast checking for command and install package if the command does not exist

# $1 - command name
# $2 - [package name]
# $3 - [needed package version]

90
epm_assure()
91 92 93 94 95 96 97 98
{
    local CMD="$1"
    local PACKAGE="$2"
    local PACKAGEVERSION="$3"
    [ -n "$PACKAGE" ] || PACKAGE="$1"

    __epm_assure_checking $CMD $PACKAGE $PACKAGEVERSION && return 0

99
    info 'Installing appropriate package for $CMD command...'
100 101
    __epm_need_update $PACKAGE $PACKAGEVERSION || return 0

102 103 104
    # can't be used in epm ei case
    #docmd epm --auto install $PACKAGE || return
    load_helper epm-install
105 106
    (repack='' pkg_names="$PACKAGE" pkg_files='' pkg_urls='' epm_install ) || return

107
    # keep auto installed packages
108
    # https://bugzilla.altlinux.org/42240
109 110
    #load_helper epm-mark
    #epm_mark_auto "$PACKAGE"
111

112 113 114
    # no check if we don't need a version
    [ -n "$PACKAGEVERSION" ] || return 0

115
    # check if we couldn't update and still need update
116 117 118 119
    __epm_need_update $PACKAGE $PACKAGEVERSION || return 0

    local textpackage
    [ -n "$PACKAGEVERSION" ] && textpackage=" >= $PACKAGEVERSION"
120
    warning 'Can'\''t assure in $CMD command from $PACKAGE$textpackage package'
121
    return 1
122
}