[system] / trunk / webwork2 / lib / WebworkWebservice / MathTranslators.pm Repository:
ViewVC logotype

View of /trunk/webwork2/lib/WebworkWebservice/MathTranslators.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6585 - (download) (as text) (annotate)
Tue Nov 30 20:22:55 2010 UTC (2 years, 5 months ago) by gage
File size: 6602 byte(s)
added omments to global.conf.dist
switch to use WWSafe;


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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9