epm-query 2.4 KB
Newer Older
Danil Mikhailov's avatar
Danil Mikhailov committed
1 2 3 4 5
#!/bin/sh
#
# Copyright (C) 2012  Etersoft
# Copyright (C) 2012  Vitaly Lipatov <lav@etersoft.ru>
#
6 7 8
# 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
Danil Mikhailov's avatar
Danil Mikhailov committed
9 10 11 12 13
# (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
14
# GNU Affero General Public License for more details.
Danil Mikhailov's avatar
Danil Mikhailov committed
15
#
16 17
# 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/>.
Danil Mikhailov's avatar
Danil Mikhailov committed
18 19
#

20 21
load_helper epm-packages

Vitaly Lipatov's avatar
Vitaly Lipatov committed
22 23
# TODO: combine with -qa (the difference only in return status now)

24 25 26 27 28
_query_via_packages_list()
{
	local res=0
	local firstpkg=$1
	shift
29 30
	# separate first line for print out command
	short=1 pkg_filenames=$firstpkg epm_packages | grep -- "^$firstpkg$" || res=1
31
	for pkg in "$@" ; do
32
		short=1 pkg_filenames=$pkg epm_packages 2>/dev/null | grep -- "^$pkg$" || res=1
33 34 35 36
	done
	return $res
}

37 38 39 40
__epm_query_file()
{
	local CMD

41
	[ -z "$*" ] && return
42 43

	case $PMTYPE in
44
		*-rpm)
45 46 47 48 49 50 51 52 53 54 55 56 57 58
			CMD="rpm -qp"
			;;
		apt-dpkg)
			CMD="dpkg-deb --show"
			# TODO: make rpm-like output
			#showcmd dpkg -l $pkg_filenames
			#dpkg -l $pkg_filenames | grep "^ii"
			#return
			;;
		*)
			fatal "Do not know command for query file package"
			;;
	esac

59
	docmd $CMD $@
60 61 62 63 64 65
}

__epm_query_name()
{
	local CMD

66
	[ -z "$*" ] && return
67 68

	case $PMTYPE in
69
		*-rpm)
70 71 72 73 74 75 76 77 78 79
			CMD="rpm -q"
			;;
		apt-dpkg)
			CMD="dpkg -l"
			# TODO: make rpm-like output
			#showcmd dpkg -l $pkg_filenames
			#dpkg -l $pkg_filenames | grep "^ii"
			#return
			;;
		npackd)
80
			CMD="npackdcl path --package=$@"
81
			;;
82 83 84 85
		brew)
			warning "fix query"
			return 1
			;;
86
		*)
87
			_query_via_packages_list $@
88 89 90 91
			return
			;;
	esac

92
	docmd $CMD $@
93 94
}

95 96 97
# check if pkg is installed
is_installed()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
98 99
	#pkg_filenames="$@" epm_query >/dev/null
	epm installed $@ >/dev/null
100
}
101

102 103 104 105 106
# fill pkg_installed and pkg_noninstalled
separate_installed()
{
	pkg_installed=
	pkg_noninstalled=
Vitaly Lipatov's avatar
Vitaly Lipatov committed
107
	for i in $* ; do
108 109 110 111
		is_installed $i && pkg_installed="$pkg_installed $i" || pkg_noninstalled="$pkg_noninstalled $i"
	done
}

Danil Mikhailov's avatar
Danil Mikhailov committed
112 113
epm_query()
{
114 115 116
	[ -n "$pkg_filenames" ] || fatal "Run query without names"

	__epm_query_file $pkg_files || return
Danil Mikhailov's avatar
Danil Mikhailov committed
117

118
	__epm_query_name $pkg_names || return
Danil Mikhailov's avatar
Danil Mikhailov committed
119
}