[Koha-patches] [PATCH] bug_15562: Use do() rather than system() to execute updatedatabase.pl from installer.pl

Srdjan srdjan at catalyst.net.nz
Fri Feb 12 05:58:09 CET 2016


That way:
* no external process is spawned
* code executes in the same perl process, which is required for plack
  multi-site

I have a dream. A dream that one day all code from .pl's will be in some
.pm's.
---
 C4/Installer/PerlDependencies.pm |  5 +++++
 installer/install.pl             | 44 +++++++++++++++++++++++++---------------
 2 files changed, 33 insertions(+), 16 deletions(-)

diff --git a/C4/Installer/PerlDependencies.pm b/C4/Installer/PerlDependencies.pm
index be84077..9fa55d8 100644
--- a/C4/Installer/PerlDependencies.pm
+++ b/C4/Installer/PerlDependencies.pm
@@ -362,6 +362,11 @@ our $PERL_DEPS = {
         'required' => '1',
         'min_ver'  => '0.46'
     },
+    'Capture::Tiny' => {
+        'usage'    => 'Core',
+        'required' => '1',
+        'min_ver'  => '0.08'
+    },
     'HTTP::Cookies' => {
         'usage'    => 'Core',
         'required' => '1',
diff --git a/installer/install.pl b/installer/install.pl
index cc67557..e84d88b 100755
--- a/installer/install.pl
+++ b/installer/install.pl
@@ -7,6 +7,7 @@ use diagnostics;
 use C4::InstallAuth;
 use CGI qw ( -utf8 );
 use POSIX qw(strftime);
+use Capture::Tiny qw(capture);
 
 use C4::Context;
 use C4::Output;
@@ -332,30 +333,41 @@ elsif ( $step && $step == 3 ) {
         my $filename_suffix = join '_', $now, $dbversion, $kohaversion;
         my ( $logfilepath, $logfilepath_errors ) = ( chk_log($logdir, "updatedatabase_$filename_suffix"), chk_log($logdir, "updatedatabase-error_$filename_suffix") );
 
-        my $cmd = C4::Context->config("intranetdir") . "/installer/data/$info{dbms}/updatedatabase.pl >> $logfilepath 2>> $logfilepath_errors";
 
-        system($cmd );
+        my $script = C4::Context->config("intranetdir") . "/installer/data/$info{dbms}/updatedatabase.pl";
 
-        my $fh;
-        open( $fh, "<", $logfilepath ) or die "Cannot open log file $logfilepath: $!";
-        my @report = <$fh>;
-        close $fh;
-        if (@report) {
-            $template->param( update_report => [ map { { line => $_ } } split( /\n/, join( '', @report ) ) ] );
+        my ($stdout, $stderr, $exit) = capture { do( $script ) };
+        warn "Cannot execute $script: ".($@ || $!);
+
+        if ($stdout) {
+            if ( open( my $fh, ">>", $logfilepath ) ) {
+                print $fh $stdout;
+                close $fh;
+            }
+            else {
+                warn "Cannot open log file $logfilepath: $!";
+            }
+            $template->param( update_report => [ map { line => $_ }, split( /\n/, $stdout ) ] );
             $template->param( has_update_succeeds => 1 );
         } else {
-            eval{ `rm $logfilepath` };
+#           eval{ `rm $logfilepath` };
         }
-        open( $fh, "<", $logfilepath_errors ) or die "Cannot open log file $logfilepath_errors: $!";
-        @report = <$fh>;
-        close $fh;
-        if (@report) {
-            $template->param( update_errors => [ map { { line => $_ } } split( /\n/, join( '', @report ) ) ] );
+
+        if ($stderr) {
+            if ( open( my $fh, ">>", $logfilepath_errors ) ) {
+                print $fh $stderr;
+                close $fh;
+            }
+            else {
+                warn "Cannot open log file $logfilepath_errors: $!";
+            }
+            my @errors = split( /\n/, $stderr );
+            $template->param( update_errors => [ map { line => $_ }, @errors ] );
             $template->param( has_update_errors => 1 );
             warn "The following errors were returned while attempting to run the updatedatabase.pl script:\n";
-            foreach my $line (@report) { warn "$line\n"; }
+            warn $_ foreach @errors;
         } else {
-            eval{ `rm $logfilepath_errors` };
+#           eval{ `rm $logfilepath_errors` };
         }
         $template->param( $op => 1 );
     }
-- 
1.9.1


More information about the Koha-patches mailing list