-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.bash_functions.debian
296 lines (283 loc) · 8.63 KB
/
.bash_functions.debian
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# vim: ft=bash noet:
! declare 2>&1 | grep -wq ^colors= && [ $BASH_VERSINFO -ge 4 ] && source $initDir/.colors
test "$debug" -gt 0 && echo "=> Running $bold${colors[blue]}$(basename ${BASH_SOURCE[0]})$normal ..."
apt="$(type -P apt) -q=0"
trap 'rc=130;set +x;echo "=> $FUNCNAME: CTRL+C Interruption trapped.">&2;return $rc' INT
function addAptKeys {
for key
do
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys $key
done
move_Extra_Keys_To_Main_APT_KeyRing
}
function apt {
local args=("$@")
local firstArg=$1
local aptGet="apt-get"
local apt="apt -q=0"
case $firstArg in
install|remove|purge|upgrade|dist-upgrade|full-upgrade)
if [ -n "$SSH_CONNECTION" ] && type -P screen >/dev/null 2>&1;then
sudo screen -L $apt "${args[@]}" -V
else
sudo $apt "${args[@]}" -V
fi
;;
autoremove)
if [ -n "$SSH_CONNECTION" ] && type -P screen >/dev/null 2>&1;then
sudo screen -L $apt autoremove -V || sudo screen -L $aptGet autoremove -V
else
sudo $apt autoremove -V || sudo $aptGet autoremove -V
fi
;;
*)
command $apt "${args[@]}";;
esac
}
function apt-cache {
local args=("$@")
local firstArg=$1
local aptCache="command apt-cache"
case $firstArg in
search)
$aptCache -q=0 "${args[@]}" | sort;;
*)
$aptCache "${args[@]}";;
esac
}
function aptCachePolicy2LaunchpadPPAURLs {
local pkg=""
if [ $# = 0 ];then
apt-cache policy | awk '/https?:..ppa.launchpad.net/{print$2}' | awk -F/ 'BEGIN{OFS=FS}{printf "ppa:"$4"/"$5" ";sub("ppa.","",$3);$4="~"$4"/+archive";tmp=$5;$5=$6;$6=tmp;print}' | sort -u
else
for pkg
do
apt-cache policy $pkg | awk '/https?:..ppa.launchpad.net/{print$2}' | awk -v pkg=$pkg -F/ 'BEGIN{OFS=FS}{printf pkg" ppa:"$4"/"$5" ";sub("ppa.","",$3);$4="~"$4"/+archive";tmp=$5;$5=$6;$6=tmp;print}'
done | uniq | column -t
fi
}
function aptGet {
local args=("$@")
local firstArg=$1
local aptGet="apt-get"
case $firstArg in
install|remove|autoremove|purge) sudo $apt "${args[@]}" -V;;
download) $aptGet "${args[@]}" --print-uris && $aptGet "${args[@]}";;
*) $aptGet "${args[@]}";;
esac
}
function aptPrintURIS {
local aptGet="apt-get"
for apt
do
$aptGet download --print-uris "$apt"
done
}
function apt_cache {
local apt_cache="command apt-cache"
firstArg=$1
if [ "$firstArg" = search ]
then
$apt_cache $@ | sort -u
else
$apt_cache $@
fi
}
function aptitude {
local args=("$@")
local aptitude="$(type -P aptitude)"
firstArg=$1
case $firstArg in
install|reinstall|purge|remove) $sudo $aptitude "${args[@]}" -V;;
*) $aptitude "${args[@]}";;
esac
}
function debExtract {
for pkg
do
dpkg-deb -X "$pkg"
done
}
function deblocate {
dlocate $(printf "bin/%s$ " "$@")
}
function debEditDependenciesAndInstall {
test $# = 0 && {
echo "=> Usage: $FUNCNAME debianPackageFileName" >&2
return 1
}
local pkg=$1
local newPackage=${pkg/.deb/_NEW.deb}
local tmp=tmp
local dpkg_deb="command dpkg-deb"
$dpkg_deb -x $pkg $tmp/
$dpkg_deb --control $pkg $tmp/DEBIAN
$EDITOR $tmp/DEBIAN/control
dpkg -l gdebi | \grep -q ^.i || $sudo apt install -Vy gdebi
dpkg -l $pkg | \grep -q ^.i || $dpkg_deb -b $tmp/ $newPackage && echo "=> Installing \"$pkg\" and its dependencies ..." && $sudo gdebi $newPackage
rm -fr $tmp/
}
function downgradePPA_Package {
type -P ppa-purge >/dev/null || sudo apt install -V ppa-purge -y
local ppa=""
for pkg
do
ppa=$(command apt-cache policy $pkg | awk '/launchpad.net/{print$2}' | awk -F/ '{print"ppa:"$4"/"$5}')
if [ -n "$ppa" ];then
echo "=> Downgrading <$pkg> by removing the launchpad <$ppa> repository ..." >&2
sudo ppa-purge $ppa
fi
done
}
function downgradePackages {
local packageList
for package
do
previousVersion=$(command apt-cache policy $package | awk '!/http:/{version=$1}/ubuntu.com\/ubuntu/{print version;exit;}')
test -z "$previousVersion" && echo "E: Version '' for '$package' was not found" >&2 || packageList="$packageList $package=$previousVersion"
done
set -x
sudo $apt install -V $packageList
set +x
}
function downgradeTo {
distribCodename=$1
case $distribCodename in
"-h"|"")
echo "=> ERROR: Usage: $FUNCNAME [ <distribCodename> ] | downgrade current" >&2
return -1
;;
"current")
distribCodename=$(lsb_release -sc)
;;
*) #Ajouter une commande pour tester si le nom donne existe
;;
esac
echo "=> distribCodename = $distribCodename"
sudo sed -i".$(date +%Y%m%d)" "s/$(lsb_release -sc)/$distribCodename/" /etc/apt/sources.list
if test -s /etc/apt/preferences.d/99-downgrade
then
time sudo $apt update
sudo $apt dist-upgrade -V
else
echo "=> ERROR: The file </etc/apt/preferences.d/99-downgrade> does not exist, generating it ..." >&2 && {
cat <<-EOF
Package: *
Pin: release a=$distribCodename
Pin-Priority: 1001
Package: *
Pin: release a=$distribCodename-updates
Pin-Priority: 1001
Package: *
Pin: release a=$distribCodename-security
Pin-Priority: 1001
Package: *
Pin: release a=$distribCodename-backports
Pin-Priority: 1001
Package: *
Pin: release a=$distribCodename-proposed
Pin-Priority: -1
EOF
} | sudo tee /etc/apt/preferences.d/99-downgrade
echo "=> Please re-run the <downgrade> function." >&2
return 1
fi
}
function fixAptKeys {
local nbKeysAdded=0
LANG=C apt-key list | awk -F"/| *" '/expired/{print"sudo apt-key del "$3}' | sh -x
time (sudo apt-get update 2>/tmp/keymissing; cat /tmp/keymissing)
for key in $(awk '/Release:.*not available: NO_PUBKEY/{print substr($NF,9)}' /tmp/keymissing | sort -u)
do
let nbKeysAdded+=1
echo
echo "=> Processing key: $key"
printf "=> Pre-deleting <$key> just in case ... "
sudo apt-key del $key
echo "=> Adding <$key> ..."
set -x
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys $key
set +x
done
[ $nbKeysAdded != 0 ] && echo "=> New keys were added so resynchronizing the package index files from their sources ..." && sudo apt-get update
for keyFile in $(awk '/gpg: keyblock resource .*resource limit/{gsub("`|.:","",$4);print$4" "}' /tmp/keymissing)
do
sudo rm -v $keyFile
done
\rm -v /tmp/keymissing
}
function isInstalled {
test $1 && dpkg -l $1 | grep -q ^.i
}
function listFiles2LaunchpadPPAURLs {
local listFile=""
for listFile
do
sed -n "s/^# //;/deb /p" $listFile | awk '/launchpad.net/{print$2}' | awk -v listFile=$listFile -F/ 'BEGIN{OFS=FS}{printf listFile" ppa:"$4"/"$5" ";sub("ppa.","",$3);$4="~"$4"/+archive";tmp=$5;$5=$6;$6=tmp;print}'
done | uniq | column -t
}
function listInstalledPackagesFromRepo {
local architecture=$(dpkg --print-architecture)
local repo=undefined_yet
if [ $# != 1 ];then
echo "=> Usage : $FUNCNAME repo" >&2
return 1
fi
lsPackagesFromRepo "$1" | \xargs dpkg-query -W --showformat='${Package} is ${Status}\n' 2>/dev/null | awk '/ installed/{print$1}' | sort -u
}
function lsPackagesFromRepo {
local architecture=$(dpkg --print-architecture)
local repo=undefined_yet
if [ $# != 1 ];then
echo "=> Usage : $FUNCNAME repo" >&2
return 1
else
repo="${1/ppa:/}"
fi
if grep-dctrl -n -sPackage . /var/lib/apt/lists/*${repo/\//_}_*Packages;then
:
elif \grep -h -P -o "^Package: \K.*" /var/lib/apt/lists/*${repo/\//_}_*Packages;then
:
else aptitude search -F '%p' "?origin(${repo/\//-}\>) ( ?architecture($architecture) | ?architecture(all) )"
:
fi
}
function move_Extra_Keys_To_Main_APT_KeyRing {
apt-key list 2>&1 >/dev/null | awk -F"[\`']" '/resource limit/{print "sudo apt-key add "$2"; sudo \\rm -vf "$2}' | sh -x
}
function reinstallOrignialPackages {
local packagesList=$@
local distribName=$(\lsb_release -si)
distribName=${distribName,} # Converts 1st letter to lower case cf. man bash | grep -m1 -B4 -A10 ,,
local distribCodeName=$(\lsb_release -sc)
# test $# != 0 && sudo $apt install -V $(printf "%s/$(lsb_release -sc) " $packagesList)
for pkg
do
version=$(apt-cache policy $pkg| \sed -zr 's/[*]{3}/ /g;s/([0-9]+)\n/\1 /g' | awk "/ppa|:$/{next}/[0-9]+\..*https?:.*$distribName.*$distribCodeName/"'{printf$1;exit}')
sudo $apt install -V $pkg=$version
done
}
function removePackages {
#echo "=> regexp = $regexp" >&2
local regexp=$1
shift
case $regexp in
""|.) echo "=> Usage: $FUNCNAME non-empty-regexp" >&2;return 1;;
esac
local packagesToBeRemoved=$(dpkg -l | awk "/^.i +.*$regexp/"'{printf $2" "}')
test -n "$packagesToBeRemoved" && sudo apt purge -V $packagesToBeRemoved
}
function updateRepositoryKeys {
time sudo apt-get update 2>&1 >/dev/null | awk '/Release:.*not available: NO_PUBKEY/{print "sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys "$NF}' | sh -x
sudo apt-key update
}
function website {
for pkg
do
printf "Package: $pkg\t"
command apt-cache show $pkg | grep "Homepage:" | uniq
done
}
trap - INT
set +x
test "$debug" -gt 0 && echo "=> END of $bold${colors[blue]}$(basename ${BASH_SOURCE[0]})$normal"