forked from CMSROMA/Utilities
-
Notifications
You must be signed in to change notification settings - Fork 2
/
makefilelist.sh
executable file
·269 lines (223 loc) · 6.62 KB
/
makefilelist.sh
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
#!/bin/bash
# author: Shervin
# date: 11/04/2010
# version 2.0
# questo script crea un file con una lista di root file contenuti in una directory
# escludento certi pattern di file indicati dal file exclude in filelist/
# con l'opzione batch crea un chain separata per ogni file
# sta
# se la directory e' su castor usa rfdir per individuare i file
# altrimenti usa ls -l
filelist_dir=./filelist
#exclude="nEle"
#exclude="-f $filelist_dir/exclude"
#if [ ! -r "$filelist_dir/exclude" ]; then
# echo "Warning: file $filelist_dir/exclude not found or not readable" >> /dev/stderr
# if [ ! -r "$HOME/bin/find" ]; then
# echo " using $HOME/bin/find instead" >> /dev/stderr
# else
# exclude="nEle"
# fi
#fi
# in batch mode il puntatore alla chain si chiama: batch_chain
usage(){
echo "`basename $0` [option] sample directory "
echo "--------- optional"
echo " -g, --grep arg: merge files matching the argument"
# tree_name e' il nome del tree dentro i root-file
# chain_name e' il nome che voglio dare alla chain
# directory e' il path in cui si trovano i root-file
# con l'opzione batch la lista dei file viene spacchettata in piu'
# chain, e il nome della chain e' batch_chain
}
get_par(){
echo $1 | cut -d '=' -f 2
}
recursive=no
#------------------------------ parsing
# options may be followed by one colon to indicate they have a required argument
if ! options=$(getopt -u -o hacrpg:lf: -l help,grep: -- "$@")
then
# something went wrong, getopt will put out an error message for us
exit 1
fi
set -- $options
while [ $# -gt 0 ]
do
case $1 in
-h|--help) usage; exit 0;;
-a)
echo "[OPTION] making filelist adding directory, if it is the same dir a new file will be created"
add=yes
;;
-c) export CONTINUE="yes";;
-r) echo "recursive"; recursive=yes;;
-g|--grep ) echo "[GREP OPTION]: $2"; GREP=$2; shift;;
-p) # configure python filelist for cmsRun
PYTHON_CFG=yes
;;
-l) LIST_OPT="true";;
-f) echo "[OPTION] Filelist directory: $2"; filelist_dir=$2; shift;;
(--) shift; break;;
(-*) echo "$0: error - unrecognized option $1" 1>&2; usage >> /dev/stderr; exit 1;;
(*) break;;
esac
shift
done
case $# in
2)
;;
3)
case $3 in
local)
local=_local
;;
*)
echo "Error. $input parameters" >> /dev/stderr
usage
exit 1
;;
esac
;;
*)
echo "[ERROR] Wrong number of parameters: $#"
echo "Usage:"
usage
exit 1
;;
esac
sample=$1
dir=$2
# se non esiste la cartella di destinazione la creo
if [ ! -e $filelist_dir ];then
mkdir -p $filelist_dir
fi
new_file=$filelist_dir/$sample.list$local
#rinomino il vecchio file per sicurezza
if [ -n "$CONTINUE" -a -e "$new_filel" ]; then
echo "File exists: CONTINUE" > /dev/stdout
exit 0
fi
if [ -z "$add" -a -e "$new_file" ]; then
echo "[ERROR] file $new_file exist" >> /dev/stderr
echo " Try to remove it!" >> /dev/stderr
exit 1;
# mv $filelist_dir/$sample.list $filelist_dir/$sample.list-old
fi
# se prendo i root file da castor cerco il comando rfdir
# if [ "`echo $dir | grep -c castor`" != "0" ]; then
case $dir in
*castor*)
if [ "`whereis rfdir | cut -d ':' -f 2 | wc -w`" = "0" ];then
echo -n "Error. " >> /dev/stderr
echo "Command rfdir not found" >> /dev/stderr
exit 1
fi
dir_command='rfdir'
;;
root://*eos*)
echo "[INFO] Files on xrootd server"
dir_command='eos ls -l'
file_prefix="root://eoscms.cern.ch/"
dir=`echo $dir | sed 's|root://eoscms.*//eos|/eos|'`
;;
/store/*)
dir_command='eos ls -l'
file_prefix="root://eoscms.cern.ch/"
dir_prefix="/eos/cms"
;;
*eos*)
dir_command='eos ls -l'
file_prefix="root://eoscms.cern.ch/"
;;
*)
dir_command='ls -l'
;;
esac
#fi
dir=$dir_prefix$dir
filelist_=(`$dir_command $dir | sed '/^d/ d' | grep '.root' | awk '(NF!=0){print $9}'`)
dirlist=(`$dir_command $dir | sed '/^-/ d' | awk '(NF!=0){print $9}'`)
if [ -n "$LIST_OPT" ];then
echo "${dirlist[@]}"
exit 0
fi
if [ "${#filelist_[@]}" = "0" ]; then
echo "No files in the directory" >> /dev/stderr
echo "List of subdirectories:" >> /dev/stderr
for i in ${dirlist[@]}; do
echo " - $i" >> /dev/stderr
done
if [ "$recursive" = "no" ]; then
exit 1;
fi
for i in ${dirlist[@]}; do
makefilelist.sh -a -r $sample-$i $dir/$i
done
exit 0
fi
# elimino le directory, prendo i rootfile, ne prendo il nome, ci metto aggiungo il path
if [ -z "${GREP}" ]; then
$dir_command $dir | sed "/^d/ d" | grep '.root' | awk '(NF!=0){print $9,$5}' | sed "s|^|$file_prefix$dir/|" >> $new_file.1
else
$dir_command $dir | sed "/^d/ d" | grep -e "${GREP}.*root" | awk '(NF!=0){print $9,$5}' | sed "s|^|$file_prefix$dir/|" >> $new_file.1
fi
#else
# echo "file list reading"
# filelist_=(`cat $dir | sed 's|rfio:/||'`);
# dir=""
#fi
#if [ ! -e $filelist_dir/$sample.blacklist ]; then
# touch $filelist_dir/$sample.blacklist
#fi
#cat $new_file.1 | sed '/^$/ d' |sort > $new_file.2
for dir in $HOME/bin $PWD/bin `echo $PATH | sed 's|:| |g'`
do
if [ -e $dir/remove_dup.awk ];then
REMDUP=$dir/remove_dup.awk
break
fi
done
if [ -z "$REMDUP" ];then
echo "[ERROR] remove_dup.awk not found in $HOME/bin $PWD/bin $PATH" >> /dev/stderr
echo "[ ] `echo $PATH | sed 's|:| |g'`"
exit 1
fi
awk -f $REMDUP $new_file.1 | \
sed '/^$/ d;s|\t.*$||;s| .*||' |sort > $new_file.2
cp $new_file.1 $new_file.dump
# rimuovo i duplicati di castor, tolgo le righe vuote, tolgo i file da escludere
if [ -n "$add" ];then
if [ ! -e "${new_file}" ];then
cp ${new_file}.2 ${new_file}
else
# echo "Adding old filelist to the new one to remove duplicates"
# echo ${new_file}.2 $new_file
diff $new_file.2 $new_file > $new_file.diff
if [ "`cat $new_file.diff | wc -l `" == "0" ];then
rm $new_file.diff $new_file.1 $new_file.2
exit 0
fi
fi
if [ "`grep -c '<' $new_file.diff`" != "0" ]; then
echo "[WARNING] Missing file for sample $sample!"
echo " Look at $new_file.missingDiff"
grep '<' $new_file.diff > $new_file.missingDiff
fi
grep '<' $new_file.diff > $new_file.diff2
sed '/</ d; s|> ||' $new_file.diff2 > $new_file.diff
rm $new_file.diff2
cat $new_file.diff >> $new_file
rm $new_file.2
exit 1
else
mv $new_file.2 $new_file
fi
rm $new_file.1
#sortCrabFilelist.sh $new_file #|| exit 2
if [ -n "$PYTHON_CFG" ]; then
echo 'myfilelist.extend( [' > $filelist_dir/$sample'_cff.py'
sed "s|^| \'rfio:\/\/|; s|$|\',|" $new_file >> $filelist_dir/$sample'_cff.py'
echo "] )" >> $filelist_dir/$sample'_cff.py'
fi
exit 0