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