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

Annotation of /FigCommon/bootstrap.pl

Parent Directory Parent Directory | Revision Log Revision Log


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