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