Changeset 24600


Ignore:
Timestamp:
2011-09-15T16:31:11+12:00 (13 years ago)
Author:
ak19
Message:

Added gs-magick.pl script which will set the environment for ImageMagick (including LD_LIBRARY_PATH) before launching the requested ImageMagick command and arguments. By setting the Imagemagick environment from this script we ensure that the modified env variables don't create conflicts with libraries needed for normal linux execution. All the Greenstone files in the *binary* that made direct calls to imagemagick now go through this script. The affected files are perl files in bin/script and perllib and Gatherer.java of GLI. (wvware has files that test for imagemagick during compilation stage, which is independent of our changs which are only for users running imagemagick from a GS binary.) The final problems were related to how different perl files made use of the return values and the output of running their imagemagick command: they would query the 127 and/or and/or run the command with backtick operators to get the output printed to STDOUT. By inserting an intermediate gs-magick.pl file, needed to ensure that the exit code stored in 127 would at least be passed on correctly, as is necessary when testing the exit code against non-zero values or greater/less than zero (instead of comparing them with equals/not equal to 0). To get the correct exit code as emitted by imagemagick, calling code needs to shift bits in 127 and converting it to a signed value.

Location:
main/trunk
Files:
1 added
10 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/gli/src/org/greenstone/gatherer/Gatherer.java

    r24388 r24600  
    439439            }
    440440
    441             // Check for ImageMagick
    442             if (Gatherer.isGsdlRemote) {
    443                 DebugStream.println("Not checking for ImageMagick.");
    444             }
    445             else if (!(new ImageMagickTest()).found()) {
    446                 // Time for a warning message
    447                 missingImageMagick();
    448             }
    449            
    450             // Check for PDFBox
    451             if (Gatherer.isGsdlRemote) {
    452                 DebugStream.println("Not checking for PDFBox.");
    453             }
    454             else {
    455                 String gs_dir = GS3 ? gsdl3_src_path : gsdl_path;
    456                 File pdfboxExtensionFolder = new File(gs_dir+File.separator+"ext"+File.separator+"pdf-box");
    457                 if (!(pdfboxExtensionFolder.exists() && pdfboxExtensionFolder.isDirectory())) {
    458                 // The user doesn't have PDFBox, inform them of it
    459                 String zipExtension = Utility.isWindows() ? "zip" : "tar.gz";
    460                 missingPDFBox(zipExtension, pdfboxExtensionFolder.getParent());
    461                 }
    462             }
    463 
    464441
    465442            if (Gatherer.isGsdlRemote) {
     
    490467                    missingPERL();
    491468                }
     469            }
     470
     471
     472            // Check for ImageMagick - dependent on perl_path
     473            if (Gatherer.isGsdlRemote) {
     474                DebugStream.println("Not checking for ImageMagick.");
     475            }
     476            else if (!(new ImageMagickTest()).found()) {
     477                // Time for a warning message
     478                missingImageMagick();
     479            }
     480           
     481            // Check for PDFBox
     482            if (Gatherer.isGsdlRemote) {
     483                DebugStream.println("Not checking for PDFBox.");
     484            }
     485            else {
     486                String gs_dir = GS3 ? gsdl3_src_path : gsdl_path;
     487                File pdfboxExtensionFolder = new File(gs_dir+File.separator+"ext"+File.separator+"pdf-box");
     488                if (!(pdfboxExtensionFolder.exists() && pdfboxExtensionFolder.isDirectory())) {
     489                // The user doesn't have PDFBox, inform them of it
     490                String zipExtension = Utility.isWindows() ? "zip" : "tar.gz";
     491                missingPDFBox(zipExtension, pdfboxExtensionFolder.getParent());
     492                }
    492493            }
    493494
     
    15631564        public boolean found()
    15641565        {
     1566            // at this stage, GLI has already sourced setup.bash, and the necessary
     1567            // env variables will be available to the perl process we're about to launch
     1568            boolean found = false;
     1569
    15651570            try {
    1566                 String[] command = new String[2];
    1567                 command[0] = (Utility.isWindows() ? "identify.exe" : "identify");
    1568                 command[1] = "-version";
    1569                 Process image_magick_process = Runtime.getRuntime().exec(command);
     1571                // run the command `/path/to/perl -S gs-magick.pl identify -version`
     1572                ArrayList cmd_list = new ArrayList();
     1573                if (!Gatherer.isGsdlRemote) {
     1574                if(Configuration.perl_path != null) {
     1575                    cmd_list.add(Configuration.perl_path);
     1576                } else {
     1577                    System.err.println("***** ImageMagickTest Warning: Perl_path not set, calling 'perl' instead.");
     1578                    cmd_list.add("perl");
     1579                }
     1580                cmd_list.add("-S");
     1581                }
     1582                cmd_list.add("gs-magick.pl");
     1583                if(Utility.isWindows()) {
     1584                cmd_list.add("identify.exe");
     1585                } else {
     1586                cmd_list.add("identify");
     1587                }
     1588                cmd_list.add("-version");
     1589
     1590                String[] command_parts = (String[]) cmd_list.toArray(new String[0]);
     1591
     1592                String cmd_str = "";
     1593                for(int i = 0; i < command_parts.length; i++) {
     1594                    cmd_str += command_parts[i] + " ";
     1595                }
     1596                DebugStream.println("***** Running ImageMagickTest command: " + cmd_str);
     1597
     1598                Process image_magick_process = Runtime.getRuntime().exec(command_parts);
    15701599                image_magick_process.waitFor();
    15711600
     
    15741603
    15751604                BufferedReader br = new BufferedReader(isr);
    1576                 // Capture the standard output stream and seach for two particular occurances: Version and ImageMagick.
     1605                // Capture the standard output stream and seach for two particular occurrences: Version and ImageMagick.
    15771606
    15781607                String line = br.readLine();
    1579                 if (line == null) {
    1580                     return false;
    1581                 }
    1582                 String lc_line = line.toLowerCase();
    1583                 if (lc_line.indexOf("version") != -1 || lc_line.indexOf("imagemagick") != -1) {
    1584                     return true;
    1585                 } else {
    1586                     return false;
    1587                 }
    1588 
     1608                if (line != null) {
     1609                    String lc_line = line.toLowerCase();
     1610                    if (lc_line.indexOf("version") != -1 || lc_line.indexOf("imagemagick") != -1) {
     1611                    //System.err.println("*** ImageMagickTest Line: " + line);
     1612                    found = true;
     1613                    } // else found var remains false
     1614                }
     1615
     1616                // Maybe put the close in a finally (but note that it can throw and IOex too)? See
     1617                // http://download.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
     1618                br.close();
     1619                return found;
    15891620                //return (image_magick_process.exitValue() == 0);
    15901621            }
    1591             catch (IOException exception) {
    1592                 return false;
    1593             }
    1594             catch (InterruptedException exception) {
    1595                 return false;
     1622            catch (Exception exception) {   
     1623                exception.printStackTrace();
     1624                return found;
    15961625            }
    15971626        }
  • main/trunk/greenstone2/bin/script/gsConvert.pl

    r24513 r24600  
    824824    # Check that ImageMagick is installed and available on the path (except for Windows 95/98)
    825825    if (!($ENV{'GSDLOS'} eq "windows" && !Win32::IsWinNT())) {
    826     my $result = `identify 2>&1`;
    827     if ($? == -1 || $? == 256) {  # Linux and Windows return different values for "program not found"
     826    my $imagick_cmd = "\"".&util::get_perl_exec()."\" -S gs-magick.pl";
     827    my $result = `$imagick_cmd identify 2>&1`;
     828
     829    # Linux and Windows return different values for "program not found".
     830    # Linux returns -1 and Windows 256 for "program not found". But once they're
     831    # converted to signed values, it will be -1 for Linux and 1 for Windows.
     832    # Whenever we test for return values other than 0, shift by 8 and perform
     833    # unsigned to signed status conversion on $? to get expected range of return vals
     834    # Although gs-magick.pl already shifts its $? by 8, converts it to a signed value
     835    # and then exits on that, by the time we get here, we need to do it again
     836    my $status = $?;
     837    $status >>= 8;
     838    $status = (($status & 0x80) ? -(0x100 - ($status & 0xFF)) : $status);   
     839    if ($status == -1 || $status == 1) { #if ($status == -1 || $status == 256) {
    828840        #ImageMagick is not installed, thus the convert utility is not available.
    829841        print STDERR "*** ImageMagick is not installed, the convert utility is not available. Unable to convert PDF/PS to images\n";
  • main/trunk/greenstone2/bin/script/pdfpstoimg.pl

    r17328 r24600  
    8888    # with quoting when GSDLHOME might contain spaces) but assume
    8989    # that the PATH is set up correctly.
    90     $cmd = "convert";
     90    $cmd = "\"".&util::get_perl_exec()."\" -S gs-magick.pl convert";
    9191
    9292    my $output_filename = &util::filename_cat($output_filestem, $input_basename);
     
    100100    # note we return 0 if the file is "encrypted"
    101101    $!=0;
    102     if (system($cmd)!=0) {
     102    my $status = system($cmd);
     103    if ($status != 0) {
    103104    print STDERR "Convert error for $input_filename $!\n";
    104105    # leave these for gsConvert.pl...
  • main/trunk/greenstone2/bin/script/pdftohtml.pl

    r7643 r24600  
    249249        $cmd = "pnmtopng \"${directory}$image\" > \"${directory}$image_base.png\" 2>/dev/null";
    250250        if (system($cmd)!=0) {
    251         $cmd = "convert \"${directory}$image\" \"${directory}$image_base.png\" 2>/dev/null";
     251        $cmd = "\"".&util::get_perl_exec()."\" -S gs-magick.pl convert \"${directory}$image\" \"${directory}$image_base.png\" 2>/dev/null";
    252252        if (system($cmd)!=0) {
    253253            print STDERR "Cannot convert $image into PNG format (tried `pnmtopng' and `convert')...\n";
  • main/trunk/greenstone2/perllib/cgiactions/imageaction.pm

    r24597 r24600  
    2929
    3030use cgiactions::baseaction;
     31use util;
    3132
    3233@imageaction::ISA = ('baseaction');
     
    158159
    159160
    160     my $cmd = "convert \"$src_full_assoc_filename\" ";
     161    my $cmd = "\"".&util::get_perl_exec()."\" -S gs-magick.pl convert \"$src_full_assoc_filename\" ";
    161162    $cmd .= "-rotate 90 " if ($orientation eq "landscape");
    162163
  • main/trunk/greenstone2/perllib/convertutil.pm

    r22469 r24600  
    175175    if ($command_status != 0) {
    176176        $had_error = 1;
    177 
    178         print $outhandle "Error: processing command failed.  Exit status $?\n";
     177       
     178        # for imagemagick commands that go via gs-magick.pl, need to shift exit code by 8 and then
     179        # convert to its signed value to get the actual exit code that imagemagick had emitted.
     180        my $signed_cmd_status = $command_status;
     181        $signed_cmd_status >>= 8;
     182        $signed_cmd_status = (($signed_cmd_status & 0x80) ? -(0x100 - ($signed_cmd_status & 0xFF)) : $signed_cmd_status);   
     183
     184        print $outhandle "Error: processing command failed.  Exit status $command_status (signed value: $signed_cmd_status)\n";
    179185
    180186        if ($verbosity >= 3) {
  • main/trunk/greenstone2/perllib/giget.pm

    r15889 r24600  
    11use strict;
     2use util;
    23
    34
     
    150151        unlink $output_fname;
    151152        }
    152         elsif (system("identify \"$output_fname\"") > 0 ) {
    153         print STDERR "**** NOT JPEG: output_fname \n";
    154         unlink $output_fname;
    155         }
    156153        else {
    157 
    158         my $command = "identify \"$output_fname\" 2>&1";
    159         my $result = `$command`;
    160 
    161         my $type =   'unknown';
    162         my $width =  'unknown';
    163         my $height = 'unknown';
    164        
    165         my $image_safe = quotemeta $output_fname;
    166         if ($result =~ /^$image_safe (\w+) (\d+)x(\d+)/) {
    167             $type = $1;
    168             $width = $2;
    169             $height = $3;
     154        # need to shift the $? exit code returned by system() by 8 bits and
     155        # then convert it to a signed value to work out whether it is indeed > 0
     156        my $status = system("\"".&util::get_perl_exec()."\" -S gs-magick.pl identify \"$output_fname\"");
     157        $status >>= 8;
     158        $status = (($status & 0x80) ? -(0x100 - ($status & 0xFF)) : $status);
     159
     160        if($status > 0 ) {
     161            print STDERR "**** NOT JPEG: output_fname \n";
     162            unlink $output_fname;
     163        }
     164        else {
     165            my $command = "\"".&util::get_perl_exec()."\" -S gs-magick.pl identify \"$output_fname\" 2>&1";
     166            my $result = `$command`;
     167           
     168            my $type =   'unknown';
     169            my $width =  'unknown';
     170            my $height = 'unknown';
     171           
     172            my $image_safe = quotemeta $output_fname;
     173            if ($result =~ /^$image_safe (\w+) (\d+)x(\d+)/) {
     174            $type = $1;
     175            $width = $2;
     176            $height = $3;
     177            }
     178           
     179            my $imagick_cmd = "\"".&util::get_perl_exec()."\" -S gs-magick.pl";
     180           
     181            if (($width ne "unknown") && ($height ne "unknown")) {
     182            if (($width>200) || ($height>200)) {
     183                `$imagick_cmd convert \"$output_fname\" -resize 200x200 /tmp/x.jpg`;
     184                `/bin/mv /tmp/x.jpg \"$output_fname\"`;
     185            }
     186            }
     187            $c++;
    170188        }
    171 
    172         if (($width ne "unknown") && ($height ne "unknown")) {
    173             if (($width>200) || ($height>200)) {
    174             `convert \"$output_fname\" -resize 200x200 /tmp/x.jpg`;
    175             `/bin/mv /tmp/x.jpg \"$output_fname\"`;
    176             }
    177         }
    178         $c++;
    179189        }
    180190
  • main/trunk/greenstone2/perllib/plugins/HTMLImagePlugin.pm

    r21742 r24600  
    451451    my ($filename, $orig_fp, $fn, $ext, $reltext, $relreltext, $crcid, $imgs,
    452452    $thumbfp, $pagetitle, $alttext, $filepath, $aggr);
     453
     454    my $imagick_cmd = "\"".&util::get_perl_exec()."\" -S gs-magick.pl";
     455
    453456    $aggr = $self->{'aggressiveness'};
    454457    $imgs = \%{$self->{'imglist'}};
     
    463466    ($fn, $ext) = $onlyfn =~ /(.*)\.(.*)/;
    464467    $fn = lc $fn; $ext = lc $ext;
    465     ($reltext) = "<tr><td>GifComment</td><td>" . `identify $filepath -ping -format "%c"` . "</td></tr>\n"
     468    ($reltext) = "<tr><td>GifComment</td><td>" . `$imagick_cmd identify $filepath -ping -format "%c"` . "</td></tr>\n"
    466469        if ($ext eq "gif");
    467470    $reltext .= "<tr><td>FilePath</td><td>$orig_fp</td></tr>\n";
     
    469472    if ($ENV{'GSDLOS'} =~ /^windows$/i) {
    470473    $crcid = "$fn.$ext." . $self->{'next_crcid'}++;
    471     } else { ($crcid) = `cksum $filepath` =~ /^(\d+)/; }
     474    } else {
     475    ($crcid) = `cksum $filepath` =~ /^(\d+)/;
     476    }
     477   
    472478    $thumbfp = "$tndir/tn_$crcid.jpg";
    473     `convert -flatten -filter Hanning $self->{'convert_params'} -geometry "$self->{'thumb_size'}x$self->{'thumb_size'}>" $filepath $thumbfp` unless -e $thumbfp;
     479    `$imagick_cmd convert -flatten -filter Hanning $self->{'convert_params'} -geometry "$self->{'thumb_size'}x$self->{'thumb_size'}>" $filepath $thumbfp` unless -e $thumbfp;
    474480    if ( ! (-e $thumbfp) ) {
    475481    print STDERR "HTMLImagePlugin: 'convert' failed. Check ImageMagicK binaries are installed and working correctly\n"; return 0;
     
    983989    # can't modify real filepath var because it
    984990    # then can't be located in the page for tag recognition later
     991    my $imagick_cmd = "\"".&util::get_perl_exec()."\" -S gs-magick.pl";
    985992    ($width, $height) =
    986         `identify $abspath -ping -format "%wx%h"` =~ /^(\d*)x(\d*)$/m;
     993        `$imagick_cmd identify $abspath -ping -format "%wx%h"` =~ /^(\d*)x(\d*)$/m;
    987994    if (! ($width && $height)) {
    988995        print STDERR "HTMLImagePlugin: ($abspath) 'identify' failed. Check ImageMagicK binaries are installed and working correctly\n"; next;
  • main/trunk/greenstone2/perllib/plugins/ImageConverter.pm

    r24346 r24600  
    3232no strict 'refs'; # allow filehandles to be variables and viceversa
    3333
     34use util;
    3435use gsprintf 'gsprintf';
    3536
     
    131132    $no_image_conversion_reason = "win95notsupported";
    132133    } else {
    133     my $result = `identify -help 2>&1`;
     134    my $imagick_cmd = "\"".&util::get_perl_exec()."\" -S gs-magick.pl";
     135    my $result = `$imagick_cmd identify -help 2>&1`;
    134136    my $return_value = $?;
    135  
    136     if ( ($ENV{'GSDLOS'} eq "windows" && $return_value == 256) || $return_value == -1) {  # Linux and Windows return different values for "program not found"
     137
     138    # When testing against non-zero return_value ($?), need to shift by 8
     139    # and convert it to its signed value. Linux returns -1 and Windows returns
     140    # 256 for "program not found". The signed equivalents are -1 and 1 respectively.
     141    $return_value >>= 8;
     142    $return_value = (($return_value & 0x80) ? -(0x100 - ($return_value & 0xFF)) : $return_value);
     143
     144    if ( ($ENV{'GSDLOS'} eq "windows" && $return_value == 1) || $return_value == -1) {  # Linux and Windows return different values for "program not found"
    137145        $image_conversion_available = 0;
    138146        $no_image_conversion_reason = "imagemagicknotinstalled";
     
    407415
    408416    # Generate and run the convert command
    409     my $convert_command = "convert -interlace plane -verbose $convert_options \"$source_file_path\" \"$target_file_path\"";
     417    my $convert_command = "\"".&util::get_perl_exec()."\" -S gs-magick.pl convert -interlace plane -verbose $convert_options \"$source_file_path\" \"$target_file_path\"";
    410418
    411419    my $print_info = { 'message_prefix' => $convert_id,
     
    439447
    440448    # Use the ImageMagick "identify" command to get the file specs
    441     my $command = "identify \"$image\" 2>&1";
     449    my $command = "\"".&util::get_perl_exec()."\" -S gs-magick.pl identify \"$image\" 2>&1";
    442450    print $outhandle "$command\n" if ($verbosity > 2);
    443451    my $result = '';
  • main/trunk/greenstone2/perllib/plugins/StructuredHTMLPlugin.pm

    r21801 r24600  
    3636use HTMLPlugin;
    3737use ImageConverter; # want the identify method
     38use util;
    3839
    3940use strict; # every perl program should have this!
     
    327328        my $command = "convert -interlace plane -verbose "
    328329        ."-geometry $newsize \"$img_filename\" \"$resized_filename\"";
     330        $command = "\"".&util::get_perl_exec()."\" -S gs-magick.pl $command";
    329331        #print $outhandle "ImageResize: $command\n" if ($verbosity > 2);
    330332        #my $result = '';
Note: See TracChangeset for help on using the changeset viewer.