Changeset 21205


Ignore:
Timestamp:
2009-12-02T14:06:02+13:00 (14 years ago)
Author:
davidb
Message:

Reworking of add/del/query scripts to be more portable in Perl

Location:
gs3-extensions/pharos/trunk/cmdline-api-srcpack/bin/script
Files:
1 added
1 deleted
3 moved

Legend:

Unmodified
Added
Removed
  • gs3-extensions/pharos/trunk/cmdline-api-srcpack/bin/script/pharos-imageis-add.pl

    r21197 r21205  
    1 #!/bin/bash
     1#!/usr/bin/perl -w
    22
    3 if [ $# != 2 ] ; then
    4   echo "Usage: imageis-add-jpg-file.sh collection file.jpg"
    5   exit
    6 fi
     3###########################################################################
     4#
     5# pharos-imageis-add.pl --
     6# A component of the Greenstone digital library software
     7# from the New Zealand Digital Library Project at the
     8# University of Waikato, New Zealand.
     9#
     10# Copyright (C) 1999 New Zealand Digital Library Project
     11#
     12# This program is free software; you can redistribute it and/or modify
     13# it under the terms of the GNU General Public License as published by
     14# the Free Software Foundation; either version 2 of the License, or
     15# (at your option) any later version.
     16#
     17# This program is distributed in the hope that it will be useful,
     18# but WITHOUT ANY WARRANTY; without even the implied warranty of
     19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     20# GNU General Public License for more details.
     21#
     22# You should have received a copy of the GNU General Public License
     23# along with this program; if not, write to the Free Software
     24# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
     25#
     26###########################################################################
    727
    8 col=$1
    9 srcfilename=$2
     28BEGIN {
     29    die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
     30    die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
     31    die "IMAGEISHOME not set\n" unless defined $ENV{'IMAGEISHOME'};
     32    unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
     33}
    1034
    11 if [ ! -f "$srcfilename" ] ; then
    12   echo "Error: failed to find $srcfilename"
    13   exit
    14 fi
     35use strict;
     36no strict 'refs'; # allow filehandles to be variables and vice versa
     37no strict 'subs'; # allow barewords (eg STDERR) as function arguments
    1538
    16 filetail=${srcfilename##*/}
    17 docid=${filetail%.*}
    1839
    19 # make sure file extension is lowercase .jpg
     40sub main
     41{
     42   
     43    my $ARGC = scalar(@ARGV);
     44   
     45   
     46    if (($ARGC < 2) || ($ARGC>3)) {
     47    my ($progname) = ($0 =~ m/^.*[\/|\\](.*?)$/);
     48    print STDERR "Usage: $progname collection file.jpg [doc-id]\n";
     49    exit 1;
     50    }
    2051
    21 dstfile=$docid.jpg
    22 if [ "x$col" != "x" ] ; then
    23   dstfile="$col:$dstfile"
    24 fi
    25 dstfilename=/tmp/vsupload/$dstfile
     52    # docid might be undefined, in which case later on is is derived from
     53    # from col and the file-name tail to srcfilename
     54    my ($col,$srcfilename,$docid) = @ARGV;
     55   
     56   
     57    if ( ! -f $srcfilename ) {
     58    print STDERR "Error: failed to find $srcfilename\n";
     59    exit 1;
     60    }
     61   
     62    if ( $srcfilename =~ m/\.je?pg$/i ) {
     63    print STDERR "Error: Unrecognized JPEG filename extension";
     64    exit 1;
     65    }
     66   
     67    if (!defined $docid) {
     68    my ($fileroot,$dirname_unused,$suffix_unused)
     69        = fileparse($srcfilename, "\\.[^\\.]+\$");
     70    $docid = $fileroot;
     71    }
     72   
     73    # make sure file extension is lowercase .jpg
     74   
     75    my $dstfile = "$docid.jpg";
     76   
     77    if ( $col ne "" ) {
     78    $dstfile="$col:$dstfile";
     79    }
     80   
     81    my $tmp_dir = &util::get_toplevel_tmp_dir();
     82    my $dstfilename=&util::filename_cat($tmp_dir,$dstfile);
     83   
     84    &util::cp($srcfilename,$dstfilename);
     85   
     86    print "Generating add_file.xml\n";
     87   
     88    my $add_template_filename
     89    = &util::filename_cat($ENV{'IMAGEISHOME'},"templates","add_file.xml");
     90    my $add_this_filename = &util::filename_cat($tmp_dir,"add_file.xml");
     91   
     92    if (open(FIN,"<$add_template_filename")) {
     93   
     94    my $add_this_file_content = "";
     95   
     96    my $line;
     97    while (defined ($line=<FIN>)) {
     98        $line =~ s@\*\*imgfile\*\*@$dstfile@g;
     99       
     100        $add_this_file_content .= $line;
     101    }
     102   
     103    close(FIN);
     104   
     105    if (open(FOUT,">$add_this_filename")) {
     106        print FOUT $add_this_file_content;
     107        close(FOUT);
     108    }
     109    else {
     110        print STDERR "Error: failed to write out $add_this_filename\n";
     111        print STDERR "$!\n";
     112    }
     113    }
     114    else {
     115    print STDERR "Error: failed to read $add_template_filename\n";
     116    print STDERR "$!\n";
     117    }
     118   
     119   
     120    my $jar_filename = &util::filename_cat($ENV{'IMAGEISHOME'},"ImageIS.jar");
     121    my $java_cmd = "java -jar \"$jar_filename\" \"$add_this_filename\"";
    26122
    27 /bin/cp "$srcfilename" "$dstfilename"
     123    print "Ingesting $dstfile\n";
     124   
     125    my $status = system($java_cmd);
     126   
     127    if ($status == 0 ) {
     128    &util::rm($add_this_filename);
     129    }
     130    else {
     131    print STDERR "Error: Failed to run:\n";
     132    print STDERR "$!\n";
     133    exit 1;
     134    }
     135}
     136 
    28137
    29 echo "Generating add_file.xml"
    30 cat "$IMAGEISHOME/templates/add_file.xml" \
    31   | sed "s@\*\*imgfile\*\*@$dstfile@g" \
    32   > "/tmp/vsupload/add_file.xml"
    33 
    34 echo "Ingesting $dstfile"
    35 
    36 java -jar "$IMAGEISHOME/ImageIS.jar" "/tmp/vsupload/add_file.xml"
    37 
    38 if [ $? == "0" ] ; then
    39   /bin/rm "/tmp/vsupload/add_file.xml"
    40 fi
     138main();
  • gs3-extensions/pharos/trunk/cmdline-api-srcpack/bin/script/pharos-imageis-delete.pl

    r21197 r21205  
    1 #!/bin/bash
     1#!/usr/bin/perl -w
    22
    3 if [ $# != 2 ] ; then
    4   echo "Usage: imageis-del-id.sh col id"
    5   exit
    6 fi
     3###########################################################################
     4#
     5# pharos-imageis-delete.pl --
     6# A component of the Greenstone digital library software
     7# from the New Zealand Digital Library Project at the
     8# University of Waikato, New Zealand.
     9#
     10# Copyright (C) 1999 New Zealand Digital Library Project
     11#
     12# This program is free software; you can redistribute it and/or modify
     13# it under the terms of the GNU General Public License as published by
     14# the Free Software Foundation; either version 2 of the License, or
     15# (at your option) any later version.
     16#
     17# This program is distributed in the hope that it will be useful,
     18# but WITHOUT ANY WARRANTY; without even the implied warranty of
     19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     20# GNU General Public License for more details.
     21#
     22# You should have received a copy of the GNU General Public License
     23# along with this program; if not, write to the Free Software
     24# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
     25#
     26###########################################################################
    727
    8 col=$1
    9 id=$2
    10 
    11 docid=$id
    12 if [ "x$col" != "x" ] ; then
    13   docid="$col:$docid"
    14 fi
     28BEGIN {
     29    die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
     30    die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
     31    die "IMAGEISHOME not set\n" unless defined $ENV{'IMAGEISHOME'};
     32    unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
     33}
    1534
    1635
    17 echo "Generating del_id.xml"
    18 cat "$IMAGEISHOME/templates/del_id.xml" \
    19   | sed "s@\*\*docid\*\*@$docid@g" \
    20   > "/tmp/vsupload/del_id.xml"
     36use strict;
     37no strict 'refs'; # allow filehandles to be variables and vice versa
     38no strict 'subs'; # allow barewords (eg STDERR) as function arguments
    2139
    22 echo "Deleting $docid"
    2340
    24 java -jar "$IMAGEISHOME/ImageIS.jar" "/tmp/vsupload/del_id.xml"
     41sub main
     42{
     43   
     44    my $ARGC = scalar(@ARGV);
     45   
     46   
     47    if ($ARGC != 2) {
     48    my ($progname) = ($0 =~ m/^.*[\/|\\](.*?)$/);
     49    print STDERR "Usage: $progname collection doc-id\n";
     50    exit 1;
     51    }
    2552
    26 if [ $? == "0" ] ; then
    27   /bin/rm "/tmp/vsupload/del_id.xml"
    28 fi
     53    my ($col,$id) = @ARGV;
     54           
     55    my $docid = $id;
     56    if ( $col ne "" ) {
     57    $docid="$col:$docid";
     58    }
     59   
     60    my $tmp_dir = &util::get_toplevel_tmp_dir();
     61   
     62    print "Generating del_id.xml\n";
     63   
     64    my $del_template_filename
     65    = &util::filename_cat($ENV{'IMAGEISHOME'},"templates","del_id.xml");
     66    my $del_this_filename = &util::filename_cat($tmp_dir,"del_id.xml");
     67   
     68    if (open(FIN,"<$del_template_filename")) {
     69   
     70    my $del_this_file_content = "";
     71   
     72    my $line;
     73    while (defined ($line=<FIN>)) {
     74        $line =~ s@\*\*docid\*\*@$docid@g;
     75       
     76        $del_this_file_content .= $line;
     77    }
     78   
     79    close(FIN);
     80   
     81    if (open(FOUT,">$del_this_filename")) {
     82        print FOUT $del_this_file_content;
     83        close(FOUT);
     84    }
     85    else {
     86        print STDERR "Error: failed to write out $del_this_filename\n";
     87        print STDERR "$!\n";
     88    }
     89    }
     90    else {
     91    print STDERR "Error: failed to read $del_template_filename\n";
     92    print STDERR "$!\n";
     93    }
     94   
     95   
     96    my $jar_filename = &util::filename_cat($ENV{'IMAGEISHOME'},"ImageIS.jar");
     97    my $java_cmd = "java -jar \"$jar_filename\" \"$del_this_filename\"";
     98
     99    print "Deleting $docid\n";
     100   
     101    my $status = system($java_cmd);
     102   
     103    if ($status == 0 ) {
     104    &util::rm($del_this_filename);
     105    }
     106    else {
     107    print STDERR "Error: Failed to run:\n";
     108    print STDERR "$!\n";
     109    exit 1;
     110    }
     111}
     112 
     113
     114main();
     115
  • gs3-extensions/pharos/trunk/cmdline-api-srcpack/bin/script/pharos-imageis-query.pl

    r21197 r21205  
    1 #!/bin/bash
     1#!/usr/bin/perl -w
    22
    3 if [ $# != 2 ] ; then
    4   echo "Usage: imageis-query-by-id.sh col id"
    5   exit
    6 fi
     3###########################################################################
     4#
     5# pharos-imageis-query.pl --
     6# A component of the Greenstone digital library software
     7# from the New Zealand Digital Library Project at the
     8# University of Waikato, New Zealand.
     9#
     10# Copyright (C) 1999 New Zealand Digital Library Project
     11#
     12# This program is free software; you can redistribute it and/or modify
     13# it under the terms of the GNU General Public License as published by
     14# the Free Software Foundation; either version 2 of the License, or
     15# (at your option) any later version.
     16#
     17# This program is distributed in the hope that it will be useful,
     18# but WITHOUT ANY WARRANTY; without even the implied warranty of
     19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     20# GNU General Public License for more details.
     21#
     22# You should have received a copy of the GNU General Public License
     23# along with this program; if not, write to the Free Software
     24# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
     25#
     26###########################################################################
    727
    8 col=$1
    9 id=$2
     28BEGIN {
     29    die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
     30    die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
     31    die "IMAGEISHOME not set\n" unless defined $ENV{'IMAGEISHOME'};
     32    unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
     33}
    1034
    11 docid=$col:$id
    1235
    13 echo "Generating query_id.xml"
    14 cat "$IMAGEISHOME/templates/query_id.xml" \
    15   | sed "s@\*\*docid\*\*@$docid@g" \
    16   > "/tmp/vsupload/query_id.xml"
     36use strict;
     37no strict 'refs'; # allow filehandles to be variables and vice versa
     38no strict 'subs'; # allow barewords (eg STDERR) as function arguments
    1739
    18 echo "Querying by $docid"
    1940
    20 java -jar "$IMAGEISHOME/ImageIS.jar" "/tmp/vsupload/query_id.xml"
     41sub main
     42{
     43   
     44    my $ARGC = scalar(@ARGV);
     45   
     46   
     47    if ($ARGC != 2) {
     48    my ($progname) = ($0 =~ m/^.*[\/|\\](.*?)$/);
     49    print STDERR "Usage: $progname collection doc-id\n";
     50    exit 1;
     51    }
    2152
    22 if [ $? == "0" ] ; then
    23   /bin/rm "/tmp/vsupload/query_id.xml"
    24 fi
     53    my ($col,$id) = @ARGV;
     54           
     55    my $docid = $id;
     56    if ( $col ne "" ) {
     57    $docid="$col:$docid";
     58    }
     59   
     60    my $tmp_dir = &util::get_toplevel_tmp_dir();
     61   
     62    print "Generating query_id.xml\n";
     63   
     64    my $query_template_filename
     65    = &util::filename_cat($ENV{'IMAGEISHOME'},"templates","query_id.xml");
     66    my $query_this_filename = &util::filename_cat($tmp_dir,"query_id.xml");
     67   
     68    if (open(FIN,"<$query_template_filename")) {
     69   
     70    my $query_this_file_content = "";
     71   
     72    my $line;
     73    while (defined ($line=<FIN>)) {
     74        $line =~ s@\*\*docid\*\*@$docid@g;
     75       
     76        $query_this_file_content .= $line;
     77    }
     78   
     79    close(FIN);
     80   
     81    if (open(FOUT,">$query_this_filename")) {
     82        print FOUT $query_this_file_content;
     83        close(FOUT);
     84    }
     85    else {
     86        print STDERR "Error: failed to write out $query_this_filename\n";
     87        print STDERR "$!\n";
     88    }
     89    }
     90    else {
     91    print STDERR "Error: failed to read $query_template_filename\n";
     92    print STDERR "$!\n";
     93    }
     94   
     95   
     96    my $jar_filename = &util::filename_cat($ENV{'IMAGEISHOME'},"ImageIS.jar");
     97    my $java_cmd = "java -jar \"$jar_filename\" \"$query_this_filename\"";
    2598
    26 if [ ! -f image_cbs_result.xml ] ; then
    27   echo ""
    28   echo "Error: Failed to generate image_cbs_result.xml"
    29   echo "       Check catalina.out for server-side error messages"
    30   echo ""
    31   exit 1
    32 elif [ ! -s image_cbs_result.xml ] ; then
    33   echo ""
    34   echo "Warning: Server returned zero sized file image_cbs_result.xml"
    35   echo "         Check that the doc-id used is valid"
    36   echo ""
    37 else
    38   cat image_cbs_result.xml
    39   echo ""
    40   /bin/rm image_cbs_result.xml
    41 fi
    4299
     100    print "Querying $docid\n";
     101   
     102    chdir($tmp_dir);
     103    my $status = system($java_cmd);
     104   
     105    if ($status == 0 ) {
     106    &util::rm($query_this_filename);
     107    }
     108    else {
     109    print STDERR "Error: Failed to run:\n";
     110    print STDERR "$!\n";
     111    exit 1;
     112    }
     113
     114    my $result_filename = &util::filename_cat($tmp_dir,"image_cbs_result.xml");
     115   
     116    if ( ! -f $result_filename ) {
     117    print STDERR "\n";
     118    print STDERR "Error: Failed to generate $result_filename.xml\n";
     119    print STDERR "       Check Tomcat's catalina.out for server-side error messages\n";
     120    print STDERR "\n";
     121    exit 1;
     122    }
     123    elsif ( -s $result_filename == 0) {
     124    print STDERR "\n";
     125    print STDERR "Warning: Server returned zero sized file image_cbs_result.xml\n";
     126    print STDERR "         Check that the doc-id used is valid\n";
     127    print STDERR "\n";
     128    &util::rm($result_filename);
     129    }
     130    else {
     131   
     132    if (open(FIN,"<$result_filename")) {
     133   
     134        my $line;
     135        while (defined ($line=<FIN>)) {
     136        print $line;
     137        }
     138       
     139        close(FIN);
     140    }
     141    else {
     142        print STDERR "Error: failed to read $result_filename\n";
     143        print STDERR "$!\n";
     144    }
     145
     146    print "\n";
     147    &util::rm($result_filename);
     148    }
     149}
     150 
     151
     152main();
     153
     154
     155
     156
Note: See TracChangeset for help on using the changeset viewer.