From 4290eb050b623943a98a8078dbaf67e0afcad252 Mon Sep 17 00:00:00 2001 From: stevenknight Date: Sun, 21 Sep 2003 15:58:08 +0000 Subject: [PATCH] New tex.py. git-svn-id: http://scons.tigris.org/svn/scons/trunk@804 fdb21ef1-2011-0410-befe-b5e4ea1792b1 --- doc/man/scons.1 | 40 +++++++++++++++++++++- src/CHANGES.txt | 6 ++++ src/engine/SCons/Tool/tex.py | 66 +++++++++++++++++++++++++++++++++++- test/TEX.py | 22 ++++++++---- 4 files changed, 125 insertions(+), 9 deletions(-) diff --git a/doc/man/scons.1 b/doc/man/scons.1 index bd35b93d..77217a09 100644 --- a/doc/man/scons.1 +++ b/doc/man/scons.1 @@ -1570,10 +1570,35 @@ Will create foo.tlb, foo.h, foo_i.c, foo_p.c, and foo_data.c. .IP DVI Builds a .dvi file from a .tex, .ltx or .latex input file. +If the source file suffix is .tex, +.B scons +will examine the contents of the file; +if the string +.B \\documentclass +or +.B \\documentstyle +is found, the file is assumed to be a LaTeX file and +the target is built by invoking the $LATEXCOM command line; +otherwise, the $TEXCOM command line is used. +If the file is a LaTeX file, +the +.B DVI +builder will also examine the contents +of the +.B .aux file +and invoke the $BIBTEX command line +if the string +.B bibdata +is found, +and will examine the contents +.B .log +file and re-run the $LATEXCOM command +if the log file says it is necessary. + The suffix .dvi (hard-coded within TeX itself) is automatically added to the target -if it is not already present. Example: +if it is not already present. Examples: .ES # builds from aaa.tex @@ -3396,6 +3421,19 @@ after first running the file through the C preprocessor. Any options specified in the $ASFLAGS and $CPPFLAGS construction variables are included on this command line. +.IP BIBTEX +The bibliography generator for the TeX formatter and typesetter and the +LaTeX structured formatter and typesetter. + +.IP BIBTEXCOM +The command line used to call the bibliography generator for the +TeX formatter and typesetter and the LaTeX structured formatter and +typesetter. + +.IP BIBTEXFLAGS +General options passed to the bibliography generator for the TeX formatter +and typesetter and the LaTeX structured formatter and typesetter. + .IP BITKEEPER The BitKeeper executable. diff --git a/src/CHANGES.txt b/src/CHANGES.txt index 7fe5cf1c..884e650c 100644 --- a/src/CHANGES.txt +++ b/src/CHANGES.txt @@ -77,6 +77,12 @@ RELEASE X.XX - XXX subclass of Environment.Base and setting a new Environment.Environment variable as the calling entry point. + From Clark McGrew: + + - Generalize the action for .tex files so that it will decide whether + a file is TeX or LaTeX, check the .aux output to decide if it should + run bibtex, and check the .log output to re-run LaTeX if needed. + From Bram Moolenaar: - Split the non-SCons-specific functionality from SConf.py to a new, diff --git a/src/engine/SCons/Tool/tex.py b/src/engine/SCons/Tool/tex.py index c3d38f15..b9a8dea3 100644 --- a/src/engine/SCons/Tool/tex.py +++ b/src/engine/SCons/Tool/tex.py @@ -33,7 +33,61 @@ selection method. __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__" +import os.path +import re +import string + +import SCons.Action import SCons.Defaults +import SCons.Node +import SCons.Node.FS +import SCons.Util + +# Define an action to build a generic tex file. This is sufficient for all +# tex files. +TeXAction = SCons.Action.CommandAction("$TEXCOM") + +# Define an action to build a latex file. This action might be needed more +# than once if we are dealing with labels and bibtex +LaTeXAction = SCons.Action.CommandAction("$LATEXCOM") + +# Define an action to run BibTeX on a file. +BibTeXAction = SCons.Action.CommandAction("$BIBTEXCOM") + +def LaTeXAuxAction(target = None, source= None, env=None): + """A builder for LaTeX files that checks the output in the aux file + and decides how many times to use LaTeXAction, and BibTeXAction.""" + # Get the base name of the target + basename, ext = os.path.splitext(str(target[0])) + # Run LaTeX once to generate a new aux file. + LaTeXAction(target,source,env) + # Now if bibtex will need to be run. + content = open(basename + ".aux","rb").read() + if string.find(content, "bibdata") != -1: + bibfile = self.fs.File(basename) + BibTeXAction(None,bibfile,env) + # Now check if latex needs to be run yet again. + for trial in range(3): + content = open(basename + ".log","rb").read() + if not re.search("^LaTeX Warning:.*Rerun",content,re.MULTILINE): + break + LaTeXAction(target,source,env) + return 0 + +def TeXLaTeXAction(target = None, source= None, env=None): + """A builder for TeX and LaTeX that scans the source file to + decide the "flavor" of the source and then executes the appropriate + program.""" + LaTeXFile = False + for src in source: + content = src.get_contents() + if re.search("\\\\document(style|class)",content): + LaTeXFile = True + if LaTeXFile: + LaTeXAuxAction(target,source,env) + else: + TeXAction(target,source,env) + return 0 def generate(env): """Add Builders and construction variables for TeX to an Environment.""" @@ -43,11 +97,21 @@ def generate(env): bld = SCons.Defaults.DVI() env['BUILDERS']['DVI'] = bld - bld.add_action('.tex', '$TEXCOM') + bld.add_action('.tex', TeXLaTeXAction) env['TEX'] = 'tex' env['TEXFLAGS'] = '' env['TEXCOM'] = '$TEX $TEXFLAGS $SOURCES' + # Duplicate from latex.py. If latex.py goes away, then this is still OK. + env['LATEX'] = 'latex' + env['LATEXFLAGS'] = '' + env['LATEXCOM'] = '$LATEX $LATEXFLAGS $SOURCES' + + env['BIBTEX'] = 'bibtex' + env['BIBTEXFLAGS'] = '' + env['BIBTEXCOM'] = '$BIBTEX $BIBTEXFLAGS $SOURCES' + + def exists(env): return env.Detect('tex') diff --git a/test/TEX.py b/test/TEX.py index 7a88e7a8..888c209e 100644 --- a/test/TEX.py +++ b/test/TEX.py @@ -81,7 +81,9 @@ foo = Environment(ENV = ENV) tex = foo.Dictionary('TEX') bar = Environment(ENV = ENV, TEX = r'%s wrapper.py ' + tex) foo.DVI(target = 'foo.dvi', source = 'foo.tex') +foo.DVI(target = 'foo-latex.dvi', source = 'foo-latex.tex') bar.DVI(target = 'bar', source = 'bar.tex') +bar.DVI(target = 'bar-latex', source = 'bar-latex.tex') """ % python) tex = r""" @@ -89,20 +91,26 @@ This is the %s TeX file. \end """ - test.write('foo.tex', tex % 'foo.tex') + latex = r""" +\document%s{letter} +\begin{document} +This is the %s LaTeX file. +\end{document} +""" + test.write('foo.tex', tex % 'foo.tex') test.write('bar.tex', tex % 'bar.tex') + test.write('foo-latex.tex', latex % ('style', 'foo-latex.tex')) + test.write('bar-latex.tex', latex % ('class', 'bar-latex.tex')) - test.run(arguments = 'foo.dvi', stderr = None) - + test.run(arguments = 'foo.dvi foo-latex.dvi', stderr = None) test.fail_test(os.path.exists(test.workpath('wrapper.out'))) - test.fail_test(not os.path.exists(test.workpath('foo.dvi'))) + test.fail_test(not os.path.exists(test.workpath('foo-latex.dvi'))) - test.run(arguments = 'bar.dvi', stderr = None) - + test.run(arguments = 'bar.dvi bar-latex.dvi', stderr = None) test.fail_test(not os.path.exists(test.workpath('wrapper.out'))) - test.fail_test(not os.path.exists(test.workpath('bar.dvi'))) + test.fail_test(not os.path.exists(test.workpath('bar-latex.dvi'))) test.pass_test() -- 2.26.2