Debian Codegraph @

pm-clean

raw
1 #!/bin/bash
2
3 # Public domain notice for all NCBI EDirect scripts is located at:
4 # https://www.ncbi.nlm.nih.gov/books/NBK179288/#chapter6.Public_Domain_Notice
5
6 # pm-clean
7
8 pth=$( dirname "$0" )
9
10 case "$pth" in
11 /* )
12 ;; # already absolute
13 * )
14 pth=$(cd "$pth" && pwd)
15 ;;
16 esac
17
18 case ":$PATH:" in
19 *:"$pth":* )
20 ;;
21 * )
22 PATH="$PATH:$pth"
23 export PATH
24 ;;
25 esac
26
27 # handle common flags - dot command is equivalent of "source"
28
29 if [ ! -f "$pth"/xcommon.sh ]
30 then
31 echo "ERROR: Unable to find '$pth/xcommon.sh' file" >&2
32 exit 1
33 fi
34
35 . "$pth"/xcommon.sh
36
37 # initialize specific flags
38
39 total_start=$(date "+%s")
40
41 dbase=""
42 fields=""
43
44 clean=false
45 scrap=false
46 scrub=false
47 scour=false
48 erase=false
49 zap=false
50
51 scratch=false
52
53 while [ $# -gt 0 ]
54 do
55 case "$1" in
56 -db )
57 dbase=$2
58 shift
59 shift
60 ;;
61 -fields )
62 fields=$2
63 shift
64 shift
65 ;;
66 clean | -clean | clear | -clear )
67 # delete Indices contents and Increment files
68 clean=true
69 shift
70 ;;
71 scrap | -scrap )
72 # only delete Postings directories
73 scrap=true
74 shift
75 ;;
76 scrub | -scrub )
77 clean=true
78 # and delete Postings directories
79 scrub=true
80 shift
81 ;;
82 scour | -scour )
83 clean=true
84 scrub=true
85 # and delete Data, Archive, and Sentinels directories
86 scour=true
87 shift
88 ;;
89 erase | -erase )
90 clean=true
91 scrub=true
92 scour=true
93 # and delete Extras directory contents
94 erase=true
95 shift
96 ;;
97 zap | -zap )
98 clean=true
99 scrub=true
100 scour=true
101 erase=true
102 # and delete Source records and all remaining directories
103 zap=true
104 shift
105 ;;
106 scratch | -scratch )
107 # only delete contents of Scratch directories
108 scratch=true
109 shift
110 ;;
111 -* )
112 exec >&2
113 echo "$0: Unrecognized option $1, cleaning must be done separately" >&2
114 exit 1
115 ;;
116 * )
117 break
118 ;;
119 esac
120 done
121
122 if [ -z "$dbase" ]
123 then
124 echo "Must supply database in -db argument" >&2
125 exit 1
126 fi
127
128 if [ "$scratch" = true ]
129 then
130 scratchBase=$( FindLocalArchiveFolder "$dbase" "Scratch" "false" )
131 if [ -n "$scratchBase" ] && [ -d "$scratchBase" ]
132 then
133 echo "Deleting Scratch Directories" >&2
134 cd "$scratchBase"
135 for dir in "$scratchBase"/*
136 do
137 if [ -d "$dir" ]
138 then
139 rm -rf "$dir" &
140 fi
141 done
142 fi
143
144 postingsBase=$( FindLocalArchiveFolder "$dbase" "Postings" "false" )
145 if [ -n "$postingsBase" ] && [ -d "$postingsBase" ] && [ "$fields" != "" ]
146 then
147 echo "Deleting Postings" >&2
148
149 cd "$postingsBase"
150 echo "$fields" |
151 xargs -n1 echo |
152 while read dir
153 do
154 rm -rf "$postingsBase/$dir" &
155 done
156 fi
157
158 wait
159
160 # do not touch regular incrementally-indexed data from primary source records
161 exit 0
162 fi
163
164 if [ "$clean" = true ]
165 then
166 indexBase=$( FindLocalArchiveFolder "$dbase" "Index" "false" )
167 if [ -n "$indexBase" ] && [ -d "$indexBase" ]
168 then
169 echo "Deleting Index" >&2
170 target="$indexBase"
171 find "$target" -name "*.e2x" -delete
172 find "$target" -name "*.e2x.gz" -delete
173 cd "$indexBase"
174 rm -rf * &
175 fi
176
177 invertBase=$( FindLocalArchiveFolder "$dbase" "Invert" "false" )
178 if [ -n "$invertBase" ] && [ -d "$invertBase" ]
179 then
180 echo "Deleting Invert" >&2
181 target="$invertBase"
182 find "$target" -name "*.inv" -delete
183 find "$target" -name "*.inv.gz" -delete
184 cd "$invertBase"
185 rm -rf * &
186 fi
187
188 mergedBase=$( FindLocalArchiveFolder "$dbase" "Merged" "false" )
189 if [ -n "$mergedBase" ] && [ -d "$mergedBase" ]
190 then
191 echo "Deleting Merged" >&2
192 target="$mergedBase"
193 find "$target" -name "*.mrg" -delete
194 find "$target" -name "*.mrg.gz" -delete
195 fi
196
197 sleep 1
198 fi
199
200 wait
201
202 if [ "$scrub" = true ] || [ "$scrap" = true ]
203 then
204 postingsBase=$( FindLocalArchiveFolder "$dbase" "Postings" "false" )
205 if [ -n "$postingsBase" ] && [ -d "$postingsBase" ]
206 then
207 echo "Deleting Postings" >&2
208 target="$postingsBase"
209 find "$target" -name "*.mst" -delete
210 find "$target" -name "*.pst" -delete
211 find "$target" -name "*.trm" -delete
212 find "$target" -name "*.ofs" -delete
213 find "$target" -name "*.uqi" -delete
214
215 wait
216
217 cd "$postingsBase"
218 for dir in "$postingsBase"/*
219 do
220 if [ -d "$dir" ]
221 then
222 rm -rf "$dir" &
223 fi
224 done
225 fi
226
227 sleep 1
228 fi
229
230 wait
231
232 if [ "$scour" = true ]
233 then
234 dataBase=$( FindLocalArchiveFolder "$dbase" "Data" "false" )
235 if [ -n "$dataBase" ] && [ -d "$dataBase" ]
236 then
237 echo "Deleting Data" >&2
238 cd "$dataBase"
239 rm -rf *
240 fi
241
242 archiveBase=$( FindLocalArchiveFolder "$dbase" "Archive" "false" )
243 if [ -n "$archiveBase" ] && [ -d "$archiveBase" ]
244 then
245 echo "Deleting Archive" >&2
246 target="$archiveBase"
247 find "$target" -name "*.xml.gz" -delete
248
249 wait
250
251 cd "$archiveBase"
252 for dir in "$archiveBase"/*
253 do
254 if [ -d "$dir" ] && [ "$dir" != "Sentinels" ]
255 then
256 rm -rf "$dir" &
257 fi
258 done
259 fi
260
261 sentinelsBase=$( echo "${archiveBase}/Sentinels" )
262 if [ -n "$sentinelsBase" ] && [ -d "$sentinelsBase" ]
263 then
264 echo "Deleting Sentinels" >&2
265 target="$sentinelsBase"
266 rm -rf "*.snt"
267 fi
268
269 rm -f versioned.uid
270
271 if [ ! -d "$sentinelsBase" ] && [ "$erase" = false ] && [ "$zap" = false ]
272 then
273 mkdir -p "$sentinelsBase"
274 fi
275
276 sleep 1
277 fi
278
279 wait
280
281 if [ "$erase" = true ]
282 then
283 extrasBase=$( FindLocalArchiveFolder "$dbase" "Extras" "false" )
284 if [ -n "$extrasBase" ] && [ -d "$extrasBase" ]
285 then
286 echo "Deleting Extras" >&2
287 cd "$extrasBase"
288 rm -rf *
289 fi
290
291 sleep 1
292 fi
293
294 wait
295
296 if [ "$zap" = true ]
297 then
298 for dir in Archive Data Postings Extras Index Invert Merged Scratch
299 do
300 target=$( FindLocalArchiveFolder "$dbase" "$dir" "false" )
301 # remove trailing slash
302 target=${target%/}
303 # remove trailing folder to get volume name
304 volume=${target%/"${dbase}"}
305 echo "$volume"
306 done |
307 sort -f | uniq -i |
308 while read dir
309 do
310 if [ -n "$dir" ] && [ -d "$dir" ]
311 then
312 echo "Deleting $dir" >&2
313 cd "$dir"
314 rm -rf *
315 fi
316 done
317
318 sleep 1
319 fi
320
321 wait
322
323 function PrintTotalElapsedTime {
324 local L=$1
325 local T=$2
326 local D=$((T/60/60/24))
327 local H=$((T/60/60%24))
328 local M=$((T/60%60))
329 local S=$((T%60))
330 printf '%s %d second' "$L" $T 1>&2
331 (( $T > 1 )) && printf 's' 1>&2
332 if [ "$T" -gt 59 ]
333 then
334 printf ', or' 1>&2
335 (( $D > 0 )) && printf ' %d day' $D 1>&2
336 (( $D > 1 )) && printf 's' 1>&2
337 (( $H > 0 )) && printf ' %d hour' $H 1>&2
338 (( $H > 1 )) && printf 's' 1>&2
339 (( $M > 0 )) && printf ' %d minute' $M 1>&2
340 (( $M > 1 )) && printf 's' 1>&2
341 (( $S > 0 )) && printf ' %d second' $S 1>&2
342 (( $S > 1 )) && printf 's' 1>&2
343 fi
344 printf '\n' 1>&2
345 }
346
347 total_end=$(date "+%s")
348 total=$((total_end - total_start))
349 TOT=$total
350 PrintTotalElapsedTime "TOT" "$TOT"
351 echo "" >&2
352 exit 0