]>
Commit | Line | Data |
---|---|---|
e57feaea JJ |
1 | #! /bin/bash |
2 | ||
3 | ######################################################################## | |
4 | # | |
5 | # File: reg_search | |
6 | # Author: Janis Johnson <janis187@us.ibm.com> | |
7 | # Date: 2002/12/15 | |
8 | # | |
9 | # Search for a small time interval within a range of dates in which | |
10 | # results for a test changed, using a binary search. The functionality | |
11 | # for getting sources, building the component to test, and running the | |
12 | # test are in other scripts that are run from here. Before the search | |
13 | # begins, we verify that we get the expected behavior for the first and | |
14 | # last dates. | |
15 | # | |
16 | # Define these in a file whose name is the argument to this script: | |
17 | # LOW_DATE: Date string recognized by the date command (local time). | |
18 | # HIGH_DATE: Date string recognized by the date command (local time). | |
19 | # REG_UPDATE: Pathname of script to update your source tree; returns | |
20 | # zero for success, nonzero for failure. | |
21 | # REG_BUILD: Pathname of script to build enough of the product to run | |
22 | # the test; returns zero for success, nonzero for failure. | |
23 | # REG_TEST: Pathname of script to run the test; returns 1 if we | |
24 | # should search later dates, 0 if we should search earlier | |
25 | # dates. | |
26 | # Optional: | |
27 | # DELTA: Search to an interval within this many seconds; default | |
28 | # is one hour (although 300 works well). | |
29 | # REG_FINISH Pathname of script to call at the end with the two final | |
30 | # dates as arguments. | |
31 | # SKIP_LOW If 1, skip verifying the low date of the range; | |
32 | # define this only if you're restarting and have already | |
33 | # tested the low date. | |
34 | # SKIP_HIGH If 1, skip verifying the high date of the range; | |
35 | # define this only if you're restarting and have already | |
36 | # tested the high date. | |
37 | # FIRST_MID Use this as the first midpoint, to avoid a midpoint that | |
38 | # is known not to build. | |
39 | # HAS_CHANGES Pathname of script to report whether the current date has | |
40 | # no differences from one of the ends of the current range | |
41 | # to skip unnecessary build and testing; default is "true". | |
42 | # VERBOSITY Default is 0, to print only errors and final message. | |
43 | # DATE_IN_MSG If set to anything but 0, include the time and date in | |
44 | # messages. | |
45 | # | |
46 | # | |
47 | # | |
48 | # Copyright (c) 2002, 2003 Free Software Foundation, Inc. | |
49 | # | |
50 | # This file is free software; you can redistribute it and/or modify | |
51 | # it under the terms of the GNU General Public License as published by | |
52 | # the Free Software Foundation; either version 2 of the License, or | |
53 | # (at your option) any later version. | |
54 | # | |
55 | # This program is distributed in the hope that it will be useful, | |
56 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
57 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
58 | # GNU General Public License for more details. | |
59 | # | |
60 | # For a copy of the GNU General Public License, write the the | |
61 | # Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
62 | # Boston, MA 02111-1307, USA. | |
63 | # | |
64 | ######################################################################## | |
65 | ||
66 | ######################################################################## | |
67 | # Functions | |
68 | ######################################################################## | |
69 | ||
70 | # Issue a message if its verbosity level is high enough. | |
71 | ||
72 | msg() { | |
73 | test ${1} -gt ${VERBOSITY} && return | |
74 | ||
75 | if [ "x${DATE_IN_MSG}" = "x" ]; then | |
76 | echo "${2}" | |
77 | else | |
78 | echo "`date` ${2}" | |
79 | fi | |
80 | } | |
81 | ||
82 | # Issue an error message and exit with a non-zero status. If there | |
83 | # is a valid current range whose end points have been tested, report | |
84 | # it so the user can start again from there. | |
85 | ||
86 | error() { | |
87 | msg 0 "error: ${1}" | |
88 | test ${VALID_RANGE} -eq 1 && \ | |
89 | echo "current range:" | |
90 | echo "LOW_DATE=\"${LATER_THAN}\"" | |
91 | echo "HIGH_DATE=\"${EARLIER_THAN}\"" | |
92 | exit 1 | |
93 | } | |
94 | ||
95 | # Turn seconds since the epoch into a date we can use with source | |
96 | # control tools and report to the user. | |
97 | ||
98 | make_date() { | |
99 | MADE_DATE="`date -u +\"%Y-%m-%d %H:%M %Z\" --date \"1970-01-01 00:00:${1}\"`" \ | |
100 | || error "make_date: date command failed" | |
101 | } | |
102 | ||
103 | # Build the components to test using sources as of a particular date and | |
104 | # run a test case. Pass each of the scripts the date that we're | |
105 | # testing; the first one needs it, the others can ignore it if they want. | |
106 | ||
107 | process_date() { | |
108 | DATE="${1}" | |
109 | ||
110 | ${REG_UPDATE} "${DATE}" || error "source update failed for ${DATE}" | |
111 | ||
112 | # If we're already in a valid range, skip this date if there are no | |
113 | # differences from either end of the range and adjust LATER. | |
114 | ||
115 | if [ ${VALID_RANGE} = 1 ]; then | |
116 | ${HAS_CHANGES} "${DATE}" "${LATER_THAN}" "${EARLIER_THAN}" | |
117 | RET=$? | |
118 | case ${RET} in | |
119 | 0) ;; | |
120 | 1) LATER=1; return;; | |
121 | 2) LATER=0; return;; | |
122 | *) error "process_date: unexpected return value from ${HAS_CHANGES}";; | |
123 | esac | |
124 | fi | |
125 | ||
126 | ${REG_BUILD} "${DATE}" || error "build failed for ${DATE}" | |
127 | ${REG_TEST} "${DATE}" | |
128 | LATER=$? | |
129 | } | |
130 | ||
131 | # Perform a binary search on dates within the range specified by | |
132 | # the arguments, bounded by the number of seconds in DELTA. | |
133 | ||
134 | search_dates() { | |
135 | let LOW=$1 | |
136 | let HIGH=$2 | |
137 | let DIFF=HIGH-LOW | |
138 | ||
139 | # Get the date in the middle of the range; MID is in seconds since | |
140 | # the epoch, DATE is readable by humans and tools. The user can | |
141 | # override the initial mid date if it is known to have problems, | |
142 | # e.g., if a build fails for that date. | |
143 | ||
144 | if [ ${FIRST_MID} -ne 0 ]; then | |
145 | let MID=${FIRST_MID} | |
146 | else | |
147 | let MID=LOW/2+HIGH/2 | |
148 | fi | |
149 | ||
150 | while [ ${DIFF} -ge ${DELTA} ]; do | |
151 | make_date ${MID} | |
152 | DATE="${MADE_DATE}" | |
153 | ||
154 | # Test it. | |
155 | ||
156 | process_date "${DATE}" | |
157 | ||
158 | # Narrow the search based on the outcome of testing DATE. | |
159 | ||
160 | if [ ${LATER} -eq 1 ]; then | |
161 | msg 1 "search dates later than \"${DATE}\"" | |
162 | LATER_THAN="${DATE}" | |
163 | let LOW=MID | |
164 | else | |
165 | msg 1 "search dates earlier than \"${DATE}\"" | |
166 | EARLIER_THAN="${DATE}" | |
167 | let HIGH=MID | |
168 | fi | |
169 | ||
170 | let DIFF=HIGH-LOW | |
171 | let MID=LOW/2+HIGH/2 | |
172 | done | |
173 | } | |
174 | ||
175 | ######################################################################## | |
176 | # Main program (so to speak) | |
177 | ######################################################################## | |
178 | ||
179 | # The error function uses this. | |
180 | ||
181 | VALID_RANGE=0 | |
182 | ||
183 | # Process the configuration file. | |
184 | ||
185 | if [ $# != 1 ]; then | |
186 | echo Usage: $0 config_file | |
187 | exit 1 | |
188 | fi | |
189 | ||
190 | CONFIG=${1} | |
191 | if [ ! -f ${CONFIG} ]; then | |
192 | error "configuration file ${CONFIG} does not exist" | |
193 | fi | |
194 | ||
195 | # OK, the config file exists. Source it, make sure required parameters | |
196 | # are defined and their files exist, and give default values to optional | |
197 | # parameters. | |
198 | ||
199 | . ${CONFIG} | |
200 | ||
201 | test "x${REG_UPDATE}" = "x" && error "REG_UPDATE is not defined" | |
202 | test "x${REG_BUILD}" = "x" && error "REG_BUILD is not defined" | |
203 | test "x${REG_TEST}" = "x" && error "REG_TEST is not defined" | |
204 | test -x ${REG_TEST} || error "REG_TEST is not an executable file" | |
205 | test "x${SKIP_LOW}" = "x" && SKIP_LOW=0 | |
206 | test "x${SKIP_HIGH}" = "x" && SKIP_HIGH=0 | |
207 | test "x${DELTA}" = "x" && DELTA=3600 | |
208 | test "x${VERBOSITY}" = "x" && VERBOSITY=0 | |
209 | test "x${HAS_CHANGES}" = "x" && HAS_CHANGES=true | |
210 | test "x${REG_FINISH}" = "x" && REG_FINISH=true | |
211 | ||
212 | msg 2 "LOW_DATE = ${LOW_DATE}" | |
213 | msg 2 "HIGH_DATE = ${HIGH_DATE}" | |
214 | msg 2 "REG_UPDATE = ${REG_UPDATE}" | |
215 | msg 2 "REG_BUILD = ${REG_BUILD}" | |
216 | msg 2 "REG_TEST = ${REG_TEST}" | |
217 | msg 2 "SKIP_LOW = ${SKIP_LOW}" | |
218 | msg 2 "SKIP_HIGH = ${SKIP_HIGH}" | |
219 | msg 2 "FIRST_MID = ${FIRST_MID}" | |
220 | msg 2 "VERBOSITY = ${VERBOSITY}" | |
221 | msg 2 "DELTA = ${DELTA}" | |
222 | ||
223 | # Verify that DELTA is at least two minutes. | |
224 | ||
225 | test ${DELTA} -lt 120 && \ | |
226 | error "DELTA is ${DELTA}, must be at least 120 (two minutes)" | |
227 | ||
228 | # Change the dates into seconds since the epoch. This uses an extension | |
229 | # in GNU date. | |
230 | ||
231 | LOW_DATE=`date +%s --date "${LOW_DATE}"` || \ | |
232 | error "date command failed for \"${LOW_DATE}\"" | |
233 | HIGH_DATE=`date +%s --date "${HIGH_DATE}"` || \ | |
234 | error "date command failed for \"${LOW_DATE}\"" | |
235 | ||
236 | # If FIRST_MID was defined, convert it and make sure it's in the range. | |
237 | ||
238 | if [ "x${FIRST_MID}" != "x" ]; then | |
239 | FIRST_MID=`date +%s --date "${FIRST_MID}"` || \ | |
240 | error "date command failed for \"${FIRST_MID}\"" | |
241 | test ${FIRST_MID} -le ${LOW_DATE} && \ | |
242 | error "FIRST_MID date is earlier than LOW_DATE" | |
243 | test ${FIRST_MID} -ge ${HIGH_DATE} && \ | |
244 | error "FIRST_MID is later than HIGH_DATE" | |
245 | else | |
246 | FIRST_MID=0 | |
247 | fi | |
248 | ||
249 | # Keep track of the bounds of the range where the test behavior changes, | |
250 | # using a human-readable version of each date. | |
251 | ||
252 | make_date ${LOW_DATE} | |
253 | LATER_THAN="${MADE_DATE}" | |
254 | make_date ${HIGH_DATE} | |
255 | EARLIER_THAN="${MADE_DATE}" | |
256 | ||
257 | msg 2 "LATER_THAN = ${LATER_THAN}" | |
258 | msg 2 "EARLIER_THAN = ${EARLIER_THAN}" | |
259 | ||
260 | # Verify that the range isn't backwards. | |
261 | ||
262 | test ${LOW_DATE} -lt ${HIGH_DATE} || error "date range is backwards" | |
263 | ||
264 | # Verify that the first and last date in the range get the results we | |
265 | # expect. If not, quit, because any of several things could be wrong. | |
266 | ||
267 | if [ ${SKIP_LOW} -eq 0 ]; then | |
268 | process_date "${LATER_THAN}" | |
269 | test ${LATER} -ne 1 && \ | |
270 | error "unexpected result for low date ${LATER_THAN}" | |
271 | msg 1 "result for low date is as expected" | |
272 | fi | |
273 | ||
274 | if [ ${SKIP_HIGH} -eq 0 ]; then | |
275 | process_date "${EARLIER_THAN}" | |
276 | test ${LATER} -ne 0 && \ | |
277 | error "unexpected result for high date ${EARLIER_THAN}" | |
278 | msg 1 "result for high date is as expected" | |
279 | fi | |
280 | ||
281 | # Search within the range, now that we know that the end points are valid. | |
282 | ||
283 | VALID_RANGE=1 | |
284 | search_dates ${LOW_DATE} ${HIGH_DATE} | |
285 | ||
286 | # Report the range that's left to investigate. | |
287 | ||
288 | echo "Continue search between ${LATER_THAN} and ${EARLIER_THAN}" | |
289 | ||
290 | # Invoke the optional script to report additional information about | |
291 | # changes between the two dates. | |
292 | ||
293 | ${REG_FINISH} "${LATER_THAN}" "${EARLIER_THAN}" |