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