Remove more unnecessary imports from test scripts.
[scons.git] / test / ZIP / ZIP.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 stat
29 import string
30
31 import TestSCons
32
33 _python_ = TestSCons._python_
34
35 test = TestSCons.TestSCons()
36
37 try:
38     import zipfile
39 except ImportError:
40     zip = test.where_is('zip')
41     if not zip:
42         x = "Python version has no 'ziplib' module nor 'zip' utility; skipping tests.\n"
43         test.skip_test(x)
44
45 test.subdir('sub1')
46
47 test.write('myzip.py', r"""\
48 import os
49 import os.path
50 import sys
51 def process(outfile, name):
52     if os.path.isdir(name):
53         list = os.listdir(name)
54         list.sort()
55         for entry in list:
56             process(outfile, os.path.join(name, entry))
57     else:
58         outfile.write(open(name, 'rb').read())
59 outfile = open(sys.argv[1], 'wb')
60 for infile in sys.argv[2:]:
61     process(outfile, infile)
62 outfile.close()
63 sys.exit(0)
64 """)
65
66 test.write('SConstruct', """
67 env = Environment(tools = ['zip'],
68                   ZIPCOM = r'%(_python_)s myzip.py $TARGET $SOURCES')
69 env.Zip(target = 'aaa.zip', source = ['file1', 'file2'])
70 env.Zip(target = 'aaa.zip', source = 'file3')
71 env.Zip(target = 'bbb', source = 'sub1')
72 env.Zip(target = 'bbb', source = 'file4')
73 """ % locals())
74
75 test.write('file1', "file1\n")
76 test.write('file2', "file2\n")
77 test.write('file3', "file3\n")
78 test.write('file4', "file4\n")
79
80 test.write(['sub1', 'file5'], "sub1/file5\n")
81 test.write(['sub1', 'file6'], "sub1/file6\n")
82
83 test.run(arguments = 'aaa.zip', stderr = None)
84
85 test.fail_test(test.read('aaa.zip') != "file1\nfile2\nfile3\n")
86
87 test.run(arguments = 'bbb.zip', stderr = None)
88
89 test.fail_test(test.read('bbb.zip') != "sub1/file5\nsub1/file6\nfile4\n")
90
91 try:
92     import zipfile
93     internal_zip = 1
94     zip = 1
95
96     def files(fname):
97         zf = zipfile.ZipFile(fname, 'r')
98         return map(lambda x: x.filename, zf.infolist())
99
100 except ImportError:
101     internal_zip = 0
102     zip = test.detect('ZIP', 'zip')
103     unzip = test.where_is('unzip')
104
105     def files(fname, test=test, unzip=unzip):
106         test.run(program = unzip, arguments = "-l -qq %s" % fname)
107         lines = string.split(test.stdout(), "\n")[:-1]
108         def lastword(line):
109             return string.split(line)[-1]
110         return map(lastword, lines)
111
112 if zip:
113
114     marker_out = string.replace(test.workpath('marker.out'), '\\', '\\\\')
115
116     test.write('SConstruct', """\
117 def marker(target, source, env):
118     open(r'%s', 'wb').write("marker\\n")
119 import types
120 f1 = Environment()
121 zipcom = f1.Dictionary('ZIPCOM')
122 if not type(zipcom) is types.ListType:
123     zipcom = [zipcom]
124 f2 = Environment(ZIPCOM = [Action(marker)] + zipcom)
125 f3 = Environment(ZIPSUFFIX = '.xyzzy')
126 f1.Zip(target = 'f1.zip', source = ['file10', 'file11'])
127 f1.Zip(target = 'f1.zip', source = 'file12')
128 f2.Zip(target = 'f2.zip', source = ['file13', 'file14'])
129 f2.Zip(target = 'f2.zip', source = 'file15')
130 f3.Zip(target = 'f3', source = 'file16')
131 f3.Zip(target = 'f3', source = ['file17', 'file18'])
132 try:
133     import zipfile
134     sources = ['file10', 'file11', 'file12', 'file13', 'file14', 'file15']
135     f1.Zip(target = 'f4.zip', source = sources)
136     f1.Zip(target = 'f4stored.zip', source = sources,
137            ZIPCOMPRESSION = zipfile.ZIP_STORED)
138     f1.Zip(target = 'f4deflated.zip', source = sources,
139            ZIPCOMPRESSION = zipfile.ZIP_DEFLATED)
140 except ImportError:
141     pass
142 """ % marker_out)
143
144     for f in ['file10', 'file11', 'file12',
145               'file13', 'file14', 'file15',
146               'file16', 'file17', 'file18']:
147         test.write(f, f + "\n")
148
149     test.run(arguments = 'f1.zip', stderr = None)
150
151     test.fail_test(os.path.exists(test.workpath('marker.out')))
152
153     test.fail_test(not os.path.exists(test.workpath('f1.zip')))
154
155     test.run(arguments = 'f2.zip', stderr = None)
156
157     test.fail_test(test.read('marker.out') != 'marker\n')
158
159     test.fail_test(not os.path.exists(test.workpath('f2.zip')))
160
161     test.run(arguments = '.', stderr = None)
162
163     test.fail_test(os.path.exists(test.workpath('f3.zip')))
164     test.fail_test(not os.path.exists(test.workpath('f3.xyzzy')))
165
166     test.fail_test(files("f1.zip") != ['file10', 'file11', 'file12'])
167
168     test.fail_test(files("f2.zip") != ['file13', 'file14', 'file15'])
169
170     test.fail_test(files("f3.xyzzy") != ['file16', 'file17', 'file18'])
171
172     if internal_zip:
173         f4_size = os.stat('f4.zip')[stat.ST_SIZE]
174         f4stored_size = os.stat('f4stored.zip')[stat.ST_SIZE]
175         f4deflated_size = os.stat('f4deflated.zip')[stat.ST_SIZE]
176
177         test.fail_test(f4_size != f4deflated_size)
178         test.fail_test(f4stored_size == f4deflated_size)
179
180 test.pass_test()
181
182 # Local Variables:
183 # tab-width:4
184 # indent-tabs-mode:nil
185 # End:
186 # vim: set expandtab tabstop=4 shiftwidth=4: