b887038652750c04e529fa1f8bbf6a842dc2ed12
[scons.git] / test / TEX / LATEXFLAGS.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 import os
28 import os.path
29 import string
30 import sys
31 import TestSCons
32
33 _python_ = TestSCons._python_
34
35 test = TestSCons.TestSCons()
36
37
38
39 test.write('mylatex.py', r"""
40 import getopt
41 import os
42 import sys
43 cmd_opts, args = getopt.getopt(sys.argv[1:], 'tx', [])
44 opt_string = ''
45 for opt, arg in cmd_opts:
46     opt_string = opt_string + ' ' + opt
47 base_name = os.path.splitext(args[0])[0]
48 infile = open(args[0], 'rb')
49 out_file = open(base_name+'.dvi', 'wb')
50 out_file.write(opt_string + "\n")
51 for l in infile.readlines():
52     if l[0] != '\\':
53         out_file.write(l)
54 sys.exit(0)
55 """)
56
57 test.write('SConstruct', """
58 env = Environment(LATEX = r'%(_python_)s mylatex.py',
59                   LATEXFLAGS = '-x',
60                   tools=['latex'])
61 env.DVI(target = 'test1.dvi', source = 'test1.ltx')
62 env.Copy(LATEXFLAGS = '-t').DVI(target = 'test2.dvi', source = 'test2.latex')
63 """ % locals())
64
65 test.write('test1.ltx', r"""This is a .ltx test.
66 \end
67 """)
68
69 test.write('test2.latex', r"""This is a .latex test.
70 \end
71 """)
72
73 test.run(arguments = '.', stderr = None)
74
75 test.fail_test(test.read('test1.dvi') != " -x\nThis is a .ltx test.\n")
76
77 test.fail_test(test.read('test2.dvi') != " -t\nThis is a .latex test.\n")
78
79
80
81 latex = test.where_is('latex')
82
83 if latex:
84
85     test.write("wrapper.py", """import os
86 import string
87 import sys
88 open('%s', 'wb').write("wrapper.py\\n")
89 os.system(string.join(sys.argv[1:], " "))
90 """ % string.replace(test.workpath('wrapper.out'), '\\', '\\\\'))
91
92     test.write('SConstruct', """
93 import os
94 ENV = { 'PATH' : os.environ['PATH'] }
95 foo = Environment(ENV = ENV, LATEXFLAGS = '--output-comment Commentary')
96 latex = foo.Dictionary('LATEX')
97 bar = Environment(ENV = ENV, LATEX = r'%(_python_)s wrapper.py ' + latex)
98 foo.DVI(target = 'foo.dvi', source = 'foo.ltx')
99 bar.DVI(target = 'bar', source = 'bar.latex')
100 """ % locals())
101
102     latex = r"""
103 \documentclass{letter}
104 \begin{document}
105 This is the %s LaTeX file.
106 \end{document}
107 """
108
109     test.write('foo.ltx', latex % 'foo.ltx')
110
111     test.write('bar.latex', latex % 'bar.latex')
112
113     test.run(arguments = 'foo.dvi', stderr = None)
114
115     test.fail_test(os.path.exists(test.workpath('wrapper.out')))
116
117     test.fail_test(not os.path.exists(test.workpath('foo.dvi')))
118
119     test.run(arguments = 'bar.dvi', stderr = None)
120
121     test.fail_test(test.read('wrapper.out') != "wrapper.py\n")
122
123     test.fail_test(not os.path.exists(test.workpath('bar.dvi')))
124
125 test.pass_test()