Merged revisions 1582-1665 via svnmerge from
[scons.git] / test / DVIPS / DVIPS.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('mytex.py', r"""
40 import os
41 import sys
42 base_name = os.path.splitext(sys.argv[1])[0]
43 infile = open(sys.argv[1], 'rb')
44 out_file = open(base_name+'.dvi', 'wb')
45 for l in infile.readlines():
46     if l[:4] != '#tex':
47         out_file.write(l)
48 sys.exit(0)
49 """)
50
51 test.write('mylatex.py', r"""
52 import os
53 import sys
54 base_name = os.path.splitext(sys.argv[1])[0]
55 infile = open(sys.argv[1], 'rb')
56 out_file = open(base_name+'.dvi', 'wb')
57 for l in infile.readlines():
58     if l[:6] != '#latex':
59         out_file.write(l)
60 sys.exit(0)
61 """)
62
63 test.write('mydvips.py', r"""
64 import os
65 import sys
66 infile = open(sys.argv[3], 'rb')
67 out_file = open(sys.argv[2], 'wb')
68 for l in infile.readlines():
69     if l[:6] != '#dvips':
70         out_file.write(l)
71 sys.exit(0)
72 """)
73
74 test.write('SConstruct', """
75 env = Environment(TEX = r'%(_python_)s mytex.py',
76                   LATEX = r'%(_python_)s mylatex.py',
77                   DVIPS = r'%(_python_)s mydvips.py',
78                   tools=['tex', 'latex', 'dvips'])
79 dvi = env.DVI(target = 'test1.dvi', source = 'test1.tex')
80 env.PostScript(target = 'test1.ps', source = dvi)
81 env.PostScript(target = 'test2.ps', source = 'test2.tex')
82 env.PostScript(target = 'test3.ps', source = 'test3.ltx')
83 env.PostScript(target = 'test4.ps', source = 'test4.latex')
84 """ % locals())
85
86 test.write('test1.tex', r"""This is a .dvi test.
87 #tex
88 #dvips
89 """)
90
91 test.write('test2.tex', r"""This is a .tex test.
92 #tex
93 #dvips
94 """)
95
96 test.write('test3.ltx', r"""This is a .ltx test.
97 #latex
98 #dvips
99 """)
100
101 test.write('test4.latex', r"""This is a .latex test.
102 #latex
103 #dvips
104 """)
105
106 test.run(arguments = '.', stderr = None)
107
108 test.fail_test(test.read('test1.ps') != "This is a .dvi test.\n")
109
110 test.fail_test(test.read('test2.ps') != "This is a .tex test.\n")
111
112 test.fail_test(test.read('test3.ps') != "This is a .ltx test.\n")
113
114 test.fail_test(test.read('test4.ps') != "This is a .latex test.\n")
115
116
117
118 dvips = test.where_is('dvips')
119
120 if dvips:
121
122     test.write("wrapper.py", """import os
123 import string
124 import sys
125 cmd = string.join(sys.argv[1:], " ")
126 open('%s', 'ab').write("%%s\\n" %% cmd)
127 os.system(cmd)
128 """ % string.replace(test.workpath('wrapper.out'), '\\', '\\\\'))
129
130     test.write('SConstruct', """
131 import os
132 ENV = { 'PATH' : os.environ['PATH'] }
133 foo = Environment(ENV = ENV)
134 dvips = foo.Dictionary('DVIPS')
135 bar = Environment(ENV = ENV, DVIPS = r'%(_python_)s wrapper.py ' + dvips)
136 foo.PostScript(target = 'foo.ps', source = 'foo.tex')
137 bar.PostScript(target = 'bar1', source = 'bar1.tex')
138 bar.PostScript(target = 'bar2', source = 'bar2.ltx')
139 bar.PostScript(target = 'bar3', source = 'bar3.latex')
140 """ % locals())
141
142     tex = r"""
143 This is the %s TeX file.
144 \end
145 """
146
147     latex = r"""
148 \documentclass{letter}
149 \begin{document}
150 This is the %s LaTeX file.
151 \end{document}
152 """
153
154     test.write('foo.tex', tex % 'foo.tex')
155     test.write('bar1.tex', tex % 'bar1.tex')
156     test.write('bar2.ltx', latex % 'bar2.ltx')
157     test.write('bar3.latex', latex % 'bar3.latex')
158
159     test.run(arguments = 'foo.dvi', stderr = None)
160
161     test.fail_test(os.path.exists(test.workpath('wrapper.out')))
162
163     test.fail_test(not os.path.exists(test.workpath('foo.dvi')))
164
165     test.run(arguments = 'bar1.ps bar2.ps bar3.ps', stderr = None)
166
167     expect = """dvips -o bar1.ps bar1.dvi
168 dvips -o bar2.ps bar2.dvi
169 dvips -o bar3.ps bar3.dvi
170 """
171
172     test.fail_test(test.read('wrapper.out') != expect)
173
174     test.fail_test(not os.path.exists(test.workpath('bar1.ps')))
175     test.fail_test(not os.path.exists(test.workpath('bar2.ps')))
176     test.fail_test(not os.path.exists(test.workpath('bar3.ps')))
177
178 test.pass_test()