epm-site 3.03 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 27
paoapi()
{
	# http://petstore.swagger.io/?url=http://packages.altlinux.org/api/docs
28
	assure_exists curl || return 1
29
	showcmd curl "$PAOURL/api/$1"
Vitaly Lipatov's avatar
Vitaly Lipatov committed
30
	a='' curl -s --header "Accept: application/json" "$PAOURL/api/$1"
31 32 33 34 35 36 37
}

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


43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
run_command_if_exists()
{
	local CMD="$1"
	shift
	if which "$CMD" 2>/dev/null >/dev/null ; then
		docmd "$CMD" "$@"
		return 0
	fi
	return 1
}

open_browser()
{
	local i
	for i in xdg-open firefox chromium links ; do
		run_command_if_exists $i "$@" && return
	done
}

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

72 73 74
query_package_url()
{
	local URL
75

76 77
	case $PMTYPE in
		*-rpm)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
78
			# TODO: for binary packages?
79
			query_package_field URL "$1" || __query_package_hl_url "$1"
80 81 82
			#LANG=C epm info "$1"
			return
			;;
83 84 85 86
		homebrew)
			docmd brew "$1" | grep "^From: " | sed -e "s|^From: ||"
			return
			;;
87 88 89 90 91 92 93
	esac
	fatal "rpm based distro supported only. TODO: Realize via web service?"
}

get_locale()
{
	local loc
Vitaly Lipatov's avatar
Vitaly Lipatov committed
94
	loc=$(a='' natspec --locale 2>/dev/null)
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
	[ -n "$loc" ] || loc=$LANG
	echo $loc
}

get_pao_url()
{
	local loc
	loc=$(get_locale | cut -c1-2)
	case $loc in
		en|ru|uk|br)
			loc=$loc
			;;
		*)
			loc=en
	esac
110
	echo "$PAOURL/$loc/Sisyphus/srpms"
111 112 113 114 115 116 117
}

query_altlinux_url()
{
	local URL
	case $PMTYPE in
		*-rpm)
118
			local srpm=$(print_srcname "$1")
119 120 121 122 123 124 125 126 127 128 129
			[ -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?"
}

epm_site()
{

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

132
local PAO=""
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
for f in $pkg_names $pkg_files ; do
	[ "$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"
done

#for f in $pkg_names ; do
#	LANG=C epm info $f
#done

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

}