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

Annotation of /FigCommon/bootstrap.pl

Parent Directory Parent Directory | Revision Log Revision Log


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