-
Notifications
You must be signed in to change notification settings - Fork 1
/
lcg-rec-del
executable file
·274 lines (259 loc) · 5.89 KB
/
lcg-rec-del
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
#!/bin/sh
#
# lcg-rec-del - Riccardo Bruno 20070725 <riccardo.bruno@ct.infn.it>
#
# Recursive lcg del command; remove files from catalog and related replicas
#
# Changelog
#
# 20101030 Options: dryrun added
#
#lcg_cp_prepare_params
lcg_cp_prepare_params()
{
PARAMS=
if [ -n "${LFCRECDL_VO}" ]
then
PARAMS=$PARAMS" --vo "$LFCRECDL_VO
fi
if [ -n "${LFCRECDL_CP}" ]
then
PARAMS=$PARAMS" lfn:"$DIRPATH/$file
fi
if [ -n "${LFCRECDL_FC}" ]
then
export LFC_HOST=$LFCRECDL_FC
fi
}
#parse arguments
parse_arguments()
{
LFCRECDL_VO=
LFCRECDL_FC=
LFCRECDL_CP=
LFCRECDL_V=
LFCRECDL_DR=
LFCRECDL_TB=
NEEDHELP="N"
PARAM=
for opt in $ARGS
do
OPTSIGN=$(echo ${opt} | awk '{ print substr($1,1,1) }')
if [ -n "${PARAM}" -a "${OPTSIGN}" != "-" ]
then
case $PARAM in
"TB" ) LFCRECDL_TB=$opt ;;
"VO" ) LFCRECDL_VO=$opt ;;
"CP" ) LFCRECDL_CP=$opt ;;
"FC" ) LFCRECDL_FC=$opt ;;
esac
PARAM=
fi
case $opt in
"--tb" | "-tb" ) PARAM="TB" ;;
"--vo" | "-vo" ) PARAM="VO" ;;
"--cp" | "-cp" ) PARAM="CP" ;;
"--fc" | "-fc" ) PARAM="FC" ;;
"--dryrun" | "-dr" ) LFCRECDL_DR="Y" ;;
"--help" | "-h" ) NEEDHELP="Y" ;;
"--verbose" | "-v" ) LFCRECDL_V="Y" ;;
esac
done
}
show_usage()
{
echo "lcg-rec-del"
echo "----------"
echo "LCG utils based recursive delete from file catalog utility and related replicas"
echo " "
echo "Usage: `basename $0` -vo <voname> -cp <catalog_path> [-fc <file_catalog>] [-v] [-dr]"
echo " "
echo " --vo | -vo Virtual Organization"
echo " --cp | -cp Catalog Path"
echo " --fc | -fc File Catalog"
echo " --verbose | -v Shows info and commands executed"
echo " --dryrun | -dr View only commands without executing them"
echo " --help | -h Shows this help"
echo " "
}
#check arguments
check_arguments()
{
if [ "${NEEDHELP}" != "Y" ]
then
ERROR=
NEWLN="\n"
TABSP="\t"
if [ -z "$LFCRECDL_TB" ]
then
LFCRECDL_TB=0
fi
if [ -z "${LFCRECDL_FC}" ]
then
if [ -n "${LFC_HOST}" ]
then
if [ -n "${LFCRECDL_V}" ]
then
ERROR=$ERROR$TABSP"Warning: Defafault LFC server: '"$LFC_HOST"' will be used"$NEWLN
fi
else
ERROR=$ERROR$TABSP"LFC_HOST environment variable or -fc option must be specified with a LFC host server"$NEWLN
NEEDHELP="Y"
fi
fi
if [ -z "${LFCRECDL_VO}" ]
then
ERROR=$ERROR$TABSP"you must specify a virtual organization"$NEWLN
NEEDHELP="Y"
fi
if [ -z "${LFCRECDL_CP}" ]
then
ERROR=$ERROR$TABSP"you must specify a catalog path"$NEWLN
NEEDHELP="Y"
fi
if [ -n "${ERROR}" -a $LFCRECDL_TB -eq 0 ]
then
echo "Errors:"
echo -e $ERROR
fi
fi
}
calc_indlevel()
{
LNTABS=
if [ $LFCRECDL_TB -gt 0 ]
then
for t in $(seq 1 $LFCRECDL_TB)
do
LNTABS=$LNTABS"\t"
done
fi
}
#
# lcg-rec-del
#
#parse the command line
if [ $# -ne 0 ]
then
ARGS=$*
parse_arguments
check_arguments
else
NEEDHELP="Y"
fi
#view verbose summary info
if [ -n "${LFCRECDL_V}" -a "${LFCRECDL_TB}" == "0" ]
then
echo "lcg-rec-del"
echo "----------"
echo "VO : "$LFCRECDL_VO
echo "LFC : "$LFCRECDL_FC
echo "Catalog path: "$LFCRECDL_CP
DRMODE="off"
if [ -n "${LFCRECDL_DR}" ]
then
DRMODE="on"
fi
echo "Dry run : "$DRMODE
echo " "
fi
#view help or execute
if [ $NEEDHELP == "Y" ]
then
show_usage
else
if [ -n "${LFCRECDL_DR}" ]
then
LFCRECDL_V="Y"
fi
#calculate indirection level
calc_indlevel
#directory or file
FLAG=$(lfc-ls -ld ${LFCRECDL_CP} | awk '{ print substr($1,1,1) }')
if [ "${FLAG}" == "d" ]
then
#directory name
DIRPATH=$(echo ${LFCRECDL_CP} | sed 's/\/\//\//g')
#process files before
FILES=$(lfc-ls -l ${LFCRECDL_CP} | awk '{ if(substr($1,1,1)!="d" && substr($1,1,1)!="l") print $9 }')
for file in $FILES
do
#prepare PARAMS for lfc-cr
lcg_cp_prepare_params
if [ -n "${LFCRECDL_V}" ]
then
echo -e $LNTABS"lcg-del -a "$PARAMS
fi
#
# lcg-del
#
if [ ! -n "${LFCRECDL_DR}" ]
then
lcg-del -a $PARAMS
fi
done
#delete sub-directories then
SUBDIRS=$(lfc-ls -l ${LFCRECDL_CP} | awk '{ if(substr($1,1,1)=="d") print $9 }')
if [ -n "$SUBDIRS" ]
then
LFCRECDL_TB=$(($LFCRECDL_TB+1))
fi
for dir in $SUBDIRS
do
#prepare arguments for the recursive call to $0
VOPT=
if [ -n "${LFCRECDL_V}" ]
then
VOPT="-v"
fi
FCOPT=
if [ -n "${LFCRECDL_FC}" ]
then
FCOPT="-fc "$LFCRECDL_FC
fi
if [ -n "${LFCRECDL_DR}" ]
then
DROPT="-dr"
fi
CATALOGPATH=$(echo ${LFCRECDL_CP}/$dir | sed 's/\/\//\//g')
LOCALPATH=$(echo ${DIRPATH}/$dir | sed 's/\/\//\//g')
#recursive call to $0
$0 -vo $LFCRECDL_VO $VOPT $FCOPT $SEOPT $DROPT -lp $LOCALPATH -cp $CATALOGPATH -tb $LFCRECDL_TB
done
# delete current dir
#prepare PARAMS for lfc-cr
file=
lcg_cp_prepare_params
if [ -n "${LFCRECDL_V}" ]
then
echo -e $LNTABS"lfc-rm -r "$DIRPATH
fi
#
# lfc-rm
#
if [ ! -n "${LFCRECDL_DR}" ]
then
lfc-rm -r $DIRPATH
fi
elif [ "${FLAG}" == "-" ]
then
# It's a file
#file name
FILEPATH=$(echo ${LFCRECDL_CP} | sed 's/\/\//\//g')
file=$FILEPATH
lcg_cp_prepare_params
if [ -n "${LFCRECDL_V}" ]
then
echo -e $LNTABS"lcg-del -a "$PARAMS
fi
#
# lcg-del
#
if [ ! -n "${LFCRECDL_DR}" ]
then
lcg-del -a $PARAMS
fi
else
echo "Error: The local path '"$LFCRECDL_LP"' is not valid"
fi
fi