checkrestart on system upgrade
[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 pp() { # package policy
52 apt-cache policy $@
53 }
54 p() {
55 local s; [[ $EUID != 0 ]] && s=sudo
56 case $1 in
57 install)
58 $s apt-get --purge --auto-remove "$@"
59 ;;
60 *)
61 $s apt-get "$@"
62 ;;
63 esac
64 }
65 pupdate() {
66 local s f; [[ $EUID != 0 ]] && s=sudo
67 # update package list if its more than an 2 hours old
68 f=/var/cache/apt/pkgcache.bin
69 if [[ ! -r $f ]] \
70 || (( $(( $(date +%s) - $(stat -c %Y $f ) )) > 60*60*2 )); then
71 $s apt-get update
72 fi
73 }
74 pi() {
75 if dpkg -s -- "$@" | grep -Fx "Status: install ok installed" &>/dev/null; then
76 return 0
77 fi
78 while fuser /var/lib/dpkg/lock &>/dev/null; do sleep 1; done
79 pupdate
80 local s; [[ $EUID != 0 ]] && s=sudo
81 $s $PI_PREFIX apt-get -y --allow-downgrades install --purge --auto-remove "$@"
82 }
83
84 pi-nostart() {
85 if dpkg -s -- "$@" &>/dev/null; then
86 return 0
87 fi
88 while fuser /var/lib/dpkg/lock &>/dev/null; do sleep 1; done
89 pupdate
90 local s; [[ $EUID != 0 ]] && s=sudo
91 local f=/usr/sbin/policy-rc.d
92 $s dd of=$f <<EOF
93 #!/bin/sh
94 exit 101
95 EOF
96 $s chmod +x $f
97 $s apt-get -y install --allow-downgrades --purge --auto-remove "$@"
98 $s rm $f
99 }
100 pf() {
101 # package name and descriptions
102 apt-cache search "$@"
103 }
104 pff() {
105 local s; [[ $EUID != 0 ]] && s=sudo
106 # nice aptitude search from emacs shell. package description width as
107 # wide as the screen, and package name field small aptitude
108 # manual can't figure out how wide emacs terminal is, of course
109 # it doesn't consult the $COLUMNS variable... and in a normal
110 # terminal, it makes the package name field ridiculously big
111 # also, remove that useless dash before the description
112 aptitude -F "%c%a%M %p %$((COLUMNS - 30))d" -w $COLUMNS search "$@"
113 }
114 pu() {
115 local s; [[ $EUID != 0 ]] && s=sudo
116 while fuser /var/lib/dpkg/lock &>/dev/null; do sleep 1; done
117 $s apt-get -y remove --allow-downgrades --purge --auto-remove "$@"
118 # seems slightly redundant, but it removes more stuff sometimes.
119 $s apt-get -y --allow-downgrades autoremove
120 }
121 pup() { # upgrade
122 while fuser /var/lib/dpkg/lock &>/dev/null; do sleep 1; done
123 pupdate
124 local s; [[ $EUID != 0 ]] && s=sudo
125 $s apt-get -y dist-upgrade --allow-downgrades --purge --auto-remove "$@"
126 $s apt-get -y autoremove --allow-downgrades
127 $s checkrestart
128 }
129 # package info
130 pl() {
131 if type -p aptitude &>/dev/null; then
132 aptitude show "$@"
133 else
134 apt-cache show "$@"
135 fi
136 }
137 pfile() {
138 local file=$1
139 # ucfq can tell us about config files which are not tracked
140 # with apt-file. but, for at least a few files I tested
141 # which are tracked with apt-file, ucfq doesn't show their
142 # package name. So, commenting this, waiting to find
143 # a config file only tracked by ucfq to see if it gives the
144 # package name and if I can identify this kind of file.
145 # if [[ $file == /* ]] && ! ucfq -w $file | grep ::: &>/dev/null; then
146 # ucfq $file
147
148 if [[ $file == */* ]]; then
149 apt-file find -x "$file"\$
150 else
151 apt-file find -x /"$file"\$
152 update-alternatives --list "$file" 2>/dev/null
153 fi
154 }
155 pkgfiles() {
156 if dpkg -s "$1" &>/dev/null; then
157 dpkg-query -L $1
158 else
159 apt-file -x list "^$1$"
160 fi
161 }
162
163 elif command -v pacman &>/dev/null; then
164 p() {
165 pacaur "$@"
166 }
167 pi() {
168 pacaur -S --noconfirm --needed --noedit "$@"
169 }
170 pf() {
171 pacaur -Ss "$@"
172 }
173 pu() {
174 pacaur -Rs --noconfirm "$@"
175 if p=$(pacaur -Qdtq); then
176 pacaur -Rs $p
177 fi
178 }
179 aurex() {
180 p="$1"
181 aur='https://aur.archlinux.org'
182 curl -s $aur/$(curl -s "$aur/rpc.php?type=info&arg=$p" \
183 | jq -r .results.URLPath) | tar xz
184 cd "$p"
185
186 }
187 pmirror() {
188 local s; [[ $EUID != 0 ]] && s=sudo
189 local x=$(mktemp)
190 curl -s "https://www.archlinux.org/mirrorlist/\
191 ?country=US&protocol=https&ip_version=4&ip_version=6&use_mirror_status=on" \
192 | sed -r 's/^[ #]*(Server *=)/\1/' > $x
193 if (( $(stat -c %s $x ) > 10 )); then
194 $s cp $x /etc/pacman.d/mirrorlist
195 rm $x
196 fi
197 }
198 pup() { # upgrade
199 local s; [[ $EUID != 0 ]] && s=sudo
200 # file_time + 24 hours > current_time
201 if ! (( $(stat -c%Y /etc/pacman.d/mirrorlist) + 60*60*24 > $(date +%s) ))
202 then
203 pmirror
204 fi
205 pacaur -Syu --noconfirm "$@"
206 }
207 # package info
208 pl() {
209 pacaur -Si "$@"
210 }
211 pfile() {
212 pkgfile "$1"
213 }
214 pkgfiles() {
215 if pacaur -Qs "^$1$" &>/dev/null; then
216 pacman -Ql $1
217 else
218 pkgfile -l $1
219 fi
220 }
221 fi