http://scons.tigris.org/issues/show_bug.cgi?id=2329
[scons.git] / test / TAR / TAR.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
29 import TestSCons
30
31 _python_ = TestSCons._python_
32
33 test = TestSCons.TestSCons()
34
35 test.subdir('sub1')
36
37 test.write('mytar.py', r"""
38 import getopt
39 import os
40 import os.path
41 import sys
42 opts, args = getopt.getopt(sys.argv[1:], 'cf:')
43 for opt, arg in opts:
44     if opt == '-f': out = arg
45 def process(outfile, name):
46     if os.path.isdir(name):
47         ## TODO 2.5: the next three lines can be replaced by
48         #for entry in sorted(os.listdir(name)):
49         list = os.listdir(name)
50         list.sort()
51         for entry in list:
52             process(outfile, os.path.join(name, entry))
53     else:
54         outfile.write(open(name, 'rb').read())
55 outfile = open(out, 'wb')
56 for infile in args:
57     process(outfile, infile)
58 outfile.close()
59 sys.exit(0)
60 """)
61
62 test.write('SConstruct', """
63 env = Environment(tools = ['tar'], TAR = r'%(_python_)s mytar.py')
64 env.Tar(target = 'aaa.tar', source = ['file1', 'file2'])
65 env.Tar(target = 'aaa.tar', source = 'file3')
66 env.Tar(target = 'bbb', source = 'sub1')
67 env.Tar(target = 'bbb', source = 'file4')
68 """ % locals())
69
70 test.write('file1', "file1\n")
71 test.write('file2', "file2\n")
72 test.write('file3', "file3\n")
73 test.write('file4', "file4\n")
74
75 test.write(['sub1', 'file5'], "sub1/file5\n")
76 test.write(['sub1', 'file6'], "sub1/file6\n")
77
78 test.run(arguments = 'aaa.tar', stderr = None)
79
80 test.fail_test(test.read('aaa.tar') != "file1\nfile2\nfile3\n")
81
82 test.run(arguments = 'bbb.tar', stderr = None)
83
84 test.fail_test(test.read('bbb.tar') != "sub1/file5\nsub1/file6\nfile4\n")
85
86
87 tar = test.detect('TAR', 'tar')
88
89 if tar:
90
91     test.write("wrapper.py", """import os
92 import sys
93 open('%s', 'wb').write("wrapper.py\\n")
94 os.system(" ".join(sys.argv[1:]))
95 """ % test.workpath('wrapper.out').replace('\\', '\\\\'))
96
97     test.write('SConstruct', """
98 foo = Environment()
99 tar = foo.Dictionary('TAR')
100 bar = Environment(TAR = r'%(_python_)s wrapper.py ' + tar)
101 f3 = Environment(TARFLAGS = '-c -z', TARSUFFIX = '.tar.gz')
102 f4 = Environment(TARFLAGS = '-c -z', TARSUFFIX = '.tgz')
103 f5 = Environment(TARFLAGS = '-c -z')
104 foo.Tar(target = 'foo.tar', source = ['file10', 'file11'])
105 foo.Tar(target = 'foo.tar', source = 'file12')
106 bar.Tar(target = 'bar.tar', source = ['file13', 'file14'])
107 bar.Tar(target = 'bar.tar', source = 'file15')
108 f3.Tar(target = 'f3', source = 'file16')
109 f3.Tar(target = 'f3', source = ['file17', 'file18'])
110 f4.Tar(target = 'f4', source = 'file19')
111 f4.Tar(target = 'f4', source = ['file20', 'file21'])
112 f5.Tar(target = 'f5.tgz', source = 'file22')
113 f5.Tar(target = 'f5.tgz', source = ['file23', 'file24'])
114 """ % locals())
115
116     for f in ['file10', 'file11', 'file12',
117               'file13', 'file14', 'file15',
118               'file16', 'file17', 'file18',
119               'file19', 'file20', 'file21',
120               'file22', 'file23', 'file24']:
121         test.write(f, f + "\n")
122
123     test.run(arguments = 'foo.tar', stderr = None)
124
125     test.fail_test(os.path.exists(test.workpath('wrapper.out')))
126
127     test.fail_test(not os.path.exists(test.workpath('foo.tar')))
128
129     test.run(arguments = 'bar.tar', stderr = None)
130
131     test.fail_test(not os.path.exists(test.workpath('wrapper.out')))
132
133     test.fail_test(not os.path.exists(test.workpath('bar.tar')))
134
135     test.run(arguments = '.', stderr = None)
136
137     test.fail_test(os.path.exists(test.workpath('f3.tar')))
138     test.fail_test(not os.path.exists(test.workpath('f3.tar.gz')))
139
140     test.fail_test(os.path.exists(test.workpath('f4.tar')))
141     test.fail_test(os.path.exists(test.workpath('f4.tar.gz')))
142     test.fail_test(not os.path.exists(test.workpath('f4.tgz')))
143
144     test.fail_test(os.path.exists(test.workpath('f5.tar')))
145     test.fail_test(os.path.exists(test.workpath('f5.tar.gz')))
146     test.fail_test(not os.path.exists(test.workpath('f5.tgz')))
147
148     test.run(program = tar, arguments = "-t -f foo.tar", stderr = None)
149     test.fail_test(test.stdout() != "file10\nfile11\nfile12\n")
150
151     test.run(program = tar, arguments = "-t -f bar.tar", stderr = None)
152     test.fail_test(test.stdout() != "file13\nfile14\nfile15\n")
153
154     test.run(program = tar, arguments = "-t -z -f f3.tar.gz", stderr = None)
155     test.fail_test(test.stdout() != "file16\nfile17\nfile18\n")
156
157     test.run(program = tar, arguments = "-t -z -f f4.tgz", stderr = None)
158     test.fail_test(test.stdout() != "file19\nfile20\nfile21\n")
159
160     test.run(program = tar, arguments = "-t -z -f f5.tgz", stderr = None)
161     test.fail_test(test.stdout() != "file22\nfile23\nfile24\n")
162
163 test.pass_test()
164
165 # Local Variables:
166 # tab-width:4
167 # indent-tabs-mode:nil
168 # End:
169 # vim: set expandtab tabstop=4 shiftwidth=4: