epm-query 4.32 KB
Newer Older
Danil Mikhailov's avatar
Danil Mikhailov committed
1 2
#!/bin/sh
#
3 4
# Copyright (C) 2012, 2013  Etersoft
# Copyright (C) 2012, 2013  Vitaly Lipatov <lav@etersoft.ru>
Danil Mikhailov's avatar
Danil Mikhailov committed
5
#
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 24 25 26 27 28 29 30 31 32
_get_grep_exp()
{
	local def="^$1$"
	[ "$PMTYPE" != "emerge" ] && echo "$def" && return
	# Gentoo hack: support for short package form
	echo "$1" | grep -q "/" && echo "$def" && return
	echo "/$1$"
}

# TODO: combine with -qa (the difference only in return status now)
33 34 35
_query_via_packages_list()
{
	local res=0
36
	local grepexp
37 38
	local firstpkg=$1
	shift
39

40 41
	grepexp=$(_get_grep_exp $firstpkg)

42
	# separate first line for print out command
43
	short=1 pkg_filenames=$firstpkg epm_packages | grep -- "$grepexp" || res=1
44

45
	for pkg in "$@" ; do
46 47
		grepexp=$(_get_grep_exp $pkg)
		short=1 pkg_filenames=$pkg epm_packages 2>/dev/null | grep -- "$grepexp" || res=1
48
	done
49

50 51 52
	return $res
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
53
# internal use only, for installed package
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
__epm_get_hilevel_nameform()
{
	[ -n "$*" ] || return

	case $PMTYPE in
		apt-rpm)
			# use # as delimeter for apt
			local pkg
			pkg=$(rpm -q --queryformat "%{NAME}#%{SERIAL}:%{VERSION}-%{RELEASE}\n" $1)
			echo $pkg | grep -q "(none)" && pkg=$(rpm -q --queryformat "%{NAME}#%{VERSION}-%{RELEASE}\n" $1)
			# HACK: can use only for multiple install packages like kernel
			echo $pkg | grep -q kernel || return 1
			echo $pkg
			return
			;;
		yum-rpm)
			# just use strict version with Epoch and Serial
			local pkg
			pkg=$(rpm -q --queryformat "%{EPOCH}:%{NAME}%{VERSION}-%{RELEASE}.${ARCH}\n" $1)
			echo $pkg | grep -q "(none)" && pkg=$(rpm -q --queryformat "%{NAME}-%{VERSION}-%{RELEASE}.${ARCH}\n" $1)
			echo $pkg
			return
			;;
		*)
			return 1
			;;
	esac
}

# for local installed packages only
# used from epm remove
__epm_get_hilevel_name()
{
	local i
	for i in $@ ; do
		local pkg
		# get short form in pkg
91
		quiet=1 short=1 pkg=$(__epm_query_name $i) || continue # drop not installed packages
92 93 94 95 96 97 98
		# if already short form, skipped
		[ "$pkg" = "$i" ] && echo "$i" && continue
		# try get long form or use short form
		__epm_get_hilevel_nameform $i || echo $pkg
	done
}

99 100 101 102
__epm_query_file()
{
	local CMD

103
	[ -z "$*" ] && return
104 105

	case $PMTYPE in
106
		*-rpm)
107
			CMD="rpm -qp"
108
			[ -n "$short" ] && CMD="rpm -qp --queryformat %{name}\n"
109
			;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
110
		*-dpkg)
111 112
			CMD="dpkg-deb --show --showformat=\${Package}-\${Version}\n"
			[ -n "$short" ] && CMD="dpkg-query --show --showformat=\${Package}\n"
113 114 115 116 117 118
			;;
		*)
			fatal "Do not know command for query file package"
			;;
	esac

119
	docmd $CMD $@
120 121 122 123 124 125
}

__epm_query_name()
{
	local CMD

126
	[ -z "$*" ] && return
127 128

	case $PMTYPE in
129
		*-rpm)
130
			CMD="rpm -q"
131
			[ -n "$short" ] && CMD="rpm -q --queryformat %{name}\n"
132
			;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
133
		*-dpkg)
134 135 136
			#docmd dpkg -l $@ | grep "^ii"
			CMD="dpkg-query -W --showformat=\${Package}-\${Version}\n"
			[ -n "$short" ] && CMD="dpkg-query -W --showformat=\${Package}\n"
137 138
			;;
		npackd)
139 140 141 142 143
			docmd "npackdcl path --package=$@"
			return
			;;
		conary)
			CMD="conary query"
144
			;;
145
		homebrew)
146 147 148
			warning "fix query"
			return 1
			;;
149 150 151 152
		# TODO: need to print name if exists
		#pkgng)
		#	CMD="pkg info -e"
		#	;;
153
		# Note: slackpkg info pkgname
154
		*)
155
			# default slow workaround
156
			_query_via_packages_list $@
157 158 159 160
			return
			;;
	esac

161
	docmd $CMD $@
162 163
}

164 165 166
# check if pkg is installed
is_installed()
{
167 168 169
	pkg_filenames="$@" pkg_names="$@" epm_query >/dev/null 2>/dev/null
	# broken way to recursive call here (overhead!)
	#epm installed $@ >/dev/null 2>/dev/null
170
}
171

172 173 174 175 176
# fill pkg_installed and pkg_noninstalled
separate_installed()
{
	pkg_installed=
	pkg_noninstalled=
Vitaly Lipatov's avatar
Vitaly Lipatov committed
177
	for i in $* ; do
178 179 180 181
		is_installed $i && pkg_installed="$pkg_installed $i" || pkg_noninstalled="$pkg_noninstalled $i"
	done
}

Danil Mikhailov's avatar
Danil Mikhailov committed
182 183
epm_query()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
184
	[ -n "$pkg_filenames" ] || fatal "Query: missing package(s) name"
185 186

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

188
	__epm_query_name $pkg_names $pkg_dirs || return
Danil Mikhailov's avatar
Danil Mikhailov committed
189
}