[Bio] / FigCommon / bootstrap.pl Repository:
ViewVC logotype

Annotation of /FigCommon/bootstrap.pl

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : olson 1.1 #
2 :     # FIG bootstrap script.
3 :     #
4 :     # Control is passed here from the toplevel configure (or dists/releases/install depending
5 :     # on how far we've gotten in the process).
6 :     #
7 :     # The configure has set the following environment variables for our use:
8 :     #
9 :     # FIG_HOME Root of the FIGdisk tree we're setting up
10 :     # FIGCONFIG_ENV Name of the environment we're configuring for
11 :     # FIGCONFIG_ENV_DIR Directory in which the binary install tree for $FIGCONFIG_ENV lives
12 :     # FIGCONFIG_RELEASE Release number we're configuring
13 :     # FIGCONFIG_RELEASE_DIR Directory in which that release lives
14 :     #
15 :     # Important note: perl system libraries may not be available when this
16 :     # script is running, if the FIGCONFIG_ENV_DIR is different than it was
17 :     # when the binaries were created. Part of the task of this
18 :     # script is to recognize this case and configure the process
19 :     # environment correctly so that the post-install process as
20 :     # handled by the Makefiles can use perl properly, without concern for paths.
21 :     #
22 :    
23 :     #
24 :     # Look into the FIGCONFIG_ENV_DIR for the PREFIX that this
25 :     # environment was compiled on. If that directory is different
26 :     # than the one we're currently using, rewrite @INC accordingly.
27 :     #
28 :     # We put this into a BEGIN block so that we can later use "use"
29 :     # without worrying about paths.
30 :     #
31 :     # We also set the $perlenv_override to true if we've done this,
32 :     # so we can insert these fixes into the perl tool header.
33 :     #
34 :    
35 :     BEGIN {
36 :    
37 :     # print "Here in the perl bootstrap. inc is:\n" . join("\n", @INC), "\n";
38 :    
39 :     my $my_prefix, $prefix, $inc, @newInc;
40 :    
41 :     $my_prefix = $ENV{FIGCONFIG_ENV_DIR};
42 :     open(P, "<$my_prefix/PREFIX");
43 :     $prefix = <P>;
44 :     chomp $prefix;
45 :    
46 :    
47 :     # print "Prefix is $prefix myprefix=$my_prefix\n";
48 :    
49 :     if ($prefix ne $my_prefix)
50 :     {
51 :     @newInc = ();
52 :     for $inc (@INC)
53 :     {
54 :     $inc =~ s,^$prefix,$my_prefix,;
55 :     push(@newInc, $inc);
56 :     }
57 :    
58 :     $::perlenv_override = 1;
59 :     }
60 :    
61 :     # print "New inc is:\n" . join("\n", @newInc), "\n";
62 :    
63 :     @INC = @newInc;
64 :     }
65 :    
66 :    
67 :     use FileHandle;
68 :     use strict;
69 :    
70 :     #
71 :     # Environment definition
72 :     #
73 :     # This list contains triples
74 :     # ($name, $value, $appendFlag). $name is the name of the
75 :     # variable; $value is the value to set, and $appendFlag
76 :     # is nonzero if we need to append this value to an existing
77 :     # environment variable.
78 :     #
79 :    
80 :     our $Env = [];
81 :    
82 :     #
83 :     # FIG configuration information.
84 :     # This list contains tuples [$name, $value, $quote]
85 :     # which are to be set in the FIG_Config modules for
86 :     # both python and perl. $quote is 1 for things that need
87 :     # to be quoted, 0 otherwise.
88 :     #
89 :     our $Config = [];
90 :    
91 :     #
92 :     # Other key/value pairs of stuff we may need to pass
93 :     # to and from the environment-specific configuration modules.
94 :     #
95 :     # Known keys:
96 :     #
97 :     # pythonpath
98 :     #
99 :    
100 :     our $OtherConfig = {};
101 :    
102 :     #
103 :     # Cache values of the various places we may be installing things.
104 :     #
105 :    
106 :     our $fig_disk = $ENV{FIG_HOME};
107 :     our $env_name = $ENV{FIGCONFIG_ENV};
108 :     our $env_bin = "$ENV{FIGCONFIG_ENV_DIR}/bin";
109 :     our $env_lib = "$ENV{FIGCONFIG_ENV_DIR}/lib";
110 : olson 1.2 #
111 :     # We are moving to use a symlink in dist/releases/current for these.
112 :     # This symlink is managed by switch_to_release.
113 :     #
114 :     #our $release_bin = "$ENV{FIGCONFIG_RELEASE_DIR}/bin/$ENV{FIGCONFIG_ENV}";
115 :     #our $release_lib = "$ENV{FIGCONFIG_RELEASE_DIR}/lib/$ENV{FIGCONFIG_ENV}";
116 :    
117 :     our $release_bin = "$fig_disk/dist/releases/current/bin/$ENV{FIGCONFIG_ENV}";
118 :     our $release_lib = "$fig_disk/dist/releases/current/lib/$ENV{FIGCONFIG_ENV}";
119 :    
120 :     our $fig_bin = "$fig_disk/FIG/bin";
121 : olson 1.1
122 :     #
123 :     # Location of start/stop servers scripts.
124 :     #
125 :     our $start_servers = "$fig_disk/bin/start-servers";
126 :     our $stop_servers = "$fig_disk/bin/stop-servers";
127 :    
128 :     sub configure_std_fig_environment
129 :     {
130 :     #
131 :     # Populate the Config with the usual stuff we need in FIG_Config.pm.
132 :     #
133 :    
134 :     my $fig = "$fig_disk/FIG";
135 :     my $data = "$fig/Data";
136 :    
137 :     my $hostname = `hostname`;
138 :     chomp $hostname;
139 :    
140 :     push(@$Config, ["fig_disk", $fig_disk, 1]);
141 :     push(@$Config, ["blastmast", "$fig_disk/BLASTMAT", 1]);
142 :     push(@$Config, ["fig", $fig, 1]);
143 : olson 1.2 push(@$Config, ["bin", $fig_bin, 1]);
144 : olson 1.1 push(@$Config, ["ext_bin", $env_bin, 1]);
145 :     push(@$Config, ["data", $data, 1]);
146 :     push(@$Config, ["global", "$data/Global", 1]);
147 :     push(@$Config, ["organisms", "$data/Organisms", 1]);
148 :     push(@$Config, ["RC", "$fig_disk/FIG/ResolutionCenter", 1]);
149 :     push(@$Config, ["NR", "$fig_disk/FIG/NR", 1]);
150 :     push(@$Config, ["temp", "$fig_disk/FIG/Tmp", 1]);
151 :     push(@$Config, ["temp_url", "http://$hostname/FIG-Tmp", 1]);
152 :     push(@$Config, ["cgi_url", "http://$hostname/FIG", 1]);
153 :     }
154 :    
155 :     sub compute_environment
156 :     {
157 :     #
158 :     # ReleaseTools configuration.
159 :     #
160 :    
161 :     push(@$Env, ["RTROOT", $ENV{FIG_HOME}, 0]);
162 :     push(@$Env, ["RTDIST", "\$RTROOT/dist", 0]);
163 :     push(@$Env, ["RTARCH", $ENV{FIGCONFIG_ENV}, 0]);
164 : olson 1.2 push(@$Env, ["RTCURRENT", "`cat \$RTROOT/CURRENT_RELEASE`", 0]);
165 : olson 1.1 push(@$Env, ["FIG_HOME", $ENV{FIG_HOME}, 0]);
166 :    
167 :     push(@$Env, ["BLASTMAT", "$ENV{FIG_HOME}/BLASTMAT", 0]);
168 :    
169 :     #
170 :     # Determine the path to use.
171 :     #
172 :     # This includes the path into the particular release we're configuring for,
173 :     # and to the environment's bin directory.
174 :     #
175 :    
176 :     my @path = ();
177 :     push(@path, $env_bin);
178 : olson 1.2 push(@path, $fig_bin);
179 : olson 1.1 push(@path, "$fig_disk/bin");
180 :    
181 :     push(@$Env, ["PATH", join(":", @path), 1]);
182 :    
183 : olson 1.2 push(@$Env, ["LD_LIBRARY_PATH", "${env_lib}", 1]);
184 : olson 1.1 }
185 :    
186 :     sub configure_python
187 :     {
188 :     #
189 :     # Determine the PYTHONPATH we're to use.
190 :     #
191 :     # Right now, we don't compute a replacement for the system path,
192 :     # but we're likely going to require that *if* python is
193 :     # distributed with the environment. We're currently not doing that.
194 :     #
195 :     # So we just need to point to any python packages in the release,
196 :     # and to packages that were installed in the environment dir.
197 :     #
198 :    
199 :     my $python = find_python();
200 :     print "Found python at $python\n";
201 :    
202 :     my $py_version = `$python -c 'import sys; print "%d.%d" % (sys.version_info[:2])'`;
203 :     chomp $py_version;
204 :     print "Python version $py_version\n";
205 :    
206 : olson 1.2 $OtherConfig->{python_site_path} = "$ENV{FIGCONFIG_ENV_DIR}/lib/python$py_version/site-packages";
207 :    
208 : olson 1.1 my @path = ();
209 : olson 1.2 push(@path, $OtherConfig->{python_site_path});
210 :     push(@path, "$release_lib");
211 : olson 1.1 push(@path, "$fig_disk/config");
212 :     push(@$Env, ["PYTHONPATH", join(":", @path), 1]);
213 :    
214 :     $OtherConfig->{pythonpath} = join(":", @path);
215 :     }
216 :    
217 :     sub find_in_path
218 :     {
219 :     my($file) = @_;
220 :     my(@path) = split(":", $ENV{PATH});
221 :    
222 :     for my $p (@path)
223 :     {
224 :     my $x = "$p/$file";
225 :     if (-x $x)
226 :     {
227 :     return $x;
228 :     }
229 :     }
230 :     return undef;
231 :     }
232 :    
233 :     #
234 :     # Find the name of the python executable we are to use.
235 :     #
236 :     sub find_python
237 :     {
238 :     my $python;
239 :    
240 :     #
241 :     # Special case for the mac.
242 :     #
243 :    
244 :     if ($ENV{FIGCONFIG_ENV} eq "mac")
245 :     {
246 :     if ($python = find_in_path("pythonw"))
247 :     {
248 :     return $python;
249 :     }
250 :    
251 :     #
252 :     # Didn't find it in the path; we require the
253 :     # MacPython build, and that installs into
254 :     # /usr/local/bin/pythonw by default.
255 :     #
256 :    
257 :     return "/usr/local/bin/pythonw";
258 :     }
259 :     else
260 :     {
261 :     #
262 :     # Non-mac, just look in the env dir or the path.
263 :     #
264 :    
265 :     $python = "$ENV{FIGCONFIG_ENV_DIR}/bin/python";
266 :     if (-x $python)
267 :     {
268 :     return $python;
269 :     }
270 :    
271 :     return find_in_path("python");
272 :     }
273 :     }
274 :    
275 :     #
276 :     # Find the name of the perl executable we are to use.
277 :     #
278 :     sub find_perl
279 :     {
280 :     my $perl;
281 :    
282 :     $perl = "$ENV{FIGCONFIG_ENV_DIR}/bin/perl";
283 :     if (-x $perl)
284 :     {
285 :     return $perl;
286 :     }
287 :    
288 :     warn "We did not find a perl executable in the SEED distribution; some modules may not be found\n";
289 :     return find_in_path("perl");
290 :     }
291 :    
292 :     sub write_bash_init
293 :     {
294 :     my($fh, $env) = @_;
295 :    
296 :     foreach my $item (@$env)
297 :     {
298 :     my($name, $value, $append) = @$item;
299 :    
300 :     if ($append)
301 :     {
302 :     #
303 :     # Write code to detect if the value is set, and act accordingly.
304 :     #
305 :    
306 :     print $fh <<END;
307 :     DELIM=
308 :     if [ -n "\$$name" ] ; then
309 :     DELIM=:
310 :     fi
311 :     $name=\${$name}\${DELIM}$value
312 :     export $name
313 :     END
314 :     }
315 :     else
316 :     {
317 :     print $fh "$name=\"$value\"\n";
318 :     print $fh "export $name\n";
319 :     }
320 :     }
321 :     }
322 :    
323 :     sub write_csh_init
324 :     {
325 :     my($fh, $env) = @_;
326 :    
327 :     foreach my $item (@$env)
328 :     {
329 :     my($name, $value, $append) = @$item;
330 :    
331 :     if ($append)
332 :     {
333 :     #
334 :     # Write code to detect if the value is set, and act accordingly.
335 :     #
336 :    
337 :     print $fh <<END;
338 :     if (\$?$name) then
339 :     setenv $name \${$name}:$value
340 :     else
341 :     setenv $name $value
342 :     endif
343 :     END
344 :     }
345 :     else
346 :     {
347 :     print $fh "setenv $name \"$value\"\n";
348 :     }
349 :     }
350 :    
351 :     }
352 :    
353 :     sub run_script
354 :     {
355 :     my($script) = @_;
356 :    
357 :    
358 :     unless (my $ret = do $script)
359 :     {
360 :     if ($@)
361 :     {
362 :     warn "Error parsing $script: $@\n";
363 :     }
364 :     elsif (!defined($ret))
365 :     {
366 :     warn "Couldn't do $script: $!\n";
367 :     }
368 :     else
369 :     {
370 :     warn "Counldn't run $script\n";
371 :     }
372 :     }
373 :    
374 :    
375 :     }
376 :    
377 :     sub configure_environment_specific
378 :     {
379 :     my $env_script = $ENV{FIGCONFIG_ENV_CONFIG};
380 :    
381 :     if (! -f $env_script)
382 :     {
383 :     warn "No environment-specific configuration file $env_script found, skipping\n";
384 :     return;
385 :     }
386 :    
387 :     run_script($env_script);
388 :     }
389 :    
390 :     #
391 :     # Create any directories that might be missing.
392 :     #
393 :    
394 :     sub setup_directories
395 :     {
396 :     my(@needed) = @_;
397 :    
398 :     for my $dir (@needed)
399 :     {
400 :     if (! -d $dir)
401 :     {
402 :     mkdir($dir) or warn "Could not create $dir: $!\n";
403 :     }
404 :     }
405 :     }
406 :    
407 :     sub setup_server_scripts
408 :     {
409 :     #
410 :     # Remove any start/stop servers scripts we might have, and write the stub
411 :     # start.
412 :     #
413 :    
414 :     unlink($start_servers);
415 :     unlink($stop_servers);
416 :    
417 :     open(F, ">$start_servers") or die "Cannot write $start_servers: $!";
418 :     print F "#!/bin/sh\n\n";
419 :     close(F);
420 :     chmod(0775, $start_servers);
421 :    
422 :     open(F, ">$stop_servers") or die "Cannot write $stop_servers: $!";
423 :     print F "#!/bin/sh\n\n";
424 :     close(F);
425 :     chmod(0775, $stop_servers);
426 :    
427 :     }
428 :    
429 :     sub write_config_pm
430 :     {
431 :     my($fh, $config) = @_;
432 :    
433 :     print $fh "package FIG_Config;\n\n";
434 :    
435 :     foreach my $item (@$config)
436 :     {
437 :     my($name, $value, $quote) = @$item;
438 :    
439 :     my $q = $quote ? '"' : '';
440 :     printf $fh "\$%-15s = $q$value$q;\n", $name
441 :     }
442 :    
443 :     print $fh "\n1;\n";
444 :     }
445 :    
446 :     sub write_config_py
447 :     {
448 :     my($fh, $config) = @_;
449 :    
450 :     foreach my $item (@$config)
451 :     {
452 :     my($name, $value, $quote) = @$item;
453 :    
454 :     #
455 :     # Ugh. "global" is a keyword in python.
456 :     #
457 :    
458 :     $name = "global_dir" if $name eq "global";
459 :    
460 :     my $q = $quote ? '"' : '';
461 :     printf $fh "%-15s = $q$value$q\n", $name
462 :     }
463 :     }
464 :    
465 :     sub write_tool_hdr_perl
466 :     {
467 :     my($fh) = @_;
468 :    
469 :     my $perl = find_perl();
470 :    
471 :     print $fh "#!$perl\n";
472 :     print $fh "\n";
473 :    
474 :     if ($::perlenv_override)
475 :     {
476 :     #
477 :     # Write the tool header BEGIN block to reset the environment.
478 :     #
479 :    
480 :     print $fh "BEGIN {\n";
481 :     print $fh " \@INC = qw(\n";
482 :     for my $inc (@INC)
483 :     {
484 :     next if $inc eq '.';
485 :     print $fh "\t$inc\n";
486 :     }
487 : olson 1.2 print $fh ");\n";
488 : olson 1.1 print $fh "}\n";
489 :     }
490 :    
491 :     print $fh "use Data::Dumper;\n";
492 :     print $fh "use Carp;\n";
493 :    
494 : olson 1.2 #
495 :     # We generate a comment here that gets expanded by switch_to_release
496 :     # This is part of the effort in making the environment bootstrapping
497 :     # code independent of releases.
498 :     #
499 :    
500 :     #print $fh "use lib \"$release_lib\";\n";
501 :     #print $fh "use lib \"$release_lib/FigKernelPackages\";\n";
502 :    
503 :     print $fh "# Following block is expanded by switch_to_release to add use lib directives\n";
504 :     print $fh "# to point at the correct locations in the release directory.\n";
505 :     print $fh "#BEGIN switch_to_release generated code\n";
506 :     print $fh "#END switch_to_release generated code\n";
507 :     print $fh "\n";
508 :    
509 : olson 1.1 print $fh "use lib \"$fig_disk/config\";\n";
510 :    
511 :     print $fh "use FIG_Config;\n";
512 :     print $fh "\n";
513 :     }
514 :    
515 :     sub write_tool_hdr_python
516 :     {
517 :     my($fh) = @_;
518 :    
519 :     my $python = find_python();
520 :    
521 :     print $fh "#!$python\n";
522 :     print $fh "\n";
523 :    
524 :     print $fh "import sys\n";
525 : olson 1.2
526 :    
527 :     print $fh "sys.path.append('$OtherConfig->{python_site_path}')\n";
528 :    
529 :     print $fh "# Following block is expanded by switch_to_release to add use lib directives\n";
530 :     print $fh "# to point at the correct locations in the release directory.\n";
531 :     print $fh "#BEGIN switch_to_release generated code\n";
532 :     print $fh "#END switch_to_release generated code\n";
533 :     print $fh "\n";
534 :    
535 :     print $fh "sys.path.append('$fig_disk/config')\n";
536 :    
537 : olson 1.1 print $fh "import FIG_Config\n";
538 :     print $fh "\n";
539 :     }
540 :    
541 :     setup_directories("$fig_disk/bin",
542 :     "$fig_disk/config");
543 :    
544 :     setup_server_scripts();
545 :    
546 :    
547 :     find_python();
548 :    
549 :     compute_environment();
550 :     configure_std_fig_environment();
551 :     configure_python();
552 :     configure_environment_specific();
553 :    
554 :     #
555 :     # Write the shell startup to the figdisk.
556 :     #
557 :     open(FH, ">$fig_disk/config/fig-user-env.sh") or die "Cannot write $fig_disk/config/fig-user-env.sh: $!";
558 :     write_bash_init(\*FH, $Env);
559 :     close(FH);
560 :    
561 :     open(FH, ">$fig_disk/config/fig-user-env.csh") or die "Cannot write $fig_disk/config/fig-user-env.csh: $!";
562 :     write_csh_init(\*FH, $Env);
563 :     close(FH);
564 :    
565 :     #
566 :     # Write the FIG_Config file.
567 :     #
568 :    
569 :     open(FH, ">$fig_disk/config/FIG_Config.pm") or die "Cannot write $fig_disk/config/FIG_Config.pm: $!";
570 :     write_config_pm(\*FH, $Config);
571 :     close(FH);
572 :    
573 :     open(FH, ">$fig_disk/config/FIG_Config.py") or die "Cannot write $fig_disk/config/FIG_Config.py: $!";
574 :     write_config_py(\*FH, $Config);
575 :     close(FH);
576 :    
577 :     #
578 :     # Write the tool headers
579 :     #
580 :    
581 : olson 1.2 open(FH, ">$fig_disk/config/base_tool_hdr") or die "Cannot write $fig_disk/config/tool_hdr: $!";
582 : olson 1.1 write_tool_hdr_perl(\*FH);
583 :     close(FH);
584 :    
585 : olson 1.2 open(FH, ">$fig_disk/config/base_tool_hdr_py") or die "Cannot write $fig_disk/config/tool_hdr_py: $!";
586 : olson 1.1 write_tool_hdr_python(\*FH);
587 :     close(FH);
588 :    
589 : olson 1.2 unshift(@INC, "$ENV{FIGCONFIG_RELEASE_DIR}");
590 :    
591 :     require FigCommon::SwitchRelease;
592 : olson 1.1
593 : olson 1.2 &FigCommon::SwitchRelease::switch_to_release($fig_disk, $ENV{FIGCONFIG_ENV}, $ENV{FIGCONFIG_RELEASE});

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3