script/moses-install.pl untested

filter: Statementstmt 0.0% 0 / 256 Branchbran 0.0% 0 / 88 Conditioncond 0.0% 0 / 96 MC/DCmcdc 0.0% 0 / 59 Subroutinesub 0.0% 0 / 26 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 536 CC 25 SCAR 28.8
CC
25
Cov
0%
BEGIN
56.0
prompt_for_directory
30.0
prompt
20.0
CRAP 17.8
1 #!/usr/local/bin/perl -w
2 #
3 # Prepare the stage...
4 #
5 # $Id: moses-install.pl,v 1.7 2009/04/08 22:42:51 kawas Exp $
6 # Contact: Martin Senger <martin.senger@gmail.com>
7 # -----------------------------------------------------------
8  
9 BEGIN {
10 use Getopt::Std;
11 use vars qw/ $opt_h $opt_F /;
12 getopt;
13  
14 # usage
15 if ($opt_h) {
16 print STDOUT <<'END_OF_USAGE';
17 Preparing stage for generating and running BioMoby services.
18 Usage: [-F]
19  
20 It creates necessary files (some of them by copying from
21 their templates):
22 moby-services.cfg
23 log4perl.properties
24 services.log
25 parser.log
26 MobyServer.cgi
27 AsyncMobyServer.cgi
28 The existing files are not overwritten - unless an option -F
29 has been used.
30  
31 It also creates and/or updates local cache of BioMoby
32 registries (after your confirmation).
33 END_OF_USAGE
34 exit (0);
35 }
36  
37 my $errors_found = 0;
38  
39 sub say { print @_, "\n"; }
40  
41 sub check_module {
42 eval "require $_[0]";
43 if ($@) {
44 $errors_found = 1;
45 say "Module $_[0] not installed.";
46 } else {
47 say "OK. Module $_[0] is installed.";
48 }
49 }
50  
51 use constant MSWIN => $^O =~ /MSWin32|Windows_NT/i ? 1 : 0;
52  
53 say 'Welcome, BioMobiers. Preparing stage for Perl MoSeS...';
54 say '------------------------------------------------------';
55  
56 # check needed modules
57 foreach $module ( qw (
58 FindBin
59 SOAP::Lite
60 XML::LibXML
61 Log::Log4perl
62 Template
63 Config::Simple
64 IO::Scalar
65 Unicode::String
66 ) ) {
67 check_module ($module);
68 }
69 # check for async libraries if user wants to ....
70 print STDOUT "Shall we check for the moby-async libraries\n\t(do this only if you plan on creating soap based\n\t async moby services)? y/n [n]";
71 my $tmp = <STDIN>;
72 $tmp =~ s/\s//g;
73 if ($tmp =~ /y/i) {
74 check_module('MOBY::Client::Central');
75 check_module('WSRF::Lite');
76 }
77  
78 if (MSWIN) {
79 check_module ('Term::ReadLine');
80 {
81 local $^W = 0;
82 $SimplePrompt::Terminal = Term::ReadLine->new ('Installation');
83 }
84 } else {
85 check_module ('IO::Prompt');
86 require IO::Prompt; import IO::Prompt;
87 }
88 if ($errors_found) {
89 say "\nSorry, some needed modules were not found.";
90 say "Please install them and run 'install.pl' again.";
91 exit (1);
92 }
93 say;
94 }
95 use File::HomeDir;
96 use File::ShareDir;
97 use File::Spec;
98 use MOSES::MOBY::Base;
99 use File::HomeDir;
100 use MOSES::MOBY::Cache::Central;
101 use MOSES::MOBY::Cache::Registries;
102 use English qw( -no_match_vars ) ;
103 use strict;
104  
105 # different prompt modules used for different OSs
106 # ('pprompt' as 'proxy_prompt')
107 sub pprompt {
108 return prompt (@_) unless MSWIN;
109 return SimplePrompt::prompt (@_);
110 }
111  
112 # $prompt ... a prompt asking for a directory
113 # $prompted_dir ... suggested directory
114 sub prompt_for_directory {
115 my ($prompt, $prompted_dir) = @_;
116 while (1) {
117 my $dir = pprompt ("$prompt [$prompted_dir] ");
118 $dir =~ s/^\s*//; $dir =~ s/\s*$//;
119 $dir = $prompted_dir unless $dir;
120 return $dir if -d $dir and -w $dir; # okay: writable directory
121 $prompted_dir = $dir;
122 next if -e $dir and say "'$dir' is not a writable directory. Try again please.";
123 next unless pprompt ("Directory '$dir' does not exists. Create? ", -yn);
124  
125 # okay, we agreed to create it
126 mkdir $dir and return $dir;
127 say "'$dir' not created: $!";
128 }
129 }
130  
131 # what registry to use
132 sub prompt_for_registry {
133 my $cache = new MOSES::MOBY::Cache::Central;
134 my @regs = MOSES::MOBY::Cache::Registries->list;
135 my $registry = pprompt ("What registry to use? [default] ",
136 -m => [@regs]);
137 return $registry ||= 'default';
138 }
139  
140 # create a file from a template
141 # - from $file_template to $file,
142 # - $file_desc used in messages,
143 # - hashref $filters tells what to change in template
144 sub file_from_template {
145 my ($file, $file_template, $file_desc, $filters) = @_;
146 eval {
147 open FILETEMPL, "<$file_template"
148 or die "Cannot read template file '$file_template': $!\n";
149 open FILEOUT, ">$file"
150 or die "Cannot open '$file' for writing: $!\n";
151 while (<FILETEMPL>) {
152 foreach my $token (keys %$filters) {
153 if ( $^O eq 'MSWin32' ) {
154 $filters->{$token} =~ s|\\|\/|;
155 }
156 s/\Q$token\E/$filters->{$token}/ge;
157 }
158 print FILEOUT
159 or die "Cannot write into '$file': $!\n";
160 }
161 close FILEOUT;
162 close FILETEMPL
163 or die "Cannot close '$file': $!\n";
164 };
165 if ($@) {
166 say "ERROR: $file_desc was (probably) not created.\n$@";
167 } else {
168 say "\n$file_desc created: '$file'\n";
169 }
170 }
171  
172  
173 # --- main ---
174 no warnings 'once';
175  
176 my $pmoses_home = File::Spec->catdir(File::HomeDir->my_home, "Perl-MoSeS");
177 my $jmoby_home = File::Spec->catdir(File::HomeDir->my_home, "Perl-MoSeS");
178 my $samples_home = File::Spec->catdir(File::HomeDir->my_home, "Perl-MoSeS", "sample-resources");
179 say "Installing in $pmoses_home\n";
180  
181 # create install directory if necessary
182 eval {
183 my ($v, $d, $f) = File::Spec->splitpath( $pmoses_home );
184 my $dir = File::Spec->catdir($v);
185 foreach my $part ( File::Spec->splitdir( ($d.$f ) ) ) {
186 $dir = File::Spec->catdir($dir, $part);
187 next if -d $dir or -e $dir;
188 mkdir( $dir ) || die("Error creating installation directory directory '".$dir."':\n$!");
189 }
190 };
191 say $@ ? $@ : "Created install directory '$pmoses_home'.";
192  
193 # create install directory if necessary
194 eval {
195 my ($v, $d, $f) = File::Spec->splitpath( $pmoses_home . "/cgi" );
196 my $dir = File::Spec->catdir($v);
197 foreach my $part ( File::Spec->splitdir( ($d.$f ) ) ) {
198 $dir = File::Spec->catdir($dir, $part);
199 next if -d $dir or -e $dir;
200 mkdir( $dir ) || die("Error creating installation directory directory '".$dir."':\n$!");
201 }
202 };
203 say $@ ? $@ : "Created install directory '$pmoses_home/cgi'.";
204  
205 # create install directory if necessary
206 eval {
207 my ($v, $d, $f) = File::Spec->splitpath( $samples_home );
208 my $dir = File::Spec->catdir($v);
209 foreach my $part ( File::Spec->splitdir( ($d.$f ) ) ) {
210 $dir = File::Spec->catdir($dir, $part);
211 next if -d $dir or -e $dir;
212 mkdir( $dir ) || die("Error creating installation directory directory '".$dir."':\n$!");
213 }
214 };
215 say $@ ? $@ : "Created sample-resources directory '$samples_home'.";
216  
217  
218 # log files (create, or just change their write permissions)
219 my $log_file1 = $MOBYCFG::LOG_FILE || File::Spec->catfile("$pmoses_home","services.log");
220 my $log_file2 = File::Spec->catfile("$pmoses_home","parser.log");
221 foreach my $file ($log_file1, $log_file2) {
222 unless (-e $file) {
223 eval {
224 open LOGFILE, ">$file" or die "Cannot create file '$file': $!\n";
225 close LOGFILE or die "Cannot create file '$file': $!\n";
226 };
227 say $@ ? $@ : "Created log file '$file'.";
228 }
229 chmod 0666, $file; # just in case a web server will be writing here
230 }
231  
232 # log4perl property file (will be found and used, or created)
233 my $log4perl_file = $MOBYCFG::LOG_CONFIG || File::Spec->catfile("$pmoses_home" ,"log4perl.properties");
234 if (-e $log4perl_file and ! $opt_F) {
235 say "\nLogging property file '$log4perl_file' exists.";
236 say "It will not be overwritten unless you start 'install.pl -F'.\n";
237 } else {
238 file_from_template
239 ($log4perl_file,
240 File::ShareDir::dist_file('MOSES-MOBY', 'log4perl.properties.template'),
241 'Log properties file',
242 { '@LOGFILE@' => $log_file1,
243 '@LOGFILE2@' => $log_file2,
244 } );
245 }
246  
247 # MobyServer.cgi file
248 my $generated_dir = $MOBYCFG::GENERATORS_OUTDIR ||
249 "$pmoses_home/generated";
250 my $services_dir = $MOBYCFG::GENERATORS_IMPL_OUTDIR ||
251 "$pmoses_home/services";
252 my $services_table = $MOBYCFG::GENERATORS_IMPL_SERVICES_TABLE ||
253 'SERVICES_TABLE';
254 my $async_services_table = $MOBYCFG::GENERATORS_IMPL_SERVICES_TABLE ||
255 'ASYNC_SERVICES_TABLE';
256  
257 my $cgibin_file = "$pmoses_home/MobyServer.cgi";
258 if (-e $cgibin_file and ! $opt_F) {
259 say "\nWeb Server file '$cgibin_file' exists.";
260 say "It will not be overwritten unless you start 'install.pl -F'.\n";
261 } else {
262 file_from_template
263 ($cgibin_file,
264 File::ShareDir::dist_file('MOSES-MOBY','MobyServer.cgi.template'),
265 'Web Server file',
266 { '@PMOSES_HOME@' => $pmoses_home,
267 '@GENERATED_DIR@' => $generated_dir,
268 '@SERVICES_DIR@' => $services_dir,
269 '@SERVICES_TABLE@' => $services_table,
270 } );
271 chmod 0755, $cgibin_file; # everybody can execute
272 }
273  
274 # AsyncMobyServer.cgi file
275  
276 my $async_cgibin_file = "$pmoses_home/AsyncMobyServer.cgi";
277 if (-e $async_cgibin_file and ! $opt_F) {
278 say "\nWeb Server file '$async_cgibin_file' exists.";
279 say "It will not be overwritten unless you start 'install.pl -F'.\n";
280 } else {
281 file_from_template
282 ($async_cgibin_file,
283 File::ShareDir::dist_file('MOSES-MOBY','AsyncMobyServer.cgi.template'),
284 'Web Server file',
285 { '@PMOSES_HOME@' => $pmoses_home,
286 '@GENERATED_DIR@' => $generated_dir,
287 '@SERVICES_DIR@' => $services_dir,
288 '@ASYNC_SERVICE_TABLE@' => $async_services_table,
289 } );
290 chmod 0755, $async_cgibin_file; # everybody can execute
291 }
292  
293 # directory for local cache
294 my $cachedir = $MOBYCFG::CACHEDIR ||
295 prompt_for_directory ( 'Directory for local cache',
296 File::Spec->catdir("$jmoby_home","myCache"));
297 say "Local cache in '$cachedir'.\n";
298  
299 # filling/updating local cache
300 my $registry = 'default';
301 if ('y' eq pprompt ('Should I try to fill or update the local cache [y]? ', -ynd=>'y')) {
302 $registry = prompt_for_registry;
303 my $details =
304 MOSES::MOBY::Cache::Registries->get ($registry);
305 if ($details) {
306 my $endpoint = $details->{endpoint};
307 my $uri = $details->{namespace};
308 say 'Using registry: ' . $registry;
309 say "(at $endpoint)\n";
310
311 my $cache = MOSES::MOBY::Cache::Central->new (cachedir => $cachedir, registry=>$registry);
312 say "Getting the BioMOBY datatypes ...\n";
313 eval {
314 $cache->update_datatype_cache();
315 };
316 eval {
317 $cache->create_datatype_cache();
318 } if $@;
319
320 say "Getting the BioMOBY services ...\n";
321 eval {
322 $cache->update_service_cache();
323 };
324 eval {
325 $cache->create_service_cache();
326 } if $@;
327
328 say "ERROR: There was a problem updating the cache." if $@;
329 }
330
331 }
332
333 # configuration file (will be found and used, or created)
334 my $config_file = File::Spec->catfile
335 ($ENV{$MOSES::MOBY::Config::ENV_CONFIG_DIR} || $pmoses_home,
336 $MOSES::MOBY::Config::DEFAULT_CONFIG_FILE);
337 if (-e $config_file and ! $opt_F) {
338 say "Configuration file $config_file exists.";
339 say "It will be used and not overwritten unless you start 'install.pl -F'.\n";
340 } else {
341 file_from_template
342 ($config_file,
343 File::ShareDir::dist_file('MOSES-MOBY','moby-services.cfg.template'),
344 'Configuration file',
345 { '@CACHE_DIR@' => $cachedir,
346 '@REGISTRY@' => $registry,
347 '@GENERATED_DIR@' => $generated_dir,
348 '@SERVICES_DIR@' => $services_dir,
349 '@SERVICES_TABLE@' => $services_table,
350 '@ASYNC_SERVICES_TABLE@' => $async_services_table,
351 '@LOG4PERL_FILE@' => $log4perl_file,
352 '@LOGFILE@' => $log_file1,
353 '@USER_REGISTRIES_FILE_DIR@' => $pmoses_home,
354 '@MABUHAY_RESOURCE@' =>
355 "$samples_home/mabuhay.file",
356 } );
357 }
358  
359 # install the mabuhay_file
360 my $mabuhay_file = $MOBYCFG::MABUHAY_RESOURCE || File::Spec->catfile("$samples_home","mabuhay.file"); #"$pmoses_home/samples-resources/mabuhay.file";
361 if (-e $mabuhay_file and ! $opt_F) {
362 say "Mabuhay file $mabuhay_file exists.";
363 say "It will be used and not overwritten unless you start 'install.pl -F'.\n";
364 } else {
365 file_from_template
366 ($mabuhay_file,
367 File::ShareDir::dist_file('MOSES-MOBY', 'mabuhay.file'),
368 'Mabuhay Resource File',
369 {} );
370 }
371  
372 # install the user_registries file
373 my $registries_file = "$MOBYCFG::USER_REGISTRIES_USER_REGISTRIES_DIR/$MOBYCFG::USER_REGISTRIES_USER_REGISTRIES_FILENAME"
374 if $MOBYCFG::USER_REGISTRIES_USER_REGISTRIES_DIR and $MOBYCFG::USER_REGISTRIES_USER_REGISTRIES_FILENAME;
375 $registries_file = File::Spec->catfile("$pmoses_home","USER_REGISTRIES") if not $registries_file or $registries_file eq "";
376 if (-e $registries_file and ! $opt_F) {
377 say "User Registrie file $registries_file exists.";
378 say "It will be used and not overwritten unless you start 'install.pl -F'.\n";
379 } else {
380 file_from_template
381 ($registries_file,
382 File::ShareDir::dist_file('MOSES-MOBY', "USER_REGISTRIES"),
383 'User Registries File',
384 {} );
385 }
386  
387 say 'Done.';
388  
389 package SimplePrompt;
390  
391 use vars qw/ $Terminal /;
392  
393 sub prompt {
394 my ($msg, $flags, $others) = @_;
395  
396 # simple prompt
397 return get_input ($msg)
398 unless $flags;
399  
400 $flags =~ s/^-//o; # ignore leading dash
401  
402 # 'waiting for yes/no' prompt, possibly with a default value
403 if ($flags =~ /^yn(d)?/i) {
404 return yes_no ($msg, $others);
405 }
406  
407 # prompt with a menu of possible answers
408 if ($flags =~ /^m/i) {
409 return menu ($msg, $others);
410 }
411  
412 # default: again a simple prompt
413 return get_input ($msg);
414 }
415  
416 sub yes_no {
417 my ($msg, $default_answer) = @_;
418 while (1) {
419 my $answer = get_input ($msg);
420 return $default_answer if $default_answer and $answer =~ /^\s*$/o;
421 return 'y' if $answer =~ /^(1|y|yes|ano)$/;
422 return 'n' if $answer =~ /^(0|n|no|ne)$/;
423 }
424 }
425  
426 sub get_input {
427 my ($msg) = @_;
428 local $^W = 0;
429 my $line = $Terminal->readline ($msg);
430 chomp $line; # remove newline
431 $line =~ s/^\s*//; $line =~ s/\s*$//; # trim whitespaces
432 $Terminal->addhistory ($line) if $line;
433 return $line;
434 }
435  
436 sub menu {
437 my ($msg, $ra_menu) = @_;
438 my @data = @$ra_menu;
439  
440 my $count = @data;
441 # die "Too many -menu items" if $count > 26;
442 # die "Too few -menu items" if $count < 1;
443  
444 my $max_char = chr(ord('a') + $count - 1);
445 my $menu = '';
446  
447 my $next = 'a';
448 foreach my $item (@data) {
449 $menu .= ' ' . $next++ . '.' . $item . "\n";
450 }
451 while (1) {
452 print STDOUT $msg . "\n$menu";
453 my $answer = get_input (">");
454  
455 # blank and escape answer accepted as undef
456 return undef if $answer =~ /^\s*$/o;
457 return undef
458 if length $answer == 1 && $answer eq "\e";
459  
460 # invalid answer not accepted
461 if (length $answer > 1 || ($answer lt 'a' || $answer gt $max_char) ) {
462 print STDOUT "(Please enter a-$max_char)\n";
463 next;
464 }
465  
466 # valid answer
467 return $data[ord($answer)-ord('a')];
468 }
469 }
470  
471  
472 __END__