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

23 24
PAOURL="https://packages.altlinux.org"

25 26
paoapi()
{
27 28 29 30
    # http://petstore.swagger.io/?url=http://packages.altlinux.org/api/docs
    assure_exists curl || return 1
    showcmd curl "$PAOURL/api/$1"
    a='' curl -s --header "Accept: application/json" "$PAOURL/api/$1"
31 32 33 34 35
}

# TODO: use /home/lav/Projects/git/JSON.sh
get_pao_var()
{
36 37 38 39
    local FIELD="$1"
    #grep '"$FIELD"' | sed -e 's|.*"$FIELD":"||g' | sed -e 's|".*||g'
    $SHAREDIR/tools_json -b | grep -E "\[.*\"$FIELD\"\]" | sed -e 's|.*[[:space:]]"\(.*\)"|\1|g'
    return 0
40 41 42
}


43 44
run_command_if_exists()
{
45 46 47 48 49 50 51
    local CMD="$1"
    shift
    if is_command "$CMD" ; then
        docmd $CMD "$@"
        return 0
    fi
    return 1
52 53
}

54
# TODO: use something like xdg-browser
55 56
open_browser()
{
57 58 59 60
    local i
    for i in xdg-open firefox chromium links ; do
        run_command_if_exists $i "$@" && return
    done
61 62
}

63 64
__query_package_hl_url()
{
65
    case $DISTRNAME in
66
        ALTLinux)
67 68 69 70
            paoapi srpms/$1 | get_pao_var url
            ;;
    esac
    return 1
71 72
}

73 74
query_package_url()
{
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    local URL

    case $PMTYPE in
        *-rpm)
            # TODO: for binary packages?
            query_package_field URL "$1" || __query_package_hl_url "$1"
            #LANG=C epm info "$1"
            return
            ;;
        homebrew)
            docmd brew "$1" | grep "^From: " | sed -e "s|^From: ||"
            return
            ;;
    esac
    fatal "rpm based distro supported only. TODO: Realize via web service?"
90 91 92 93
}

get_locale()
{
94 95 96 97
    local loc
    loc=$(a='' natspec --locale 2>/dev/null)
    [ -n "$loc" ] || loc=$LANG
    echo $loc
98 99 100 101
}

get_pao_url()
{
102 103 104 105 106 107 108 109 110 111
    local loc
    loc=$(get_locale | cut -c1-2)
    case $loc in
        en|ru|uk|br)
            loc=$loc
            ;;
        *)
            loc=en
    esac
    echo "$PAOURL/$loc/Sisyphus/srpms"
112 113 114 115
}

query_altlinux_url()
{
116 117 118 119 120 121 122 123 124 125
    local URL
    case $PMTYPE in
        *-rpm)
            local srpm=$(print_srcname "$1")
            [ -n "$srpm" ] || fatal "Can't get source name for $1"
            echo "$(get_pao_url)/$srpm"
            return
            ;;
    esac
    fatal "rpm based distro supported only. TODO: Realize via web service?"
126 127 128 129 130
}

epm_site()
{

Vitaly Lipatov's avatar
Vitaly Lipatov committed
131
[ -n "$pkg_filenames" ] || fatal "Info: package name is missed"
132

133
local PAO=""
134
for f in $pkg_names $pkg_files ; do
135 136 137 138 139 140 141 142
    [ "$f" = "-p" ] && PAO="$f" && continue
    if [ -n "$PAO" ] ; then
        pkg_url=$(query_altlinux_url $f)
    else
        pkg_url=$(query_package_url $f)
    fi
    [ -n "$pkg_url" ] && open_browser "$pkg_url" && continue
    warning "Can't get URL for $f package"
143 144 145
done

#for f in $pkg_names ; do
146
#    LANG=C epm info $f
147 148 149 150 151
#done

# TODO: -p for p.a.o (see rpmurl)

}