debian.plugin.zsh
6915 bytes
1# Use aptitude or apt if installed, fallback is apt-get
2# You can just set apt_pref='apt-get' to override it.
3
4if [[ -z $apt_pref || -z $apt_upgr ]]; then
5 if [[ -e $commands[aptitude] ]]; then
6 apt_pref='aptitude'
7 apt_upgr='safe-upgrade'
8 elif [[ -e $commands[apt] ]]; then
9 apt_pref='apt'
10 apt_upgr='upgrade'
11 else
12 apt_pref='apt-get'
13 apt_upgr='upgrade'
14 fi
15fi
16
17# Use sudo by default if it's installed
18if [[ -e $commands[sudo] ]]; then
19 use_sudo=1
20fi
21
22# Aliases ###################################################################
23# These are for more obscure uses of apt-get and aptitude that aren't covered
24# below.
25alias age='apt-get'
26alias api='aptitude'
27
28# Some self-explanatory aliases
29alias acs="apt-cache search"
30alias aps='aptitude search'
31alias as="aptitude -F '* %p -> %d \n(%v/%V)' --no-gui --disable-columns search"
32
33# apt-file
34alias afs='apt-file search --regexp'
35
36
37# These are apt-get only
38alias asrc='apt-get source'
39alias app='apt-cache policy'
40
41# superuser operations ######################################################
42if [[ $use_sudo -eq 1 ]]; then
43# commands using sudo #######
44 alias aac="sudo $apt_pref autoclean"
45 alias abd="sudo $apt_pref build-dep"
46 alias ac="sudo $apt_pref clean"
47 alias ad="sudo $apt_pref update"
48 alias adg="sudo $apt_pref update && sudo $apt_pref $apt_upgr"
49 alias adu="sudo $apt_pref update && sudo $apt_pref dist-upgrade"
50 alias afu="sudo apt-file update"
51 alias au="sudo $apt_pref $apt_upgr"
52 alias ai="sudo $apt_pref install"
53 # Install all packages given on the command line while using only the first word of each line:
54 # acse ... | ail
55
56 alias ail="sed -e 's/ */ /g' -e 's/ *//' | cut -s -d ' ' -f 1 | xargs sudo $apt_pref install"
57 alias ap="sudo $apt_pref purge"
58 alias aar="sudo $apt_pref autoremove"
59
60 # apt-get only
61 alias ads="sudo apt-get dselect-upgrade"
62
63 # apt only
64 alias alu="sudo apt update && apt list -u && sudo apt upgrade"
65
66 # Install all .deb files in the current directory.
67 # Warning: you will need to put the glob in single quotes if you use:
68 # glob_subst
69 alias dia="sudo dpkg -i ./*.deb"
70 alias di="sudo dpkg -i"
71
72 # Remove ALL kernel images and headers EXCEPT the one in use
73 alias kclean='sudo aptitude remove -P "?and(~i~nlinux-(ima|hea) ?not(~n$(uname -r)))"'
74
75
76# commands using su #########
77else
78 alias aac="su -ls '$apt_pref autoclean' root"
79 function abd() {
80 cmd="su -lc '$apt_pref build-dep $@' root"
81 print "$cmd"
82 eval "$cmd"
83 }
84 alias ac="su -ls '$apt_pref clean' root"
85 alias ad="su -lc '$apt_pref update' root"
86 alias adg="su -lc '$apt_pref update && $apt_pref $apt_upgr' root"
87 alias adu="su -lc '$apt_pref update && $apt_pref dist-upgrade' root"
88 alias afu="su -lc 'apt-file update'"
89 alias au="su -lc '$apt_pref $apt_upgr' root"
90 function ai() {
91 cmd="su -lc '$apt_pref install $@' root"
92 print "$cmd"
93 eval "$cmd"
94 }
95 function ap() {
96 cmd="su -lc '$apt_pref purge $@' root"
97 print "$cmd"
98 eval "$cmd"
99 }
100 function aar() {
101 cmd="su -lc '$apt_pref autoremove $@' root"
102 print "$cmd"
103 eval "$cmd"
104 }
105 # Install all .deb files in the current directory
106 # Assumes glob_subst is off
107 alias dia='su -lc "dpkg -i ./*.deb" root'
108 alias di='su -lc "dpkg -i" root'
109
110 # Remove ALL kernel images and headers EXCEPT the one in use
111 alias kclean='su -lc "aptitude remove -P \"?and(~i~nlinux-(ima|hea) ?not(~n$(uname -r)))\"" root'
112fi
113
114# Completion ################################################################
115
116#
117# Registers a compdef for $1 that calls $apt_pref with the commands $2
118# To do that it creates a new completion function called _apt_pref_$2
119#
120function apt_pref_compdef() {
121 local f fb
122 f="_apt_pref_${2}"
123
124 eval "function ${f}() {
125 shift words;
126 service=\"\$apt_pref\";
127 words=(\"\$apt_pref\" '$2' \$words);
128 ((CURRENT++))
129 test \"\${apt_pref}\" = 'aptitude' && _aptitude || _apt
130 }"
131
132 compdef "$f" "$1"
133}
134
135apt_pref_compdef aac "autoclean"
136apt_pref_compdef abd "build-dep"
137apt_pref_compdef ac "clean"
138apt_pref_compdef ad "update"
139apt_pref_compdef afu "update"
140apt_pref_compdef au "$apt_upgr"
141apt_pref_compdef ai "install"
142apt_pref_compdef ail "install"
143apt_pref_compdef ap "purge"
144apt_pref_compdef aar "autoremove"
145apt_pref_compdef ads "dselect-upgrade"
146
147# Misc. #####################################################################
148# print all installed packages
149alias allpkgs='aptitude search -F "%p" --disable-columns ~i'
150
151# Create a basic .deb package
152alias mydeb='time dpkg-buildpackage -rfakeroot -us -uc'
153
154
155# Functions #################################################################
156# create a simple script that can be used to 'duplicate' a system
157function apt-copy() {
158 print '#!/bin/sh'"\n" > apt-copy.sh
159
160 cmd='$apt_pref install'
161
162 for p in ${(f)"$(aptitude search -F "%p" --disable-columns \~i)"}; {
163 cmd="${cmd} ${p}"
164 }
165
166 print $cmd "\n" >> apt-copy.sh
167
168 chmod +x apt-copy.sh
169}
170
171# Prints apt history
172# Usage:
173# apt-history install
174# apt-history upgrade
175# apt-history remove
176# apt-history rollback
177# apt-history list
178# Based On: https://linuxcommando.blogspot.com/2008/08/how-to-show-apt-log-history.html
179function apt-history() {
180 case "$1" in
181 install)
182 zgrep --no-filename 'install ' $(ls -rt /var/log/dpkg*)
183 ;;
184 upgrade|remove)
185 zgrep --no-filename $1 $(ls -rt /var/log/dpkg*)
186 ;;
187 rollback)
188 zgrep --no-filename upgrade $(ls -rt /var/log/dpkg*) | \
189 grep "$2" -A10000000 | \
190 grep "$3" -B10000000 | \
191 awk '{print $4"="$5}'
192 ;;
193 list)
194 zgrep --no-filename '' $(ls -rt /var/log/dpkg*)
195 ;;
196 *)
197 echo "Parameters:"
198 echo " install - Lists all packages that have been installed."
199 echo " upgrade - Lists all packages that have been upgraded."
200 echo " remove - Lists all packages that have been removed."
201 echo " rollback - Lists rollback information."
202 echo " list - Lists all contains of dpkg logs."
203 ;;
204 esac
205}
206
207# Kernel-package building shortcut
208function kerndeb() {
209 # temporarily unset MAKEFLAGS ( '-j3' will fail )
210 MAKEFLAGS=$( print - $MAKEFLAGS | perl -pe 's/-j\s*[\d]+//g' )
211 print '$MAKEFLAGS set to '"'$MAKEFLAGS'"
212 appendage='-custom' # this shows up in $(uname -r )
213 revision=$(date +"%Y%m%d") # this shows up in the .deb file name
214
215 make-kpkg clean
216
217 time fakeroot make-kpkg --append-to-version "$appendage" --revision \
218 "$revision" kernel_image kernel_headers
219}
220
221# List packages by size
222function apt-list-packages() {
223 dpkg-query -W --showformat='${Installed-Size} ${Package} ${Status}\n' | \
224 grep -v deinstall | \
225 sort -n | \
226 awk '{print $1" "$2}'
227}