Ignore:
Timestamp:
2012-05-11T19:23:42+12:00 (12 years ago)
Author:
ak19
Message:
  1. Introduced the verbosity flag into activate.pl to print out debugging statements depending on verbosity, and this is passed in from buildcol.pl and full-rebuild.pl too now. 2. Cleaned up some debugging code.
Location:
main/trunk/greenstone2/bin/script
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/bin/script/activate.pl

    r25573 r25579  
    6060use scriptutil;
    6161use util;
    62 
     62#use enum;
     63
     64# enumerations in perl, http://stackoverflow.com/questions/473666/does-perl-have-an-enumeration-type
     65# Unfortunately, not part of perl's core
     66#use enum qw(LEVEL_NONE LEVEL_ERROR LEVEL_INFO LEVEL_DEBUG); # debugging levels NONE == 0, ERROR=1 INFO=2 DEBUG=3
     67
     68# global variables
     69#my $default_verbosity = LEVEL_ERROR; # by default we display basic error messages
     70
     71my $default_verbosity = 2; # by default we display basic error and info messages
    6372
    6473sub print_task_msg {
    65     my $task_msg = shift(@_);
    66     print STDERR "\n";
    67     print STDERR "************************\n";
    68     print STDERR "* $task_msg\n";
    69     print STDERR "************************\n";
     74    my ($task_msg, $verbosity_setting) = @_;
     75   
     76    $verbosity_setting = $default_verbosity unless $verbosity_setting;
     77    #$verbosity_setting = 1 unless defined $verbosity;
     78    if($verbosity_setting >= 1) {
     79        print STDERR "\n";
     80        print STDERR "************************\n";
     81        print STDERR "* $task_msg\n";
     82        print STDERR "************************\n";
     83    }
     84}
     85
     86# Prints messages if the verbosity is right. Does not add new lines.
     87sub print_msg {
     88    my ($msg, $min_verbosity, $verbosity_setting) = @_;
     89   
     90    # only display error messages if the current
     91    # verbosity setting >= the minimum verbosity level
     92    # needed for that message to be displayed.
     93   
     94    $verbosity_setting = $default_verbosity unless defined $verbosity_setting;
     95    $min_verbosity = 1 unless defined $min_verbosity;
     96    if($verbosity_setting >= $min_verbosity) { # by default display all 1 messages
     97        print STDERR "$msg";
     98    }
    7099}
    71100
     
    99128       
    100129        if(LWP::Simple::is_success($response_code)) {# $response_code eq RC_OK) { # LWP::Simple::is_success($response_code)
    101             print STDERR "*** Command $library_url$command\n";
    102             print STDERR "*** HTTP Response Status: $response_code - Complete.";           
     130            &print_msg("*** Command $library_url$command\n", 3);
     131            &print_msg("*** HTTP Response Status: $response_code - Complete.", 3);
    103132           
    104133            # check the page content is as expected
    105134            my $response_content = $response_obj->content;     
    106135            if($response_content =~ m/$check_message_against_regex/) {
    107                 print STDERR " Response as expected.\n";
     136                &print_msg(" Response as expected.\n", 3);
    108137                return 1;
    109138            } else {
    110                 print STDERR "\n\t BUT: command $library_url$command response UNEXPECTED.\n";               
    111                 #print STDERR "*** Got message:\n$response_content.\n";
     139                &print_msg("\n\tBUT: command $library_url$command response UNEXPECTED.\n", 3);
     140                &print_msg("*** Got message:\n$response_content.\n", 4);
    112141                return 0; # ping on a collection may "not succeed."
    113142            }
    114143        }
    115144        elsif(LWP::Simple::is_error($response_code)) { # method exported by LWP::Simple, along with HTTP::Status constants
    116             print STDERR "*** $response_code - Failed.\n";
     145            &print_msg("*** Command $library_url$command\n");
     146            &print_msg("*** HTTP Response Status: $response_code - Failed.\n");
    117147            return 0;
    118148        }
     
    199229            # Read in the entire contents of the file in one hit
    200230            if (!open (FIN, $llssite_cfg)) {
    201                 print STDERR "activate.pl::get_library_URL failed to open $llssite_cfg ($!)\n";
     231                &print_msg("activate.pl::get_library_URL failed to open $llssite_cfg ($!)\n");
    202232                return undef;
    203233            }
     
    276306            close(PIN);
    277307        } else {
    278             print STDERR "**** activate.pl::get_library_URL: Failed to run $perl_command to work out library URL for $gs_mode\n";
     308            &print_msg("activate.pl::get_library_URL: Failed to run $perl_command to work out library URL for $gs_mode\n");
    279309        }       
    280310    }
     
    284314    #print STDERR "\n@@@@@ URL still undef\n" if !$url;
    285315    return $url;
    286 }
    287 
    288 ### METHODS TO MOVE TO util.pm?
    289 
    290 # Given the qualified collection name (colgroup/collection),
    291 # returns the collection and colgroup parts
    292 sub get_collection_parts {
    293     # http://perldoc.perl.org/File/Basename.html
    294     # my($filename, $directories, $suffix) = fileparse($path);
    295     # "$directories contains everything up to and including the last directory separator in the $path
    296     # including the volume (if applicable). The remainder of the $path is the $filename."
    297     #my ($collection, $colgroup) = &File::Basename::fileparse($qualified_collection);   
    298 
    299     my $qualified_collection = shift(@_);
    300 
    301     # Since activate.pl can be launched from the command-line, including by a user,
    302     # best not to assume colgroup uses URL-style slashes as would be the case with GLI
    303     # Also allow for the accidental inclusion of multiple slashes
    304     my ($colgroup, $collection) = split(/[\/\\]+/, $qualified_collection); #split('/', $qualified_collection);
    305    
    306     if(!defined $collection) {
    307         $collection = $colgroup;
    308         $colgroup = "";
    309     }
    310     return ($collection, $colgroup);
    311 }
    312 
    313 # work out the "collectdir/collection" location
    314 sub resolve_collection_dir {
    315     my ($collect_dir, $qualified_collection, $site) = @_; #, $gs_mode
    316    
    317     my ($colgroup, $collection) = get_collection_parts($qualified_collection); 
    318    
    319     if (defined $collect_dir) {
    320         return &util::filename_cat($collect_dir,$colgroup, $collection);
    321     }
    322     else {
    323         if (defined $site) {
    324             return &util::filename_cat($ENV{'GSDL3HOME'},"sites",$site,"collect",$colgroup, $collection);
    325         }
    326         else {
    327             return &util::filename_cat($ENV{'GSDLHOME'},"collect",$colgroup, $collection);
    328         }
    329     }
    330316}
    331317
     
    394380
    395381    if (($argc==0)  || (($argc==1) && ($argv[0] =~ m/^--?h(elp)?$/))) {
    396     my ($progname) = ($0 =~ m/^.*[\/|\\](.*?)$/);
    397 
    398 
    399     print STDERR "\n";
    400     print STDERR "Usage: $progname [-collectdir c -builddir b -indexdir i -site s -removeold -keepold -library_url URL] <[colgroup/]collection>\n";
    401     print STDERR "\n";
    402 
    403     exit(-1);
     382        my ($progname) = ($0 =~ m/^.*[\/|\\](.*?)$/);
     383
     384
     385        print STDERR "\n";
     386        print STDERR "Usage: $progname [-collectdir c -builddir b -indexdir i -site s -removeold -keepold -verbosity v\n";
     387        print STDERR "\t-library_url URL] <[colgroup/]collection>\n";
     388        print STDERR "\n";
     389
     390        exit(-1);
    404391    }
    405392   
     
    415402    my $keepold = 0;
    416403   
    417     my $library_url = undef; # to be specified on the cmdline if not using a GS-included web server
    418    
     404    my $library_url = undef; # to be specified on the cmdline if not using a GS-included web server
    419405   
    420406    while (my $arg = shift @argv) {
     
    440426            $library_url = shift @argv;
    441427        }
     428        elsif ($arg eq "-verbosity") {
     429            $default_verbosity = shift @argv; # global variable
     430           
     431            # ensure we're working with ints not strings (int context not str context), in case verbosity=0
     432            # http://stackoverflow.com/questions/288900/how-can-i-convert-a-string-to-a-number-in-perl
     433            $default_verbosity = int($default_verbosity || 0); ### is this the best way?
     434        }
    442435    }
    443436   
    444437    # work out the building and index dirs
    445     #$build_dir = &util::filename_cat($collect_dir, $qualified_collection, "building") unless (defined $build_dir);
    446     #$index_dir = &util::filename_cat($collect_dir, $qualified_collection, "index") unless (defined $index_dir);
    447     my $collection_dir = &resolve_collection_dir($collect_dir, $qualified_collection, $site);
    448     $build_dir = &util::filename_cat($collection_dir, "building") unless (defined $build_dir); 
     438    my $collection_dir = &util::resolve_collection_dir($collect_dir, $qualified_collection, $site);
     439    $build_dir = &util::filename_cat($collection_dir, "building") unless (defined $build_dir);
    449440    $index_dir = &util::filename_cat($collection_dir, "index") unless (defined $index_dir);
    450441   
     
    453444    # can't do anything without a build directory with something in it to move into index
    454445    if(!&util::dir_exists($build_dir)) {
    455         print STDERR "Can't move building to index: no building folder at $build_dir\n";
     446        &print_msg("No building folder at $build_dir to move to index.\n");
    456447        exit -1;
    457448    } elsif (&util::is_dir_empty($build_dir)) {
    458         print STDERR "Nothing in building folder at $build_dir to move to index folder\n";
     449        &print_msg("Nothing in building folder $build_dir to move into index folder.\n");
    459450        exit -1;
    460451    }
     
    462453    # get and check the collection name
    463454    if ((&colcfg::use_collection($site, $qualified_collection, $collect_dir)) eq "") {
    464     print STDERR "Unable to use collection \"$qualified_collection\" within \"$collect_dir\"\n";
    465     exit -1;
     455        &print_msg("Unable to use collection \"$qualified_collection\" within \"$collect_dir\"\n");
     456        exit -1;
    466457    }
    467458   
     
    513504    # and apache for GS2), we work out the library URL:
    514505    if(!$library_url) {
    515         $library_url = &get_library_URL($gs_mode);
     506        $library_url = &get_library_URL($gs_mode); # returns undef if no server is running
    516507    }
    517508   
     
    520511   
    521512    my $is_persistent_server = undef;
    522     if(&ping_library($library_url, $gs_mode, $site)) {
    523         # server is running, so release the collection if the server is persistent
    524         $is_persistent_server = &is_persistent($library_url, $gs_mode);
    525         if($is_persistent_server) {
    526             &deactivate_collection($library_url, $gs_mode, $qualified_collection, $site);
    527         }   
    528     }   
    529    
     513    if($library_url) { # running server
     514        &print_msg("Pinging $library_url\n");
     515        if($library_url && &ping_library($library_url, $gs_mode, $site)) {
     516            # server is running, so release the collection if the server is persistent
     517            &print_msg("Checking if Greenstone server is persistent\n");
     518            $is_persistent_server = &is_persistent($library_url, $gs_mode);     
     519            if($is_persistent_server) {
     520                &print_msg("De-activating collection $qualified_collection\n");
     521                &deactivate_collection($library_url, $gs_mode, $qualified_collection, $site);
     522            }   
     523        }
     524    }
    530525    # 3. Do all the moving building to index stuff now 
    531526
    532     ### If removeold: replace index dir with building dir
    533     # if keepold: move building's contents into index, where only duplicates will get deleted
    534 
    535     #if(!$removeold && !$keepold) {
    536     #   $removeold = 1;
    537     #} elsif($removeold && $keepold) {
    538     #   $keepold = 0;
    539     #} 
    540    
     527    # If removeold: replace index dir with building dir.
     528    # If keepold: move building's contents into index, where only duplicates will get deleted.
     529    # removeold and keepold can't both be on at the same time
    541530    ($removeold, $keepold) = &scriptutil::check_removeold_and_keepold($removeold, $keepold,
    542531                           0, # incremental is irrelevant to what activate.pl does, setting this = 0
     
    554543           
    555544            if (&util::dir_exists($index_dir)) {
    556                 print STDERR "The index directory $index_dir could not be deleted.\n"; # CollectionManager.Index_Not_Deleted
     545                &print_msg("The index directory $index_dir could not be deleted.\n"); # CollectionManager.Index_Not_Deleted
    557546            }
    558547        }
     
    565554        &util::mv($build_dir, $index_dir);
    566555        if(&util::dir_exists($build_dir) || !&util::dir_exists($index_dir)) {           
    567             print STDERR "Could not move move $build_dir to $index_dir.\n"; # CollectionManager.Build_Not_Moved
     556            &print_msg("Could not move $build_dir to $index_dir.\n"); # CollectionManager.Build_Not_Moved
    568557        }
    569558    }
     
    580569    # Check for success: if building does not exist OR is empty
    581570    if(!&util::dir_exists($build_dir) || &util::is_dir_empty($build_dir)) {
    582         if(&ping_library($library_url, $gs_mode, $site)) {
    583             # don't need to work out persistency of server more than once
    584             $is_persistent_server = &is_persistent($library_url, $gs_mode) unless (defined $is_persistent_server);
    585             if($is_persistent_server) {
    586                 &activate_collection($library_url, $gs_mode, $qualified_collection, $site);
    587             }   
     571        if($library_url) { # undef if no server is running
     572            &print_msg("Pinging $library_url\n");
     573            if(&ping_library($library_url, $gs_mode, $site)) {
     574                # don't need to work out persistency of server more than once
     575                if(!defined $is_persistent_server) {
     576                    &print_msg("Checking if Greenstone server is persistent\n");
     577                    $is_persistent_server = &is_persistent($library_url, $gs_mode);
     578                }   
     579                if($is_persistent_server) {
     580                    &print_msg("Activating collection $qualified_collection\n");
     581                    &activate_collection($library_url, $gs_mode, $qualified_collection, $site);
     582                }
     583            }
    588584        }
    589585    } else { # installcollection failed     
    590586        #CollectionManager.Preview_Ready_Failed
    591         print STDERR "Building directory is not empty or still exists. Failed to properly move $build_dir to $index_dir.\n";
     587        &print_msg("Building directory is not empty or still exists. Failed to properly move $build_dir to $index_dir.\n");
    592588    }
    593589}
  • main/trunk/greenstone2/bin/script/buildcol.pl

    r25573 r25579  
    694694       
    695695        my @activate_argv = ();
    696         push(@activate_argv,"-collectdir","$collectdir") if($collectdir);
    697         push(@activate_argv,"-builddir","$builddir") if($builddir);
    698         push(@activate_argv,"-site","$site") if($site);
     696        push(@activate_argv,"-collectdir",$collectdir) if($collectdir);
     697        push(@activate_argv,"-builddir",$builddir) if($builddir);
     698        push(@activate_argv,"-site",$site) if($site);
     699        push(@activate_argv,"-verbosity",$verbosity) if($verbosity);
    699700        push(@activate_argv,"-removeold") if($removeold);
    700701        push(@activate_argv,"-keepold") if($keepold);
  • main/trunk/greenstone2/bin/script/full-rebuild.pl

    r25573 r25579  
    3131# Followed by: full-buildcol.pl -removeold ...
    3232# (assumming import.pl did not end with an error)
    33 
    34 # The -keepold argument can be passed into this script since this calls activate.pl
    35 # And activate.pl allows the contents of a src dir to be moved into the dest_dir
    36 # without destroying other (uniquely-named) contents of dest_dir.
    3733
    3834BEGIN {
     
    129125    my $build_dir   = undef;
    130126    my $index_dir   = undef;
     127    my $verbosity   = 2; # same as the default in buildcol.pl
    131128
    132129    while (my $arg = shift @argv) {
     
    160157        push(@activate_argv,$arg,$index_dir);
    161158    }
     159    elsif ($arg eq "-verbosity") {     
     160        $verbosity = shift @argv;
     161        push(@import_argv,$arg,$verbosity);
     162        push(@buildcol_argv,$arg,$verbosity);
     163        push(@activate_argv,$arg,$verbosity);
     164    }
    162165    else {
    163166        push(@import_argv,$arg);
    164167        push(@buildcol_argv,$arg);
    165         push(@activate_argv,$arg);
     168        push(@activate_argv,$arg);
    166169    }
    167170    }
     
    198201
    199202        #full_replace($site,$collect_dir,$collect,$build_dir,$index_dir);
    200         my $activatecol_cmd = $launch_cmd . "activate.pl $quoted_activate_argv \"$collect\"";
     203       
     204        # run activate with -removeold, just like full-buildcol.pl called above runs buildcol.pl
     205        my $activatecol_cmd = $launch_cmd . "activate.pl -removeold $quoted_activate_argv \"$collect\"";
    201206        my $activatecol_status = system($activatecol_cmd)/256;
    202207    }
Note: See TracChangeset for help on using the changeset viewer.