fix pfile
[distro-functions] / src / package-manager-abstractions
1 #!/bin/bash
2 # Copyright (C) 2014 Ian Kelling
3
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 # basic yum/apt package manager abstraction, plus a few minor conveniences
17 if command -v yum &> /dev/null; then
18 # package manager
19 p() {
20 local s; [[ $EUID != 0 ]] && s=sudo
21 $s yum "$@"
22 }
23 # package install
24 pi() {
25 local s; [[ $EUID != 0 ]] && s=sudo
26 $s yum -y install "$@"
27 }
28 # package find
29 pf() {
30 local s; [[ $EUID != 0 ]] && s=sudo
31 $s yum search "$@"
32 }
33 # package remove/uninstall
34 pu() {
35 local s; [[ $EUID != 0 ]] && s=sudo
36 $s yum autoremove "$@"
37 }
38 pup() { # upgrade
39 local s; [[ $EUID != 0 ]] && s=sudo
40 $s yum -y distro-sync full "$@"
41 }
42 # package list info
43 pl() {
44 yum info "$@"
45 }
46 pfile() {
47 yum whatprovides \*/$1
48 }
49
50 elif command -v apt-get &>/dev/null; then
51 plock-wait() {
52 local i
53 i=0
54 while fuser /var/lib/dpkg/lock &>/dev/null; do
55 sleep 1
56 i=$(( i+1 ))
57 if (( i > 300 )); then
58 echo "error: timed out waiting for /var/lib/dpkg/lock" >&2
59 return 1
60 fi
61 done
62 }
63 pcheck() {
64 for arg; do
65 if [[ $1 == -* ]]; then
66 shift
67 else
68 break
69 fi
70 done
71 if dpkg -s -- "$@" |& grep -Fx "Status: install ok installed" &>/dev/null; then
72 return 1
73 fi
74 return 0
75 }
76 pp() { # package policy
77 apt-cache policy $@
78 }
79 p() {
80 local s; [[ $EUID != 0 ]] && s=sudo
81 case $1 in
82 install)
83 $s apt-get --purge --auto-remove "$@"
84 ;;
85 *)
86 $s apt-get "$@"
87 ;;
88 esac
89 }
90 pupdate() {
91 local now t s f cachetime limittime; [[ $EUID != 0 ]] && s=sudo
92 # update package list if its more than an 2 hours old
93 f=/var/cache/apt/pkgcache.bin
94 if [[ -r $f ]]; then
95 cachetime=$(stat -c %Y $f )
96 else
97 cachetime=0
98 fi
99 now=$(date +%s)
100 limittime=$(( now - 60*60*2 ))
101 for f in /etc/apt/sources.list /etc/apt/sources.list.d/*.list; do
102 if [[ -r $f ]]; then
103 t=$(stat -c %Y $f )
104 if (( t > limittime )); then
105 limittime=$t
106 fi
107 fi
108 done
109 if (( cachtime > limittime )); then
110 $s apt-get update
111 fi
112 }
113 pi() {
114 pcheck "$@" || return 0
115 pupdate
116 if [[ $- != *i* ]]; then
117 echo pi "$*"
118 fi
119 if [[ $EUID == 0 ]]; then
120 DEBIAN_FRONTEND=noninteractive apt-get -y install --purge --auto-remove "$@"
121 else
122 sudo DEBIAN_FRONTEND=noninteractive apt-get -y install --purge --auto-remove "$@"
123 fi
124
125 }
126
127 pi-nostart() {
128 local ret=
129 pcheck "$@" || return 0
130 plock-wait
131 pupdate
132 local f=/usr/sbin/policy-rc.d
133 if [[ $- != *i* ]]; then
134 echo pi-nostart "$@"
135 fi
136 if [[ $EUID == 0 ]]; then
137 dd of=$f 2>/dev/null <<EOF
138 #!/bin/sh
139 exit 101
140 EOF
141 chmod +x $f
142 DEBIAN_FRONTEND=noninteractive apt-get -y install --purge --auto-remove "$@" || ret=$?
143 rm $f
144 else
145 sudo dd of=$f 2>/dev/null <<EOF
146 #!/bin/sh
147 exit 101
148 EOF
149 sudo chmod +x $f
150 sudo DEBIAN_FRONTEND=noninteractive apt-get -y install --purge --auto-remove "$@" || ret=$?
151 sudo rm $f
152 fi
153 return $ret
154 }
155 pf() {
156 # package name and descriptions
157 apt-cache search "$@"
158 }
159 pff() {
160 local s; [[ $EUID != 0 ]] && s=sudo
161 # nice aptitude search from emacs shell. package description width as
162 # wide as the screen, and package name field small aptitude
163 # manual can't figure out how wide emacs terminal is, of course
164 # it doesn't consult the $COLUMNS variable... and in a normal
165 # terminal, it makes the package name field ridiculously big
166 # also, remove that useless dash before the description
167 aptitude -F "%c%a%M %p %$((COLUMNS - 30))d" -w $COLUMNS search "$@"
168 }
169 pu() {
170 local s; [[ $EUID != 0 ]] && s=sudo
171 local needed=false
172 for arg; do
173 if dpkg -s -- "$arg" &>/dev/null; then
174 needed=true
175 break
176 fi
177 done
178 $needed || return 0
179 plock-wait
180 $s apt-get -y remove --purge --auto-remove "$@"
181 # seems slightly redundant, but it removes more stuff sometimes.
182 $s apt-get -y autoremove
183 }
184 pup() { # upgrade
185 plock-wait
186 pupdate
187 local s; [[ $EUID != 0 ]] && s=sudo
188 $s apt-get -y dist-upgrade --purge --auto-remove "$@"
189 $s apt-get -y autoremove
190 if [[ -e /usr/sbin/checkrestart ]]; then
191 $s /usr/sbin/checkrestart -p
192 fi
193 }
194 # package info
195 pl() {
196 if type -p aptitude &>/dev/null; then
197 aptitude show "$@"
198 else
199 apt-cache show "$@"
200 fi
201 }
202 pfile() {
203 # -a = search all repos
204 local -a arg
205 if [[ $1 != -a ]]; then
206 arg=(--filter-origins "$(positive-origins)")
207 fi
208 local file=$1
209 # ucfq can tell us about config files which are not tracked
210 # with apt-file. but, for at least a few files I tested
211 # which are tracked with apt-file, ucfq doesn't show their
212 # package name. So, commenting this, waiting to find
213 # a config file only tracked by ucfq to see if it gives the
214 # package name and if I can identify this kind of file.
215 # if [[ $file == /* ]] && ! ucfq -w $file | grep ::: &>/dev/null; then
216 # ucfq $file
217
218 if [[ $file == /* ]]; then
219 dpkg -S "$file"
220 elif [[ $file == */* ]]; then
221 apt-file "${arg[@]}" find -x "$file"\$
222 else
223 apt-file "${arg[@]}" find -x /"$file"\$
224 update-alternatives --list "$file" 2>/dev/null
225 fi
226 }
227 pkgfiles() {
228 if dpkg -s "$1" &>/dev/null; then
229 dpkg-query -L $1
230 else
231 apt-file -x list "^$1$"
232 fi
233 }
234
235 elif command -v pacman &>/dev/null; then
236 p() {
237 pacaur "$@"
238 }
239 pi() {
240 pacaur -S --noconfirm --needed --noedit "$@"
241 }
242 pf() {
243 pacaur -Ss "$@"
244 }
245 pu() {
246 pacaur -Rs --noconfirm "$@"
247 if p=$(pacaur -Qdtq); then
248 pacaur -Rs $p
249 fi
250 }
251 aurex() {
252 p="$1"
253 aur='https://aur.archlinux.org'
254 curl -s $aur/$(curl -s "$aur/rpc.php?type=info&arg=$p" \
255 | jq -r .results.URLPath) | tar xz
256 cd "$p"
257
258 }
259 pmirror() {
260 local s; [[ $EUID != 0 ]] && s=sudo
261 local x=$(mktemp)
262 curl -s "https://www.archlinux.org/mirrorlist/\
263 ?country=US&protocol=https&ip_version=4&ip_version=6&use_mirror_status=on" \
264 | sed -r 's/^[ #]*(Server *=)/\1/' > $x
265 if (( $(stat -c %s $x ) > 10 )); then
266 $s cp $x /etc/pacman.d/mirrorlist
267 rm $x
268 fi
269 }
270 pup() { # upgrade
271 local s; [[ $EUID != 0 ]] && s=sudo
272 # file_time + 24 hours > current_time
273 if ! (( $(stat -c%Y /etc/pacman.d/mirrorlist) + 60*60*24 > $(date +%s) ))
274 then
275 pmirror
276 fi
277 pacaur -Syu --noconfirm "$@"
278 }
279 # package info
280 pl() {
281 pacaur -Si "$@"
282 }
283 pfile() {
284 pkgfile "$1"
285 }
286 pkgfiles() {
287 if pacaur -Qs "^$1$" &>/dev/null; then
288 pacman -Ql $1
289 else
290 pkgfile -l $1
291 fi
292 }
293 fi