Remove (lots) more unnecessary imports.
[scons.git] / test / TEX / TEX.py
1 #!/usr/bin/env python
2 #
3 # __COPYRIGHT__
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 #
24
25 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
26
27 """
28 Validate that we can set the TEX string to our own utility, that
29 the produced .dvi, .aux and .log files get removed by the -c option,
30 and that we can use this to wrap calls to the real latex utility.
31 """
32
33 import os
34 import string
35 import sys
36 import TestSCons
37
38 _python_ = TestSCons._python_
39
40 test = TestSCons.TestSCons()
41
42
43
44 test.write('mytex.py', r"""
45 import sys
46 import os
47 import getopt
48 cmd_opts, arg = getopt.getopt(sys.argv[1:], 'i:', [])
49 base_name = os.path.splitext(arg[0])[0]
50 infile = open(arg[0], 'rb')
51 dvi_file = open(base_name+'.dvi', 'wb')
52 aux_file = open(base_name+'.aux', 'wb')
53 log_file = open(base_name+'.log', 'wb')
54 for l in infile.readlines():
55     if l[0] != '\\':
56         dvi_file.write(l)
57         aux_file.write(l)
58         log_file.write(l)
59 sys.exit(0)
60 """)
61
62 test.write('SConstruct', """
63 env = Environment(TEX = r'%(_python_)s mytex.py', tools=['tex'])
64 env.DVI(target = 'test.dvi', source = 'test.tex')
65 """ % locals())
66
67 test.write('test.tex', r"""This is a test.
68 \end
69 """)
70
71 test.run(arguments = 'test.dvi')
72
73 test.must_match('test.dvi', "This is a test.\n")
74 test.must_match('test.aux', "This is a test.\n")
75 test.must_match('test.log', "This is a test.\n")
76
77 test.run(arguments = '-c test.dvi')
78
79 test.must_not_exist('test.dvi')
80 test.must_not_exist('test.aux')
81 test.must_not_exist('test.log')
82
83
84
85 tex = test.where_is('tex')
86
87 if tex:
88
89     test.write("wrapper.py", """import os
90 import string
91 import sys
92 open('%s', 'wb').write("wrapper.py\\n")
93 os.system(string.join(sys.argv[1:], " "))
94 """ % string.replace(test.workpath('wrapper.out'), '\\', '\\\\'))
95
96     test.write('SConstruct', """
97 import os
98 ENV = { 'PATH' : os.environ['PATH'] }
99 foo = Environment(ENV = ENV)
100 tex = foo.Dictionary('TEX')
101 bar = Environment(ENV = ENV, TEX = r'%(_python_)s wrapper.py ' + tex)
102 foo.DVI(target = 'foo.dvi', source = 'foo.tex')
103 foo.DVI(target = 'foo-latex.dvi', source = 'foo-latex.tex')
104 bar.DVI(target = 'bar', source = 'bar.tex')
105 bar.DVI(target = 'bar-latex', source = 'bar-latex.tex')
106 foo.DVI('rerun.tex')
107 foo.DVI('bibtex-test.tex')
108 """ % locals())
109
110     tex = r"""
111 This is the %s TeX file.
112 \end
113 """
114
115     latex = r"""
116 \document%s{letter}
117 \begin{document}
118 This is the %s LaTeX file.
119 \end{document}
120 """
121
122     rerun = r"""
123 \documentclass{article}
124
125 \begin{document}
126
127 \LaTeX\ will need to run twice on this document to get a reference to section
128 \ref{sec}.
129
130 \section{Next section}
131 \label{sec}
132
133 \end{document}
134 """
135
136     bibtex = r"""
137 \documentclass{article}
138
139 \begin{document}
140
141 Run \texttt{latex}, then \texttt{bibtex}, then \texttt{latex} twice again \cite{lamport}.
142
143 \bibliographystyle{plain}
144 \bibliography{test}
145
146 \end{document}
147 """
148
149     bib = r"""
150 @Book{lamport,
151   author =      {L. Lamport},
152   title =       {{\LaTeX: A} Document Preparation System},
153   publisher =   {Addison-Wesley},
154   year =        1994
155 }
156 """
157
158     test.write('foo.tex', tex % 'foo.tex')
159     test.write('bar.tex', tex % 'bar.tex')
160     test.write('foo-latex.tex', latex % ('style', 'foo-latex.tex'))
161     test.write('bar-latex.tex', latex % ('class', 'bar-latex.tex'))
162     test.write('rerun.tex', rerun)
163     test.write('bibtex-test.tex', bibtex)
164     test.write('test.bib', bib)
165
166     test.run(arguments = 'foo.dvi foo-latex.dvi', stderr = None)
167     test.must_not_exist('wrapper.out')
168     test.must_exist('foo.dvi')
169     test.must_exist('foo-latex.dvi')
170
171     test.run(arguments = 'bar.dvi bar-latex.dvi', stderr = None)
172     test.must_exist('wrapper.out')
173     test.must_exist('bar.dvi')
174     test.must_exist('bar-latex.dvi')
175
176     test.run(stderr = None)
177     output_lines = string.split(test.stdout(), '\n')
178
179     reruns = filter(lambda x: string.find(x, 'latex -interaction=nonstopmode rerun.tex') != -1, output_lines)
180     if len(reruns) != 2:
181         print "Expected 2 latex calls, got %s:" % len(reruns)
182         print string.join(reruns, '\n')
183         test.fail_test()
184
185     bibtex = filter(lambda x: string.find(x, 'bibtex bibtex-test') != -1, output_lines)
186     if len(bibtex) != 1:
187         print "Expected 1 bibtex call, got %s:" % len(bibtex)
188         print string.join(bibtex, '\n')
189         test.fail_test()
190
191 test.pass_test()