]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGLF/FORWARD/analysis2/scripts/libGridDownload.sh
Merge branch 'master' of https://git.cern.ch/reps/AliRoot
[u/mrichter/AliRoot.git] / PWGLF / FORWARD / analysis2 / scripts / libGridDownload.sh
1 #!/bin/bash
2 #
3 # Library of some download functions 
4
5 # --- Check AliEn token ----------------------------------------------
6 check_token()
7 {
8     uid=`id -u`
9     genv_file=/tmp/gclient_env_${uid}
10     
11     if test ! -f ${genv_file} ; then 
12         echo "No such file: ${genv_file}, please do alien-token-init" \
13             >/dev/stderr
14         exit 1
15     fi
16     . ${genv_file}
17     alien-token-info | grep -q "Token is still valid"
18     if test $? -ne 0 ; then 
19         echo "Token not valid, please re-new" > /dev/stderr 
20         exit 1
21     fi
22 }
23
24 # --- Diagnostics output ---------------------------------------------
25 verb=0
26 mess()
27 {
28     if test $1 -gt $verb ; then return ; fi 
29     shift
30     echo $*
31 }
32
33 # --- Handling of exit -----------------------------------------------
34 lock=
35 handle_exit()
36 {
37     if test "x$lock" = "x" ; then return ; fi 
38     rm -rf $lock 
39 }
40 trap handle_exit EXIT
41
42 # --- Handling of errors ---------------------------------------------
43 last=
44 handle_err()
45 {
46     echo "Error: $last" 
47     exit 1
48 }
49 enable_trap()
50 {
51     trap handle_err ERR
52 }
53 disable_trap()
54 {
55     trap - ERR
56 }
57
58 # --- Check the lock -------------------------------------------------
59 check_lock()
60 {
61     local loc=$1 
62     lock=$loc/.lock
63     local nam=$2 
64
65     if test -f $lock ; then 
66         echo "Another $nam process is already running:" > /dev/stderr 
67         echo "Content of ${lock}:" > /dev/stderr 
68         cat $lock > /dev/stderr 
69         trap - EXIT
70     exit 1
71     local now=`date` 
72     cat <<EOF > $lock
73 Process: $$
74 User:    $USER
75 Start:   $now
76 EOF
77     fi
78 }
79
80 # --- Append path element --------------------------------------------
81 append_to_path()
82 {
83     local tmp=$1 ; shift 
84     local add=$1
85     if test "x$tmp" != "x" ; then tmp="${tmp}/" ; fi 
86     echo ${tmp}${add}
87 }
88
89 # --- Get list of files ----------------------------------------------
90 # Output is stored in .list
91 _get_file_list()
92 {
93     local path=$1
94     local search=$2
95     local maxf=$3
96     if test x$maxf = x ; then maxf=-1 ; fi
97     
98     mess 1 "Getting list of files from AliEn - can take minutes - be patient"
99     mess 2 "alien_find ${path} ${search}"
100     files=`alien_find ${path} ${search} | grep -v "\(does not\|files found\)" 2>> ${redir}` 
101     rm -f .list
102     for i in $files ; do 
103         echo $i >> .list 
104         let numf=$numf+1
105     done 
106     mess 1 -n "Total of $numf files ... "
107     if test $maxf -lt 0 ; then 
108         mess 1 "using all" 
109     else
110         mess 1 "using $maxf first of these"
111     fi
112 }
113 # --- Change permissions on files ------------------------------------
114 fix_perm()
115 {
116     if test ! -f $1 ; then return ; fi 
117     chmod g+rwX $1
118     chmod o+rX $1
119 }
120 # --- Download a single file -----------------------------------------
121 #  $1: Source file
122 #  $2: Output directory
123 #  $3: Number
124 #  $4: Current count
125 #  $5: Maximum
126 #  $6: redirect
127 _download_file()
128 {
129     local source=$1 ; shift 
130     local store=$1  ; shift 
131     local r=$1      ; shift
132     local cur=$1    ; shift
133     local max=$1    ; shift
134     local redir=$1  ; shift
135     local noact=$1  ; shift
136     local o=${store}/ 
137     case $file in 
138         *.root)
139             o=${o}`basename $file .root`_`printf %04d ${r}`.root 
140             ;;
141         *.zip)
142             local d=`printf %04d ${r}` 
143             mkdir -p ${o}/${d}
144             o=${o}/${d}/${file}
145             ;;
146     esac
147     printf "%4d/%4d: %20s -> %20s ..." $cur $max $source $o 
148
149     mess 2 -n "$source -> $o ... "
150     if test -f $o ; then 
151         printf "exists\n"
152         # mess 2 "exists" 
153         # sleep 1
154     else
155         printf "copying\n"
156         # mess 2 -n "copying ... " 
157         if test $noact -lt 1 ; then 
158             alien_cp alien:${source} file:${o} >> ${redir} 2>&1 
159             fix_perm $o 
160         else 
161             sleep 1
162         fi
163         mess 2 "done"
164     fi
165     if test $noact -gt 0 ; then return 0 ; fi 
166     if test ! -f $o ; then return 1 ; fi 
167         
168
169     case $o in 
170         *.root) 
171             check_file ${o} 
172             local ret=$? 
173             case $ret in 
174                 0|2) ;; 
175                 1|3|4|5|6) return 2 ;; 
176             esac
177             ;;
178         *.zip)
179             d=`dirname $o` ;
180             b=`basename $o` ; 
181             mess 3 "Unzipping $b in $d"
182             if ! unzip -n -qq -l $o > /dev/null 2>&1 ; then
183                 mess 1 "Bad zip file: $o" 
184                 rm -rf $d 
185             fi
186             # (cd $d && unzip -n -qq $b)
187             ;;
188     esac
189     # analyse_file ${o}
190
191     return 0
192 }
193
194 # --- Submit run analysis to background ------------------------------
195 # $1: Output directory
196 # $2: Sarting off-set
197 # $3: Maximum jobs
198 # $4: Maximum number of files
199 # $5: noact 
200 submit_jobs()
201 {
202     local out=$1 ; shift
203     local sta=$1 ; shift 
204     local max=$1 ; shift
205     local maxf=$1 ; shift
206     local noact=$1 ; shift
207     
208     local joblist=
209     local counter=0
210     mess 5 "Submitting $maxjobs jobs from $sta/$maxf" 
211     for i in $@ ; do 
212         let cur=$sta+$counter
213
214         local b=`echo $i | sed -e "s,${path},,"` 
215         local r=`echo $b | sed -e "s,/.*,,"` 
216
217
218         let counter=$counter+1
219
220         download_file $i $out $cur $maxf $noact&
221         j=`jobs %% | sed -e 's/^[^0-9]*//' -e 's/[^0-9]*$//'` 
222         joblist="$joblist $j"
223     done
224     
225     counter=0
226     mess 5 "will wait for jobs $joblist"
227     for i in $joblist ; do 
228         mess 5 "waiting for $i of $joblist"
229         wait %$i
230         let counter=$counter+1
231     done
232 }