Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-ports
Path: blob/main/Tools/scripts/getpatch.sh
18157 views
1
#!/bin/sh
2
3
# Copyright (c) 2015 Rodrigo Osorio
4
# All rights reserved.
5
#
6
# Redistribution and use in source and binary forms, with or without
7
# modification, are permitted provided that the following conditions
8
# are met:
9
# 1. Redistributions of source code must retain the above copyright
10
# notice, this list of conditions and the following disclaimer.
11
# 2. Redistributions in binary form must reproduce the above copyright
12
# notice, this list of conditions and the following disclaimer in the
13
# documentation and/or other materials provided with the distribution.
14
#
15
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25
# SUCH DAMAGE.
26
#
27
# MAINTAINER= [email protected]
28
#
29
30
XML_URL='https://bugs.freebsd.org/bugzilla/show_bug.cgi?ctype=xml&id='
31
GET_URL='https://bz-attachments.freebsd.org/attachment.cgi?id='
32
33
TEMP_DIR=
34
PR_DIR=
35
deprecate=0
36
build_path=1
37
verbose=0
38
39
usage() {
40
echo "usage: getpatch [-dp] tknum tknum ..."
41
echo " -d : download deprecate patches too"
42
echo " -p : store the patches in the current dir, instead of the dedicated bug id directory"
43
echo "use the GETPATCH_DIR variable to define the root location for the downloaded patches"
44
echo
45
exit 1
46
}
47
48
die() {
49
echo $@
50
erase_env
51
exit 1
52
}
53
54
build_env() {
55
if [ -z ${TEMP_DIR} ]; then
56
TEMP_DIR=`mktemp -d -t 'getpatch'`
57
fi
58
}
59
60
erase_env() {
61
if [ -n ${TEMP_DIR} ]; then
62
rm -rf ${TEMP_DIR} >/dev/null 2>&1
63
fi
64
}
65
66
getpatch() {
67
echo -n " + attachment ${2}-${3} "
68
if [ $deprecate -eq 1 ] || [ "$1" != "1" ]; then
69
if [ ! -f "${PR_DIR}${2}-${3}" ]; then
70
echo -n "download "
71
fetch --no-verify-peer -o "${PR_DIR}${2}-${3}" "${GET_URL}${2}" > /dev/null 2>&1
72
if [ $? -ne 0 ]; then
73
echo "error"
74
die "Unable to download ${GET_URL}${2}"
75
fi
76
echo "success"
77
else
78
echo "is present, skip"
79
fi
80
else
81
echo "is obsolete, skip"
82
fi
83
}
84
85
parse_and_fetch(){
86
87
for patchline in `awk '
88
/<\/attachment>/ { IN_ATTACH=0;
89
printf "%d>%s>%s\n",obsolete, patchid, patchname ;
90
}
91
/isobsolete/ || /ispatch/ && IN_ATTACH == 1 {
92
gsub(/^[ \t]+|[ \t]+$|"/, "");
93
split($0,val,"=");
94
if(val[1] == "isobsolete")
95
obsolete = val[2];
96
}
97
( /<filename>/ || /<attachid>/ ) && IN_ATTACH == 1 {
98
gsub(/^[ \t]+|[ \t]+$/, "");
99
gsub(/[<]/, ">");
100
split($0,val,">");
101
if(val[2] == "filename")
102
patchname=val[3];
103
else if(val[2] == "attachid")
104
patchid=val[3];
105
}
106
/<attachment/ { IN_ATTACH=1;
107
obsolete = 0;
108
patchid = "";
109
patchname = "";
110
}
111
' $1 ` ; do
112
getpatch `echo $patchline | tr ">" " "`
113
done
114
}
115
116
get_list_of_patches() {
117
## Get xml file
118
PR_ID=$1
119
echo "Bug ID: $1"
120
build_env
121
fetch --no-verify-peer -o "${TEMP_DIR}/my${1}.xml" "${XML_URL}${1}" > /dev/null 2>&1
122
if [ $? -ne 0 ]; then
123
die "Can't upload ${XML_URL}${1}"
124
fi
125
## Search for error
126
error_msg=`grep "bug error=" "${TEMP_DIR}/my${1}.xml"`
127
if [ -n "${error_msg}" ]; then
128
echo " !! Can't recover the bug attachements : " "`echo "${error_msg}" | cut -d '"' -f 2` !!"
129
return 1
130
fi
131
132
PR_DIR=${GETPATCH_DIR%%/}
133
if [ ${build_path} == 1 ]; then
134
PR_DIR=${PR_DIR}/${PR_ID}/
135
fi
136
137
if [ ! -d ${PR_DIR} ]; then
138
mkdir -p ${PR_DIR}
139
if [ $? != 0 ]; then
140
die "Can't create ${PR_DIR} directory"
141
fi
142
fi
143
144
parse_and_fetch "${TEMP_DIR}/my${1}.xml"
145
echo -n " >> Patches stored in "
146
realpath ${PR_DIR}
147
148
}
149
150
trap "die 'Interruption caught, exit'" 2 3 6 9 14 15
151
152
if [ -z ${GETPATCH_DIR} ]; then
153
GETPATCH_DIR='.'
154
fi
155
156
157
158
while getopts ":pd" arg; do
159
case "$arg" in
160
d ) deprecate=1 ;;
161
p ) build_path=0 ;;
162
* ) usage ;;
163
esac
164
shift
165
done
166
167
if [ "$#" -eq 0 ]; then
168
usage
169
exit 1
170
fi
171
172
for arg in "$@"; do
173
if [ -z "`echo "${arg}" | sed 's/[0-9]//g'`" ]; then
174
get_list_of_patches ${arg}
175
else
176
echo "Error: ${arg} isn't a valid PR number"
177
exit 1
178
fi
179
done
180
erase_env
181
182