Ticket #10637: add_sws2rst.patch

File add_sws2rst.patch, 4.4 KB (added by pang, 10 years ago)

patch to be applied on local/bin

  • sage-sage

    # HG changeset patch
    # User Pablo Angulo <pablo.angulo@uam.es>
    # Date 1295454069 -3600
    # Node ID c537b10238fb2cb459fa94cbaaafe43b064782b4
    # Parent  afec3d7d46feb80352aef4ff7f5d4211f015b66c
    [mq]: add_sws2rst.patch
    
    diff -r afec3d7d46fe -r c537b10238fb sage-sage
    a b  
    144144    echo "  -fixdoctests <file.py> -- create <file.py>.out that would pass the doctests"
    145145    echo "                            and output a patch"
    146146    echo "  -startuptime        -- display how long each component of Sage takes to"
     147    echo "  -sws2rst <sws document>  Generates a rst file from the given document"
    147148    echo "                         start up"
    148149    echo "  -t [options] <files|dir>"
    149150    echo "                      -- test examples in .py, .pyx, .sage or .tex files"
     
    700701   echo -n " Do you want to proceed? [y/n] "
    701702   read CHOICE
    702703   while [ "$CHOICE" != "y" -a "$CHOICE" != "n" ]; do
    703            echo -n " Choose one of y, n: "
    704            read CHOICE
     704      echo -n " Choose one of y, n: "
     705      read CHOICE
    705706   done
    706707   if [ $CHOICE = 'n' ]; then
    707            exit 0
     708       exit 0
    708709   fi
    709710   build_sage -b "$@"
    710711   exit $?
     
    804805       PKG_NAME=`echo "$PKG" | sed -e "s/\.spkg$//"`
    805806       PKG_NAME=`basename "$PKG_NAME"`
    806807       case $PKG in
    807            /*)
    808                sage-spkg $OPT "$PKG" 2>&1 | (trap "" SIGINT; tee -a ../install.log "$SAGE_LOGS/$PKG_NAME".log)
    809            ;;
    810            *)
    811                sage-spkg $OPT "$CUR/$PKG" 2>&1 | (trap "" SIGINT; tee -a ../install.log "$SAGE_LOGS/$PKG_NAME".log)
    812            ;;
     808       /*)
     809           sage-spkg $OPT "$PKG" 2>&1 | (trap "" SIGINT; tee -a ../install.log "$SAGE_LOGS/$PKG_NAME".log)
     810       ;;
     811       *)
     812           sage-spkg $OPT "$CUR/$PKG" 2>&1 | (trap "" SIGINT; tee -a ../install.log "$SAGE_LOGS/$PKG_NAME".log)
     813       ;;
    813814       esac
    814815
    815816       if [ $? -ne 0 ]; then
     
    10081009    python "$SAGE_LOCAL"/bin/sage-startuptime.py
    10091010    exit $?
    10101011fi
     1012
     1013if [ "$1" = '-sws2rst' -o "$1" = '--sws2rst' ]; then
     1014   cd "$CUR"
     1015   echo hola, "$CUR"
     1016   shift
     1017   sage-sws2rst "$@"
     1018   exit $?
     1019fi
     1020
    10111021if [ "$1" = '-gthread' -o "$1" = '-qthread' -o "$1" = '-q4thread' -o "$1" = '-wthread' -o "$1" = '-pylab' ]; then
    10121022    sage "$1"
    10131023    exit $?
  • new file sage-sws2rst

    diff -r afec3d7d46fe -r c537b10238fb sage-sws2rst
    - +  
     1#!/usr/bin/python
     2# -*- coding: utf-8 -*-
     3
     4import sys
     5import tarfile
     6import os
     7import shutil
     8import codecs
     9from sagenb.misc.worksheet2rst import parse
     10
     11if __name__=='__main__':
     12    if len(sys.argv)<=1:
     13        print 'First argument should be a sws file'
     14        sys.exit()
     15
     16#    retcode = call(['tar','jxf', sys.argv[1], '-o',path2])
     17   
     18    fichero_sws = tarfile.open(sys.argv[1], mode='r:bz2')
     19    #TODO: python complains about using tempnam, but I don't
     20    #know hot to fix it or see any danger
     21    tempname = os.tempnam('.')
     22    fichero_sws.extractall(tempname)
     23    nombre_base = os.path.splitext(sys.argv[1])[0]
     24
     25    #Imagenes
     26    images_dir = nombre_base + '_media'
     27    if not os.path.exists(images_dir):
     28        os.mkdir(images_dir)
     29   
     30    #"data" dir
     31    data_path = os.path.join(tempname,'sage_worksheet','data')
     32    if os.path.exists(data_path):
     33        for image in os.listdir(data_path):
     34            try:
     35                shutil.move(os.path.join(data_path, image), images_dir)
     36            except shutil.Error:
     37                pass
     38
     39    #cells
     40    cells_path = os.path.join(tempname,'sage_worksheet','cells')   
     41    for cell in os.listdir(cells_path):
     42        cell_path = os.path.join(cells_path, cell)
     43        for image in os.listdir(cell_path):
     44            shutil.copy2(os.path.join(cell_path, image), images_dir)
     45            shutil.move(os.path.join(images_dir, image),
     46                        os.path.join(images_dir, 'cell_%s_%s'%(cell,image)))
     47   
     48    #Fichero rst
     49    fichero = codecs.open(os.path.join('.',tempname,'sage_worksheet','worksheet.html'),
     50                          mode='r',
     51                          encoding='utf-8')
     52    html_text = fichero.read()
     53    fichero.close()
     54    rst_text = parse(html_text, images_dir = images_dir)
     55    fichero_rst = nombre_base + '.rst'
     56    fichero_out = codecs.open(fichero_rst, mode='w',
     57                  encoding='utf-8')
     58    fichero_out.write(rst_text)
     59    fichero_out.close()
     60
     61    shutil.rmtree(tempname)