http://scons.tigris.org/issues/show_bug.cgi?id=2329
[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
30 import TestSCons
31
32 _python_ = TestSCons._python_
33
34 test = TestSCons.TestSCons()
35
36 try:
37     import zipfile
38 except ImportError:
39     zip = test.where_is('zip')
40     if not zip:
41         x = "Python version has no 'ziplib' module nor 'zip' utility; skipping tests.\n"
42         test.skip_test(x)
43
44 test.subdir('sub1')
45
46 test.write('myzip.py', r"""\
47 import os
48 import os.path
49 import sys
50 def process(outfile, name):
51     if os.path.isdir(name):
52         ## TODO 2.5: the next three lines can be replaced by
53         #for entry in sorted(os.listdir(name)):
54         list = os.listdir(name)
55         list.sort()
56         for entry in list:
57             process(outfile, os.path.join(name, entry))
58     else:
59         outfile.write(open(name, 'rb').read())
60 outfile = open(sys.argv[1], 'wb')
61 for infile in sys.argv[2:]:
62     process(outfile, infile)
63 outfile.close()
64 sys.exit(0)
65 """)
66
67 test.write('SConstruct', """
68 env = Environment(tools = ['zip'],
69                   ZIPCOM = r'%(_python_)s myzip.py $TARGET $SOURCES')
70 env.Zip(target = 'aaa.zip', source = ['file1', 'file2'])
71 env.Zip(target = 'aaa.zip', source = 'file3')
72 env.Zip(target = 'bbb', source = 'sub1')
73 env.Zip(target = 'bbb', source = 'file4')
74 """ % locals())
75
76 test.write('file1', "file1\n")
77 test.write('file2', "file2\n")
78 test.write('file3', "file3\n")
79 test.write('file4', "file4\n")
80
81 test.write(['sub1', 'file5'], "sub1/file5\n")
82 test.write(['sub1', 'file6'], "sub1/file6\n")
83
84 test.run(arguments = 'aaa.zip', stderr = None)
85
86 test.fail_test(test.read('aaa.zip') != "file1\nfile2\nfile3\n")
87
88 test.run(arguments = 'bbb.zip', stderr = None)
89
90 test.fail_test(test.read('bbb.zip') != "sub1/file5\nsub1/file6\nfile4\n")
91
92 try:
93     import zipfile
94     internal_zip = 1
95     zip = 1
96
97     def files(fname):
98         zf = zipfile.ZipFile(fname, 'r')
99         return [x.filename for x in zf.infolist()]
100
101 except ImportError:
102     internal_zip = 0
103     zip = test.detect('ZIP', 'zip')
104     unzip = test.where_is('unzip')
105
106     def files(fname, test=test, unzip=unzip):
107         test.run(program = unzip, arguments = "-l -qq %s" % fname)
108         lines = test.stdout().split("\n")[:-1]
109         def lastword(line):
110             return line.split()[-1]
111         return list(map(lastword, lines))
112
113 if zip:
114
115     marker_out = test.workpath('marker.out').replace('\\', '\\\\')
116
117     test.write('SConstruct', """\
118 def marker(target, source, env):
119     open(r'%s', 'wb').write("marker\\n")
120 f1 = Environment()
121 zipcom = f1.Dictionary('ZIPCOM')
122 if not isinstance(zipcom, list):
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: