blib/script/owl2perl-install.pl untested

filter: Statementstmt 0.0% 0 / 179 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 62 MC/DCmcdc 0.0% 0 / 38 Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 372 CC 24 SCAR 27.8
CC
24
Cov
0%
BEGIN
42.0
prompt_for_directory
30.0
prompt
20.0
CRAP 16.1
1 #!/usr/local/bin/perl -w
2 #
3 # Prepare the stage...
4 #
5 # $Id: owl2perl-install.pl,v 1.7 2010-03-09 16:39:14 ubuntu Exp $
6 # Contact: Edward Kawas <edward.kawas+owl2perl@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 Perl modules from owl ontologies.
18 Usage: [-F]
19
20 It creates necessary files (some of them by copying from
21 their templates):
22 owl2perl-config.cfg
23 log4perl.properties
24 generator.log
25 The existing files are not overwritten - unless an option -F
26 has been used.
27
28 END_OF_USAGE
29 exit(0);
30 }
31
32 my $errors_found = 0;
33
34 sub say { print @_, "\n"; }
35
36 sub check_module {
37 eval "require $_[0]";
38 if ($@) {
39 $errors_found = 1;
40 say "Module $_[0] not installed.";
41 } else {
42 say "OK. Module $_[0] is installed.";
43 }
44 }
45
46 use constant MSWIN => $^O =~ /MSWin32|Windows_NT/i ? 1 : 0;
47
48 say 'Welcome! Preparing stage for OWL2Perl ...';
49 say '------------------------------------------------------';
50
51 # check needed modules
52 foreach $module (
53 qw (
54 Carp
55 File::Spec
56 Config::Simple
57 File::HomeDir
58 File::ShareDir
59 Log::Log4perl
60 HTTP::Date
61 Template
62 Params::Util
63 Class::Inspector
64 Unicode::String
65 IO::String
66 RDF::Core
67 )
68 )
69 {
70 check_module($module);
71 }
72
73 if (MSWIN) {
74 check_module('Term::ReadLine');
75 {
76 local $^W = 0;
77 $SimplePrompt::Terminal = Term::ReadLine->new('Installation');
78 }
79 } else {
80 check_module('IO::Prompt');
81 require IO::Prompt;
82 import IO::Prompt;
83 }
84 if ($errors_found) {
85 say "\nSorry, some needed modules were not found.";
86 say "Please install them and run 'owl2perl-install.pl' again.";
87 exit(1);
88 }
89 say;
90 }
91 use File::HomeDir;
92 use File::ShareDir;
93 use File::Spec;
94 use OWL::Base;
95 use File::HomeDir;
96 use English qw( -no_match_vars );
97 use strict;
98
99 # different prompt modules used for different OSs
100 # ('pprompt' as 'proxy_prompt')
101 sub pprompt {
102 return prompt(@_) unless MSWIN;
103 return SimplePrompt::prompt(@_);
104 }
105
106 # $prompt ... a prompt asking for a directory
107 # $prompted_dir ... suggested directory
108 sub prompt_for_directory {
109 my ( $prompt, $prompted_dir ) = @_;
110 while (1) {
111 my $dir = pprompt("$prompt [$prompted_dir] ");
112 $dir =~ s/^\s*//;
113 $dir =~ s/\s*$//;
114 $dir = $prompted_dir unless $dir;
115 return $dir if -d $dir and -w $dir; # okay: writable directory
116 $prompted_dir = $dir;
117 next if -e $dir and say "'$dir' is not a writable directory. Try again please.";
118 next unless pprompt( "Directory '$dir' does not exists. Create? ", -yn );
119
120 # okay, we agreed to create it
121 mkdir $dir and return $dir;
122 say "'$dir' not created: $!";
123 }
124 }
125
126 # create a file from a template
127 # - from $file_template to $file,
128 # - $file_desc used in messages,
129 # - hashref $filters tells what to change in template
130 sub file_from_template {
131 my ( $file, $file_template, $file_desc, $filters ) = @_;
132 eval {
133 open FILETEMPL, "<$file_template"
134 or die "Cannot read template file '$file_template': $!\n";
135 open FILEOUT, ">$file"
136 or die "Cannot open '$file' for writing: $!\n";
137 while (<FILETEMPL>) {
138 foreach my $token ( keys %$filters ) {
139 if ( $^O eq 'MSWin32' ) {
140 $filters->{$token} =~ s|\\|\/|;
141 }
142 s/\Q$token\E/$filters->{$token}/ge;
143 }
144 print FILEOUT
145 or die "Cannot write into '$file': $!\n";
146 }
147 close FILEOUT;
148 close FILETEMPL
149 or die "Cannot close '$file': $!\n";
150 };
151 if ($@) {
152 say "ERROR: $file_desc was (probably) not created.\n$@";
153 } else {
154 say "\n$file_desc created: '$file'\n";
155 }
156 }
157
158 # --- main ---
159 no warnings 'once';
160
161 my $owl2perl_home = File::Spec->catdir( File::HomeDir->my_home, "Perl-OWL2Perl" );
162 my $samples_home =
163 File::Spec->catdir( File::HomeDir->my_home, "Perl-OWL2Perl", "sample-resources" );
164 say "Installing in $owl2perl_home\n";
165
166 # create install directory if necessary
167 eval {
168 my ( $v, $d, $f ) = File::Spec->splitpath($owl2perl_home);
169 my $dir = File::Spec->catdir($v);
170 foreach my $part ( File::Spec->splitdir( ( $d . $f ) ) ) {
171 $dir = File::Spec->catdir( $dir, $part );
172 next if -d $dir or -e $dir;
173 mkdir($dir)
174 || die(
175 "Error creating installation directory directory '" . $dir . "':\n$!" );
176 }
177 };
178 say $@ ? $@ : "Created install directory '$owl2perl_home'.";
179
180 # log files (create, or just change their write permissions)
181 my $log_file1 = $OWLCFG::LOG_FILE
182 || File::Spec->catfile( "$owl2perl_home", "generator.log" );
183 foreach my $file ( $log_file1 ) {
184 unless ( -e $file ) {
185 eval {
186 open LOGFILE, ">$file" or die "Cannot create file '$file': $!\n";
187 close LOGFILE or die "Cannot create file '$file': $!\n";
188 };
189 say $@ ? $@ : "Created log file '$file'.";
190 }
191 chmod 0666, $file; # just in case a web server will be writing here
192 }
193
194 # log4perl property file (will be found and used, or created)
195 my $log4perl_file = $OWLCFG::LOG_CONFIG
196 || File::Spec->catfile( "$owl2perl_home", "log4perl.properties" );
197 if ( -e $log4perl_file and !$opt_F ) {
198 say "\nLogging property file '$log4perl_file' exists.";
199 say "It will not be overwritten unless you start 'install.pl -F'.\n";
200 } else {
201 file_from_template(
202 $log4perl_file,
203 File::ShareDir::dist_file(
204 'OWL2Perl', 'log4perl.properties.template'
205 ),
206 'Log properties file',
207 {
208 '@LOGFILE@' => $log_file1,
209 }
210 );
211 }
212
213 # define some directories
214 my $generated_dir = $OWLCFG::GENERATORS_OUTDIR
215 || "$owl2perl_home/generated";
216
217 eval {
218 my ( $v, $d, $f ) = File::Spec->splitpath( $generated_dir );
219 my $dir = File::Spec->catdir($v);
220 foreach my $part ( File::Spec->splitdir( ( $d . $f ) ) ) {
221 $dir = File::Spec->catdir( $dir, $part );
222 next if -d $dir or -e $dir;
223 mkdir($dir)
224 || die( "Error creating generated_dir directory '" . $dir . "':\n$!" );
225 }
226 open (FHO,">$generated_dir/README")
227 and print FHO 'This directory will contain any generated OWL2Perl modules.';
228 close(FHO);
229 };
230 say $@ ? $@ : "Created generated '$generated_dir'.";
231
232 # configuration file (will be found and used, or created)
233 my $config_file =
234 File::Spec->catfile( $ENV{$OWL::Config::ENV_CONFIG_DIR} || $owl2perl_home,
235 $OWL::Config::DEFAULT_CONFIG_FILE );
236 if ( -e $config_file and !$opt_F ) {
237 say "Configuration file $config_file exists.";
238 say "It will be used and not overwritten unless you start 'owl2perl-install.pl -F'.\n";
239 } else {
240 file_from_template(
241 $config_file,
242 File::ShareDir::dist_file(
243 'OWL2Perl', 'owl2perl-config.cfg.template'
244 ),
245 'Configuration file',
246 {
247 '@GENERATED_DIR@' => $generated_dir,
248 '@HOME_DIR@' => $owl2perl_home,
249 '@LOG4PERL_FILE@' => $log4perl_file,
250 '@LOGFILE@' => $log_file1,
251 }
252 );
253 }
254
255 say 'Done.';
256
257 package SimplePrompt;
258
259 use vars qw/ $Terminal /;
260
261 sub prompt {
262 my ( $msg, $flags, $others ) = @_;
263
264 # simple prompt
265 return get_input($msg)
266 unless $flags;
267
268 $flags =~ s/^-//o; # ignore leading dash
269
270 # 'waiting for yes/no' prompt, possibly with a default value
271 if ( $flags =~ /^yn(d)?/i ) {
272 return yes_no( $msg, $others );
273 }
274
275 # prompt with a menu of possible answers
276 if ( $flags =~ /^m/i ) {
277 return menu( $msg, $others );
278 }
279
280 # default: again a simple prompt
281 return get_input($msg);
282 }
283
284 sub yes_no {
285 my ( $msg, $default_answer ) = @_;
286 while (1) {
287 my $answer = get_input($msg);
288 return $default_answer if $default_answer and $answer =~ /^\s*$/o;
289 return 'y' if $answer =~ /^(1|y|yes|ano)$/;
290 return 'n' if $answer =~ /^(0|n|no|ne)$/;
291 }
292 }
293
294 sub get_input {
295 my ($msg) = @_;
296 local $^W = 0;
297 my $line = $Terminal->readline($msg);
298 chomp $line; # remove newline
299 $line =~ s/^\s*//;
300 $line =~ s/\s*$//; # trim whitespaces
301 $Terminal->addhistory($line) if $line;
302 return $line;
303 }
304
305 sub menu {
306 my ( $msg, $ra_menu ) = @_;
307 my @data = @$ra_menu;
308
309 my $count = @data;
310
311 # die "Too many -menu items" if $count > 26;
312 # die "Too few -menu items" if $count < 1;
313
314 my $max_char = chr( ord('a') + $count - 1 );
315 my $menu = '';
316
317 my $next = 'a';
318 foreach my $item (@data) {
319 $menu .= ' ' . $next++ . '.' . $item . "\n";
320 }
321 while (1) {
322 print STDOUT $msg . "\n$menu";
323 my $answer = get_input(">");
324
325 # blank and escape answer accepted as undef
326 return undef if $answer =~ /^\s*$/o;
327 return undef
328 if length $answer == 1 && $answer eq "\e";
329
330 # invalid answer not accepted
331 if ( length $answer > 1 || ( $answer lt 'a' || $answer gt $max_char ) ) {
332 print STDOUT "(Please enter a-$max_char)\n";
333 next;
334 }
335
336 # valid answer
337 return $data[ ord($answer) - ord('a') ];
338 }
339 }
340
341 __END__