source: main/trunk/greenstone2/bin/script/gs-magick.pl@ 24600

Last change on this file since 24600 was 24600, checked in by ak19, 13 years ago

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.

  • Property svn:executable set to *
File size: 5.8 KB
Line 
1#!/usr/bin/perl -w
2
3###########################################################################
4#
5# A component of the Greenstone digital library software
6# from the New Zealand Digital Library Project at the
7# University of Waikato, New Zealand.
8#
9# Copyright (C) 2009 New Zealand Digital Library Project
10#
11# This program is free software; you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation; either version 2 of the License, or
14# (at your option) any later version.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License
22# along with this program; if not, write to the Free Software
23# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24#
25###########################################################################
26
27
28# gs-magick.pl:
29# Script to set the environment for imagemagick and then run it, returning
30# both the exit code and printing the output to STDOUT.
31# Setting the env vars necessary for imagemagick here locally means
32# they won't interfere with the normal environment it would have
33# if the environment had been set in setup.bash/setup.bat instead
34
35
36BEGIN {
37 die "GSDLHOME not set - run the (gs3-)setup script\n" unless defined $ENV{'GSDLHOME'};
38 die "GSDLOS not set - run (gs3-)setup script\n" unless defined $ENV{'GSDLOS'};
39 die "GSDLARCH not set - run (gs3-)setup script\n" unless defined $ENV{'GSDLARCH'};
40
41 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
42}
43
44
45use strict;
46no strict 'refs'; # make an exception so we can use variables as filehandles
47use util;
48
49
50sub main
51{
52 my ($argc,@argv) = @_;
53
54 my $usage = "Usage: $0 [--usage|--verbosity <num>] <imagick-command> <arguments to imagick command>";
55
56 my $verbosity = 0;
57 my $magick_cmd = "";
58
59
60 # Construct the imagemagick cmd string from all the arguments,
61 # embedding any arguments that contain spaces in quotes.
62 # We'll remove the --options afterwards.
63 my $count = 0;
64 for ($count = 0; $count < scalar(@argv); $count++) {
65 if($argv[$count] =~ m/ /) {
66 $argv[$count] = "\"".$argv[$count]."\"";
67 }
68 $magick_cmd = "$magick_cmd $argv[$count]";
69 }
70
71 # process the --options in the imagemagick command
72 # Tried using the official GetOptions module to parse options, except that
73 # the --verbosity|--v option to gs-magick.pl interfered with the -verbose
74 # option that image-magick accepts.
75
76 if($magick_cmd =~ m/--usage/) {
77 print STDERR "$usage\n";
78 exit(0);
79 }
80
81 $magick_cmd =~ s/\s*--verbosity(\s+|=)(\d*)\s*/ /; # --verbosity=4 or --verbosity 4
82 $verbosity = $2 if defined $2; # print STDERR "subst 2 is : $2\n" if defined $2;
83
84 if($verbosity > 2) {
85 print STDERR "***** Running MAGICK_CMD: $magick_cmd\n";
86 #print STDERR "***** verbosity: $verbosity\n";
87 }
88
89 ## SET THE ENVIRONMENT AS USED TO BE DONE IN SETUP.BASH/BAT
90
91 my $magick_home = &util::filename_cat($ENV{'GSDLHOME'},"bin",$ENV{'GSDLOS'}.$ENV{'GSDLARCH'},"imagemagick");
92 if (-d $magick_home) { # "$GSDLHOME/bin/$GSDLOS$GSDLARCH/imagemagick"
93 $ENV{'MAGICK_HOME'} = $magick_home;
94 }
95
96 # if Greenstone came with imagick, or if the user otherwise has an
97 # imagick to fall back on (and set MAGICK_HOME that way) we use that
98 if(defined $ENV{'MAGICK_HOME'} && -d $ENV{'MAGICK_HOME'}) {
99 &util::envvar_prepend("PATH", &util::filename_cat($ENV{'MAGICK_HOME'}, "bin"));
100
101 my $magick_lib = &util::filename_cat($ENV{'MAGICK_HOME'}, "lib");
102 if($ENV{'GSDLOS'} eq "linux") {
103 &util::envvar_prepend("LD_LIBRARY_PATH", $magick_lib);
104 } elsif ($ENV{'GSDLOS'} eq "darwin") {
105 &util::envvar_prepend("DYLD_LIBRARY_PATH", $magick_lib);
106 }
107
108 if($verbosity > 2) {
109 print STDERR "\t*** MAGICK_HOME".$ENV{'MAGICK_HOME'}."\n";
110 print STDERR "\t*** LD_LIB_PATH".$ENV{'LD_LIBRARY_PATH'}."\n";
111 print STDERR "\t*** PATH".$ENV{'PATH'}."\n\n";
112 }
113 }
114
115 # if no MAGICK_HOME, maybe they want to run with the system imagemagick
116 elsif($verbosity > 2) {
117 print STDERR "**** No ImageMagick in Greenstone. Will try to use any imagemagick on the system.\n\n";
118 }
119
120 # RUN THE IMAGEMAGICK COMMAND
121
122 # John Thompson's manner of using backticks to preserve the output of
123 # running imagemagick.
124 # $? contains the exit code of running the imagemagick command.
125 # This needs to be shifted by 8 and then converted to be a signed value
126 # to work out the actual exit code value.
127
128 #my $result = `$magick_cmd`; # This way will trap STDOUT into local variable
129
130 if (!open(PIN, "$magick_cmd |")) {
131 print STDERR "*** Can't run $magick_cmd. Error was: $!";
132 }
133 my $result = "";
134 while (defined (my $imagick_output_line = <PIN>)) {
135 $result = $result.$imagick_output_line;
136 }
137 close(PIN);
138
139 # Perl Special Variables http://www.kichwa.com/quik_ref/spec_variables.html
140 # $? The status returned by the last pipe close, backtick(``) command or system operator.
141 # Note that this is the status word returned by the wait() system call, so the exit value
142 # of the subprocess is actually ($? >>*). $? & 255 gives which signal, if any, the process
143 # died from, and whether there was a core dump.
144
145 # Shift by 8 to get a value between 0 and 255, then work out if it is signed or unsigned
146 # http://stackoverflow.com/questions/2726447/why-is-the-exit-code-255-instead-of-1-in-perl
147 my $status = $?;
148 $status >>= 8;
149 $status = (($status & 0x80) ? -(0x100 - ($status & 0xFF)) : $status);
150
151 # send the output to STDOUT, since calling functions may call gs-magick.pl with backticks
152 print STDOUT $result;
153 exit($status);
154}
155
156&main(scalar(@ARGV),@ARGV);
Note: See TracBrowser for help on using the repository browser.