| 1 |
#!/usr/bin/env python3 |
| 2 |
r''' |
| 3 |
------------------------------------------------------------------------------ |
| 4 |
webquiz | Online quizzes generated from LaTeX using python and TeX4ht |
| 5 |
| This module mainly deals with command-line options and |
| 6 |
| settings and then calls MakeWebQuiz to build the quiz |
| 7 |
------------------------------------------------------------------------------ |
| 8 |
Copyright (C) Andrew Mathas and Donald Taylor, University of Sydney |
| 9 |
|
| 10 |
Distributed under the terms of the GNU General Public License (GPL) |
| 11 |
http://www.gnu.org/licenses/ |
| 12 |
|
| 13 |
This file is part of the WebQuiz system. |
| 14 |
|
| 15 |
<[email protected]> |
| 16 |
------------------------------------------------------------------------------ |
| 17 |
''' |
| 18 |
|
| 19 |
import argparse |
| 20 |
import codecs |
| 21 |
import errno |
| 22 |
import glob |
| 23 |
import os |
| 24 |
import re |
| 25 |
import shutil |
| 26 |
import signal |
| 27 |
import subprocess |
| 28 |
import sys |
| 29 |
|
| 30 |
# imports of webquiz code |
| 31 |
import webquiz_makequiz |
| 32 |
import webquiz_templates |
| 33 |
import webquiz_util |
| 34 |
|
| 35 |
################################################################################# |
| 36 |
# read in basic meta data such as author, version, ... and set debugging=False |
| 37 |
try: |
| 38 |
metadata = webquiz_util.MetaData(webquiz_util.kpsewhich('webquiz.ini'), debugging=False) |
| 39 |
except subprocess.CalledProcessError: |
| 40 |
# check to see if we are running from the zip file |
| 41 |
ini_file = os.path.join(webquiz_util.webquiz_file(''), '..', 'latex', 'webquiz.ini') |
| 42 |
try: |
| 43 |
metadata = webquiz_util.MetaData(ini_file, debugging=False) |
| 44 |
except (FileNotFoundError, subprocess.CalledProcessError): |
| 45 |
print(webquiz_templates.missing_webquiz_ini.format(ini_file)) |
| 46 |
sys.exit(1) |
| 47 |
|
| 48 |
# --------------------------------------------------------------------------------------- |
| 49 |
def graceful_exit(sig, frame): |
| 50 |
''' exit gracefully on SIGINT and SIGTERM''' |
| 51 |
if metadata: |
| 52 |
webquiz_util.webquiz_error(False, 'program terminated (signal {}\n {})'.format(sig, frame)) |
| 53 |
else: |
| 54 |
webquiz_util.webquiz_error(False, 'program terminated (signal {})'.format(sig)) |
| 55 |
|
| 56 |
signal.signal(signal.SIGINT, graceful_exit) |
| 57 |
signal.signal(signal.SIGTERM, graceful_exit) |
| 58 |
|
| 59 |
|
| 60 |
################################################################################# |
| 61 |
def preprocess_with_pst2pdf(options, quiz_file): |
| 62 |
r''' |
| 63 |
Preprocess the latex file using pst2pdf. As we are preprocessing the file it |
| 64 |
is not enough to have latex pass us a flag that tells us to use pst2pdf. |
| 65 |
Instead, we have to extract the class file option from the tex file |
| 66 |
|
| 67 |
INPUT: quiz_file should be the name of the quiz file, WITHOUT the .tex extension |
| 68 |
''' |
| 69 |
options.talk('Preprocessing {} with pst2pdsf'.format(quiz_file)) |
| 70 |
try: |
| 71 |
# pst2pdf converts pspicture environments to svg images and makes a |
| 72 |
# new latex file quiz_file+'-pdf' that includes these |
| 73 |
cmd = 'pst2pdf --svg --imgdir={q_file} {q_file}.tex'.format(q_file=quiz_file) |
| 74 |
# pst2pdf is missing a #!-header so we need shell=True |
| 75 |
options.run(cmd, shell=True) |
| 76 |
except OSError as err: |
| 77 |
if err.errno == errno.ENOENT: |
| 78 |
webquiz_util.webquiz_error(options.debugging, 'pst2pdf not found. You need to install pst2pdf to use the pst2pdf option', err) |
| 79 |
else: |
| 80 |
webquiz_util.webquiz_error(options.debugging, 'error running pst2pdf on {}'.format(quiz_file), err) |
| 81 |
|
| 82 |
# match \includegraphics commands |
| 83 |
fix_svg = re.compile(r'(\\includegraphics\[scale=1\])\{('+quiz_file+r'-fig-[0-9]*)\}') |
| 84 |
# the svg images are in the quiz_file subdirectory but latex can't |
| 85 |
# find them so we update the tex file to look in the right place |
| 86 |
try: |
| 87 |
with codecs.open(quiz_file + '-pdf.tex', 'r', encoding='utf8') as pst_file: |
| 88 |
with codecs.open(quiz_file + '-pdf-fixed.tex', 'w', encoding='utf8') as pst_fixed: |
| 89 |
for line in pst_file: |
| 90 |
pst_fixed.write(fix_svg.sub(r'\1{%s/\2.svg}' % quiz_file, line)) |
| 91 |
except OSError as err: |
| 92 |
webquiz_util.webquiz_error(options.debugging, |
| 93 |
'there was an problem running pst2pdf for {}'.format(quiz_file), |
| 94 |
err |
| 95 |
) |
| 96 |
|
| 97 |
class WebQuizSettings: |
| 98 |
r''' |
| 99 |
Class for initialising webquiz. This covers both reading and writing the |
| 100 |
webquizrc file and copying files into the web directories during |
| 101 |
initialisation. The settings themselves are stored in the attribute |
| 102 |
settings, which is a dictionary. The class reads and writes the settings to |
| 103 |
the webquizrc file and the values of the settings are available as items: |
| 104 |
>>> wq = WebQuizSettings() |
| 105 |
>>> wq['webquiz_url'] |
| 106 |
... /WebQuiz |
| 107 |
>>> wq['webquiz_url'] = '/new_url' |
| 108 |
''' |
| 109 |
|
| 110 |
# default of settings for the webquizrc file - a dictionary of dictionaries |
| 111 |
# the 'help' field is for printing descriptions of the settings to help the |
| 112 |
# user - they are also printed in the webquizrc file |
| 113 |
settings = dict( |
| 114 |
webquiz_url={ |
| 115 |
'default': '', |
| 116 |
'advanced': False, |
| 117 |
'help': 'Relative URL for the webquiz web directory', |
| 118 |
}, |
| 119 |
webquiz_www={ |
| 120 |
'default': '', |
| 121 |
'advanced': False, |
| 122 |
'help': 'Full path to WebQuiz web directory', |
| 123 |
}, |
| 124 |
language={ |
| 125 |
'default': 'english', |
| 126 |
'advanced': False, |
| 127 |
'help': 'Default language used on web pages' |
| 128 |
}, |
| 129 |
engine = { |
| 130 |
'default': 'latex', |
| 131 |
'advanced': False, |
| 132 |
'help': 'Default TeX engine used to compile web pages', |
| 133 |
'values': dict(latex='', lua='--lua', xelatex='--xetex') |
| 134 |
}, |
| 135 |
theme={ |
| 136 |
'default': 'default', |
| 137 |
'advanced': False, |
| 138 |
'help': 'Default colour theme used on web pages' |
| 139 |
}, |
| 140 |
breadcrumbs={ |
| 141 |
'default': '', |
| 142 |
'advanced': False, |
| 143 |
'help': 'Breadcrumbs at the top of quiz page', |
| 144 |
}, |
| 145 |
department={ |
| 146 |
'default': '', |
| 147 |
'advanced': False, |
| 148 |
'help': 'Name of department', |
| 149 |
}, |
| 150 |
department_url={ |
| 151 |
'default': '/', |
| 152 |
'advanced': False, |
| 153 |
'help': 'URL for department', |
| 154 |
}, |
| 155 |
institution={ |
| 156 |
'default': '', |
| 157 |
'advanced': False, |
| 158 |
'help': 'Institution or university', |
| 159 |
}, |
| 160 |
institution_url={ |
| 161 |
'default': '/', |
| 162 |
'advanced': False, |
| 163 |
'help': 'URL for institution or university', |
| 164 |
}, |
| 165 |
hide_side_menu={ |
| 166 |
'default': 'false', |
| 167 |
'advanced': False, |
| 168 |
'help': 'Do not display the side menu at start of quiz', |
| 169 |
}, |
| 170 |
one_page={ |
| 171 |
'default': 'false', |
| 172 |
'advanced': False, |
| 173 |
'help': 'Display questions on one page', |
| 174 |
}, |
| 175 |
random_order={ |
| 176 |
'default': 'false', |
| 177 |
'advanced': False, |
| 178 |
'help': 'Randomly order the quiz questions', |
| 179 |
}, |
| 180 |
webquiz_layout={ |
| 181 |
'default': 'webquiz_layout', |
| 182 |
'advanced': True, |
| 183 |
'help': 'Name of python module that formats the quizzes', |
| 184 |
}, |
| 185 |
make4ht={ |
| 186 |
'default': '', |
| 187 |
'advanced': True, |
| 188 |
'help': 'Build file for make4ht', |
| 189 |
}, |
| 190 |
mathjax={ |
| 191 |
'default': |
| 192 |
'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js', |
| 193 |
'advanced': |
| 194 |
True, |
| 195 |
'help': |
| 196 |
'URL for mathjax', |
| 197 |
}, |
| 198 |
version={ |
| 199 |
'advanced': False, |
| 200 |
'help': 'WebQuiz version number for webquizrc settings', |
| 201 |
}) |
| 202 |
|
| 203 |
# by default we assume we don't need to print a initialisation warning |
| 204 |
initialise_warning = '' |
| 205 |
|
| 206 |
# turn debugging on by default because any error message that we hit before |
| 207 |
# we process the command line options really should not happen |
| 208 |
debugging = True |
| 209 |
|
| 210 |
# keep track of whether we have initialised |
| 211 |
have_initialised = False |
| 212 |
|
| 213 |
def __init__(self): |
| 214 |
''' |
| 215 |
First read the system webquizrc file and then read the |
| 216 |
to use some system settings and to override others. |
| 217 |
|
| 218 |
By default, there is no webquiz initialisation file. We first |
| 219 |
look for webquizrc in the webquiz source directory and then |
| 220 |
for .webquizrc file in the users home directory. |
| 221 |
''' |
| 222 |
self.settings['version']['default'] = metadata.version |
| 223 |
for key in self.settings: |
| 224 |
self.settings[key]['value'] = self.settings[key]['default'] |
| 225 |
if not 'editable' in self.settings[key]: |
| 226 |
self.settings[key]['editable'] = False |
| 227 |
|
| 228 |
# define user and system rc file and load the ones that exist |
| 229 |
|
| 230 |
TEXMFLOCAL='' |
| 231 |
try: |
| 232 |
TEXMFLOCAL = webquiz_util.kpsewhich('-var-value TEXMFLOCAL') |
| 233 |
except subprocess.CalledProcessError: |
| 234 |
pass |
| 235 |
|
| 236 |
if TEXMFLOCAL == '': |
| 237 |
TEXMFLOCAL = webquiz_util.kpsewhich('-var-value TEXMFMAIN') |
| 238 |
|
| 239 |
self.system_rcfile = os.path.join(TEXMFLOCAL, 'tex', 'latex', 'webquiz', 'webquizrc') |
| 240 |
self.read_webquizrc(self.system_rcfile) |
| 241 |
|
| 242 |
# the user rc file defaults to: |
| 243 |
# ~/.dotfiles/config/webquizrc if .dotfiles/config exists |
| 244 |
# ~/.config/webquizrc if .config exists |
| 245 |
# and otherwise to ~/.webquizrc |
| 246 |
if os.path.isdir(os.path.join(os.path.expanduser('~'), '.dotfiles', 'config')): |
| 247 |
self.user_rcfile = os.path.join(os.path.expanduser('~'), '.dotfiles', 'config', 'webquizrc') |
| 248 |
elif os.path.isdir(os.path.join(os.path.expanduser('~'), '.config')): |
| 249 |
self.user_rcfile = os.path.join(os.path.expanduser('~'), '.config', 'webquizrc') |
| 250 |
else: |
| 251 |
self.user_rcfile = os.path.join(os.path.expanduser('~'), '.webquizrc') |
| 252 |
|
| 253 |
self.read_webquizrc(self.user_rcfile) |
| 254 |
|
| 255 |
def webquiz_debug(self, msg): |
| 256 |
r''' |
| 257 |
Customised debugging message for the MakeSettings module |
| 258 |
''' |
| 259 |
webquiz_util.webquiz_debug(self.debugging, 'main: '+msg) |
| 260 |
|
| 261 |
def webquiz_error(self, msg, err=None): |
| 262 |
r''' |
| 263 |
Customised error messages for the Module |
| 264 |
''' |
| 265 |
webquiz_util.webquiz_error(self.debugging, 'settings: '+msg, err) |
| 266 |
|
| 267 |
def __getitem__(self, key): |
| 268 |
r''' |
| 269 |
Return the value of the corresponding setting. That is, it returns |
| 270 |
self.settings[key]['value'] |
| 271 |
and an error if the key is unknown. |
| 272 |
''' |
| 273 |
if key in self.settings: |
| 274 |
return self.settings[key]['value'] |
| 275 |
|
| 276 |
self.webquiz_error('getitem: unknown setting "{}" in webquizrc.'.format(key)) |
| 277 |
|
| 278 |
def __setitem__(self, key, value): |
| 279 |
r''' |
| 280 |
Set the value of the corresponding setting. This is the equivalent of |
| 281 |
self.settings[key]['value'] = value |
| 282 |
and an error if the key is unknown. |
| 283 |
''' |
| 284 |
if key in self.settings: |
| 285 |
self.settings[key]['value'] = value |
| 286 |
else: |
| 287 |
self.webquiz_error('setitem: unknown setting "{}" in webquizrc'.format(key)) |
| 288 |
|
| 289 |
def read_webquizrc(self, rcfile, must_exist=False): |
| 290 |
r''' |
| 291 |
Read the settings from the specified webquizrc file - if it exists, in |
| 292 |
which case set self.rcfile equal to this directory. If the file does |
| 293 |
not exist then return without changing the current settings. |
| 294 |
''' |
| 295 |
if os.path.isfile(rcfile): |
| 296 |
try: |
| 297 |
with codecs.open(rcfile, 'r', encoding='utf8') as webquizrc: |
| 298 |
for line in webquizrc: |
| 299 |
if '#' in line: # remove comments |
| 300 |
line = line[:line.index('#')] |
| 301 |
if '=' in line: |
| 302 |
key, value = line.split('=') |
| 303 |
key = key.strip().lower().replace('-','_') |
| 304 |
value = value.strip() |
| 305 |
if key in self.settings: |
| 306 |
if value != self[key]: |
| 307 |
self[key] = value |
| 308 |
elif key != '': |
| 309 |
self.webquiz_error('unknown setting "{}" in {}'.format(key, rcfile)) |
| 310 |
|
| 311 |
# record the rcfile for later use |
| 312 |
self.rcfile = rcfile |
| 313 |
|
| 314 |
except OSError as err: |
| 315 |
self.webquiz_error('there was a problem reading the rc-file {}'.format(rcfile), err) |
| 316 |
|
| 317 |
except Exception as err: |
| 318 |
self.webquiz_error('there was an error reading the webquizrc file,', err) |
| 319 |
|
| 320 |
elif must_exist: |
| 321 |
# this is only an error if we have been asked to read this file |
| 322 |
self.webquiz_error('the rc-file "{}" does not exist'.format(rcfile)) |
| 323 |
|
| 324 |
def keys(self): |
| 325 |
r''' |
| 326 |
Return a list of keys for all settings, ordered alphabetically with the |
| 327 |
advanced options last/ |
| 328 |
''' |
| 329 |
return sorted(self.settings.keys(), key=lambda k: '{}{}'.format(self.settings[k]['advanced'], k)) |
| 330 |
|
| 331 |
def write_webquizrc(self): |
| 332 |
r''' |
| 333 |
Write the settings to the webquizrc file, defaulting to the user |
| 334 |
rcfile if unable to write to the system rcfile |
| 335 |
''' |
| 336 |
if not hasattr(self, 'rcfile'): |
| 337 |
# when initialising an rcfile will not exist yet |
| 338 |
self.rcfile = self.system_rcfile |
| 339 |
|
| 340 |
file_not_written = True |
| 341 |
while file_not_written: |
| 342 |
try: |
| 343 |
dire = os.path.dirname(self.rcfile) |
| 344 |
if dire != '' and not os.path.isdir(dire): |
| 345 |
os.makedirs(dire, exist_ok=True) |
| 346 |
with codecs.open(self.rcfile, 'w', encoding='utf8') as rcfile: |
| 347 |
for key in self.keys(): |
| 348 |
# Only save settings in the rcfile if they have changed |
| 349 |
# Note that changed means changed from the last read |
| 350 |
# rcfile rather than from the default (of course, the |
| 351 |
# defaults serve as the "initial rcfile") |
| 352 |
if key == 'version' or self.settings[key]['default']!=self[key]: |
| 353 |
rcfile.write('# {}\n{:<17} = {}\n'.format( |
| 354 |
self.settings[key]['help'], |
| 355 |
key.replace('_','-'), |
| 356 |
self[key]) |
| 357 |
) |
| 358 |
|
| 359 |
print('\nWebQuiz settings saved in {}\n'.format( self.rcfile)) |
| 360 |
input('Press RETURN to continue... ') |
| 361 |
file_not_written = False |
| 362 |
|
| 363 |
except (OSError, PermissionError) as err: |
| 364 |
# if writing to the system_rcfile then try to write to user_rcfile |
| 365 |
alt_rcfile = self.user_rcfile if self.rcfile != self.user_rcfile else self.system_rcfile |
| 366 |
response = input( |
| 367 |
webquiz_templates.rc_permission_error.format( |
| 368 |
error=err, |
| 369 |
rcfile=self.rcfile, |
| 370 |
alt_rcfile=alt_rcfile)) |
| 371 |
if response.startswith('2'): |
| 372 |
self.rcfile = alt_rcfile |
| 373 |
elif response.startswith('3'): |
| 374 |
rcfile = input('WebQuiz rc-file: ') |
| 375 |
print('\nTo access this rc-file you will need to use: webquiz --rcfile {} ...'.format(rcfile)) |
| 376 |
self.rcfile = os.path.expanduser(rcfile) |
| 377 |
elif not response.startswith('1'): |
| 378 |
print('exiting...') |
| 379 |
sys.exit(1) |
| 380 |
|
| 381 |
def list_settings(self, setting='all'): |
| 382 |
r''' |
| 383 |
Print the non-default settings for webquiz from the webquizrc |
| 384 |
''' |
| 385 |
if not hasattr(self, 'rcfile'): |
| 386 |
print(webquiz_templates.initialise_settings) |
| 387 |
sys.exit(1) |
| 388 |
|
| 389 |
if setting not in ['all', 'verbose', 'help']: |
| 390 |
setting = setting.replace('-', '_') |
| 391 |
if setting in self.settings: |
| 392 |
print(self.settings[setting]['value']) |
| 393 |
else: |
| 394 |
self.webquiz_error('{} is an invalid setting'.format(setting)) |
| 395 |
|
| 396 |
elif setting=='all': |
| 397 |
dash = '-'*len('WebQuiz rc-file: {}'.format(self.rcfile)) |
| 398 |
print('{dash}\nWebQuiz rc-file: {rcfile}\n{dash}'.format(rcfile=self.rcfile, dash=dash)) |
| 399 |
for key in self.keys(): |
| 400 |
print('{:<17} = {}'.format(key.replace('_', '-'), self[key])) |
| 401 |
print('{dash}'.format(dash=dash)) |
| 402 |
|
| 403 |
elif setting=='help': |
| 404 |
for key in self.keys(): |
| 405 |
print('{:<17} {}'.format(key.replace('_', '-'), self.settings[key]['help'].lower())) |
| 406 |
|
| 407 |
else: |
| 408 |
print('WebQuiz settings from {}'.format(self.rcfile)) |
| 409 |
for key in self.keys(): |
| 410 |
print('# {}{}\n{:<17} = {:<17} {}'.format( |
| 411 |
self.settings[key]['help'], |
| 412 |
' (advanced)' if self.settings[key]['advanced'] else '', |
| 413 |
key.replace('_', '-'), |
| 414 |
self[key], |
| 415 |
'(default)' if self[key]==self.settings[key]['default'] else '' |
| 416 |
) |
| 417 |
) |
| 418 |
|
| 419 |
def initialise_webquiz(self, need_to_initialise=False, developer=False): |
| 420 |
r''' |
| 421 |
Set the root for the WebQuiz web directory and copy the www files into |
| 422 |
this directory. Once this is done save the settings to webquizrc. |
| 423 |
This method should only be used when WebQuiz is being set up. |
| 424 |
|
| 425 |
If `need_to_initialise` is `True` then this is a forced initialisation. |
| 426 |
''' |
| 427 |
|
| 428 |
# keep track of whether we have initialised |
| 429 |
self.have_initialised = True |
| 430 |
|
| 431 |
if need_to_initialise: |
| 432 |
self.initialise_warning = webquiz_templates.web_initialise_warning |
| 433 |
initialise = input(webquiz_templates.initialise_invite) |
| 434 |
if initialise!='' and initialise.strip().lower()[0]!='y': |
| 435 |
self['webquiz_url'] = 'http://www.maths.usyd.edu.au/u/mathas/WebQuiz' |
| 436 |
return |
| 437 |
|
| 438 |
if self['webquiz_url']=='': |
| 439 |
self['webquiz_url'] = '/WebQuiz' |
| 440 |
|
| 441 |
# prompt for directory and copy files - are these reasonable defaults |
| 442 |
# for each OS? |
| 443 |
if sys.platform == 'darwin': |
| 444 |
default_root = '/Library/WebServer/Documents/WebQuiz' |
| 445 |
platform = 'Mac OSX' |
| 446 |
elif sys.platform.startswith('win'): |
| 447 |
default_root = 'c:\inetpub\wwwroot\WebQuiz' |
| 448 |
platform = 'Windows' |
| 449 |
else: |
| 450 |
default_root = '/var/www/html/WebQuiz' |
| 451 |
platform = sys.platform.capitalize() |
| 452 |
|
| 453 |
if self['webquiz_www'] != '': |
| 454 |
webquiz_root = self['webquiz_www'] |
| 455 |
else: |
| 456 |
webquiz_root = default_root |
| 457 |
|
| 458 |
print(webquiz_templates.initialise_introduction) |
| 459 |
input('Press RETURN to continue... ') |
| 460 |
|
| 461 |
print(webquiz_templates.webroot_request.format( |
| 462 |
platform=platform, |
| 463 |
webquiz_dir = webquiz_root) |
| 464 |
) |
| 465 |
input('Press RETURN to continue... ') |
| 466 |
|
| 467 |
files_copied = False |
| 468 |
while not files_copied: |
| 469 |
web_dir = input('\nWebQuiz web directory:\n[{}] '.format(webquiz_root)) |
| 470 |
if web_dir == '': |
| 471 |
web_dir = webquiz_root |
| 472 |
else: |
| 473 |
web_dir = os.path.expanduser(web_dir) |
| 474 |
|
| 475 |
print('Web directory set to {}'.format(web_dir)) |
| 476 |
if web_dir=='SMS': |
| 477 |
# undocumented: allow links to SMS web pages |
| 478 |
self['webquiz_www'] = 'SMS' |
| 479 |
self['webquiz_url'] = 'http://www.maths.usyd.edu.au/u/mathas/WebQuiz' |
| 480 |
|
| 481 |
else: |
| 482 |
try: |
| 483 |
# ...remove the doc directory |
| 484 |
web_doc = os.path.join(web_dir, 'doc') |
| 485 |
if os.path.isfile(web_doc) or os.path.islink(web_doc): |
| 486 |
os.remove(web_doc) |
| 487 |
elif os.path.isdir(web_doc): |
| 488 |
shutil.rmtree(web_doc) |
| 489 |
|
| 490 |
# Need to locate the www directory, which should be a subdirectory |
| 491 |
# of the webquiz doc directory. First try using texdoc |
| 492 |
webquiz_doc = '' |
| 493 |
try: |
| 494 |
webquiz_pdf = webquiz_util.shell_command('texdoc --list --machine webquiz.pdf').split()[-1] |
| 495 |
if webquiz_pdf.endswith('webquiz.pdf'): |
| 496 |
webquiz_doc = os.path.dirname(webquiz_pdf) |
| 497 |
except subprocess.CalledProcessError: |
| 498 |
pass |
| 499 |
|
| 500 |
# if texdoc failed then try using TEXMFMAIN |
| 501 |
if webquiz_doc=='': |
| 502 |
try: |
| 503 |
webquiz_doc = os.path.join(webquiz_util.kpsewhich('-var-value TEXMFMAIN'), 'doc','latex', 'webquiz') |
| 504 |
except subprocess.CalledProcessError: |
| 505 |
pass |
| 506 |
|
| 507 |
# if we still don't have webquiz_doc then try working backwards from webquiz.cls |
| 508 |
# unlikely to work if TEXMFMAIN doesn't |
| 509 |
if not os.path.isdir(webquiz_doc): |
| 510 |
parent = os.path.dirname |
| 511 |
try: |
| 512 |
texdist_dir = parent(parent(parent(parent(parent(webquiz_util.kpsewhich('webquiz.cls')))))) |
| 513 |
except subprocess.CalledProcessError: |
| 514 |
print(webquiz_templates.not_installed.format(metadata.repository)) |
| 515 |
sys.exit(1) |
| 516 |
|
| 517 |
webquiz_doc = os.path.join(texdist_dir, 'doc', 'latex', 'webquiz') |
| 518 |
|
| 519 |
# get the root directory of the source code for developer |
| 520 |
# mode and just in case webquiz_www still does not exist |
| 521 |
webquiz_src = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) |
| 522 |
|
| 523 |
webquiz_www = os.path.join(webquiz_doc, 'www') |
| 524 |
if not os.path.isdir(webquiz_www): |
| 525 |
webquiz_www = os.path.join(webquiz_src, 'doc', 'www') |
| 526 |
|
| 527 |
if developer and os.path.isdir(os.path.join(webquiz_src, 'doc')): |
| 528 |
# this is a development version so add links from the |
| 529 |
# web directory to the css,doc and js directories |
| 530 |
print('\nInstalling files for development version') |
| 531 |
print('Linking web files {} -> {} ...\n'.format(web_dir, webquiz_src)) |
| 532 |
if not os.path.exists(web_dir): |
| 533 |
os.makedirs(web_dir) |
| 534 |
|
| 535 |
for (src, target) in [('javascript', 'js'), ('css', 'css'), ('doc', 'doc')]: |
| 536 |
newlink = os.path.join(web_dir, target) |
| 537 |
try: |
| 538 |
os.remove(newlink) |
| 539 |
except FileNotFoundError: |
| 540 |
pass |
| 541 |
try: |
| 542 |
os.symlink(os.path.join(webquiz_src,src), newlink) |
| 543 |
except OSError as err: |
| 544 |
print('There was a problem linking {}: {}'.format(newlink, err)) |
| 545 |
|
| 546 |
else: |
| 547 |
# loop until we find some files to install or exit |
| 548 |
while not os.path.isdir(webquiz_www) or webquiz_www=='': |
| 549 |
print('\nUnable to find the WebQuiz web files') |
| 550 |
webquiz_www = input('Please enter the location of the WebQuiz www directory\nor press RETURN to exit: ') |
| 551 |
webquiz_www = os.path.expanduser(webquiz_www) |
| 552 |
if webquiz_www=='': |
| 553 |
sys.exit() |
| 554 |
if not (webquiz_www.endswith('www/') or webquiz_www.endswith('www')): |
| 555 |
print('\nThe webquiz web directory is called www, so\n {}\ncannot be the right directory.'.format( |
| 556 |
webquiz_www) |
| 557 |
) |
| 558 |
webquiz_www = False |
| 559 |
|
| 560 |
|
| 561 |
# the www directory exists so we copy it to web_dir |
| 562 |
print('\nCopying web files to {} ...'.format(web_dir)) |
| 563 |
webquiz_util.copytree(webquiz_www, web_dir) |
| 564 |
|
| 565 |
self['webquiz_www'] = web_dir |
| 566 |
files_copied = True |
| 567 |
|
| 568 |
except PermissionError: |
| 569 |
print(webquiz_templates.permission_error.format(web_dir)) |
| 570 |
|
| 571 |
except OSError as err: |
| 572 |
print(webquiz_templates.oserror_copying.format(web_dir=web_dir, err=err)) |
| 573 |
|
| 574 |
if self['webquiz_www']!='SMS': |
| 575 |
# now prompt for the relative url |
| 576 |
webquiz_url = input(webquiz_templates.webquiz_url_message.format(self['webquiz_url'])) |
| 577 |
if webquiz_url != '': |
| 578 |
# removing trailing slashes from webquiz_url |
| 579 |
while webquiz_url[-1] == '/': |
| 580 |
webquiz_url = webquiz_url[:len(webquiz_url) - 1] |
| 581 |
|
| 582 |
if webquiz_url[0] != '/': # force URL to start with / |
| 583 |
webquiz_url = '/' + webquiz_url |
| 584 |
|
| 585 |
if not web_dir.endswith(webquiz_url): |
| 586 |
print(webquiz_templates.webquiz_url_warning) |
| 587 |
input('Press RETURN to continue... ') |
| 588 |
|
| 589 |
self['webquiz_url'] = webquiz_url |
| 590 |
|
| 591 |
# save the settings and exit |
| 592 |
self.write_webquizrc() |
| 593 |
print(webquiz_templates.initialise_ending.format(web_dir=self['webquiz_www'])) |
| 594 |
|
| 595 |
def edit_settings(self): |
| 596 |
r''' |
| 597 |
Change current default values for the WebQuiz settings |
| 598 |
''' |
| 599 |
advanced_not_started = True |
| 600 |
for key in self.keys(): |
| 601 |
if key not in ['webquiz_www', 'version']: |
| 602 |
if advanced_not_started and self.settings[key]['advanced']: |
| 603 |
print(webquiz_templates.advanced_settings) |
| 604 |
advanced_not_started = False |
| 605 |
|
| 606 |
skey = '{}'.format(self[key]) |
| 607 |
setting = input('{}{}[{}]: '.format( |
| 608 |
self.settings[key]['help'], |
| 609 |
' ' if len(skey)<40 else '\n', |
| 610 |
skey |
| 611 |
) |
| 612 |
).strip() |
| 613 |
if setting != '': |
| 614 |
if key == 'webquiz_url' and setting[0] != '/': |
| 615 |
print(" ** prepending '/' to webquiz_url **") |
| 616 |
setting = '/' + setting |
| 617 |
|
| 618 |
elif key == 'webquiz_layout': |
| 619 |
setting = os.path.expanduser(setting) |
| 620 |
if setting.endswith('.py'): |
| 621 |
print(" ** removing .py extension from webquiz_layout **") |
| 622 |
setting = setting[:-3] |
| 623 |
|
| 624 |
elif key == 'engine' and setting not in self.settings['engine'].values: |
| 625 |
print('setting not changed: {} is not a valid TeX engine'.format(setting)) |
| 626 |
setting = self['engine'] |
| 627 |
|
| 628 |
elif key in ['hide_side_menu', 'random_order']: |
| 629 |
setting = setting.lower() |
| 630 |
if setting not in ['true', 'false']: |
| 631 |
print('setting not changed: {} must be True or False'.format(key)) |
| 632 |
setting = self[key] |
| 633 |
|
| 634 |
elif setting=='NONE': |
| 635 |
setting = '' |
| 636 |
|
| 637 |
self[key] = setting |
| 638 |
|
| 639 |
# save the settings, print them and exit |
| 640 |
self.write_webquizrc() |
| 641 |
self.list_settings() |
| 642 |
|
| 643 |
def tex_install(self): |
| 644 |
r''' |
| 645 |
Install the tex files into the standard locations in TEXMFMAIN: |
| 646 |
scripts -> TEXMFMAIN/scripts/webquiz |
| 647 |
doc -> TEXMFMAIN/doc/latex/webquiz |
| 648 |
latex -> TEXMFMAIN/tex/latex/webquiz |
| 649 |
It is assumed that this is run from the zipfile installation. There |
| 650 |
is little in the way of error checking or debugging. |
| 651 |
|
| 652 |
Undocumented feature - useful for debugging initialisation routine |
| 653 |
''' |
| 654 |
webquiz_top = os.path.abspath(webquiz_util.webquiz_file('..')) |
| 655 |
texmf = webquiz_util.kpsewhich('-var-value TEXMFMAIN') |
| 656 |
for (src, target) in [('scripts', 'scripts'), |
| 657 |
('latex', 'tex/latex'), |
| 658 |
('doc', 'doc/latex')]: |
| 659 |
try: |
| 660 |
webquiz_util.copytree(os.path.join(webquiz_top,src), os.path.join(texmf, target, 'webquiz')) |
| 661 |
|
| 662 |
except (FileExistsError,FileNotFoundError): |
| 663 |
continue |
| 664 |
|
| 665 |
except PermissionError as err: |
| 666 |
print(webquiz_templates.insufficient_permissions.format(err)) |
| 667 |
sys.exit(1) |
| 668 |
|
| 669 |
try: |
| 670 |
|
| 671 |
# add a link to webquiz.py |
| 672 |
texbin = os.path.dirname(shutil.which('pdflatex')) |
| 673 |
if sys.platform.startswith('win'): |
| 674 |
shutil.copyfile( |
| 675 |
os.path.join(texmf,'scripts','webquiz','webquiz.bat'), |
| 676 |
os.path.join(texbin, 'webquiz.bat') |
| 677 |
) |
| 678 |
else: |
| 679 |
os.symlink( |
| 680 |
os.path.join(texmf,'scripts','webquiz','webquiz.py'), |
| 681 |
os.path.join(texbin, 'webquiz') |
| 682 |
) |
| 683 |
subprocess.call('mktexlsr', shell=True) |
| 684 |
|
| 685 |
except (FileExistsError,FileNotFoundError): |
| 686 |
pass |
| 687 |
|
| 688 |
except PermissionError as err: |
| 689 |
print(webquiz_templates.insufficient_permissions.format(err)) |
| 690 |
sys.exit(1) |
| 691 |
|
| 692 |
except subprocess.CalledProcessError as err: |
| 693 |
self.webquiz_error('There was a problem running mktexlsr', err) |
| 694 |
|
| 695 |
def tex_uninstall(self): |
| 696 |
r''' |
| 697 |
UnInstall the tex files into TEXMFMAIN. It is assumed that the files |
| 698 |
are installed in the natural locations in the TEXMFMAIN tree, namely: |
| 699 |
scripts -> TEXMFMAIN/scripts/webquiz |
| 700 |
doc -> TEXMFMAIN/doc/latex/webquiz |
| 701 |
latex -> TEXMFMAIN/tex/latex/webquiz |
| 702 |
There is little in the way of error checking or debugging. |
| 703 |
|
| 704 |
Undocumented feature - useful for debugging initialisation routine |
| 705 |
''' |
| 706 |
webquiz_top = os.path.abspath(webquiz_util.webquiz_file('..')) |
| 707 |
texmf = webquiz_util.kpsewhich('-var-value TEXMFMAIN') |
| 708 |
for target in ['scripts', 'tex/latex', 'doc/latex']: |
| 709 |
try: |
| 710 |
shutil.rmtree(os.path.join(texmf, target, 'webquiz')) |
| 711 |
|
| 712 |
except (FileExistsError,FileNotFoundError): |
| 713 |
pass |
| 714 |
|
| 715 |
except PermissionError as err: |
| 716 |
print(webquiz_templates.insufficient_permissions.format(err)) |
| 717 |
sys.exit(1) |
| 718 |
|
| 719 |
try: |
| 720 |
# remove link from texbin to webquiz.py |
| 721 |
texbin = os.path.dirname(shutil.which('pdflatex')) |
| 722 |
if sys.platform.startswith('win'): |
| 723 |
os.remove(os.path.join(texbin, 'webquiz.bat')) |
| 724 |
else: |
| 725 |
os.remove(os.path.join(texbin, 'webquiz')) |
| 726 |
|
| 727 |
except (FileExistsError,FileNotFoundError): |
| 728 |
pass |
| 729 |
|
| 730 |
except PermissionError as err: |
| 731 |
print(webquiz_templates.insufficient_permissions.format(err)) |
| 732 |
sys.exit(1) |
| 733 |
|
| 734 |
# remove any rcfiles that exist in obvious places |
| 735 |
try: |
| 736 |
if os.path.isfile(self.system_rcfile): |
| 737 |
os.remove(self.system_rcfile) |
| 738 |
if os.path.isfile(self.user_rcfile): |
| 739 |
os.remove(self.user_rcfile) |
| 740 |
if os.path.isfile(self.rcfile): |
| 741 |
os.remove(self.rcfile) |
| 742 |
except PermissionError: |
| 743 |
print(webquiz_templates.insufficient_permissions.format(err)) |
| 744 |
sys.exit(1) |
| 745 |
|
| 746 |
# remove link to webquiz.py |
| 747 |
texbin = os.path.dirname(shutil.which('pdflatex')) |
| 748 |
try: |
| 749 |
if sys.platform.startswith('win'): |
| 750 |
webquiz = os.path.join(texbin, 'webquiz.bat') |
| 751 |
os.remove(webquiz) |
| 752 |
else: |
| 753 |
webquiz = os.path.join(texbin,'webquiz') |
| 754 |
target = os.readlink(webquiz) |
| 755 |
if target==os.path.join(texmf,'scripts','webquiz','webquiz.py'): |
| 756 |
os.remove(webquiz) |
| 757 |
|
| 758 |
except (FileExistsError,FileNotFoundError): |
| 759 |
pass |
| 760 |
|
| 761 |
except OSError as err: |
| 762 |
print('There was a problem removing the link to webquiz: {}'.format(err)) |
| 763 |
|
| 764 |
def uninstall_webquiz(self): |
| 765 |
r''' |
| 766 |
Remove all of the webquiz files from the webserver |
| 767 |
''' |
| 768 |
|
| 769 |
if os.path.isdir(self['webquiz_www']): |
| 770 |
remove = input('Do you really want to remove the WebQuiz from your web server [N/yes]? ') |
| 771 |
if remove != 'yes': |
| 772 |
print('WebQuiz unistall aborted!') |
| 773 |
return |
| 774 |
|
| 775 |
try: |
| 776 |
shutil.rmtree(self['webquiz_www']) |
| 777 |
print('WebQuiz files successfully removed from {}'.format(self['webquiz_www'])) |
| 778 |
|
| 779 |
except PermissionError as err: |
| 780 |
print(webquiz_templates.insufficient_permissions.format(err)) |
| 781 |
sys.exit(1) |
| 782 |
|
| 783 |
except OSError as err: |
| 784 |
self.webquiz_error('There was a problem removing webquiz files from {}'.format(self['webquiz_www']), err) |
| 785 |
|
| 786 |
# now reset and save the locations of the webquiz files and URL |
| 787 |
self['webquiz_url'] = '' |
| 788 |
self['webquiz_www'] = '' |
| 789 |
self.write_webquizrc() |
| 790 |
|
| 791 |
else: |
| 792 |
self.webquiz_error('uninstall: no webwquiz files are installed on your web server??') |
| 793 |
|
| 794 |
for rfile in ['system', 'user']: |
| 795 |
rcfile = getattr(self, rfile+'_rcfile') |
| 796 |
if os.path.isfile(rcfile): |
| 797 |
rm = input('Remove {} rcfile {}\n[Y/no] '.format(rfile, rcfile)) |
| 798 |
if rm != 'no': |
| 799 |
try: |
| 800 |
os.remove(rcfile) |
| 801 |
except (OSError, PermissionError) as err: |
| 802 |
self.webquiz_error('There was a problem deleting {}'.format(rcfile), err) |
| 803 |
|
| 804 |
|
| 805 |
# ===================================================== |
| 806 |
if __name__ == '__main__': |
| 807 |
try: |
| 808 |
settings = WebQuizSettings() |
| 809 |
|
| 810 |
# parse the command line options |
| 811 |
parser = argparse.ArgumentParser(description=metadata.description) |
| 812 |
|
| 813 |
parser.add_argument( |
| 814 |
'quiz_file', |
| 815 |
nargs='*', |
| 816 |
type=str, |
| 817 |
default=None, |
| 818 |
help='latex quiz files') |
| 819 |
|
| 820 |
parser.add_argument( |
| 821 |
'-q', |
| 822 |
'--quiet', |
| 823 |
action='count', |
| 824 |
default=0, |
| 825 |
help='Suppress tex4ht messages (also -qq etc)') |
| 826 |
|
| 827 |
parser.add_argument( |
| 828 |
'-d', '--draft', |
| 829 |
action='store_true', |
| 830 |
default=False, |
| 831 |
help='Use make4ht draft mode') |
| 832 |
|
| 833 |
parser.add_argument( |
| 834 |
'-s', |
| 835 |
'--shell-escape', |
| 836 |
action='store_true', |
| 837 |
default=False, |
| 838 |
help='Shell escape for tex4ht/make4ht') |
| 839 |
|
| 840 |
engine = parser.add_mutually_exclusive_group() |
| 841 |
engine.add_argument( |
| 842 |
'--latex', |
| 843 |
action='store_const', |
| 844 |
const='latex', |
| 845 |
default=settings['engine'], |
| 846 |
dest='engine', |
| 847 |
help='Use latex to compile document with make4ht (default)') |
| 848 |
engine.add_argument( |
| 849 |
'-l', |
| 850 |
'--lua', |
| 851 |
action='store_const', |
| 852 |
const='lua', |
| 853 |
dest='engine', |
| 854 |
help='Use lualatex to compile the quiz') |
| 855 |
engine.add_argument( |
| 856 |
'-x', |
| 857 |
'--xelatex', |
| 858 |
action='store_const', |
| 859 |
const='xelatex', |
| 860 |
dest='engine', |
| 861 |
help='Use xelatex to compile the quiz') |
| 862 |
|
| 863 |
parser.add_argument( |
| 864 |
'-r', |
| 865 |
'--rcfile', |
| 866 |
action='store', |
| 867 |
default=None, |
| 868 |
help='Specify location of the webquiz rc-file ') |
| 869 |
|
| 870 |
settings_parser = parser.add_mutually_exclusive_group() |
| 871 |
settings_parser.add_argument( |
| 872 |
'-i', |
| 873 |
'--initialise', |
| 874 |
'--initialize', |
| 875 |
action='store_true', |
| 876 |
default=False, |
| 877 |
help='Install web components of webquiz') |
| 878 |
settings_parser.add_argument( |
| 879 |
'-e', '--edit-settings', |
| 880 |
action='store_true', |
| 881 |
default=False, |
| 882 |
help='Edit default settings for webquiz') |
| 883 |
settings_parser.add_argument( |
| 884 |
'--settings', |
| 885 |
action='store', |
| 886 |
const='all', |
| 887 |
default='', |
| 888 |
nargs='?', |
| 889 |
type=str, |
| 890 |
help='List default settings for webquiz' |
| 891 |
) |
| 892 |
settings_parser.add_argument( |
| 893 |
'--developer', |
| 894 |
action='store_true', |
| 895 |
default=False, |
| 896 |
help=argparse.SUPPRESS |
| 897 |
) |
| 898 |
|
| 899 |
# options suppressed from the help message |
| 900 |
parser.add_argument( |
| 901 |
'-m', |
| 902 |
'--make4ht', |
| 903 |
action='store', |
| 904 |
type=str, |
| 905 |
dest='make4ht_options', |
| 906 |
default=settings['make4ht'], |
| 907 |
help=argparse.SUPPRESS |
| 908 |
) |
| 909 |
|
| 910 |
parser.add_argument( |
| 911 |
'--webquiz_layout', |
| 912 |
action='store', |
| 913 |
type=str, |
| 914 |
dest='webquiz_layout', |
| 915 |
default=settings['webquiz_layout'], |
| 916 |
help=argparse.SUPPRESS |
| 917 |
) |
| 918 |
|
| 919 |
install_parser = parser.add_mutually_exclusive_group() |
| 920 |
install_parser.add_argument( |
| 921 |
'--tex-install', |
| 922 |
action='store_true', |
| 923 |
default=False, |
| 924 |
help=argparse.SUPPRESS |
| 925 |
) |
| 926 |
install_parser.add_argument( |
| 927 |
'--tex-uninstall', |
| 928 |
action='store_true', |
| 929 |
default=False, |
| 930 |
help=argparse.SUPPRESS |
| 931 |
) |
| 932 |
install_parser.add_argument( |
| 933 |
'--uninstall', |
| 934 |
action='store_true', |
| 935 |
default=False, |
| 936 |
help=argparse.SUPPRESS |
| 937 |
) |
| 938 |
|
| 939 |
parser.add_argument( |
| 940 |
'--version', |
| 941 |
action='version', |
| 942 |
version='%(prog)s version {}'.format(metadata.version), |
| 943 |
help=argparse.SUPPRESS) |
| 944 |
|
| 945 |
parser.add_argument( |
| 946 |
'--debugging', |
| 947 |
action='store_true', |
| 948 |
default=False, |
| 949 |
help=argparse.SUPPRESS) |
| 950 |
|
| 951 |
parser.add_argument( |
| 952 |
'--shorthelp', |
| 953 |
action='store_true', |
| 954 |
default=False, |
| 955 |
help=argparse.SUPPRESS |
| 956 |
) |
| 957 |
|
| 958 |
# parse the options |
| 959 |
options = parser.parse_args() |
| 960 |
options.prog = parser.prog |
| 961 |
|
| 962 |
# set debugging mode from options |
| 963 |
settings.debugging = options.debugging |
| 964 |
|
| 965 |
# read the rcfile and throw an error if we are not adjusting the settings |
| 966 |
if options.rcfile is not None: |
| 967 |
rcfile = os.path.expanduser(options.rcfile) |
| 968 |
settings.read_webquizrc(rcfile) |
| 969 |
|
| 970 |
if options.uninstall: |
| 971 |
# uninstall web files and exit |
| 972 |
settings.uninstall_webquiz() |
| 973 |
sys.exit() |
| 974 |
elif options.tex_install: |
| 975 |
# install files from zip file into tex distribution and then exit |
| 976 |
settings.tex_install() |
| 977 |
sys.exit() |
| 978 |
elif options.tex_uninstall: |
| 979 |
# install files from zip file into tex distribution and then exit |
| 980 |
settings.tex_uninstall() |
| 981 |
sys.exit() |
| 982 |
|
| 983 |
# initialise and exit |
| 984 |
if options.initialise or options.developer: |
| 985 |
settings.initialise_webquiz(developer=options.developer) |
| 986 |
|
| 987 |
# force initialisation if the url is not set |
| 988 |
elif settings['webquiz_url'] == '': |
| 989 |
settings.initialise_webquiz(need_to_initialise=True) |
| 990 |
|
| 991 |
# list settings and exit |
| 992 |
if options.settings != '': |
| 993 |
settings.list_settings(options.settings) |
| 994 |
sys.exit() |
| 995 |
|
| 996 |
# edit settings and exit |
| 997 |
if options.edit_settings: |
| 998 |
settings.edit_settings() |
| 999 |
sys.exit() |
| 1000 |
|
| 1001 |
# print short help and exit |
| 1002 |
if options.shorthelp: |
| 1003 |
parser.print_usage() |
| 1004 |
sys.exit() |
| 1005 |
|
| 1006 |
# if no filename then exit |
| 1007 |
if options.quiz_file==[]: |
| 1008 |
if settings.have_initialised: |
| 1009 |
sys.exit() |
| 1010 |
else: |
| 1011 |
parser.print_help() |
| 1012 |
sys.exit(1) |
| 1013 |
|
| 1014 |
# import the local page formatter |
| 1015 |
mod_dir, mod_layout = os.path.split(options.webquiz_layout) |
| 1016 |
if mod_dir != '': |
| 1017 |
sys.path.insert(0, mod_dir) |
| 1018 |
options.write_web_page = __import__(mod_layout).write_web_page |
| 1019 |
|
| 1020 |
# run() is a shorthand for executing system commands depending on the quietness |
| 1021 |
# - we need to use shell=True because otherwise pst2pdf gives an error |
| 1022 |
# options.talk() is a shorthand for letting the user know what is happening |
| 1023 |
if options.quiet == 0: |
| 1024 |
options.run = webquiz_util.run |
| 1025 |
options.talk = lambda msg: print(msg) |
| 1026 |
elif options.quiet == 1: |
| 1027 |
options.run = webquiz_util.quiet_run |
| 1028 |
options.talk = lambda msg: print(msg) |
| 1029 |
else: |
| 1030 |
options.run = webquiz_util.silent_run |
| 1031 |
options.talk = lambda msg: None |
| 1032 |
|
| 1033 |
# run through the list of quizzes and make them |
| 1034 |
for quiz_file in options.quiz_file: |
| 1035 |
if len(options.quiz_file) > 1 and options.quiet < 3: |
| 1036 |
print('Making web page for {}'.format(quiz_file)) |
| 1037 |
|
| 1038 |
quiz_name, ext = os.path.splitext(quiz_file) # extract filename and extension |
| 1039 |
# quiz_file is assumed to be a tex file if no extension is given |
| 1040 |
if ext=='': |
| 1041 |
ext = '.tex' |
| 1042 |
quiz_file += ext |
| 1043 |
|
| 1044 |
# windows likes adding a prefix of '.\\'to filename and this causes havoc with latex |
| 1045 |
if os.path.dirname(quiz_file)=='.': |
| 1046 |
quiz_file = os.path.basename(quiz_file) |
| 1047 |
|
| 1048 |
if ext not in ['.tex', '.xml']: |
| 1049 |
webquiz_util.webquiz_error(True, 'unrecognised file extension {}'.format(ext)) |
| 1050 |
|
| 1051 |
if not os.path.isfile(quiz_file): |
| 1052 |
webquiz_util.webquiz+error(True, 'WebQuiz error: cannot read file {}'.format(quiz_file)) |
| 1053 |
|
| 1054 |
# the quiz name and the quiz_file will be different if pst2pdf is used |
| 1055 |
quiz_name = quiz_file |
| 1056 |
if options.quiet < 2: |
| 1057 |
print('WebQuiz generating web page for {}'.format(quiz_file)) |
| 1058 |
|
| 1059 |
options.pst2pdf = False |
| 1060 |
if ext==".tex": |
| 1061 |
# If the pst2pdf option is used then we need to preprocess |
| 1062 |
# the latex file BEFORE passing it to MakeWebQuiz. Set |
| 1063 |
# options.pst2pdf = True if pst2pdf is given as an option to |
| 1064 |
# the webquiz documentclass |
| 1065 |
with codecs.open(quiz_file, 'r', encoding='utf8') as q_file: |
| 1066 |
doc = q_file.read() |
| 1067 |
|
| 1068 |
try: |
| 1069 |
brac = doc.index(r'\documentclass[') + 15 # start of class options |
| 1070 |
if 'pst2pdf' in [ |
| 1071 |
opt.strip() |
| 1072 |
for opt in doc[brac:brac+doc[brac:].index(']')].split(',') |
| 1073 |
]: |
| 1074 |
preprocess_with_pst2pdf(options, quiz_file[:-4]) |
| 1075 |
options.pst2pdf = True |
| 1076 |
# now run webquiz on the modified tex file |
| 1077 |
quiz_file = quiz_file[:-4] + '-pdf-fixed.tex' |
| 1078 |
except ValueError: |
| 1079 |
pass |
| 1080 |
|
| 1081 |
# the file exists and is readable so make the quiz |
| 1082 |
webquiz_makequiz.MakeWebQuiz(quiz_name, quiz_file, options, settings, metadata) |
| 1083 |
|
| 1084 |
quiz_name, ext = os.path.splitext(quiz_name) # extract filename and extension |
| 1085 |
|
| 1086 |
# move the css file into the directory for the quiz |
| 1087 |
css_file = os.path.join(quiz_name, quiz_name + '.css') |
| 1088 |
if os.path.isfile(quiz_name + '.css'): |
| 1089 |
if os.path.isfile(css_file): |
| 1090 |
os.remove(css_file) |
| 1091 |
shutil.move(quiz_name + '.css', css_file) |
| 1092 |
|
| 1093 |
# now clean up unless debugging |
| 1094 |
if not options.debugging: |
| 1095 |
for ext in ['4ct', '4tc', 'dvi', 'idv', 'lg', 'log', |
| 1096 |
'ps', 'pdf', 'tmp', 'xml', 'xref' |
| 1097 |
]: |
| 1098 |
if os.path.isfile(quiz_name + '.' + ext): |
| 1099 |
os.remove(quiz_name + '.' + ext) |
| 1100 |
|
| 1101 |
# files created when using pst2pdf |
| 1102 |
if options.pst2pdf: |
| 1103 |
for file in glob.glob(quiz_name + '-pdf.*'): |
| 1104 |
os.remove(file) |
| 1105 |
for file in glob.glob(quiz_name + '-pdf-fixed.*'): |
| 1106 |
os.remove(file) |
| 1107 |
for extention in ['.preamble', '.plog', '-tmp.tex', |
| 1108 |
'-pst.tex', '-fig.tex' |
| 1109 |
]: |
| 1110 |
if os.path.isfile(quiz_name + extention): |
| 1111 |
os.remove(quiz_name + extention) |
| 1112 |
if os.path.isdir(os.path.join(quiz_name, quiz_name)): |
| 1113 |
shutil.rmtree(os.path.join(quiz_name, quiz_name)) |
| 1114 |
|
| 1115 |
if settings.initialise_warning != '' and not settings.have_initialised: |
| 1116 |
print(webquiz_templates.text_initialise_warning) |
| 1117 |
|
| 1118 |
except Exception as err: |
| 1119 |
|
| 1120 |
# there is a small chance that there is an error before the |
| 1121 |
# settings.debugging flag has been set |
| 1122 |
webquiz_util.webquiz_error(settings.debugging if 'settings' in globals() else True, |
| 1123 |
'unknown problem.\n\nIf you think this is a bug please report it by creating an issue at\n {}\n' |
| 1124 |
.format(metadata.repository), err) |