epm-status 7.15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#!/bin/sh
#
# Copyright (C) 2023  Etersoft
# Copyright (C) 2023  Vitaly Lipatov <lav@etersoft.ru>
#
# 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/>.
#

load_helper epm-query

22 23 24

__convert_pkgallowscripts_to_regexp()
{
25 26
    local tmpalf
    tmpalf="$(mktemp)" || fatal
27 28 29 30 31 32 33 34 35 36 37 38 39 40
    # copied from eget's filter_glob
    # check man glob
    # remove commentы and translate glob to regexp
    grep -v "^[[:space:]]*#" "$1" | grep -v "^[[:space:]]*$" | sed -e "s|\*|.*|g" -e "s|?|.|g" -e "s|^|^|" -e "s|$|\$|" >$tmpalf
    echo "$tmpalf"
}

__epm_package_name_ok_scripts()
{
    local name="$1"
    local alf="$CONFIGDIR/pkgallowscripts.list"
    [ -s "$alf" ] || return 1
    [ -n "$name" ] || return 1
    local tmpalf=$(__convert_pkgallowscripts_to_regexp "$alf")
41
    remove_on_exit $tmpalf
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    echo "$name" | grep -q -f $tmpalf
    local res=$?
    rm $tmpalf
    return $res
}

__epm_package_ok_scripts()
{
    local pkg="$1"
    local name
    # TODO: improve epm print name and use it here
    name="$(epm print field Name for "$pkg" 2>/dev/null)"
    [ -n "$name" ] || return 1
    __epm_package_name_ok_scripts "$name"
}

__epm_vendor_ok_scripts()
{
    local vendor="$1"
    local alf="$CONFIGDIR/vendorallowscripts.list"
    [ -s "$alf" ] || return 1
    [ -n "$vendor" ] || return 1
    local tmpalf=$(__convert_pkgallowscripts_to_regexp "$alf")
65
    remove_on_exit $tmpalf
66 67 68 69 70 71 72
    echo "$vendor" | grep -q -f $tmpalf
    local res=$?
    rm $tmpalf
    return $res
}


73 74 75 76 77 78 79 80 81 82 83
epm_status_installable()
{
    local pkg="$1"
    #LANG=C epm policy "$pkg" | grep Candidate >/dev/null 2>/dev/null
    if [ -n "$verbose" ] ; then
        docmd epm install --simulate "$pkg"
    else
        epm install --simulate "$pkg" 2>/dev/null >/dev/null
    fi
}

84
# allowed to use scripts 
85 86 87 88
epm_status_certified()
{
    local pkg="$1"
    load_helper epm-install
89 90 91 92 93 94
    __epm_package_ok_scripts "$pkg" && return

    local vendor
    vendor="$(epm print field Vendor for "$pkg" 2>/dev/null)"
    [ -n "$vendor" ] || return
    __epm_vendor_ok_scripts "$vendor" && return
95 96
}

97

98 99 100 101 102 103 104
# check if the package is really package (check accessibility)
epm_status_validate()
{
    local pkg="$1"
    local rpmversion="$(epm print field Version for "$pkg" 2>/dev/null)"
    [ -n "$rpmversion" ]
}
105 106 107 108 109 110 111 112

epm_status_original()
{
    local pkg="$1"

    #is_installed $pkg || fatal "FIXME: implemented for installed packages as for now"

    case $DISTRNAME in
113
        ALTLinux)
114
            epm_status_validate $pkg || return 1
115
            epm_status_repacked $pkg && return 1
116 117 118 119 120 121

            # not for all packages
            #[ "$(epm print field Vendor for package $pkg)" = "ALT Linux Team" ] || return

            local distribution
            distribution="$(epm print field Distribution for "$pkg" 2>/dev/null )"
122
            echo "$distribution" | grep -q "^ALT" || return 1
123

124 125 126 127
            # mc in Sisyphus has not a signature
            #local sig
            #sig="$(epm print field sigpgp for "$pkg" 2>/dev/null )"
            #[ "$sig" = "(none)" ] && return 1
128 129 130

            # FIXME: how to check if the package is from ALT repo (verified)?
            local release="$(epm print release from package "$pkg" 2>/dev/null )"
131
            echo "$release" | grep -q "^alt" || return 1
132
            return 0
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
            ;;
        *)
            fatal "Unsupported $DISTRNAME"
            ;;
    esac
    return 1
}

epm_status_repacked()
{
    local pkg="$1"

    #is_installed $pkg || fatal "FIXME: implemented for installed packages as for now"

    case $BASEDISTRNAME in
        alt)
149
            epm_status_validate $pkg || return
150 151 152 153 154
            local packager="$(epm print field Packager for "$1" 2>/dev/null)"
            [ "$packager" = "EPM <support@etersoft.ru>" ] && return 0
            [ "$packager" = "EPM <support@eepm.ru>" ] && return 0
            ;;
        *)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
155
            fatal "Unsupported $BASEDISTRNAME"
156 157 158 159 160 161
            ;;
    esac
    return 1
}


162
epm_status_thirdparty()
163 164 165 166 167 168 169 170 171 172 173
{
    local pkg="$1"

    #is_installed $pkg || fatal "FIXME: implemented for installed packages as for now"

    case $BASEDISTRNAME in
        alt)
            ## FIXME: some repo packages have wrong Packager
            #local packager="$(epm print field Packager for "$1" 2>/dev/null)"
            #echo "$packager" && grep -q "altlinux" && return 0
            #echo "$packager" && grep -q "basealt" && return 0
174
            epm_status_validate $pkg || return 1
175 176 177

            local distribution
            distribution="$(epm print field Distribution for "$pkg" 2>/dev/null )"
178 179 180
            echo "$distribution" | grep -q "^ALT" && return 1
            echo "$distribution" | grep -q "^EEPM" && return 1
            return 0
181 182
            ;;
        *)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
183
            fatal "Unsupported $BASEDISTRNAME"
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
            ;;
    esac
    return 1
}


epm_status_help()
{
    cat <<EOF

epm status - check status of the package and return result via exit code
Usage: epm status [options] <package>

Options:
  --installed           check if <package> is installed
199
  --installable         check if <package> can be installed from the repo
200
  --original            check if <package> is from distro repo
201
  --certified           check if <package> is certified that it can be installed without repacking
202
  --thirdparty          check if <package> from a third-party source (didn't packed for this distro)
203
  --repacked            check if <package> was repacked with epm repack
204
  --validate            check if <package> is accessible (we can get a fields from it)
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233

EOF
}

epm_status()
{
    local option="$1"

    if [ -z "$1" ] ; then
        epm_status_help >&2
        exit 1
    fi

    shift

    # TODO: allow both option
    case "$option" in
        -h|--help)
            epm_status_help
            return
            ;;
        --installed)
            is_installed "$@"
            return
            ;;
        --original)
            epm_status_original "$@"
            return
            ;;
234
        --certified|--allowed-scripts)
235 236 237
            epm_status_certified "$@"
            return
            ;;
238 239
         --third-party|--thirdparty|--thirdpart)
            epm_status_thirdparty "$@"
240 241 242 243 244 245
            return
            ;;
        --repacked)
            epm_status_repacked "$@"
            return
            ;;
246 247 248 249
        --validate)
            epm_status_validate "$@"
            return
            ;;
250 251 252 253
        --installable)
            epm_status_installable "$@"
            return
            ;;
254
        -*)
255 256
            fatal "Unknown option $option, use epm status --help to get info"
            ;;
257 258 259
        *)
            fatal "No option before $option, use epm status --help to get info"
            ;;
260 261 262 263 264
    esac

    epm_status_help >&2
    fatal "Run with appropriate option"
}