-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmiri_cdp_dhas.bash.php
207 lines (182 loc) · 4.79 KB
/
miri_cdp_dhas.bash.php
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
<?php
// miri_cdp_dhas.bash
// exports a bash file to download the CDP files
header ( "Content-Type: text/plain" );
header ( "Content-Disposition: attachment; filename=\"miri_cdp_dhas.bash\"" );
ini_set('max_execution_time', 600);
miri_cdp_dhas ();
function miri_cdp_dhas() {
$loginErrorCode = "";
$loginErrorText = "";
require_once 'common/entryexit/preludes.php';
global $ftp_server, $ftp_directory, $ftp_user, $ftp_password, $objCdp;
print "#!/bin/bash
#
# bash script to synchronize the CDPs between the miri ftp repository
# and your drive. If you have DHAS installed and thus the environment
# variable CDP_DIR defined, the CDPs will be put into the appropriate
# location for DHAS.
#
# After synchronization a md5sum check is run on all the files.
#
# miri_cdp.bash --help
# for help
#
# If you run into any problems email me [email protected]
#
function md5_check {
echo \"\"
echo \"Checking the MD5 hashes of the CDP files...\"
echo \"\"
failed=0";
// Here, we add all files which belong to the CDP releases.
$items = $objCdp->getFilesForDHASDelivery ();
foreach ( $items as $key ) {
echo "\n file=\"" . $key ['filename'] . "\"
if [[ -e \$file ]] ; then
md5v=`grep \"" . $key ['filename'] . "\" md5_miri_cdps | uniq`
if [ -n \"\$md5v\" ] ; then
md5v=`echo \$md5v | awk '{if(NF != 2){print \"0\"} else {print \$1}}'`
else
md5v=\"1\"
fi
if [ \"\$md5v\" == \"1\" ]; then
echo \"\$file NO MD5 HASH\"
else
if [ `md5_value \$file` != \$md5v ] ; then
echo \"\$file FAILED\"
failed=1
fi
fi
else
echo \"\$file does not exist\"
failed=1
fi";
}
echo "\n if [ \$failed == 1 ]; then
echo \"\"
echo \"Something has gone wrong in the transfer of these files.\"
echo \"Please remove FAILED files by hand and start this script again\"
echo \"\"
else
echo \"\"
echo \"All files are correct\"
echo \"\"
fi
}
function remove_old {
files=`ls MIRI_*.fits MRS_*`
for file in \$files
do
okay=`grep \$file masterlist_dhas.txt | wc -l`
if [ \$okay == 0 ] ; then
#echo \$file\" \"old
rm -f \$file
fi
done
}
function md5_value {
case \"\$OSTYPE\" in
\"linux\"*)
md5sum $1 | awk '{print $1}'
;;
\"darwin\"*)
md5 -q $1
;;
*)
echo \"null\"
;;
esac
}
while [ \"$1\" != \"\" ]
do
case $1 in
\"--test\")
test=1
;;
\"--check\")
check=1
;;
\"--remove_old\")
remove_old
exit
;;
\"--clean\")
remove_old
exit
;;
\"--help\")
echo \"\"
echo \" miri_cdp_dhas.bash [--check] [--remove_old] [--clean] [--test]\"
echo \"\"
echo \"when run without arguments it will sync all the delivered CDPs in a directory structure to be used with DHAS.\"
echo \" --check\"
echo \" for files in the delivery check if local files match those of the ftp\"
echo \" --remove_old\"
echo \" remove any CDP files not in the CDP deliveries\"
echo \" --clean\"
echo \" same as --remove_old\"
exit
;;
*)
esac
shift
done
which lftp &> /dev/null
case $? in
0)
#echo okay
;;
*)
echo \"lftp is either not installed or non existent, please fix this\"
echo \"E.g. on Mac OS X, install with 'fink install lftp'\"
echo \"on debian/ubuntu, install with 'sudo apt-get install lftp'\"
exit
esac
if [[ -z \$CDP_DIR ]]; then
cdpdir=\"\$HOME/MIRI/CDP\"
else
cdpdir=\"\$CDP_DIR\"
fi
mkdir -p \"\$cdpdir\"
cd \"\$cdpdir\"
echo \"\"
HOST=\"" . $ftp_user . ":" . $ftp_password . "@" . $ftp_server . "\"
LCD=\"\$cdpdir\"
RCD=\"$ftp_directory\"
lftp -c \"set ftp:list-options -a;
open \$HOST ;
lcd \$LCD ;
cd \$RCD ;
mirror --verbose \
--include-glob md5_miri_cdps\"
if [ \$check ] ; then
md5_check
exit
fi
echo \"Updating CDP files to \"\$cdpdir
echo \"Beware that this can take quite a long time\"
echo \"\"
echo \"set ftp:list-options -a\" > lftp_script
echo \"open \$HOST \" >> lftp_script
echo \"lcd \$LCD \" >> lftp_script
echo \"cd \$RCD \" >> lftp_script
echo \"mirror --verbose \\\\\" >> lftp_script";
// Here, we add all files which belong to the CDP releases.
$items = $objCdp->getFilesForDHASDelivery ();
foreach ( $items as $key ) {
echo "\necho \" --include-glob '" . $key ["filename"] . "' \\\\\" >> lftp_script";
}
echo "\necho \" --parallel\" >> lftp_script
lftp -f lftp_script
md5_check
# Remove all the md5_miri_cdps files.
cd \"\$cdpdir\"
find . -type f -name md5_miri_cdps -exec rm -f {} \\;
find . -type f -name lftp_script -exec rm -f {} \\;
echo \"\"
echo \"MIRI CDP synchronization finished\"
echo \"Files are located in \"\$cdpdir
echo \"\"";
}
?>