-
Notifications
You must be signed in to change notification settings - Fork 1
/
fgetpatchwork
executable file
·276 lines (210 loc) · 5.52 KB
/
fgetpatchwork
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
#!/bin/bash
#
# fgetpatchwork
#
source ~/bin/lib/ui.source
declare -i optcount=0
declare patchend="-- "
declare directory="./"
declare tempfile="/dev/shm/fgetpatchwork.tmp"
declare linkfile=
# Other exit codes
declare -i EXIT_OK=0
declare -i EXIT_INVARG=$ui_err_invargc
declare -i EXIT_INVFIL=$ui_err_invfile
declare -i EXIT_INVDIR=$ui_err_invdir
declare -a exitmsgary=(
""
"Invalid number of arguments."
" is an invalid filename."
" is an invalid directory name."
" ctl-c exit ..."
)
declare -i extmsgarysiz=${#exitmsgary[@]}
declare usagestr=$(
cat <<EOF
$(basename $0) linkfile [directory]
linkfile - file containing a list of patchwork links, one on
each line of the following form.
http://patchwork.lab.bos.redhat.com/patch/262503/
directory - optional directory into which to write the patches.
default is PWD.
Creates a set of patches from patchworks archive given a file containing
a list of links to the patches. The number at the end of each link will
be used to construct the full mbox link to each patch.
Extracted patch files wil be named according to their respective subject
lines with a leading zero-padded number for the order in which each
patch file was encountered in the list file
RHEL patchworks is at http://patchwork.lab.bos.redhat.com/
\0
EOF
)
# exitme() - print an exit message and exit gracefully
#
# $1 - exit code
# $2 - optional arg, e.g. filename for EXIT_INVFIL
#
exitme() {
local exitval=$1
local strarg=""
local exitmsg
if ([ $exitval -ne $EXIT_OK ] && [ $exitval -ne $CTLC_EXIT ]); then
[ $# -eq $EXIT_INVFIL ] && strarg=$2
[ ${#exitmsgary[@]} -gt $exitval ] \
&& exitmsg="${exitmsgary[$exitval]}"
echo -e "$BLD$strarg$exitmsg$OFF"
[ $exitval -ne $EXIT_OK ] && echo -e "$usagestr"
fi
[ $exitval -eq $CTLC_EXIT ] && \
echo -e "$BLD${exitmsgary[$eximsgarysiz]}"$OFF
exit $exitval
}
# run if user hits control-c
#
control_c()
{
echo -en "\nCtrl-c detected\nCleaning up and exiting.\n"
exitme $CTLC_EXIT
}
usage() {
echo -e "$usagestr"
exit
}
extract_subjectline() {
local filename="$1"
local subject=
# Extract the subject line.
#
subject=$(grep -m1 "Subject: " "$filename")
# Remove any asterisks from the subject line.
#
subject=$(sed 's/\*//g' <<< "$subject")
# Remove the word "Subject: " from the line
#
subject=$(echo "$subject" | cut -d' ' -f2-)
# Replace non-alphanumerics with a hyphen, then delete the leading
# hyphen that replaced the left bracket '['
#
subject=$(echo "$subject" | sed 's/[^[:alnum:]]\+/-/g')
subject=$(echo "$subject" | sed 's/^-//')
# If the last character is a hyphen, remove it.
#
[ "${subject:(-1)}" = "-" ] && subject=${subject%?}
echo "$subject.patch"
}
extract_patchfile_mbox() {
local link="$1"
local count=$2
local dir="$3"
local newfile
[ -z "$dir" ] || {
[ ${dir:(-1)} == '/' ] || dir="$dir/"
}
wget -q $link -O "$tempfile"
newfile=$(extract_subjectline "$tempfile")
newfile=$(printf "%04d-%s" $count $newfile)
> $dir$newfile
echo "$link -> $dir$newfile"
while IFS= read -r line; do
echo "$line" >> "$dir$newfile"
done < $tempfile
}
extract_patchfile_mbox2() {
local link="$1"
local dir="$2"
local newfile
declare found=false
declare newname=
declare temp=
declare lastfrom=
declare lastto=
declare lastsub=
declare lastdat=
declare lastmid=
declare b_header=true
wget -q $link -O "$tempfile"
while IFS= read -r line; do
if ! $found; then
[ "${line:0:5}" == "From:" ] && lastfrom="$line"
[ "${line:0:3}" == "To:" ] && lastto="$line"
[ "${line:0:8}" == "Subject:" ] && lastsub="$line"
[ "${line:0:5}" == "Date:" ] && lastdat="$line"
if [ "${line:0:11}" == "Message-Id:" ]; then
if [ "${line:13:5}" == "dummy" ]; then
continue
else
lastmid="$line"
found=true
fi
fi
else
if $b_header; then
# Remove the word "Subject: " from the lastsub
#
temp=$(echo "$lastsub" | cut -d' ' -f2-)
# Remove any brackets, parentheses, and quotes
#
temp=$(echo "$temp" | sed 's/[][{}<>(),"]//g')
temp=$(echo "$temp" | sed "s/'//g")
# Replace spaces, slashes, colons and semicolons
# with hyphens
#
temp=$(echo "$temp" | sed "s\[/:; ]\-\g")
# Put a slash at the end of the dir if it's not
# already there.
#
[ "${dir:(-1)}" == "/" ] || dir="$dir/"
# Create the new name
#
newname=$(printf "%s%s.patch" "$dir" "$temp")
echo "$newname"
echo "$lastfrom" > "$newname"
echo "$lastto" >> "$newname"
echo "$lastsub" >> "$newname"
echo "$lastdat" >> "$newname"
b_header=false
fi
fi
! $b_header && echo "$line" >> "$newname"
# Quit copying lines at the end of the patch.
#
if $found && [ "$line" == "$patchend" ]; then
break
fi
done < "$tempfile"
rm -f "$tempfile"
}
# read_linkfile - read the list of links from the linkfile
#
# GLOBALS
# linkfile
# directory
#
read_linkfile() {
[ -z "$directory" ] || {
ui_verifydir "$directory"
[ $? -ne 0 ] && usage
}
local count=1
while read link; do
# If the last char in the link is a '/', then remove it.
#
[ ${link:(-1)} == '/' ] && link="${link%?}"
# Add the '/mbox2
link="$link/mbox2"
extract_patchfile_mbox2 "$link" $directory
done < $linkfile
}
main() {
# Trap for control-c
trap control_c SIGINT
[ $# -ge 1 ] || exitme $EXIT_INVARG
linkfile=$1
[ -f $linkfile ] || exitme $EXIT_INVFIL $linkfile
[ "$2" ] && directory=$2
[ -d $directory ] || exitme $EXIT_INVDIR $directory
read_linkfile
exitme $EXIT_OK
}
main $@
exitme $EXIT_OK