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

View of /trunk/xmlrpc/daemon/MathTranslators.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: 6226 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 translates TeX to pdf or to HTML
    7 ###############################################################################
    8 package MathTranslators;
    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 
   19 my $WW_DIRECTORY = $ENV{WEBWORK_ROOT};
   20 
   21 our $COURSENAME = 'daemon_course';
   22 our $HOSTURL   =  'http://devel.webwork.rochester.edu:11002';
   23 
   24 
   25 our $ce = WeBWorK::CourseEnvironment->new($WW_DIRECTORY, "", "", $COURSENAME);
   26 
   27 
   28 
   29 my $debugOn   = 1;
   30 my $PASSWORD  =   $Global::PASSWORD;
   31 
   32 
   33 my $TEMPDIRECTORY         =   $ce->{webworkDirs}->{htdocs_temp};
   34 my $TEMP_BASE_URL         =   $ce->{webworkURLs}->{htdocs_temp};
   35 my $externalLatexPath       =   $ce->{externalPrograms}->{latex};
   36 # my $externalDvipsPath       =   $Global::externalDvipsPath;
   37 my $externalpdflatexPath    =   $ce->{externalPrograms}->{pdflatex};;
   38 # my $externalGsPath        =   $Global::externalGsPath;
   39 
   40 
   41 # variables formerly set in Global
   42   $Global::tmp_directory_permission = 0775;
   43   $Global::numericalGroupID='100';  # group ID for wwdev
   44 
   45 my $tmp_directory_permission  =   $Global::tmp_directory_permission;
   46 my $numericalGroupID      = $Global::numericalGroupID;  # group ID for webadmin
   47 
   48 sub tex2pdf {
   49 
   50   my $rh    =   shift;
   51   local($|) = 1;
   52   my $out   =   {};
   53   unless ($rh->{pw} eq $PASSWORD ) {
   54     $out->{error} = 404;
   55     return($out);
   56   }
   57   #obtain the path to the file
   58   my $filePath    =   $rh->{fileName};
   59   my @pathElements  =   split("/", $filePath);
   60 
   61   # grab the last element as the fileName
   62   #remove the extension from the file name
   63   my $fileName  =   pop @pathElements;
   64   $fileName     =~  s/\.pg$//;
   65 
   66   #Create the full url and the full directory path -- If pathElements is empty this can give an extra //? Should I worry? maybe
   67   my $url        =  $TEMP_BASE_URL. join("/",@pathElements);
   68   $url        .=  '/' unless $url =~ m|/$|;   # only add / if it is needed.
   69   $url        .=  "$fileName.pdf";
   70   my $texFileBasePath  =  $TEMPDIRECTORY.join("/",@pathElements);
   71   $texFileBasePath  .=  '/' unless $texFileBasePath =~ m|/$|;
   72   $texFileBasePath  .= "$fileName";
   73 
   74   # create the intermediate directories if they don't exists
   75   # create a dummy .pdf file
   76   surePathToTmpFile2($texFileBasePath.'.pdf');
   77 # my $filePermission = '0775';
   78 # chmod("$filePermission","$texFileBasePath.pdf")
   79 #      or die "Can't change file permissions on $texFileBasePath.pdf to $filePermission";
   80 
   81   # Decode and cleanup the tex string
   82   my $texString = decode_base64(  $rh->{'texString'});
   83   #Make sure the line endings are correct
   84   $texString=~s/\r\n/\n/g;
   85   $texString=~s/\r/\n/g;
   86 
   87   # Make sure that TeX is run in batchmode so that the tex program doesn't hang on errors
   88   $texString = "\\batchmode\n".$texString;  # force errors to log.
   89 
   90   # Remove any old files
   91   unlink("$texFileBasePath.tex","$texFileBasePath.dvi","$texFileBasePath.log","$texFileBasePath.aux",
   92          "$texFileBasePath.ps","$texFileBasePath.pdf");
   93   # Create the texfile of the problem.
   94   local(*TEX);
   95   open(TEX,"> $texFileBasePath.tex") or die "Can't open $texFileBasePath.tex to store tex code";
   96   local($/)=undef;
   97   print TEX $texString;
   98   close(TEX);
   99 
  100 
  101   # my $dviCommandLine = "$externalLatexPath $texFileBasePath.tex";# >/dev/null 2>/dev/null";
  102   # my $psCommandLine = "$externalDvipsPath -o $texFileBasePath.ps $texFileBasePath.dvi >/dev/null";# 2>/dev/null";
  103   # my $pdfCommandLine = "$externalGsPath -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=$texFileBasePath.pdf -c save pop -f $texFileBasePath.ps";
  104   # print "dviCommandLine:  $dviCommandLine\n";
  105   # print "psCommandLine:   $psCommandLine\n";
  106   # print "pdfCommandLine:  $pdfCommandLine\n";
  107     #print "execute pdflatex", `$externalpdflatexPath $texFileBasePath.tex`, "\n";
  108     #print "done $externalpdflatexPath   $texFileBasePath.tex\n";
  109   # Change to the working directory and create the pdf files.
  110   my $wd = $TEMPDIRECTORY.join("/",@pathElements); # working directory
  111   # print "---cd $wd &&  $externalpdflatexPath $fileName.tex\n";
  112 
  113   system "cd $wd &&  $externalpdflatexPath $fileName.tex >>$fileName.log";
  114   chmod 0777, "$texFileBasePath.pdf";
  115   unless ($debugOn) {
  116     unlink("$texFileBasePath.tex","$texFileBasePath.log","$texFileBasePath.aux",
  117          );
  118   }
  119   return({pdfURL => $url});
  120 
  121 
  122 
  123 
  124 }
  125 
  126 sub surePathToTmpFile2 {  # constructs intermediate directories if needed beginning at ${Global::htmlDirectory}tmp/
  127                # the input path must be either the full path, or the path relative to this tmp sub directory
  128          my $path      = shift;
  129          my $delim    = getDirDelim();
  130          my $tmpDirectory = $TEMPDIRECTORY;
  131     # if the path starts with $tmpDirectory (which is permitted but optional) remove this initial segment
  132     print "Original path $path\n";
  133         $path =~ s|^$tmpDirectory|| if $path =~ m|^$tmpDirectory|;
  134         $path = convertPath($path);
  135         print "Creating path to $path using $delim\n";
  136     # find the nodes on the given path
  137         my @nodes     = split("$delim",$path);
  138     # create new path
  139         $path   = convertPath("$tmpDirectory");
  140     print  "Creating path: $path\n ";
  141         while (@nodes>1 ) {
  142 
  143             $path = convertPath($path . shift (@nodes) ."/");
  144             print  "Creating path: $path\n ";
  145             unless (-e $path) {
  146             #   system("mkdir $path");
  147                 createDirectory($path,$tmp_directory_permission, $numericalGroupID) ||
  148                 die "Failed to create directory $path";
  149 
  150             }
  151 
  152         }
  153         $path = convertPath($path . shift(@nodes));
  154         print  "Creating path: $path\n ";
  155        # system(qq!echo "" > $path! );
  156 
  157 $path;
  158 
  159 }
  160 
  161 
  162 sub pretty_print_rh {
  163   my $rh = shift;
  164   my $out = "";
  165   my $type = ref($rh);
  166   if ( ref($rh) =~/HASH/ ) {
  167     foreach my $key (sort keys %{$rh})  {
  168       $out .= "  $key => " . pretty_print_rh( $rh->{$key} ) . "\n";
  169     }
  170   } elsif ( ref($rh) =~ /SCALAR/ ) {
  171     $out = "scalar reference ". ${$rh};
  172   } elsif ( ref($rh) =~/Base64/ ) {
  173     $out .= "base64 reference " .$$rh;
  174   } else {
  175     $out =  $rh;
  176   }
  177   if (defined($type) ) {
  178     $out .= "type = $type \n";
  179   }
  180   return $out;
  181 }
  182 
  183 
  184 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9