017c389128e2e0ef287381b00937f9bdc1a9eac1
[scons.git] / test / TARFLAGS.py
1 #!/usr/bin/env python
2 #
3 # Copyright (c) 2001, 2002, 2003 Steven Knight
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 test.subdir('sub1')
38
39 test.write('mytar.py', """
40 import getopt
41 import os
42 import os.path
43 import string
44 import sys
45 cmd_opts, args = getopt.getopt(sys.argv[1:], 'cf:x', [])
46 opt_string = ''
47 for opt, arg in cmd_opts:
48     if opt == '-f': out = arg
49     else: opt_string = opt_string + ' ' + opt
50 def process(outfile, name):
51     if os.path.isdir(name):
52         for entry in os.listdir(name):
53             process(outfile, os.path.join(name, entry))
54     else:
55         outfile.write(open(name, 'rb').read())
56 outfile = open(out, 'wb')
57 outfile.write('options: %s\\n' % opt_string)
58 for infile in args:
59     process(outfile, infile)
60 outfile.close()
61 sys.exit(0)
62 """)
63
64 test.write('SConstruct', """
65 env = Environment(tools = ['tar'], TAR = r'%s mytar.py', TARFLAGS = '-x')
66 env.Tar(target = 'aaa.tar', source = ['file1', 'file2'])
67 env.Tar(target = 'aaa.tar', source = 'file3')
68 env.Tar(target = 'bbb', source = 'sub1')
69 env.Tar(target = 'bbb', source = 'file4')
70 """ % python)
71
72 test.write('file1', "file1\n")
73 test.write('file2', "file2\n")
74 test.write('file3', "file3\n")
75 test.write('file4', "file4\n")
76
77 test.write(['sub1', 'file5'], "sub1/file5\n")
78 test.write(['sub1', 'file6'], "sub1/file6\n")
79
80 test.run(arguments = 'aaa.tar', stderr = None)
81
82 test.fail_test(test.read('aaa.tar') != "options:  -x\nfile1\nfile2\nfile3\n")
83
84 test.run(arguments = 'bbb.tar', stderr = None)
85
86 test.fail_test(test.read('bbb.tar') != "options:  -x\nsub1/file5\nsub1/file6\nfile4\n")
87
88
89
90 tar = test.detect('TAR', 'tar')
91
92 if tar:
93
94     test.write("wrapper.py", """import os
95 import string
96 import sys
97 open('%s', 'wb').write("wrapper.py\\n")
98 os.system(string.join(sys.argv[1:], " "))
99 """ % string.replace(test.workpath('wrapper.out'), '\\', '\\\\'))
100
101     test.write('SConstruct', """
102 foo = Environment()
103 tar = foo['TAR']
104 bar = Environment(TAR = '',
105                   TARFLAGS = r'%s wrapper.py ' + tar + ' -c -b 1')
106 foo.Tar(target = 'foo.tar', source = ['file10', 'file11'])
107 foo.Tar(target = 'foo.tar', source = 'file12')
108 bar.Tar(target = 'bar.tar', source = ['file13', 'file14'])
109 bar.Tar(target = 'bar.tar', source = 'file15')
110 """ % python)
111
112     test.write('file10', "file10\n")
113     test.write('file11', "file11\n")
114     test.write('file12', "file12\n")
115     test.write('file13', "file13\n")
116     test.write('file14', "file14\n")
117     test.write('file15', "file15\n")
118
119     test.run(arguments = 'foo.tar', stderr = None)
120
121     test.fail_test(os.path.exists(test.workpath('wrapper.out')))
122
123     test.fail_test(not os.path.exists(test.workpath('foo.tar')))
124
125     test.run(arguments = 'bar.tar', stderr = None)
126
127     test.fail_test(not os.path.exists(test.workpath('wrapper.out')))
128
129     test.fail_test(not os.path.exists(test.workpath('bar.tar')))
130
131     test.run(program = tar, arguments = "-t -f foo.tar", stderr = None)
132     test.fail_test(test.stdout() != "file10\nfile11\nfile12\n")
133
134     test.run(program = tar, arguments = "-t -f bar.tar", stderr = None)
135     test.fail_test(test.stdout() != "file13\nfile14\nfile15\n")
136
137 test.pass_test()