[Bio] / FigKernelScripts / install-data.pl Repository:
ViewVC logotype

Annotation of /FigKernelScripts/install-data.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.8 - (view) (download) (as text)

1 : olson 1.7 #
2 :     # Copyright (c) 2003-2006 University of Chicago and Fellowship
3 :     # for Interpretations of Genomes. All Rights Reserved.
4 :     #
5 :     # This file is part of the SEED Toolkit.
6 :     #
7 :     # The SEED Toolkit is free software. You can redistribute
8 :     # it and/or modify it under the terms of the SEED Toolkit
9 :     # Public License.
10 :     #
11 :     # You should have received a copy of the SEED Toolkit Public License
12 :     # along with this program; if not write to the University of Chicago
13 :     # at info@ci.uchicago.edu or the Fellowship for Interpretation of
14 :     # Genomes at veronika@thefig.info or download a copy from
15 :     # http://www.theseed.org/LICENSE.TXT.
16 :     #
17 :    
18 : olson 1.4
19 : olson 1.1 use FIG;
20 : olson 1.4 use strict;
21 : olson 1.6 use File::Spec::Functions 'rel2abs';
22 :     use Text::Wrap;
23 :     use File::Copy;
24 :     use File::Basename;
25 :     use File::Path;
26 : olson 1.1
27 :     #
28 :     # Install the contents of a data disk.
29 :     #
30 :    
31 :     if (@ARGV != 1)
32 :     {
33 :     die "Usage: install-data <data-disk-directory>\n";
34 :     }
35 :    
36 : olson 1.6
37 :    
38 : olson 1.1 my($data_dir) = @ARGV;
39 :    
40 : olson 1.6 $data_dir = rel2abs($data_dir);
41 :    
42 : olson 1.1 #
43 :     # First check that the data directory is valid.
44 :     #
45 :    
46 :     if (! -d $data_dir or
47 :     ! -f "$data_dir/RELEASE" or
48 :     ! -f "$data_dir/MANIFEST")
49 :     {
50 :     die "Data directory $data_dir is not a valid data release disk.\n";
51 :     }
52 :    
53 :     #
54 :     # Read the release information.
55 :     #
56 :    
57 : olson 1.6 my @r = read_release_file("$data_dir/RELEASE");
58 : olson 1.1 my($d_name, $d_id, $d_institution, $d_contact, $d_superset_id, @d_info) = @r;
59 :    
60 :     opendir(D, $data_dir) or die "Cannot open data directory $data_dir: $!\n";
61 :    
62 : olson 1.8 my @disk_tags = grep { /^DISK_\d+_OF_\d+$/ } readdir(D);
63 : olson 1.1
64 :     @disk_tags == 0 and die "Disk tag file DISK_X_OF_Y not found in $data_dir\n";
65 :     @disk_tags > 1 and die "Multiple disk tag file files @disk_tags found in $data_dir\n";
66 :    
67 :     my($this_disk, $num_disks) = $disk_tags[0] =~ /DISK_(\d+)_OF_(\d+)/;
68 :    
69 : olson 1.6 #
70 :     # At this point we have a valid data disk.
71 :     #
72 :     # Determine if we are in mid-install already. This is determined by the existence
73 :     # of a $FIG_Config::fig/DataInstall directory.
74 :     #
75 :     # If the directory already exists, we assume we are mid-install. Check the file
76 :     # DataInstall/INSTALL_RELEASE to see if it corresponds to the release we
77 :     # are now installing. If it does not, bail out, and give the user some idea
78 :     # of what is going on.
79 :     #
80 :     # If the directory doesn't exist, create it, and write the release info
81 :     # from this diesk to INSTALL_RELEASE.
82 :     #
83 :    
84 :     my $install_dir = "$FIG_Config::fig/DataInstall";
85 :    
86 :     if (-d $install_dir)
87 :     {
88 :     if (-f "$install_dir/INSTALL_RELEASE")
89 :     {
90 :     my @r = read_release_file("$install_dir/INSTALL_RELEASE");
91 :     my($r_name, $r_id, $r_institution, $r_contact, $r_superset_id, @r_info) = @r;
92 :    
93 :     if ($r_id ne $d_id)
94 :     {
95 :     bad_release($d_name, $r_name);
96 :     }
97 :     #
98 :     # If we get here, we're okay.
99 :     #
100 :     }
101 :     else
102 :     {
103 :     bad_inst_dir();
104 :     }
105 :     }
106 :     else
107 :     {
108 :     #
109 :     # We're just starting the install.
110 :     #
111 :     # Create the staging directory, and copy the release info into it.
112 :     #
113 :    
114 :     mkdir $install_dir or die "Cannot mkdir $install_dir: $!\n";
115 :    
116 :     copy("$data_dir/RELEASE", "$install_dir/INSTALL_RELEASE");
117 :     }
118 :    
119 :     #
120 :     # To do: check to see if we've already installed this disk. Skip this for now.
121 :     #
122 :    
123 : olson 1.2 print "Installing data release \"$d_name\". Disk $this_disk of $num_disks\n";
124 : olson 1.1
125 :     #
126 : olson 1.6 # We untar into $install_dir/Stage
127 : olson 1.1 #
128 :    
129 : olson 1.6 my $target_dir = "$install_dir/Stage";
130 : olson 1.1 &FIG::verify_dir($target_dir);
131 :    
132 :     chdir $target_dir or die "Could not chdir $target_dir: $!\n";
133 :    
134 :     open(M, "<$data_dir/MANIFEST") or die "Cannot open manifest $data_dir/MANIFEST: $!\n";
135 :    
136 :     while (<M>)
137 :     {
138 :     chomp;
139 :    
140 : olson 1.2 my($cksum, $size, $file) = split(/\s+/);
141 :     print "cksum=$cksum size=$size file=$file\n";
142 : olson 1.1
143 :     my $path = "$data_dir/$file";
144 :    
145 :     -f $path or die "File $path missing from disk.\n";
146 :    
147 :     my $psize = -s $path;
148 :     $psize == $size or die "File $path is of the wrong size (is $psize, should be $size\n";
149 :    
150 :     #
151 :     # Skip checksum check for now.
152 :     #
153 :    
154 : olson 1.4
155 :     #
156 :     # We only handle data files.
157 :     #
158 :    
159 :     $file =~ /^data\.\d+\.tgz/ or next;
160 :    
161 : olson 1.1 #
162 :     # Untar file.
163 :     #
164 :    
165 : olson 1.4 if (system("tar -z -x -v -f $path") != 0)
166 : olson 1.3 {
167 : olson 1.4 die "Untar of $path failed\n";
168 : olson 1.3 }
169 : olson 1.6 }
170 :    
171 :     #
172 :     # Install complete.
173 :     #
174 :     # Create the "INSTALLED_X_OF_Y" file, and see if we have installed all disks.
175 :     #
176 :    
177 :     my $fn = sprintf("$install_dir/INSTALLED_%s_OF_%s", $this_disk, $num_disks);
178 :     open(F, ">$fn") or die "Cannot create $fn: $!\n";
179 :     close(F);
180 :    
181 :     #
182 :     # Check to see if all are done.
183 :     #
184 :    
185 :     my $more_needed = 0;
186 :    
187 :     for (my $i = 1; $i <= $num_disks; $i++)
188 :     {
189 :     my $fn = sprintf("$install_dir/INSTALLED_%s_OF_%s", $i, $num_disks);
190 :     if (-f $fn)
191 :     {
192 :     print "Disk $i is installed.\n";
193 :     }
194 :     else
195 :     {
196 :     print "Disk $i is not installed.\n";
197 :     $more_needed++;
198 :     }
199 :     }
200 :    
201 :     if ($more_needed > 0)
202 :     {
203 :     print "Finished installing disk $this_disk. $more_needed disks need to be installed.\n";
204 :     exit;
205 :     }
206 :    
207 :     #
208 :     # We've installed them all.
209 :     #
210 :    
211 :     print "All disks have been installed. Setting up SEED data directory.\n";
212 :    
213 :     #
214 :     # First determine what the data directory looks like. It should be
215 :     # a directory that contains directories Organisms, NR, Sims, etc.
216 :     #
217 :    
218 :     opendir(D, "$target_dir") or die "Cannot open $target_dir: $!\n";
219 :     my @f = grep (!/^\./, readdir(D));
220 :     print "$target_dir has files @f\n";
221 :    
222 :     #
223 :     # Take the easy case first.
224 :     #
225 :    
226 :     my $installed_data;
227 :    
228 :     if (@f == 1)
229 :     {
230 :     #
231 :     # This should be our data path.
232 :     #
233 :    
234 :     my $dp = "$target_dir/$f[0]";
235 : olson 1.8 if (-d "$dp/Organisms" and -d "$dp/Global")
236 : olson 1.6 {
237 :     print "$dp appears to be a valid Data directory\n";
238 :     $installed_data = $dp;
239 :     }
240 :     else
241 :     {
242 :     print "$dp is not a valid Data directory\n";
243 :     }
244 :     }
245 :    
246 :     if (!$installed_data)
247 :     {
248 :     bad_data_format($target_dir);
249 :     }
250 :    
251 :     #
252 :     # Now, $installed_data is our data directory. See if a
253 :     # directory of that name is present in the fig directory.
254 :     #
255 :    
256 :     my $data_basename = basename($installed_data);
257 :    
258 :     if (-e "$FIG_Config::fig/$data_basename")
259 :     {
260 :     #
261 :     # It already exists. Find a new name to call it.
262 :     #
263 :    
264 :     my $idx = 0;
265 :     while (1)
266 :     {
267 :     my $fn = sprintf("Data.%03d", $idx);
268 :     if (! -e "$FIG_Config::fig/$fn")
269 :     {
270 :     $data_basename = $fn;
271 :     print "Picking $data_basename for the new data\n";
272 :     last;
273 :     }
274 :     $idx++;
275 :     }
276 :     }
277 :    
278 :     print "Moving data to $FIG_Config::fig/$data_basename\n";
279 :    
280 :     rename($installed_data, "$FIG_Config::fig/$data_basename");
281 :    
282 :     #
283 :     # Check if fig/Data is a symlink that we can redirect.
284 :     # We don't have to do this if the data directory started
285 :     # out being called Data.
286 :     #
287 :    
288 :     if ($data_basename ne "Data")
289 :     {
290 :    
291 :     my $data_link = "$FIG_Config::data";
292 :    
293 :     if (-l $data_link)
294 :     {
295 :     my $targ = readlink($data_link);
296 :     print "$FIG_Config::data is a symlink pointing to $targ. Not changing this link\n";
297 :     print "To use the newly installed data, run the following commands:\n";
298 :     print "\trm $FIG_Config::data\n";
299 :     print "\tln -s $data_basename $FIG_Config::data\n";
300 :     }
301 :     elsif (-d $data_link)
302 :     {
303 :     my $new;
304 :     $new = "$FIG_Config::fig/Data." . int(time);
305 : olson 1.3
306 : olson 1.6 print "$FIG_Config::data is a directory. It will\n";
307 :     print "be have to moved out of the way to use the newly-installed data.\n";
308 :     print "To do this, run the following commands:\n";
309 :     print "\tmv $FIG_Config::data $new\n";
310 :     print "\tln -s $data_basename $FIG_Config::data\n";
311 :     }
312 :     else
313 :     {
314 :     symlink($data_basename, $FIG_Config::data);
315 :     }
316 :     }
317 :    
318 :     print "Installation succeded.\n";
319 :    
320 :     #
321 :     # Remove the staging directory.
322 :     #
323 :    
324 :     rmtree($install_dir);
325 :    
326 :     #
327 :     # Read a release info file.
328 :     #
329 :     sub read_release_file
330 :     {
331 :     my($file) = @_;
332 :    
333 :     open(my $fh, "<$file") or die "Cannot open data release file $file: $!\n";
334 :    
335 :     my @r = <$fh>;
336 :     chomp(@r);
337 :     map { s/\r//g; } @r;
338 :    
339 :     if (@r < 5)
340 :     {
341 :     die "Invalid data release file $file: Not enough lines.\n";
342 :     }
343 :    
344 :     return @r;
345 :     }
346 :    
347 :     sub bad_release
348 :     {
349 :     my($d_name, $r_name) = @_;
350 :     die join("\n", '', fill('', '', <<END), '', '')
351 :     The data on the installation media ($data_dir)
352 :     is from a different release ($d_name)
353 :     than the data present in the installation temporary directory
354 :     $FIG_Config::fig/DataInstall ($r_name). The installation temporary directory
355 :     may be from a different disk set, or you have mounted an incorrect data disk.
356 :     END
357 :     }
358 :    
359 :     sub bad_inst_dir
360 :     {
361 :     die join("\n", '', fill('', '', <<END), '', '')
362 :     The installation temporary directory $FIG_Config::fig/DataInstall exists,
363 :     but does not have what we expect - the INSTALL_RELEASE file is missing.
364 :    
365 :     If there was a problem with an earlier attempt at installation, try
366 :     removing the directory with this command:
367 :    
368 :     rm -f $FIG_Config::fig/DataInstall
369 :    
370 :     and retrying the install.
371 :     END
372 : olson 1.1 }
373 :    
374 : olson 1.6 sub bad_data_format
375 :     {
376 :     my($dir) = @_;
377 : olson 1.1
378 : olson 1.6 die <<END;
379 :     The data as extracted to $dir
380 :     is not recognizable as a valid SEED data directory. It
381 :     may correct, but not organized properly for this script
382 :     to understand. If you wish, you can peruse the extracted data
383 :     in the directory
384 :    
385 :     \t$dir
386 :    
387 :     to see if you can find the proper data. If you do, you can
388 :     manually copy or move it to the FIG data directory
389 :     $FIG_Config::data and proceed with installation.
390 :     END
391 :     }

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3