2012-07-08 17:28:19 +00:00
|
|
|
#! /usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# file src/tex2lyx/test/runtests.py
|
|
|
|
# This file is part of LyX, the document processor.
|
|
|
|
# Licence details can be found in the file COPYING.
|
|
|
|
|
|
|
|
# author Georg Baum
|
|
|
|
|
|
|
|
# Full author contact details are available in file CREDITS
|
|
|
|
|
2012-10-03 11:59:20 +00:00
|
|
|
# This script is a very basic test suite runner for tex2lyx
|
|
|
|
# The defaults for optional command line arguments are tailored to the
|
|
|
|
# standard use case of testing without special build settings like a version
|
|
|
|
# suffix, since I don't know how to transport command line arguments through
|
|
|
|
# the autotools "make check" mechanism.
|
2012-07-08 17:28:19 +00:00
|
|
|
|
2012-12-14 18:07:28 +00:00
|
|
|
import os, string, sys, time, difflib, filecmp, subprocess, re
|
2012-07-08 17:28:19 +00:00
|
|
|
|
|
|
|
def usage(prog_name):
|
2012-12-28 15:22:25 +00:00
|
|
|
return "Usage: %s [uselyx2lyx] [<tex2lyx binary> [[<script dir>] [[<output dir>] [testfile]]]]" % prog_name
|
2012-07-08 17:28:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main(argv):
|
|
|
|
# Parse and manipulate the command line arguments.
|
2012-12-28 15:22:25 +00:00
|
|
|
skipcount = 0
|
|
|
|
uselyx2lyx = False
|
|
|
|
if len(argv) > 1:
|
|
|
|
if argv[1] == "uselyx2lyx":
|
|
|
|
uselyx2lyx = True
|
|
|
|
skipcount = 1
|
|
|
|
if len(argv) >= 3+skipcount:
|
2013-05-23 05:52:49 +00:00
|
|
|
sys.path.append(os.path.join(sys.argv[2+skipcount]))
|
2012-07-30 07:30:16 +00:00
|
|
|
else:
|
2013-05-23 05:52:49 +00:00
|
|
|
sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), '../../../lib/scripts'))
|
2012-07-30 07:30:16 +00:00
|
|
|
|
2012-07-08 17:28:19 +00:00
|
|
|
from lyxpreview_tools import error
|
|
|
|
|
2012-12-28 15:22:25 +00:00
|
|
|
if len(argv) < 2+skipcount:
|
2012-07-08 17:28:19 +00:00
|
|
|
tex2lyx = './tex2lyx'
|
2012-12-28 15:22:25 +00:00
|
|
|
elif len(argv) <= 5+skipcount:
|
|
|
|
tex2lyx = argv[1+skipcount]
|
2012-07-08 17:28:19 +00:00
|
|
|
else:
|
|
|
|
error(usage(argv[0]))
|
|
|
|
|
2013-01-03 12:02:34 +00:00
|
|
|
suffixre = re.search(r'\d+\.\d+$', tex2lyx)
|
|
|
|
if suffixre:
|
2013-05-23 05:52:49 +00:00
|
|
|
suffix = suffixre.group()
|
2013-01-03 12:02:34 +00:00
|
|
|
else:
|
2013-05-23 05:52:49 +00:00
|
|
|
suffix = ""
|
2013-01-03 12:02:34 +00:00
|
|
|
lyx = os.path.join(os.path.dirname(tex2lyx), "lyx" + suffix)
|
2012-10-03 11:59:20 +00:00
|
|
|
inputdir = os.path.dirname(argv[0])
|
2012-12-28 15:22:25 +00:00
|
|
|
if len(argv) >= 4+skipcount:
|
|
|
|
outputdir = sys.argv[3+skipcount]
|
2012-10-03 11:59:20 +00:00
|
|
|
else:
|
2012-10-03 13:23:09 +00:00
|
|
|
# outputdir = inputdir
|
|
|
|
outputdir = os.path.join(os.path.dirname(tex2lyx), "test")
|
2012-07-08 17:28:19 +00:00
|
|
|
|
2012-12-28 15:22:25 +00:00
|
|
|
if len(argv) >= 5+skipcount:
|
|
|
|
files = [sys.argv[4+skipcount]]
|
2012-12-14 18:07:28 +00:00
|
|
|
else:
|
2013-02-20 11:43:36 +00:00
|
|
|
files = ['test.ltx', \
|
|
|
|
'box-color-size-space-align.tex', \
|
|
|
|
'CJK.tex', \
|
|
|
|
'CJKutf8.tex', \
|
|
|
|
'test-insets.tex', \
|
|
|
|
'test-modules.tex', \
|
|
|
|
'test-refstyle-theorems.tex', \
|
|
|
|
'test-structure.tex', \
|
|
|
|
'verbatim.tex', \
|
|
|
|
'XeTeX-polyglossia.tex']
|
2012-07-08 17:28:19 +00:00
|
|
|
|
2012-07-08 18:52:26 +00:00
|
|
|
errors = []
|
2012-10-03 13:01:54 +00:00
|
|
|
overwrite = (outputdir == inputdir)
|
2012-07-08 17:28:19 +00:00
|
|
|
for f in files:
|
2012-10-03 13:01:54 +00:00
|
|
|
(base, ext) = os.path.splitext(f)
|
2012-10-03 11:59:20 +00:00
|
|
|
texfile = os.path.join(inputdir, f)
|
2012-10-03 13:01:54 +00:00
|
|
|
if overwrite:
|
2012-12-15 16:47:09 +00:00
|
|
|
# we are updating the test references, so use roundtrip to allow
|
|
|
|
# for checking the LyX export as well.
|
2012-10-03 11:59:20 +00:00
|
|
|
cmd = '%s -roundtrip -f %s' % (tex2lyx, texfile)
|
|
|
|
else:
|
2012-10-03 13:01:54 +00:00
|
|
|
lyxfile = os.path.join(outputdir, base + ".lyx")
|
2012-12-28 15:22:25 +00:00
|
|
|
cmd = '%s -roundtrip -copyfiles -f %s %s' % (tex2lyx, texfile, lyxfile)
|
2012-12-30 10:30:15 +00:00
|
|
|
proc = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
2012-12-14 18:07:28 +00:00
|
|
|
proc.wait()
|
|
|
|
err = proc.returncode
|
|
|
|
errorstring = proc.stderr.read()
|
|
|
|
if not errorstring is None:
|
|
|
|
print errorstring
|
|
|
|
if err != 0:
|
2012-07-08 18:52:26 +00:00
|
|
|
errors.append(f)
|
2012-10-03 13:01:54 +00:00
|
|
|
elif not overwrite:
|
2012-12-28 15:22:25 +00:00
|
|
|
lyxfile1 = getlyxinput(lyx,
|
|
|
|
os.path.join(inputdir, base + ".lyx.lyx"),
|
|
|
|
os.path.join(outputdir, base + ".lyx1.lyx") , uselyx2lyx)
|
|
|
|
if lyxfile1 is None:
|
2012-10-03 13:01:54 +00:00
|
|
|
errors.append(f)
|
2012-12-28 15:22:25 +00:00
|
|
|
else:
|
|
|
|
lyxfile2 = getlyxinput(lyx,
|
|
|
|
os.path.join(outputdir, base + ".lyx"),
|
|
|
|
os.path.join(outputdir, base + ".lyx2.lyx"), uselyx2lyx)
|
|
|
|
if lyxfile2 is None:
|
|
|
|
errors.append(f)
|
2013-02-16 13:20:04 +00:00
|
|
|
elif not filecmp.cmp(lyxfile1, lyxfile2, False):
|
2012-12-28 15:22:25 +00:00
|
|
|
t1 = time.ctime(os.path.getmtime(lyxfile1))
|
|
|
|
t2 = time.ctime(os.path.getmtime(lyxfile2))
|
|
|
|
f1 = open(lyxfile1, 'r')
|
|
|
|
f2 = open(lyxfile2, 'r')
|
|
|
|
lines1 = f1.readlines()
|
|
|
|
lines2 = f2.readlines()
|
2013-02-16 13:20:04 +00:00
|
|
|
diff = difflib.unified_diff(lines1, lines2, lyxfile1, lyxfile2, t1, t2)
|
2012-12-28 15:22:25 +00:00
|
|
|
f1.close()
|
|
|
|
f2.close()
|
2013-02-16 13:20:04 +00:00
|
|
|
sys.stdout.writelines(diff)
|
|
|
|
errors.append(f)
|
2012-07-08 17:28:19 +00:00
|
|
|
|
2012-07-08 18:52:26 +00:00
|
|
|
if len(errors) > 0:
|
|
|
|
error('Converting the following files failed: %s' % ', '.join(errors))
|
2012-07-08 17:28:19 +00:00
|
|
|
|
2012-12-28 15:22:25 +00:00
|
|
|
def getlyxinput(lyx, lyxfx, lyxf, uselyx2lyx):
|
|
|
|
if uselyx2lyx:
|
|
|
|
cmd = '%s -E lyx %s %s' % (lyx, lyxf, lyxfx)
|
|
|
|
sys.stdout.writelines(cmd)
|
|
|
|
if os.system(cmd) != 0:
|
|
|
|
return None
|
|
|
|
return lyxf
|
|
|
|
else:
|
|
|
|
return lyxfx
|
|
|
|
|
2012-07-08 17:28:19 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main(sys.argv)
|
|
|
|
|