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