1
  1   ˆl–ßi~”Êa,jm@»q—î4â˜´o�þ@³‚+…d}ÿçÃ0ÂÁÀ  5   ˆl–Ým_JJi?u8 ¸®¥1hß�þB g�Œ‰ÂÈ×óÄƒh¹ÃÂÁ      0000004f
  5   	ˆl–Ým_JJi?u8 ¸Û÷ò˜´o�þB g�¼	Z+àùÅƒh¹ÄÃÂ      	numa_hit 1306661511
numa_miss 0
numa_foreign 0
interleave_hit 867
local_node 1306661511
other_node 0
  7   ˆl–Ðz¾” T†»ú‚À;q�©‹Fÿ�þcBY·›t®<ùüÿÆ„i¶]{ÅÄÃ ?÷     #!/usr/bin/env python3
r'''
------------------------------------------------------------------------------
    webquiz | Online quizzes generated from LaTeX using python and TeX4ht
               | This module mainly deals with command-line options and
               | settings and then calls MakeWebQuiz to build the quiz
------------------------------------------------------------------------------
    Copyright (C) Andrew Mathas and Donald Taylor, University of Sydney

    Distributed under the terms of the GNU General Public License (GPL)
                  http://www.gnu.org/licenses/

    This file is part of the WebQuiz system.

    <Andrew.Mathas@sydney.edu.au>
------------------------------------------------------------------------------
'''

import argparse
import codecs
import errno
import glob
import os
import re
import shutil
import signal
import subprocess
import sys

# imports of webquiz code
import webquiz_makequiz
import webquiz_templates
import webquiz_util

#################################################################################
# read in basic meta data such as author, version, ... and set debugging=False
try:
    metadata = webquiz_util.MetaData(webquiz_util.kpsewhich('webquiz.ini'), debugging=False)
except subprocess.CalledProcessError:
    # check to see if we are running from the zip file
    ini_file = os.path.join(webquiz_util.webquiz_file(''), '..', 'latex', 'webquiz.ini')
    try:
        metadata = webquiz_util.MetaData(ini_file, debugging=False)
    except (FileNotFoundError, subprocess.CalledProcessError):
        print(webquiz_templates.missing_webquiz_ini.format(ini_file))
        sys.exit(1)

# ---------------------------------------------------------------------------------------
def graceful_exit(sig, frame):
    ''' exit gracefully on SIGINT and SIGTERM'''
    if metadata:
        webquiz_util.webquiz_error(False, 'program terminated (signal {}\n  {})'.format(sig, frame))
    else:
        webquiz_util.webquiz_error(False, 'program terminated (signal {})'.format(sig))

signal.signal(signal.SIGINT, graceful_exit)
signal.signal(signal.SIGTERM, graceful_exit)


#################################################################################
def preprocess_with_pst2pdf(options, quiz_file):
    r'''
    Preprocess the latex file using pst2pdf. As we are preprocessing the file it
    is not enough to have latex pass us a flag that tells us to use pst2pdf.
    Instead, we have to extract the class file option from the tex file

    INPUT: quiz_file should be the name of the quiz file, WITHOUT the .tex extension
    '''
    options.talk('Preprocessing {} with pst2pdsf'.format(quiz_file))
    try:
        # pst2pdf converts pspicture environments to svg images and makes a
        # new latex file quiz_file+'-pdf' that includes these
        cmd = 'pst2pdf --svg --imgdir={q_file} {q_file}.tex'.format(q_file=quiz_file)
        # pst2pdf is missing a #!-header so we need shell=True
        options.run(cmd, shell=True)
    except OSError as err:
        if err.errno == errno.ENOENT:
            webquiz_util.webquiz_error(options.debugging, 'pst2pdf not found. You need to install pst2pdf to use the pst2pdf option', err)
        else:
            webquiz_util.webquiz_error(options.debugging, 'error running pst2pdf on {}'.format(quiz_file), err)

    # match \includegraphics commands
    fix_svg = re.compile(r'(\\includegraphics\[scale=1\])\{('+quiz_file+r'-fig-[0-9]*)\}')
    # the svg images are in the quiz_file subdirectory but latex can't
    # find them so we update the tex file to look in the right place
    try:
        with codecs.open(quiz_file + '-pdf.tex', 'r', encoding='utf8') as pst_file:
            w