epm-query 5.6 KB
Newer Older
Danil Mikhailov's avatar
Danil Mikhailov committed
1 2
#!/bin/sh
#
3 4
# Copyright (C) 2012, 2013, 2015  Etersoft
# Copyright (C) 2012, 2013, 2015  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
load_helper epm-packages
21
load_helper epm-print
22

Vitaly Lipatov's avatar
Vitaly Lipatov committed
23

24 25 26 27 28 29 30 31 32 33
_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)
34
_shortquery_via_packages_list()
35 36
{
	local res=0
37
	local grepexp
38 39
	local firstpkg=$1
	shift
40

41 42
	grepexp=$(_get_grep_exp $firstpkg)

43
	# Note: we use short=1 here due grep by ^name$
44
	# separate first line for print out command
45
	short=1 pkg_filenames=$firstpkg epm_packages | grep -- "$grepexp" || res=1
46

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

53 54 55
	return $res
}

56 57 58 59 60 61 62 63 64 65 66 67 68
# Note: modified from _shortquery_via_packages_list
# TODO: non optimal double epm packages call
_query_via_packages_list()
{
	local res=0
	local grepexp
	local firstpkg=$1
	shift

	grepexp=$(_get_grep_exp $firstpkg)

	# Note: we use short=1 here due grep by ^name$
	# separate first line for print out command
Vitaly Lipatov's avatar
Vitaly Lipatov committed
69
	short=1 pkg_filenames=$firstpkg epm_packages | grep -q -- "$grepexp" && quiet=1 pkg_filenames=$firstpkg epm_packages || res=1
70 71 72 73

	local pkg
	for pkg in "$@" ; do
		grepexp=$(_get_grep_exp $pkg)
Vitaly Lipatov's avatar
Vitaly Lipatov committed
74
		short=1 pkg_filenames=$pkg epm_packages 2>/dev/null | grep -q -- "$grepexp" && quiet=1 pkg_filenames=$pkg epm_packages || res=1
75 76 77 78 79
	done

	return $res
}

Vitaly Lipatov's avatar
Vitaly Lipatov committed
80
# internal use only, for installed package
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
__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
118
		quiet=1 pkg=$(__epm_query_shortname $i) || continue # drop not installed packages
119 120 121 122 123 124 125
		# 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
}

126 127 128 129
__epm_query_file()
{
	local CMD

130
	[ -z "$*" ] && return
131 132

	case $PMTYPE in
133
		*-rpm)
134
			CMD="rpm -qp"
135
			[ -n "$short" ] && CMD="rpm -qp --queryformat %{name}\n"
136
			;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
137
		*-dpkg)
138 139
			CMD="dpkg-deb --show --showformat=\${Package}-\${Version}\n"
			[ -n "$short" ] && CMD="dpkg-query --show --showformat=\${Package}\n"
140 141 142 143 144 145
			;;
		*)
			fatal "Do not know command for query file package"
			;;
	esac

146
	docmd $CMD $@
147 148 149 150 151 152
}

__epm_query_name()
{
	local CMD

153
	[ -z "$*" ] && return
154 155

	case $PMTYPE in
156
		*-rpm)
157 158
			CMD="rpm -q"
			;;
Vitaly Lipatov's avatar
Vitaly Lipatov committed
159
		*-dpkg)
160 161
			#docmd dpkg -l $@ | grep "^ii"
			CMD="dpkg-query -W --showformat=\${Package}-\${Version}\n"
162 163
			;;
		npackd)
164 165 166 167 168
			docmd "npackdcl path --package=$@"
			return
			;;
		conary)
			CMD="conary query"
169
			;;
170
		homebrew)
171 172 173
			warning "fix query"
			return 1
			;;
174 175 176 177
		# TODO: need to print name if exists
		#pkgng)
		#	CMD="pkg info -e"
		#	;;
178
		# Note: slackpkg info pkgname
179
		*)
180
			# default slow workaround
181
			_query_via_packages_list $@
182 183 184 185
			return
			;;
	esac

186
	docmd $CMD $@
187 188
}

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
__epm_query_shortname()
{
	local CMD

	[ -z "$*" ] && return

	case $PMTYPE in
		*-rpm)
			CMD="rpm -q --queryformat %{name}\n"
			;;
		*-dpkg)
			CMD="dpkg-query -W --showformat=\${Package}\n"
			;;
		npackd)
			docmd "npackdcl path --package=$@"
			return
			;;
		conary)
			CMD="conary query"
			;;
		homebrew)
			warning "fix query"
			return 1
			;;
		# TODO: need to print name if exists
		#pkgng)
		#	CMD="pkg info -e"
		#	;;
		# Note: slackpkg info pkgname
		*)
			# default slow workaround
			_shortquery_via_packages_list $@
			return
			;;
	esac

	docmd $CMD $@
}

228 229 230
# check if pkg is installed
is_installed()
{
231
	short=1 pkg_filenames="$@" pkg_names="$@" epm_query >/dev/null 2>/dev/null
232 233
	# broken way to recursive call here (overhead!)
	#epm installed $@ >/dev/null 2>/dev/null
234
}
235

236 237 238 239 240
# fill pkg_installed and pkg_noninstalled
separate_installed()
{
	pkg_installed=
	pkg_noninstalled=
Vitaly Lipatov's avatar
Vitaly Lipatov committed
241
	for i in $* ; do
242 243 244 245
		is_installed $i && pkg_installed="$pkg_installed $i" || pkg_noninstalled="$pkg_noninstalled $i"
	done
}

Danil Mikhailov's avatar
Danil Mikhailov committed
246 247
epm_query()
{
Vitaly Lipatov's avatar
Vitaly Lipatov committed
248
	[ -n "$pkg_filenames" ] || fatal "Query: missing package(s) name"
249 250

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

252 253 254 255 256
	if [ -n "$short" ] ; then
		__epm_query_shortname $(print_name $pkg_names) || return
	else
		__epm_query_name $(print_name $pkg_names) || return
	fi
Danil Mikhailov's avatar
Danil Mikhailov committed
257
}