-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepo_excludepkg.sh
More file actions
69 lines (63 loc) · 1.93 KB
/
repo_excludepkg.sh
File metadata and controls
69 lines (63 loc) · 1.93 KB
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
##
# Disable a package from a given yum repo
function yum_repo_excludepkg() {
local REPO_FILE="$1"
local PACKAGE="$2"
if [ ! -e "$REPO_FILE" ]; then
# If the repo file does not exist at all, nothing to do.
return 1
fi
local STARTED=0
local FOUND=0
local TMP_FILE="$(mktemp)"
local SECTION=""
if [ -z "$TMP_FILE" ]; then
# Ensure a temp file exists for temporary writing, (even if mktemp fails)
TMP_FILE="/tmp/tmp.$(tr -dc A-Za-z0-9 </dev/urandom | head -c 13)"
touch $TMP_FILE
fi
while read LINE; do
if [[ "$LINE" =~ ^\[.*\] ]]; then
# "[...] indicates the start of a new section
if [ $STARTED -eq 1 -a $FOUND -eq 0 ]; then
# If a new section has started with the directive being written, ensure it's written
# prior to the next section starting.
# Exception is the first section in the file.
echo "Excluding $PACKAGE from $SECTION in $REPO_FILE"
echo "excludepkgs=$PACKAGE" >> $TMP_FILE
echo "" >> $TMP_FILE
fi
SECTION="$LINE"
STARTED=1
FOUND=0
fi
if [[ "$LINE" =~ ^excludepkgs= ]]; then
# Line contains "excludepkgs", just append the requested package if necessary!
FOUND=1
if [ -z "$(echo $LINE | grep "$PACKAGE")" ]; then
echo "Excluding $PACKAGE from $SECTION in $REPO_FILE"
if [ "$LINE" == "excludepkgs=" ]; then
LINE="$LINE$PACKAGE"
else
LINE="$LINE,$PACKAGE"
fi
fi
fi
if [ "$LINE" == "" -a $STARTED -eq 1 ]; then
# End of a section
if [ $FOUND -eq 0 ]; then
echo "Excluding $PACKAGE from $SECTION in $REPO_FILE"
echo "excludepkgs=$PACKAGE" >> $TMP_FILE
FOUND=1
fi
fi
echo "$LINE" >> $TMP_FILE
done <$REPO_FILE
if [ $FOUND -eq 0 ]; then
# Last section in the file, if it was not written yet, ensure it's set.
echo "Excluding $PACKAGE from $SECTION in $REPO_FILE"
echo "excludepkgs=$PACKAGE" >> $TMP_FILE
fi
# Now that all operations are complete, replace the original file with the parsed one.
mv $TMP_FILE $REPO_FILE
}