forked from kbaseattic/dev_container
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.pl
More file actions
1648 lines (1411 loc) · 55.6 KB
/
Config.pl
File metadata and controls
1648 lines (1411 loc) · 55.6 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env perl
#
# Copyright (c) 2003-2006 University of Chicago and Fellowship
# for Interpretations of Genomes. All Rights Reserved.
#
# This file is part of the SEED Toolkit.
#
# The SEED Toolkit is free software. You can redistribute
# it and/or modify it under the terms of the SEED Toolkit
# Public License.
#
# You should have received a copy of the SEED Toolkit Public License
# along with this program; if not write to the University of Chicago
# at info@ci.uchicago.edu or the Fellowship for Interpretation of
# Genomes at veronika@thefig.info or download a copy from
# http://www.theseed.org/LICENSE.TXT.
#
use strict;
use File::Basename;
use File::Spec;
use File::Copy;
use File::Copy::Recursive;
use File::Path;
use Getopt::Long::Descriptive;
use XML::Writer;
use IO::File;
use Config;
use Cwd;
use File::Slurp qw(read_file);
# We need to look inside the FIG_Config even though it is loaded at
# run-time, so we will get lots of warnings about one-time variables.
no warnings qw(once);
## THIS CONSTANT DEFINES THE CORE MODULES
use constant CORE => qw(utils kernel p3_code p3_core p3_cli RASTtk seed_core);
## THIS CONSTANT DEFINES MODULES WITH SPECIAL INCLUDE LISTS
use constant INCLUDES => { utils => ['utils', 'RASTtk', 'p3_code', 'p3_core', 'seed_core'],
RASTtk => ['RASTtk', 'utils', 'p3_code', 'p3_core', 'p3_cli', 'seed_core'],
p3_code => ['p3_code', 'p3_core', 'seed_core'],
p3_cli => ['p3_cli', 'p3_code', 'p3_core', 'seed_core'],
p3_core => ['p3_code', 'p3_core', 'seed_core'] };
## THIS CONSTANT DEFINES MODULES THAT RELY ON DEV_CONTAINER INCLUDES
use constant DEV_MODS => { p3_cli => 1, kernel => 1 };
## THIS CONSTANT DEFINES DEV_CONTAINER MODULES
use constant DEV_CONTAINED => { app_service => 'https://github.com/PATRIC3/app_service',
p3_auth => 'https://github.com/PATRIC3/p3_auth',
Workspace => 'https://github.com/olsonanl/Workspace' };
## THIS CONSTANT DEFINES EXTERNAL MODULES
use constant EXTERNALS => { seed_core => 1 };
## THIS CONSTANT CONTAINS SPECIAL GIT MAPPINGS
use constant ODD_MODS => { p3_cli => 'https://github.com/PATRIC3/p3_cli' };
## THIS CONSTANT CONTAINS MODULES WE ARE DELETING
use constant OBSOLETE => { p3_scripts => 1 };
## THIS CONSTANT LISTS CLI COMMANDS IN KERNEL USED BY JAVA
use constant CLI_SPECIAL => [ 'appserv-enumerate-tasks', 'appserv-query-task', 'appserv-start-app',
'p3-cp', 'p3-ls', 'p3-mkdir', 'p3-echo', 'p3-login' ];
## THIS CONSTANT LISTS FLASK PROJECTS
use constant FLASKS => [ 'biosyntheticmachines' ];
=head1 Generate SEEDtk Configuration Files
Config [ options ] dataDirectory
This method generates (or re-generates) the L<FIG_Config> and B<UConfig.sh> files for a
SEEDtk environment.
=head2 Parameters
The positional parameters are the location of the data folder and the location
of the web folder (see L<ReadMe> for more information about SEEDtk folders). If a
B<FIG_Config> file already exists, this information is not needed-- the existing values
will be used.
The command-line options are as follows.
=over 4
=item noconda
Skip the conda activation in the python scripts. This is necessary on some machines.
=item fc
If specified, the name of the B<FIG_Config> file for the output. If the name is specified
without a path, it will be put in the project directory's C<config> folder. If C<off>,
no B<FIG_Config> file will be written.
=item clear
If specified, the current B<FIG_Config> values will be ignored, and the configuration information will
be generated from scratch.
=item links
If specified, a prototype C<Links.html> file will be generated in the web directory if one does not
already exist.
=item dirs
If specified, the default data and web subdirectories will be set up.
=item dna
If specified, the location for the DNA repository. If you are using a shared database, then
this insures you are using the same repository as everyone else. If C<none>, then the DNA
repository is turned off and DNA requests will fail.
=item eclipse
If this is an Eclipse environment, the name of the project cloned from the main SEEDtk
project (that is, the name of the I<project directory project>). If you specify this
parameter without a value, the value is presumed to be C<SEEDtk>.
=item gfw
If this is specified, then it is presumed you have GitHub for Windows installed. Its copy of
GIT will be added to your path in the C<user-env> script.
=item dbhost
Name of the database host on which the database will be placed. The default is C<localhost>.
=item dbname
Name to give to the shrub database. The default is an empty string, meaning the default name will
be used.
=item dbuser
User name for signing on to the database. The default is C<seed>.
=item dbpass
Password for signing on to the database. The default is an empty string, meaning no password.
=item kbase
Name of a directory in which to create a shadow FIG_Config for the kbase environment.
=back
=head2 Notes for Programmers
To add a new L<FIG_Config> parameter, simply add a call to L<Env/WriteParam> to the
L</WriteAllParams> method.
=cut
$| = 1; # Prevent buffering on STDOUT.
# Determine the operating system.
my $winMode = ($^O =~ /Win/ ? 1 : 0);
# Analyze the command line.
my ($opt, $usage) = describe_options('%o %c dataRootDirectory',
["clear|c", "ignore current configuration values"],
["fc=s", "name of a file to use for the FIG_Config output, or \"off\" to turn off FIG_Config output",
{ default => "FIG_Config.pm" }],
["dirs", "verify default subdirectories exist"],
["dna=s", "location of the DNA repository (if other than local)"],
["dbhost=s", "name of the database host to which we should connect", { default => 'localhost' }],
["links", "generate Links.html file"],
["gfw", "add GitHub for Windows GIT to the path (Windows only)"],
["gitbash", "configure for gitbash use"],
["dbname=s", "Shrub database name"],
["dbuser=s", "Shrub database user", { default => 'seed' }],
["dbpass=s", "Shrub database password"],
["kbase=s", "kbase lib directory"],
["eclipse:s", "if specified, then we will set up for Eclipse"],
["homeFix", "if specified, a root path of 'homes/' will be changed to '~' (Argonne kludge)"],
["noconda", "if specified, do not activate conda in the python scripts"],
["java=s", "if specified, the root directory of the Eclipse workspace, from which java modules will be derived"]
);
print "Retrieving current configuration.\n";
# Compute the eclipse option.
my $eclipseMode;
my $eclipseParm = $opt->eclipse;
if (defined $eclipseParm) {
$eclipseMode = 1;
if (! $eclipseParm) {
$eclipseParm = 'SEEDtk';
}
}
# Save the conda option.
my $noConda = $opt->noconda;
# Get the directory this script is running in.
my $base_dir = dirname(File::Spec->rel2abs(__FILE__));
# Get into it.
chdir $base_dir;
# Compute the remote base directory.
my $rstr = `git remote -v`;
my($remote) = $rstr =~ /^origin\s+(\S+)/;
my $remote_base = dirname($remote);
# Get the real base directory. For Unix, this is the project
# directory. For vanilla mode, this is the project directory's
# parent. We will also figure out the eclipse mode here.
my ($vanillaMode, $projName, $dataParm);
if ($ENV{KB_TOP}) {
# Here we are in a Unix setup. The base directory has been
# stored in the environment.
$base_dir = $ENV{KB_TOP};
($dataParm) = @ARGV;
} else {
# Clean up the incoming path names.
if ($ARGV[0]) {
$dataParm = File::Spec->rel2abs($ARGV[0]);
}
# Fix Windows slash craziness.
$base_dir =~ tr/\\/\//;
# Check our directory name.
$projName = $eclipseParm || 'SEEDtk';
if ($base_dir =~ /(.+)\/$projName/) {
$base_dir = $1;
} else {
die "Configuration script not found in $projName.";
}
}
if ($opt->homefix) {
$base_dir =~ s#/homes/#/home/#;
print "Fixing home directory.\n";
}
print "Base directory is $base_dir.\n";
if (! $ENV{KB_TOP}) {
# Denote this is vanilla mode.
$vanillaMode = 1;
# Clone all the core modules we don't have.
chdir $base_dir;
for my $module (CORE) {
if (! -d "$base_dir/$module") {
my $url = ODD_MODS->{$module} // "$remote_base/$module.git";
print "Cloning $module from $url.\n";
my $rc = system("git", "clone", $url);
if ($rc != 0) {
die "Error cloning $url\n";
}
}
}
# Get access to the utilities library.
unshift @INC, "$base_dir/utils/lib";
}
# Establish the dev-container if it does not already exist.
my $userHome = $ENV{HOME};
my $dev_base = "$userHome/dev_container/modules";
if (! -d $dev_base) {
chdir $userHome;
my $url = "https://github.com/olsonanl/dev_container";
my $rc = system("git", "clone", $url);
if ($rc != 0) {
die "Error cloning $url\n";
}
}
chdir $dev_base;
for my $module (keys %{DEV_CONTAINED()}) {
if (! -d "$dev_base/$module") {
my $url = DEV_CONTAINED->{$module};
my $rc = system("git", "clone", $url);
if ($rc != 0) {
die "Error cloning $url\n";
}
}
}
require Env;
print "Analyzing directories.\n";
# The root directories will be put in here.
my ($dataRootDir, $webRootDir) = ('', '');
# This points to the project directory project.
my $projDir = ($vanillaMode ? join("/", $base_dir, $projName) : $base_dir);
if (! -d "$projDir/config") {
die "Project directory not found in $projDir.";
}
my $clientModule = "$dev_base/app_service/lib/Bio/KBase/AppService/Client.pm";
if (! -f $clientModule) {
File::Copy::Recursive::fcopy("$projDir/Client.txt", $clientModule);
}
# Verify that the default BV-BRC workspace URL is correct.
my $wsClientFile = "$dev_base/Workspace/lib/Bio/P3/Workspace/WorkspaceClient.pm";
my $wsClientText = read_file($wsClientFile);
my $count = ($wsClientText =~ s/http:/https:/gsi);
if ($count > 0) {
print "Updating WorkspaceClient.\n";
open(my $oh, '>', $wsClientFile) || die "Could not update WorkspaceClient: $!";
print $oh $wsClientText;
close $oh;
}
# Save the current environment, before it's been modified by FIG_Config.
my %oldenv = %ENV;
# Get the name of the real FIG_Config file (not the output file,
# if one was specified, the real one).
my $fig_config_name = "$projDir/config/FIG_Config.pm";
# Now we want to get the current environment. If the CLEAR option is
# specified or there is no file present, we stay blank; otherwise, we
# load the existing FIG_Config.
if (! $opt->clear && -f $fig_config_name) {
RunFigConfig($fig_config_name);
}
# Insure the list of modules includes the cores. If they are not
# present, we add them to the front of the list.
for my $module (CORE) {
if (! grep { $_ eq $module } @FIG_Config::modules) {
unshift @FIG_Config::modules, $module;
}
}
# Insure the list of modules does not include obsolete ones.
my @modules = grep { ! OBSOLETE->{$_} } @FIG_Config::modules;
@FIG_Config::modules = @modules;
# This hash will map each module to its directory.
my $modBaseDir = ($vanillaMode ? $base_dir : "$projDir/modules");
my %modules;
for my $module (@FIG_Config::modules) {
# Compute the directory name depending on the mode.
my $dir = "$modBaseDir/$module";
# Make sure it exists.
if (! -d $dir) {
die "Could not find expected module directory $dir.";
}
# Add it to the hash.
$modules{$module} = $dir;
}
# Remember the jar directory.
my $jarDir = "$modules{kernel}/jars";
# Check for older files that use a different name for FIG_Config::data.
if (! defined $FIG_Config::data && $FIG_Config::shrub_dir) {
$FIG_Config::data = $FIG_Config::shrub_dir;
}
# Make sure we have the data directory if there is no data root
# in the command-line parameters.
if (! defined $FIG_Config::data) {
$dataRootDir = FixPath($ARGV[0]);
if (! defined $dataRootDir) {
die "A data root directory is required if no current value exists in FIG_Config.";
} elsif (! -d $dataRootDir) {
print "Creating $dataRootDir\n";
File::Copy::Recursive::pathmk($dataRootDir) || die "Could not create $dataRootDir: $!";
}
} else {
$dataRootDir = $FIG_Config::data;
}
# Insure we have the log directory.
my $logDir = $FIG_Config::logDir || "$dataRootDir/logs";
if (! -d $logDir) {
File::Copy::Recursive::pathmk($logDir);
if (! $winMode) {
chmod 0777, $logDir;
}
}
# Update the GTO definition if we have a master copy.
my $gSpecFile = "$modBaseDir/genome_annotation/GenomeAnnotation.spec";
if (-s $gSpecFile) {
print "Updating GenomeAnnotation.spec.\n";
chdir "$modBaseDir/genome_annotation";
system("git", "pull", "--ff-only");
File::Copy::Recursive::fcopy($gSpecFile, "$modBaseDir/kernel/lib");
}
# Get the dev-container libraries and compute their locations.
my $devList = [map { "$dev_base/$_/lib" } keys %{DEV_CONTAINED()}];
# If the FIG_Config write has NOT been turned off, then write the FIG_Config.
if ($opt->fc eq 'off') {
print "FIG_Config output suppressed.\n";
} else {
# Compute the FIG_Config file name.
my $outputName = $opt->fc;
# Fix the slash craziness for Windows.
$outputName =~ tr/\\/\//;
# If the name is pathless, put it in the config directory.
if ($outputName !~ /\//) {
$outputName = "$projDir/config/$outputName";
}
# If we are overwriting the real FIG_Config, back it up.
if (-f $fig_config_name && $outputName eq $fig_config_name) {
print "Backing up $fig_config_name.\n";
copy $fig_config_name, "$projDir/config/FIG_Config_old.pm";
}
# Write the FIG_Config.
print "Writing configuration to $outputName.\n";
WriteAllParams($outputName, $modBaseDir, \%modules, $projDir, $dataRootDir, undef, undef, $winMode, $opt);
# Execute it to get the latest variable values.
print "Reading back new configuration.\n";
RunFigConfig($outputName);
# Check for a KBase shadow.
if ($opt->kbase) {
my $kbFigConfig = $opt->kbase;
# Create a module directory map.
my $kbModBase = "/kb/module/SEEDtk/modules";
my %kbModules;
for my $module (@FIG_Config::modules) {
$kbModules{$module} = "$kbModBase/$module";
}
WriteAllParams($kbFigConfig, $kbModBase, \%kbModules, '/kb/module/SEEDtk', '/kb/module/SEEDtk/Data',
'', '', 0, $opt, 1);
}
}
# Are we setting up default data directories?
if ($opt->dirs) {
# Yes. Insure we have the data paths.
BuildPaths($winMode, Data => $FIG_Config::data, qw(Inputs Inputs/GenomeData Inputs/SubSystemData LoadFiles));
# Are we using a local DNA repository?
if (! $opt->dna) {
# Yes. Build that, too.
BuildPaths($winMode, Data => $FIG_Config::data, qw(DnaRepo));
}
# Insure we have the web paths.
if ($webRootDir) {
BuildPaths($winMode, Web => $FIG_Config::web_dir, qw(img Tmp logs));
}
}
# Insure we have the global directory path.
if (! -d $FIG_Config::global) {
print "Creating $FIG_Config::global.\n";
File::Path::make_path($FIG_Config::global)
}
BuildPaths($winMode, Data => $FIG_Config::data, qw(Global));
# Insure we have the eval/binning directory path.
if (! -d $FIG_Config::p3data) {
print "Creating $FIG_Config::p3data.\n";
File::Path::make_path($FIG_Config::p3data);
}
BuildPaths($winMode, Data => $FIG_Config::data, qw(P3Data));
# Do we have a Web project?
if ($webRootDir) {
my $weblib = "$FIG_Config::web_dir/lib";
if (-d $weblib) {
# Yes. Create the web configuration files.
my $webConfig = "$weblib/Web_Config.pm";
my $jobConfig = "$projDir/config/Job_Config.pm";
# Open the web configuration file for output.
my ($oh1, $oh2);
if (! open(my $oh1, ">$webConfig")) {
# Web system problems are considered warnings, not fatal errors.
warn "Could not open web configuration file $webConfig: $!\n";
} elsif (! open(my $oh2, ">$jobConfig")) {
warn "Could not open background job configuration file $jobConfig: $!\n";
} else {
# Write the files.
for my $oh ($oh1, $oh2) {
print $oh "\n";
print $oh "use lib\n";
print $oh " '" . join("',\n '", @FIG_Config::libs) . "';\n";
print $oh "\n";
print $oh "use FIG_Config;\n";
print $oh "\n";
print $oh "1;\n";
# Close the file.
close $oh;
}
print "Web configuration files $webConfig and $jobConfig created.\n";
}
}
}
# If this is vanilla mode, we need to set up the PERL libraries and
# execution paths.
if ($vanillaMode) {
# Set up the paths and PERL libraries.
WriteAllConfigs($winMode, \%modules, $projDir, $jarDir, $opt, \%oldenv);
if (! $winMode) {
# For an vanilla Mac installation, we have to set up binary versions of the scripts
# and make them executable.
SetupBinaries($projDir, \%modules, $opt);
# We also need to fix the CGI permissions.
SetupCGIs($FIG_Config::web_dir, $opt);
} else {
# For a vanilla Windows installation, we have to set up DOS-style versions of the
# shell scripts.
SetupCommands(\%modules, $opt);
}
} else {
# For the alternate environment, we need to generate the Config script.
SetupScript('Config.pl', "$projDir/bin", $projDir);
# Then we need to do a make.
my $oldDir = getcwd();
chdir $projDir;
system("make");
chdir $oldDir;
# Now we need to fix the path if there is a custom JDK. Note that the current JDK must be
# in a directory named "jdk". It should point to the latest version.
opendir my $dh, $projDir || die "Could not open project subdirectories: $!";
my @java = grep { $_ =~ /^(?:jdk|apache-maven-)/ && -d "$projDir/$_" } readdir $dh;
if (@java) {
my ($mvnVersion) = grep { $_ =~ /^apache-maven/ } @java;
my $jdkPath = "$projDir/jdk";
my $mvnPath = "$projDir/$mvnVersion";
my @lines;
# Insure we have the java environment variables.
push @lines, "export SEED_JARS=$jarDir\n";
push @lines, "export MAVEN_HOME=$mvnPath\n";
push @lines, "export JAVA_HOME=$jdkPath\n";
push @lines, "export CODE_BASE=$modBaseDir\n";
# Now copy the user-env and edit the JAVA stuff.
open (my $ih, '<', "$projDir/user-env.sh") || die "Could not open user-env.sh: $!";
while (! eof $ih) {
my $skip;
my $line = <$ih>;
if ($line =~ /^export PATH=(.+)/) {
my $path = $1;
if (index($path, 'JAVA_HOME/bin') < 0) {
$path = "\$JAVA_HOME/bin:$path";
}
if (index($path, 'MAVEN_HOME/bin') < 0) {
$path = "\$MAVEN_HOME/bin:$path";
}
$line = "export PATH=$path\n";
} elsif ($line =~ /^export SEED_JARS/) {
$skip = 1
} elsif ($line =~ /^export JAVA_HOME/) {
$skip = 1;
} elsif ($line =~ /^export MAVEN_HOME/) {
$skip = 1;
}
if (! $skip) {
push @lines, $line;
}
}
close $ih;
open(my $oh, '>', "$projDir/user-env.sh") || die "Could not re-open user-env.sh $!";
print $oh @lines;
}
}
# Setup the java commands.
SetupJava($jarDir, $projDir);
# Setup the python commands.
my $binDir = ($winMode ? $projDir : "$projDir/bin");
SetupPython($binDir, $modBaseDir);
# Set up the FLASK projects.
SetupFlask($winMode, $modBaseDir, "$projDir/bin");
# Setup the CLI commands.
if ($winMode) {
# In Windows, we need to set up a special directory of CMD files for the CLI commands used by Java.
my $dir = $modules{kernel};
for my $script (@{CLI_SPECIAL()}) {
my $fileName = "$dir/$script.cmd";
my $scriptName = "$dir/scripts/$script.pl";
if (! -f $scriptName) {
$scriptName = "$modules{p3_cli}/scripts/$script.pl"
}
open(my $oh, '>', $fileName) || die "Could not write command file for $fileName: $!";
print $oh "\@echo off\n";
print $oh "perl $scriptName \%*\n";
close $oh;
}
}
# Now we need to create the pull-all script.
my $fileName = ($winMode ? "$projDir/pull-all.cmd" : "$projDir/bin/pull-all");
open(my $oh, ">$fileName") || die "Could not open $fileName: $!";
# The pushd command in windows can't handle forward slashes in directory names,
# so if this is windows we have to translate.
my $projDirForPush = $projDir;
if ($winMode) {
# Windows, so we translate.
$projDirForPush =~ tr/\//\\/;
# We also want to turn off echoing.
print $oh "\@echo off\n";
} else {
# In Unix, we need the shebang.
print $oh "#!/usr/bin/env bash\n";
}
# Now write the commands to run through the directories and pull.
print $oh "echo Pulling project directory.\n";
print $oh "pushd $projDirForPush\n";
print $oh "git pull --ff-only\n";
for my $module (@FIG_Config::modules) {
print $oh "echo Pulling $module\n";
print $oh "cd $modules{$module}\n";
print $oh "git pull --ff-only\n";
}
# Add the java directories. We do the maven projects first, then the remaining ones.
my %pull = map { $_ => 1 } @FIG_Config::java;
for my $javaDir (@FIG_Config::java) {
my $realDir = "$modBaseDir/$javaDir";
# Check for a maven project.
if (-d "$realDir/.mvn") {
# This is a maven project, so we need to pull the project and the sub-projects.
print $oh "echo Pulling java directory $javaDir\n";
print $oh "cd $realDir\n";
print $oh "git pull --recurse-submodules\n";
delete $pull{$javaDir};
# Now insure we don't re-pull the submodules.
opendir(my $dh, $realDir) || die "Could not open $javaDir: $!";
my @subs = grep { -s "$realDir/$_/pom.xml" && substr($_,0,1) ne '.' } readdir $dh;
closedir $dh;
for my $sub (@subs) {
delete $pull{$sub};
}
}
}
for my $javaDir (sort keys %pull) {
# Now the non-maven projects.
print $oh "echo Pulling java directory $javaDir\n";
my $realDir = "$modBaseDir/$javaDir";
print $oh "cd $realDir\n";
print $oh "git pull --ff-only\n";
}
# Add the web directory if needed.
if ($FIG_Config::web_dir && ! $ENV{KB_TOP} && ! $opt->remoteweb) {
print $oh "echo Pulling web directory\n";
print $oh "cd $FIG_Config::web_dir\n";
print $oh "git pull --ff-only\n";
}
# Restore the old directory.
print $oh "popd\n";
close $oh;
# In Unix, set the permissions.
if (! $winMode) {
chmod 0755, $fileName;
}
print "Pull-all script written to $fileName.\n";
# Finally, check for the links file.
if ($opt->links) {
if (! $webRootDir) {
die "Web support is required if --links is specified.";
}
# Determine the output location for the links file.
my $linksDest = "$FIG_Config::web_dir/Links.html";
# Do we need to generate a links file?
if (-f $linksDest) {
# No need. We already have one.
print "$linksDest file already exists-- not updated.\n";
} else {
# We don't have a links file yet.
print "Generating new $linksDest.\n";
# Find the source copy of the file.
my $linksSrc = "$FIG_Config::web_dir/lib/Links.html";
# Copy it to the destination.
copy $linksSrc, $linksDest;
print "$linksDest file created.\n";
}
}
# We are done.
print "All done.\n";
# Display the user-env command syntax.
if ($vanillaMode) {
my $cmd = "$FIG_Config::proj/user-env";
if (! $winMode) {
$cmd = "source $cmd.sh";
}
print "\nUse\n\n $cmd\n\nto establish a command-line environment.\n";
}
=head2 Internal Subroutines
=head3 RunFigConfig
RunFigConfig($fileName);
Execute the L<FIG_Config> module. This uses the PERL C<do> function, which
unlike C<require> can execute a module more than once, but requires error
checking. The error checking is done by this method.
=over 4
=item fileName
The name of the B<FIG_Config> file to load.
=back
=cut
sub RunFigConfig {
# Get the parameters.
my ($fileName) = @_;
# Execute the FIG_Config;
do $fileName;
if ($@) {
# An error occurred compiling the module.
die "Error compiling FIG_Config: $@";
} elsif ($!) {
# An error occurred reading the module.
die "Error reading FIG_Config: $!";
}
}
=head3 WriteAllParams
WriteAllParams($fig_config_name, $modBaseDir, \%modules, $projDir,
$dataRootDir, $webRootDir, $winMode, $opt, $force);
Write out the B<FIG_Config> file to the specified location. This method
is mostly calls to the L</WriteParam> method, which provides a concise
way of writing parameters to the file and checking for pre-existing
values. It is presumed that L</RunFigConfig> has been executed first so
that the existing values are known.
=over 4
=item fig_config_name
File name for the B<FIG_Config> file. The parameter code will be written to
this file.
=item modBaseDir
Base directory for the program modules.
=item modules
Reference to a hash mapping each module name to its directory.
=item projDir
Name of the project directory.
=item dataRootDir
Location of the base directory for the data files.
=item webRootDir
Location of the base directory for the web files.
=item winMode
TRUE for Windows, FALSE for Unix/Mac.
=item opt
Command-line options object.
=item kbase
If TRUE, the file will be modified for KBase use.
=back
=cut
sub WriteAllParams {
# Get the parameters.
my ($fig_config_name, $modBaseDir, $modules, $projDir, $dataRootDir, $webRootDir, $alexaDir, $winMode, $opt, $kbase) = @_;
# Open the FIG_Config for output.
open(my $oh, ">$fig_config_name") || die "Could not open $fig_config_name: $!";
# Write the initial lines.
print $oh "package FIG_Config;\n";
Env::WriteLines($oh,
"",
"## WHEN YOU ADD ITEMS TO THIS FILE, BE SURE TO UPDATE WriteAllParams in Config.pl.",
"## All paths should be absolute, not relative.",
"");
# Write each parameter.
if ($webRootDir) {
Env::WriteParam($oh, 'root directory of the local web server', web_dir => $webRootDir, $kbase);
Env::WriteParam($oh, 'URL for the directory of temporary files', temp_url => 'http://fig.localhost/Tmp');
Env::WriteParam($oh, 'directory for Alexa workspaces', alexa => $alexaDir);
}
Env::WriteParam($oh, 'root directory for Shrub data files (should have subdirectories "Inputs" (optional) and "LoadFiles" (required))',
data => $dataRootDir, $kbase);
Env::WriteParam($oh, 'directory for temporary files', temp => "$dataRootDir/Tmp", $kbase);
Env::WriteParam($oh, 'TRUE for windows mode', win_mode => ($winMode ? 1 : 0));
Env::WriteParam($oh, 'source code project directory', proj => $projDir, $kbase);
Env::WriteParam($oh, 'location of shared code', cvsroot => '');
Env::WriteParam($oh, 'TRUE to switch to the data directory during setup', data_switch => 0);
Env::WriteParam($oh, 'location of global file directory', global => "$dataRootDir/Global", $kbase);
Env::WriteParam($oh, 'location of eval/binning file directory', p3data => "$dataRootDir/P3Data", $kbase);
Env::WriteParam($oh, 'default conserved domain search URL', ConservedDomainSearchURL => "http://maple.mcs.anl.gov:5600");
Env::WriteParam($oh, 'Patric Data API URL', p3_data_api_url => '');
Env::WriteParam($oh, 'user home directory', userHome => ($ENV{HOME} || $ENV{HOMEPATH}));
# Write the perl path. Note this is a forced override.
Env::WriteParam($oh, 'perl execution path', perl_path => $Config{perlpath}, 1);
## Put new non-Shrub parameters here.
# Now we need to build our directory lists. We start with the module base directory.
Env::WriteLines($oh, "", "# code module base directory",
"our \$mod_base = '$modBaseDir';");
# If specified, update the java module list.
if ($opt->java) {
@FIG_Config::java = findJavaModules($opt->java);
print scalar(@FIG_Config::java) . " java modules found in configuration.\n";
}
# Now we set up the directory and module lists.
my @scripts = grep { -d $_ } map { "$modules->{$_}/scripts" } @FIG_Config::modules;
my @libs = map { "$modules->{$_}/lib" } @FIG_Config::modules;
push @libs, @$devList;
Env::WriteLines($oh, "", "# list of script directories",
"our \@scripts = ('" . join("', '", @scripts) . "');",
"", "# list of PERL libraries",
"our \@libs = ('" . join("', '", "$projDir/config", @libs) . "');",
"", "# list of project modules",
"our \@modules = qw(" . join(" ", @FIG_Config::modules) . ");",
"", "# list of shared modules",
"our \@shared = qw(" . join(" ", @FIG_Config::shared) . ");",
"", "# list of java modules",
"our \@java = qw(" . join(" ", @FIG_Config::java) . ");",
"", "# map of python projects to conda environments",
"our %python_envs = (" . join(", ", map { "'$_' => \"" . $FIG_Config::python_envs{$_} . "\"" } keys %FIG_Config::python_envs) . ");",
);
# Set up the tool directories.
my $packages = "$FIG_Config::proj/packages";
my @toolDirs;
if (opendir(my $dh, $packages)) {
my @dirs = grep { substr($_,0,1) ne '.' && -d "$packages/$_" } readdir($dh);
print "Package directories found: " . join(", ", @dirs) . "\n";
@toolDirs = map { "$_/bin" } grep { -d "$packages/$_/bin" } @dirs;
push @toolDirs, grep { $_ =~ /^art_bin_/ && -f "$packages/$_/art_illumina" } @dirs;
}
Env::WriteLines($oh, "", "# list of tool directories",
"our \@tools = (" . join(", ", map { "'$projDir/packages/$_'" } @toolDirs) .
");");
if ($vanillaMode || $kbase) {
# For a vanilla project or KBase, we need to convince FIG_Config to modify the path and the libpath.
my @paths = ($winMode ? ($projDir, @scripts) : "$FIG_Config::proj/bin");
GeneratePathFix($oh, $winMode, scripts => 'PATH', @paths);
# Do the same with PERL5LIB.
GeneratePathFix($oh, $winMode, libraries => 'PERL5LIB', @libs, "$FIG_Config::proj/config");
}
if ($vanillaMode) {
if ($winMode) {
# On Windows, we need to upgrade the PATHEXT.
Env::WriteLines($oh, "", "# Insure PERL is executable.",
"unless (\$ENV{PATHEXT} =~ /\.pl/i) {",
" \$ENV{PATHEXT} .= ';.pl';",
"}");
Env::WriteLines($oh, "", "# Insure PYTHON is executable.",
"unless (\$ENV{PATHEXT} =~ /\.py/i) {",
" \$ENV{PATHEXT} .= ';.py';",
"}");
}
}
if ($kbase) {
# For a KBase project, we need to add the include libraries to @INC.
Env::WriteLines($oh, "", "# Add include paths.", "push \@INC, '" . join("', '", @libs) . "';");
}
# Write the trailer.
print $oh "\n1;\n";
# Close the output file.
close $oh;
}
=head3 GeneratePathFix
GeneratePathFix($oh, $winMode, $type => $var, @paths);
Generate the FIG_Config PERL code to update an environment variable with new
path information. This is used for both the system search path and
the PERL module libraries.
=over 4
=item oh
Open output handle for the code lines generated.
=item winMode
TRUE if this is Windows, FALSE otherwise.
=item type
Type of thing being added to the environment variable value, plural, for comments.
=item var
Name of the environment variable being updated.
=item paths
List of the path elements to add to the environment variable.
=back
=cut
sub GeneratePathFix {
# Get the parameters.
my ($oh, $winMode, $type => $var, @paths) = @_;
# Compute the delimiter.
my $delim = ($winMode ? ';' : ':');
# Create the string to add to the path.
my $newPath = Env::BuildPathList($winMode, $delim, @paths);
my $newPathLen = length($newPath);
# Now we have the text and length of the new path string. Escape any backslashes
# in the path string and convert it to a quoted string.
$newPath =~ s/\\/\\\\/g;
$newPath = '"' . $newPath . '"';
# Append the code to fix the path.
Env::WriteLines($oh, "",
"# Insure the $var has our $type in it.",
"\$_ = $newPath;",
"if (! \$ENV{$var}) {",
" \$ENV{$var} = \$_;",
"} elsif (substr(\$ENV{$var}, 0, $newPathLen) ne \$_) {",
" \$ENV{$var} = \"\$_$delim\$ENV{$var}\";",
"}");
}
=head3 WriteAllConfigs
WriteAllConfigs($winMode, \%modules, $projDir, $opt);
Write out the path and library configuration parameters for a
vanilla environment. This creates a C<user-env> file in the
project directory and optionally builds the C<.includepath> files for all
the Eclipse projects. The C<user-env> file is used in a
command shell to set up environment variables for PERL includes and
execution paths and switch to the Data directory.
This method presumes the B<FIG_Config> file has been updated and L</RunFigConfig> has
been called to load its variables.
=over 4
=item winMode
TRUE if we are in Windows, else FALSE.
=item modules
Reference to a hash mapping each module name to its directory on
disk.
=item projDir
Name of the project directory.
=item jarDir
Name of the JAR directory.
=item opt
Command-line options object.
=item oldenv
Reference to a hash that contains the original environment.
=back
=cut
sub WriteAllConfigs {
# Get the parameters.
my ($winMode, $modules, $projDir, $jarDir, $opt, $oldenv) = @_;
# Compute the output file, the comment mark, and the path delimiter.
my $fileName = "$projDir/user-env.";
my ($delim, $rem);
if ($winMode) {
$fileName .= "cmd";
$delim = ";";
$rem = "REM ";
} else {
$fileName .= "sh";
$delim = ":";
$rem = "#";
}
# Open the output file.
open(my $oh, ">$fileName") || die "Could not open shell configuration file $fileName: $!";
# Do an ECHO OFF for Windows.
if ($winMode) {
print $oh "\@ECHO OFF\n";
}
print "Writing environment changes to $fileName.\n";
# Compute the script paths.
my $paths = join($delim, @FIG_Config::scripts);
# Write the comment.
print $oh "$rem Add SEEDtk scripts to the execution path.\n";
# Do the path update.
if ($winMode) {
# In Windows it's complicated, because variables at the command prompt are
# double-interpolated and we can't use them safely. We have to use an
# explicit path-- the current environment has the path we need already in it.
$paths = "$ENV{PATH}";
$paths =~ s/&/^&/g;
# Check for the GitHub for Windows option.
if ($opt->gfw) {
$paths .= ';%localappdata%\GitHub\PORTAB~1\cmd'
}
# Add the project directory if we need it.
my $projDirForPath = $projDir;
$projDirForPath =~ tr/\//\\/;
if (index($paths, $projDirForPath) < 0) {
if (substr($paths, -1, 1) ne ';') {
$paths .= ';';
}
$paths .= $projDirForPath;
}
print $oh "path $paths\n";
} else {
# Here we just need to add the bin directory to the path, and possibly a custom JDK.
# Start with the bin directory.
print $oh "export PATH=$projDir/bin:\$PATH\n";
}
# Set the PERL libraries.
my $libs = join($delim, @FIG_Config::libs);
print $oh "$rem Add SEEDtk libraries to the PERL library path.\n";
if ($ENV{PERL5LIB}) {
# There are already libraries, so set this up as an append operation.
$libs .= ($winMode ? ';%PERL5LIB%' : ':$PERL5LIB');
}
if ($winMode) {
print $oh "SET PERL5LIB=$libs\n";
} else {