[system] / trunk / xmlrpc / daemon / FetchLibraryProblems.pm Repository:
ViewVC logotype

View of /trunk/xmlrpc/daemon/FetchLibraryProblems.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2989 - (download) (as text) (annotate)
Tue Nov 9 00:03:37 2004 UTC (15 years ago) by gage
File size: 4329 byte(s)
Made changes which remove duplication between the code in Webwork.pm
and code in WW2.1  -- This should make it easier to maintain the webwork
daemons when changes are made in the core webwork code.

More changes are on the way so that the daemon can run behind apache.

    1 #!/usr/local/bin/perl -w
    2 
    3 # Copyright (C) 2002 Michael Gage
    4 
    5 ###############################################################################
    6 # Web service which fetches WeBWorK problems from a library.
    7 ###############################################################################
    8 package FetchLibraryProblems;
    9 use strict;
   10 use sigtrap;
   11 use Carp;
   12 use Safe;
   13 use WeBWorK::PG::Translator;
   14 use WeBWorK::PG::IO;
   15 use Benchmark;
   16 use MIME::Base64 qw( encode_base64 decode_base64);
   17 
   18 my $WW_DIRECTORY = $ENV{WEBWORK_ROOT};
   19 
   20 our $COURSENAME = 'daemon_course';
   21 our $HOSTURL   =  'http://devel.webwork.rochester.edu:11002';
   22 
   23 
   24 our $ce = WeBWorK::CourseEnvironment->new($WW_DIRECTORY, "", "", $COURSENAME);
   25 
   26 
   27 # imported variables
   28  my %AVAILABLE_PROBLEM_LIBRARIES  = %{$ce->{courseFiles}->{problibs} };
   29  # replace library names with paths
   30  my $templateDir              = $ce->{courseDirs}->{template};
   31  foreach my $key (keys %AVAILABLE_PROBLEM_LIBRARIES) {
   32   $AVAILABLE_PROBLEM_LIBRARIES{$key} = "$templateDir/$key";
   33  }
   34  $AVAILABLE_PROBLEM_LIBRARIES{ww_prob_lib}  = '/webwork/shared-courses/daemon_course/templates/';
   35  my $PASSWORD = $Global::PASSWORD;
   36 
   37 use File::stat;
   38 sub readFile {
   39   my $rh = shift;
   40   local($|)=1;
   41   my $out = {};
   42   my $filePath = $rh->{filePath};
   43   unless ($rh->{pw} eq $PASSWORD ) {
   44     $out->{error}=404;
   45     return($out);
   46   }
   47   if (  defined($AVAILABLE_PROBLEM_LIBRARIES{$rh->{library_name}} )   ) {
   48     $filePath = $AVAILABLE_PROBLEM_LIBRARIES{$rh->{library_name}} . $filePath;
   49   } else {
   50     $out->{error} = "Could not find library:".$rh->{library_name}.":";
   51     return($out);
   52   }
   53 
   54   if (-r $filePath) {
   55     open IN, "<$filePath";
   56     local($/)=undef;
   57     my $text = <IN>;
   58     $out->{text}= encode_base64($text);
   59     my $sb=stat($filePath);
   60     $out->{size}=$sb->size;
   61     $out->{path}=$filePath;
   62     $out->{permissions}=$sb->mode&07777;
   63     $out->{modTime}=scalar localtime $sb->mtime;
   64     close(IN);
   65   } else {
   66     $out->{error} = "Could not read file at |$filePath|";
   67   }
   68   return($out);
   69 }
   70 
   71 
   72 
   73 use File::Find;
   74 sub listLib {
   75   my $rh = shift;
   76   my $out = {};
   77   my $dirPath;
   78   unless ($rh->{pw} eq $PASSWORD ) {
   79     $out->{error}=404;
   80     return($out);
   81   }
   82 
   83   if (  defined($AVAILABLE_PROBLEM_LIBRARIES{$rh->{library_name}} )   ) {
   84     $dirPath = $AVAILABLE_PROBLEM_LIBRARIES{$rh->{library_name}} ;
   85   } else {
   86     $out->{error} = "Could not find library:".$rh->{library_name}.":";
   87     return($out);
   88   }
   89 
   90   my @outListLib;
   91   my $wanted = sub {
   92     my $name = $File::Find::name;
   93     my @out=();
   94     if ($name =~/\S/ ) {
   95       $name =~ s|^$dirPath||o;  # cut the first directory
   96             # $name =~ s|^\w*\/||;     # cut the set name
   97       push(@outListLib, "$name") if $name =~/\.pg/;
   98 
   99     }
  100   };
  101 
  102   my $command = $rh->{command};
  103   $command = 'all' unless defined($command);
  104       $command eq 'all' &&    do {print "$dirPath\n\n";
  105                     find($wanted, $dirPath);
  106                     @outListLib = sort @outListLib;
  107                     $out->{ra_out} = \@outListLib;
  108                     $out->{text} = join("\n", @outListLib);
  109                     return($out);
  110       };
  111       $command eq 'setsOnly' &&   do {
  112                       if ( opendir(DIR, $dirPath) ) {
  113                           my @fileList=();
  114                         while (defined(my $file = readdir(DIR))) {
  115                           push(@fileList,$file) if -d "$dirPath/$file";
  116 
  117                         }
  118                         $out->{text} = join("\n",sort @fileList);
  119                         closedir(DIR);
  120                       } else {
  121                         $out->{error}= "Can't open directory $dirPath";
  122                       }
  123                       return($out);
  124       };
  125 
  126       $command eq 'listSet' && do {@outListLib=();
  127                      my $dirPath2 = $dirPath . $rh->{set};
  128 
  129                      if ( -e $dirPath2) {
  130                        find($wanted, $dirPath2);
  131                        $out ->{text} = join("\n", sort @outListLib );
  132                      } else {
  133                        $out->{error} = "Can't open directory $dirPath2";
  134                      }
  135                      return($out);
  136 
  137       };
  138       # else
  139       $out->{error}="Unrecognized command $command";
  140       $out;
  141 }
  142 
  143 
  144 sub pretty_print_rh {
  145   my $rh = shift;
  146   my $out = "";
  147   my $type = ref($rh);
  148   if ( ref($rh) =~/HASH/ ) {
  149     foreach my $key (sort keys %{$rh})  {
  150       $out .= "  $key => " . pretty_print_rh( $rh->{$key} ) . "\n";
  151     }
  152   } elsif ( ref($rh) =~ /SCALAR/ ) {
  153     $out = "scalar reference ". ${$rh};
  154   } elsif ( ref($rh) =~/Base64/ ) {
  155     $out .= "base64 reference " .$$rh;
  156   } else {
  157     $out =  $rh;
  158   }
  159   if (defined($type) ) {
  160     $out .= "type = $type \n";
  161   }
  162   return $out;
  163 }
  164 
  165 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9