-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrac_packages.sh
executable file
·218 lines (195 loc) · 5.05 KB
/
trac_packages.sh
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
#!/bin/bash
########################################################################
#** Version: 2.1
#* Very basic script to track todays debian package installations
#* and print it in a trac-wiki compatible form (or markdown)..
########################################################################
# author/copyright: <mic@inofix.ch>
# license: gladly sharing with the universe and any lifeform,
# be it naturally born or constructed, alien or kin..
# USE AT YOUR OWN RISK.
########################################################################
# default output is stdout
outfile=
# search all possible files per default
logfile=( "/var/log/dpkg.log" "/var/log/dpkg.log.1" )
# default search for everything
target_pattern=( "remove" "install" "upgrade" )
# default title
title="Upgrades"
#--- auto ---
# default is today
day=`date +%Y-%m-%d`
# who to log for
me=`whoami`
# register the hostname to log for
host=`hostname`
up=`uptime -p`
tmpfile=""
autofilename=""
autosuffix=".txt"
# title format
t="="
b="'''"
lf=()
for f in ${logfile[@]} ; do
if [ -r "$f" ] ; then
lf=(${lf[@]} $f)
fi
done
print_usage()
{
echo "usage: $0"
}
print_help()
{
print_usage
grep "^#\* " $0 | sed 's;^#\*;;'
}
print_version()
{
grep "^#\*\* " $0 | sed 's;^#\*\*;;'
}
found_something=0
print_package_log()
{
text="$1"
pattern="$2"
day="$3"
echo "$t$t$t$t$t$t $text $t$t$t$t$t$t" | tee -a $tmpfile
if [ "$autosuffix" == ".md" ] ; then
grep $day ${logfile[@]} | grep "$pattern " | \
awk '{ print " * '"$b"'"$4"'"$b"': "$5" -> "$6"" }' | \
sed -e 's|<|\<|g' -e 's|>|\>|g' | tee -a $tmpfile | grep ".*"
retval=$?
else
grep $day ${logfile[@]} | grep "$pattern " | \
awk '{ print " * '"$b"'"$4"'"$b"': "$5" -> "$6"" }' | \
tee -a $tmpfile | grep ".*"
retval=$?
fi
if [ $retval -eq 0 ] ; then
let found_something+=1
fi
echo "" | tee -a $tmpfile
}
die()
{
echo "$@"
exit 1
}
error()
{
print_usage
echo ""
die "Error: $@"
}
#* options:
while true ; do
case "$1" in
#* -i |--infile logfilename alternative log file
-i|--infile)
shift
if [ -r "$1" ] ; then
logfile=( "$1" )
else
die " log file $1 does not exist."
fi
;;
#* -h |--help print this help
-h|--help)
print_help
exit 0
;;
#* -M |--markdown search for 'remove', 'install' or 'upgrade'
-M|--markdown)
autosuffix=".md"
t="#"
b="**"
;;
#* -o |--outfile filename output file name
-o|--outfile)
shift
tmpfile=`mktemp`
if [ -d "$1" ] ; then
error "this is a direcory, did you mean --outdir instead?"
else
outfile="$1"
fi
;;
#* -O |--outdir dirname directory to put automatic file names
-O|--outdir)
shift
tmpfile=`mktemp`
if [ -d "$1" ] ; then
outfile="${1}/packages_by_date_${day}_${host}"
else
error "this is not a direcory, did you mean --outdir instead?"
fi
autofilename="yes"
;;
#* -s |--search pattern search for 'remove', 'install' or 'upgrade'
-s|--search)
shift
case "$1" in
remove|install|upgrade)
target_pattern=( "$1" )
;;
*)
error "search pattern is not supported"
;;
esac
;;
#* -t |--title title title for the report
-t|--title)
shift
title="$1"
;;
#* -v |--version print the version and exit
-v|--version)
print_version
exit
;;
#* -y |--yesterday search for the past day
-y|--yesterday)
day=`date -d yesterday +%Y-%m-%d`
;;
-*|--*)
error "option $1 is not supported"
;;
*)
break
;;
esac
shift
done
if [ -n "$autofilename" ] ; then
#TODO add .md..
outfile+="$autosuffix"
fi
echo "$t Package Log $t" | tee -a $tmpfile
echo "$t$t Date Based View $t$t" | tee -a $tmpfile
echo "$t$t$t $day $t$t$t" | tee -a $tmpfile
echo "$t$t$t$t $title ($me) $t$t$t$t" | tee -a $tmpfile
echo "$t$t$t$t$t $host ($up) $t$t$t$t$t" | tee -a $tmpfile
for p in ${target_pattern[@]} ; do
case "$p" in
remove)
print_package_log "removed" "$p" "$day"
;;
install)
print_package_log "installed" "$p" "$day"
;;
upgrade)
print_package_log "upgraded" "$p" "$day"
;;
esac
done
if [ -n "$outfile" ] ; then
if [ $found_something -gt 0 ] ; then
cp $tmpfile $outfile
else
echo "no package changes found"
fi
rm $tmpfile
fi