1 | n/a | # -*- coding: utf-8 -*- |
---|
2 | n/a | """Tests for distutils.archive_util.""" |
---|
3 | n/a | import unittest |
---|
4 | n/a | import os |
---|
5 | n/a | import sys |
---|
6 | n/a | import tarfile |
---|
7 | n/a | from os.path import splitdrive |
---|
8 | n/a | import warnings |
---|
9 | n/a | |
---|
10 | n/a | from distutils import archive_util |
---|
11 | n/a | from distutils.archive_util import (check_archive_formats, make_tarball, |
---|
12 | n/a | make_zipfile, make_archive, |
---|
13 | n/a | ARCHIVE_FORMATS) |
---|
14 | n/a | from distutils.spawn import find_executable, spawn |
---|
15 | n/a | from distutils.tests import support |
---|
16 | n/a | from test.support import check_warnings, run_unittest, patch, change_cwd |
---|
17 | n/a | |
---|
18 | n/a | try: |
---|
19 | n/a | import grp |
---|
20 | n/a | import pwd |
---|
21 | n/a | UID_GID_SUPPORT = True |
---|
22 | n/a | except ImportError: |
---|
23 | n/a | UID_GID_SUPPORT = False |
---|
24 | n/a | |
---|
25 | n/a | try: |
---|
26 | n/a | import zipfile |
---|
27 | n/a | ZIP_SUPPORT = True |
---|
28 | n/a | except ImportError: |
---|
29 | n/a | ZIP_SUPPORT = find_executable('zip') |
---|
30 | n/a | |
---|
31 | n/a | try: |
---|
32 | n/a | import zlib |
---|
33 | n/a | ZLIB_SUPPORT = True |
---|
34 | n/a | except ImportError: |
---|
35 | n/a | ZLIB_SUPPORT = False |
---|
36 | n/a | |
---|
37 | n/a | try: |
---|
38 | n/a | import bz2 |
---|
39 | n/a | except ImportError: |
---|
40 | n/a | bz2 = None |
---|
41 | n/a | |
---|
42 | n/a | try: |
---|
43 | n/a | import lzma |
---|
44 | n/a | except ImportError: |
---|
45 | n/a | lzma = None |
---|
46 | n/a | |
---|
47 | n/a | def can_fs_encode(filename): |
---|
48 | n/a | """ |
---|
49 | n/a | Return True if the filename can be saved in the file system. |
---|
50 | n/a | """ |
---|
51 | n/a | if os.path.supports_unicode_filenames: |
---|
52 | n/a | return True |
---|
53 | n/a | try: |
---|
54 | n/a | filename.encode(sys.getfilesystemencoding()) |
---|
55 | n/a | except UnicodeEncodeError: |
---|
56 | n/a | return False |
---|
57 | n/a | return True |
---|
58 | n/a | |
---|
59 | n/a | |
---|
60 | n/a | class ArchiveUtilTestCase(support.TempdirManager, |
---|
61 | n/a | support.LoggingSilencer, |
---|
62 | n/a | unittest.TestCase): |
---|
63 | n/a | |
---|
64 | n/a | @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run') |
---|
65 | n/a | def test_make_tarball(self, name='archive'): |
---|
66 | n/a | # creating something to tar |
---|
67 | n/a | tmpdir = self._create_files() |
---|
68 | n/a | self._make_tarball(tmpdir, name, '.tar.gz') |
---|
69 | n/a | # trying an uncompressed one |
---|
70 | n/a | self._make_tarball(tmpdir, name, '.tar', compress=None) |
---|
71 | n/a | |
---|
72 | n/a | @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run') |
---|
73 | n/a | def test_make_tarball_gzip(self): |
---|
74 | n/a | tmpdir = self._create_files() |
---|
75 | n/a | self._make_tarball(tmpdir, 'archive', '.tar.gz', compress='gzip') |
---|
76 | n/a | |
---|
77 | n/a | @unittest.skipUnless(bz2, 'Need bz2 support to run') |
---|
78 | n/a | def test_make_tarball_bzip2(self): |
---|
79 | n/a | tmpdir = self._create_files() |
---|
80 | n/a | self._make_tarball(tmpdir, 'archive', '.tar.bz2', compress='bzip2') |
---|
81 | n/a | |
---|
82 | n/a | @unittest.skipUnless(lzma, 'Need lzma support to run') |
---|
83 | n/a | def test_make_tarball_xz(self): |
---|
84 | n/a | tmpdir = self._create_files() |
---|
85 | n/a | self._make_tarball(tmpdir, 'archive', '.tar.xz', compress='xz') |
---|
86 | n/a | |
---|
87 | n/a | @unittest.skipUnless(can_fs_encode('Ã¥rchiv'), |
---|
88 | n/a | 'File system cannot handle this filename') |
---|
89 | n/a | def test_make_tarball_latin1(self): |
---|
90 | n/a | """ |
---|
91 | n/a | Mirror test_make_tarball, except filename contains latin characters. |
---|
92 | n/a | """ |
---|
93 | n/a | self.test_make_tarball('Ã¥rchiv') # note this isn't a real word |
---|
94 | n/a | |
---|
95 | n/a | @unittest.skipUnless(can_fs_encode('ã®ã¢ã¼ã«ã¤ã'), |
---|
96 | n/a | 'File system cannot handle this filename') |
---|
97 | n/a | def test_make_tarball_extended(self): |
---|
98 | n/a | """ |
---|
99 | n/a | Mirror test_make_tarball, except filename contains extended |
---|
100 | n/a | characters outside the latin charset. |
---|
101 | n/a | """ |
---|
102 | n/a | self.test_make_tarball('ã®ã¢ã¼ã«ã¤ã') # japanese for archive |
---|
103 | n/a | |
---|
104 | n/a | def _make_tarball(self, tmpdir, target_name, suffix, **kwargs): |
---|
105 | n/a | tmpdir2 = self.mkdtemp() |
---|
106 | n/a | unittest.skipUnless(splitdrive(tmpdir)[0] == splitdrive(tmpdir2)[0], |
---|
107 | n/a | "source and target should be on same drive") |
---|
108 | n/a | |
---|
109 | n/a | base_name = os.path.join(tmpdir2, target_name) |
---|
110 | n/a | |
---|
111 | n/a | # working with relative paths to avoid tar warnings |
---|
112 | n/a | with change_cwd(tmpdir): |
---|
113 | n/a | make_tarball(splitdrive(base_name)[1], 'dist', **kwargs) |
---|
114 | n/a | |
---|
115 | n/a | # check if the compressed tarball was created |
---|
116 | n/a | tarball = base_name + suffix |
---|
117 | n/a | self.assertTrue(os.path.exists(tarball)) |
---|
118 | n/a | self.assertEqual(self._tarinfo(tarball), self._created_files) |
---|
119 | n/a | |
---|
120 | n/a | def _tarinfo(self, path): |
---|
121 | n/a | tar = tarfile.open(path) |
---|
122 | n/a | try: |
---|
123 | n/a | names = tar.getnames() |
---|
124 | n/a | names.sort() |
---|
125 | n/a | return tuple(names) |
---|
126 | n/a | finally: |
---|
127 | n/a | tar.close() |
---|
128 | n/a | |
---|
129 | n/a | _created_files = ('dist', 'dist/file1', 'dist/file2', |
---|
130 | n/a | 'dist/sub', 'dist/sub/file3', 'dist/sub2') |
---|
131 | n/a | |
---|
132 | n/a | def _create_files(self): |
---|
133 | n/a | # creating something to tar |
---|
134 | n/a | tmpdir = self.mkdtemp() |
---|
135 | n/a | dist = os.path.join(tmpdir, 'dist') |
---|
136 | n/a | os.mkdir(dist) |
---|
137 | n/a | self.write_file([dist, 'file1'], 'xxx') |
---|
138 | n/a | self.write_file([dist, 'file2'], 'xxx') |
---|
139 | n/a | os.mkdir(os.path.join(dist, 'sub')) |
---|
140 | n/a | self.write_file([dist, 'sub', 'file3'], 'xxx') |
---|
141 | n/a | os.mkdir(os.path.join(dist, 'sub2')) |
---|
142 | n/a | return tmpdir |
---|
143 | n/a | |
---|
144 | n/a | @unittest.skipUnless(find_executable('tar') and find_executable('gzip') |
---|
145 | n/a | and ZLIB_SUPPORT, |
---|
146 | n/a | 'Need the tar, gzip and zlib command to run') |
---|
147 | n/a | def test_tarfile_vs_tar(self): |
---|
148 | n/a | tmpdir = self._create_files() |
---|
149 | n/a | tmpdir2 = self.mkdtemp() |
---|
150 | n/a | base_name = os.path.join(tmpdir2, 'archive') |
---|
151 | n/a | old_dir = os.getcwd() |
---|
152 | n/a | os.chdir(tmpdir) |
---|
153 | n/a | try: |
---|
154 | n/a | make_tarball(base_name, 'dist') |
---|
155 | n/a | finally: |
---|
156 | n/a | os.chdir(old_dir) |
---|
157 | n/a | |
---|
158 | n/a | # check if the compressed tarball was created |
---|
159 | n/a | tarball = base_name + '.tar.gz' |
---|
160 | n/a | self.assertTrue(os.path.exists(tarball)) |
---|
161 | n/a | |
---|
162 | n/a | # now create another tarball using `tar` |
---|
163 | n/a | tarball2 = os.path.join(tmpdir, 'archive2.tar.gz') |
---|
164 | n/a | tar_cmd = ['tar', '-cf', 'archive2.tar', 'dist'] |
---|
165 | n/a | gzip_cmd = ['gzip', '-f9', 'archive2.tar'] |
---|
166 | n/a | old_dir = os.getcwd() |
---|
167 | n/a | os.chdir(tmpdir) |
---|
168 | n/a | try: |
---|
169 | n/a | spawn(tar_cmd) |
---|
170 | n/a | spawn(gzip_cmd) |
---|
171 | n/a | finally: |
---|
172 | n/a | os.chdir(old_dir) |
---|
173 | n/a | |
---|
174 | n/a | self.assertTrue(os.path.exists(tarball2)) |
---|
175 | n/a | # let's compare both tarballs |
---|
176 | n/a | self.assertEqual(self._tarinfo(tarball), self._created_files) |
---|
177 | n/a | self.assertEqual(self._tarinfo(tarball2), self._created_files) |
---|
178 | n/a | |
---|
179 | n/a | # trying an uncompressed one |
---|
180 | n/a | base_name = os.path.join(tmpdir2, 'archive') |
---|
181 | n/a | old_dir = os.getcwd() |
---|
182 | n/a | os.chdir(tmpdir) |
---|
183 | n/a | try: |
---|
184 | n/a | make_tarball(base_name, 'dist', compress=None) |
---|
185 | n/a | finally: |
---|
186 | n/a | os.chdir(old_dir) |
---|
187 | n/a | tarball = base_name + '.tar' |
---|
188 | n/a | self.assertTrue(os.path.exists(tarball)) |
---|
189 | n/a | |
---|
190 | n/a | # now for a dry_run |
---|
191 | n/a | base_name = os.path.join(tmpdir2, 'archive') |
---|
192 | n/a | old_dir = os.getcwd() |
---|
193 | n/a | os.chdir(tmpdir) |
---|
194 | n/a | try: |
---|
195 | n/a | make_tarball(base_name, 'dist', compress=None, dry_run=True) |
---|
196 | n/a | finally: |
---|
197 | n/a | os.chdir(old_dir) |
---|
198 | n/a | tarball = base_name + '.tar' |
---|
199 | n/a | self.assertTrue(os.path.exists(tarball)) |
---|
200 | n/a | |
---|
201 | n/a | @unittest.skipUnless(find_executable('compress'), |
---|
202 | n/a | 'The compress program is required') |
---|
203 | n/a | def test_compress_deprecated(self): |
---|
204 | n/a | tmpdir = self._create_files() |
---|
205 | n/a | base_name = os.path.join(self.mkdtemp(), 'archive') |
---|
206 | n/a | |
---|
207 | n/a | # using compress and testing the PendingDeprecationWarning |
---|
208 | n/a | old_dir = os.getcwd() |
---|
209 | n/a | os.chdir(tmpdir) |
---|
210 | n/a | try: |
---|
211 | n/a | with check_warnings() as w: |
---|
212 | n/a | warnings.simplefilter("always") |
---|
213 | n/a | make_tarball(base_name, 'dist', compress='compress') |
---|
214 | n/a | finally: |
---|
215 | n/a | os.chdir(old_dir) |
---|
216 | n/a | tarball = base_name + '.tar.Z' |
---|
217 | n/a | self.assertTrue(os.path.exists(tarball)) |
---|
218 | n/a | self.assertEqual(len(w.warnings), 1) |
---|
219 | n/a | |
---|
220 | n/a | # same test with dry_run |
---|
221 | n/a | os.remove(tarball) |
---|
222 | n/a | old_dir = os.getcwd() |
---|
223 | n/a | os.chdir(tmpdir) |
---|
224 | n/a | try: |
---|
225 | n/a | with check_warnings() as w: |
---|
226 | n/a | warnings.simplefilter("always") |
---|
227 | n/a | make_tarball(base_name, 'dist', compress='compress', |
---|
228 | n/a | dry_run=True) |
---|
229 | n/a | finally: |
---|
230 | n/a | os.chdir(old_dir) |
---|
231 | n/a | self.assertFalse(os.path.exists(tarball)) |
---|
232 | n/a | self.assertEqual(len(w.warnings), 1) |
---|
233 | n/a | |
---|
234 | n/a | @unittest.skipUnless(ZIP_SUPPORT and ZLIB_SUPPORT, |
---|
235 | n/a | 'Need zip and zlib support to run') |
---|
236 | n/a | def test_make_zipfile(self): |
---|
237 | n/a | # creating something to tar |
---|
238 | n/a | tmpdir = self._create_files() |
---|
239 | n/a | base_name = os.path.join(self.mkdtemp(), 'archive') |
---|
240 | n/a | with change_cwd(tmpdir): |
---|
241 | n/a | make_zipfile(base_name, 'dist') |
---|
242 | n/a | |
---|
243 | n/a | # check if the compressed tarball was created |
---|
244 | n/a | tarball = base_name + '.zip' |
---|
245 | n/a | self.assertTrue(os.path.exists(tarball)) |
---|
246 | n/a | with zipfile.ZipFile(tarball) as zf: |
---|
247 | n/a | self.assertEqual(sorted(zf.namelist()), |
---|
248 | n/a | ['dist/file1', 'dist/file2', 'dist/sub/file3']) |
---|
249 | n/a | |
---|
250 | n/a | @unittest.skipUnless(ZIP_SUPPORT, 'Need zip support to run') |
---|
251 | n/a | def test_make_zipfile_no_zlib(self): |
---|
252 | n/a | patch(self, archive_util.zipfile, 'zlib', None) # force zlib ImportError |
---|
253 | n/a | |
---|
254 | n/a | called = [] |
---|
255 | n/a | zipfile_class = zipfile.ZipFile |
---|
256 | n/a | def fake_zipfile(*a, **kw): |
---|
257 | n/a | if kw.get('compression', None) == zipfile.ZIP_STORED: |
---|
258 | n/a | called.append((a, kw)) |
---|
259 | n/a | return zipfile_class(*a, **kw) |
---|
260 | n/a | |
---|
261 | n/a | patch(self, archive_util.zipfile, 'ZipFile', fake_zipfile) |
---|
262 | n/a | |
---|
263 | n/a | # create something to tar and compress |
---|
264 | n/a | tmpdir = self._create_files() |
---|
265 | n/a | base_name = os.path.join(self.mkdtemp(), 'archive') |
---|
266 | n/a | with change_cwd(tmpdir): |
---|
267 | n/a | make_zipfile(base_name, 'dist') |
---|
268 | n/a | |
---|
269 | n/a | tarball = base_name + '.zip' |
---|
270 | n/a | self.assertEqual(called, |
---|
271 | n/a | [((tarball, "w"), {'compression': zipfile.ZIP_STORED})]) |
---|
272 | n/a | self.assertTrue(os.path.exists(tarball)) |
---|
273 | n/a | with zipfile.ZipFile(tarball) as zf: |
---|
274 | n/a | self.assertEqual(sorted(zf.namelist()), |
---|
275 | n/a | ['dist/file1', 'dist/file2', 'dist/sub/file3']) |
---|
276 | n/a | |
---|
277 | n/a | def test_check_archive_formats(self): |
---|
278 | n/a | self.assertEqual(check_archive_formats(['gztar', 'xxx', 'zip']), |
---|
279 | n/a | 'xxx') |
---|
280 | n/a | self.assertIsNone(check_archive_formats(['gztar', 'bztar', 'xztar', |
---|
281 | n/a | 'ztar', 'tar', 'zip'])) |
---|
282 | n/a | |
---|
283 | n/a | def test_make_archive(self): |
---|
284 | n/a | tmpdir = self.mkdtemp() |
---|
285 | n/a | base_name = os.path.join(tmpdir, 'archive') |
---|
286 | n/a | self.assertRaises(ValueError, make_archive, base_name, 'xxx') |
---|
287 | n/a | |
---|
288 | n/a | def test_make_archive_cwd(self): |
---|
289 | n/a | current_dir = os.getcwd() |
---|
290 | n/a | def _breaks(*args, **kw): |
---|
291 | n/a | raise RuntimeError() |
---|
292 | n/a | ARCHIVE_FORMATS['xxx'] = (_breaks, [], 'xxx file') |
---|
293 | n/a | try: |
---|
294 | n/a | try: |
---|
295 | n/a | make_archive('xxx', 'xxx', root_dir=self.mkdtemp()) |
---|
296 | n/a | except: |
---|
297 | n/a | pass |
---|
298 | n/a | self.assertEqual(os.getcwd(), current_dir) |
---|
299 | n/a | finally: |
---|
300 | n/a | del ARCHIVE_FORMATS['xxx'] |
---|
301 | n/a | |
---|
302 | n/a | def test_make_archive_tar(self): |
---|
303 | n/a | base_dir = self._create_files() |
---|
304 | n/a | base_name = os.path.join(self.mkdtemp() , 'archive') |
---|
305 | n/a | res = make_archive(base_name, 'tar', base_dir, 'dist') |
---|
306 | n/a | self.assertTrue(os.path.exists(res)) |
---|
307 | n/a | self.assertEqual(os.path.basename(res), 'archive.tar') |
---|
308 | n/a | self.assertEqual(self._tarinfo(res), self._created_files) |
---|
309 | n/a | |
---|
310 | n/a | @unittest.skipUnless(ZLIB_SUPPORT, 'Need zlib support to run') |
---|
311 | n/a | def test_make_archive_gztar(self): |
---|
312 | n/a | base_dir = self._create_files() |
---|
313 | n/a | base_name = os.path.join(self.mkdtemp() , 'archive') |
---|
314 | n/a | res = make_archive(base_name, 'gztar', base_dir, 'dist') |
---|
315 | n/a | self.assertTrue(os.path.exists(res)) |
---|
316 | n/a | self.assertEqual(os.path.basename(res), 'archive.tar.gz') |
---|
317 | n/a | self.assertEqual(self._tarinfo(res), self._created_files) |
---|
318 | n/a | |
---|
319 | n/a | @unittest.skipUnless(bz2, 'Need bz2 support to run') |
---|
320 | n/a | def test_make_archive_bztar(self): |
---|
321 | n/a | base_dir = self._create_files() |
---|
322 | n/a | base_name = os.path.join(self.mkdtemp() , 'archive') |
---|
323 | n/a | res = make_archive(base_name, 'bztar', base_dir, 'dist') |
---|
324 | n/a | self.assertTrue(os.path.exists(res)) |
---|
325 | n/a | self.assertEqual(os.path.basename(res), 'archive.tar.bz2') |
---|
326 | n/a | self.assertEqual(self._tarinfo(res), self._created_files) |
---|
327 | n/a | |
---|
328 | n/a | @unittest.skipUnless(lzma, 'Need xz support to run') |
---|
329 | n/a | def test_make_archive_xztar(self): |
---|
330 | n/a | base_dir = self._create_files() |
---|
331 | n/a | base_name = os.path.join(self.mkdtemp() , 'archive') |
---|
332 | n/a | res = make_archive(base_name, 'xztar', base_dir, 'dist') |
---|
333 | n/a | self.assertTrue(os.path.exists(res)) |
---|
334 | n/a | self.assertEqual(os.path.basename(res), 'archive.tar.xz') |
---|
335 | n/a | self.assertEqual(self._tarinfo(res), self._created_files) |
---|
336 | n/a | |
---|
337 | n/a | def test_make_archive_owner_group(self): |
---|
338 | n/a | # testing make_archive with owner and group, with various combinations |
---|
339 | n/a | # this works even if there's not gid/uid support |
---|
340 | n/a | if UID_GID_SUPPORT: |
---|
341 | n/a | group = grp.getgrgid(0)[0] |
---|
342 | n/a | owner = pwd.getpwuid(0)[0] |
---|
343 | n/a | else: |
---|
344 | n/a | group = owner = 'root' |
---|
345 | n/a | |
---|
346 | n/a | base_dir = self._create_files() |
---|
347 | n/a | root_dir = self.mkdtemp() |
---|
348 | n/a | base_name = os.path.join(self.mkdtemp() , 'archive') |
---|
349 | n/a | res = make_archive(base_name, 'zip', root_dir, base_dir, owner=owner, |
---|
350 | n/a | group=group) |
---|
351 | n/a | self.assertTrue(os.path.exists(res)) |
---|
352 | n/a | |
---|
353 | n/a | res = make_archive(base_name, 'zip', root_dir, base_dir) |
---|
354 | n/a | self.assertTrue(os.path.exists(res)) |
---|
355 | n/a | |
---|
356 | n/a | res = make_archive(base_name, 'tar', root_dir, base_dir, |
---|
357 | n/a | owner=owner, group=group) |
---|
358 | n/a | self.assertTrue(os.path.exists(res)) |
---|
359 | n/a | |
---|
360 | n/a | res = make_archive(base_name, 'tar', root_dir, base_dir, |
---|
361 | n/a | owner='kjhkjhkjg', group='oihohoh') |
---|
362 | n/a | self.assertTrue(os.path.exists(res)) |
---|
363 | n/a | |
---|
364 | n/a | @unittest.skipUnless(ZLIB_SUPPORT, "Requires zlib") |
---|
365 | n/a | @unittest.skipUnless(UID_GID_SUPPORT, "Requires grp and pwd support") |
---|
366 | n/a | def test_tarfile_root_owner(self): |
---|
367 | n/a | tmpdir = self._create_files() |
---|
368 | n/a | base_name = os.path.join(self.mkdtemp(), 'archive') |
---|
369 | n/a | old_dir = os.getcwd() |
---|
370 | n/a | os.chdir(tmpdir) |
---|
371 | n/a | group = grp.getgrgid(0)[0] |
---|
372 | n/a | owner = pwd.getpwuid(0)[0] |
---|
373 | n/a | try: |
---|
374 | n/a | archive_name = make_tarball(base_name, 'dist', compress=None, |
---|
375 | n/a | owner=owner, group=group) |
---|
376 | n/a | finally: |
---|
377 | n/a | os.chdir(old_dir) |
---|
378 | n/a | |
---|
379 | n/a | # check if the compressed tarball was created |
---|
380 | n/a | self.assertTrue(os.path.exists(archive_name)) |
---|
381 | n/a | |
---|
382 | n/a | # now checks the rights |
---|
383 | n/a | archive = tarfile.open(archive_name) |
---|
384 | n/a | try: |
---|
385 | n/a | for member in archive.getmembers(): |
---|
386 | n/a | self.assertEqual(member.uid, 0) |
---|
387 | n/a | self.assertEqual(member.gid, 0) |
---|
388 | n/a | finally: |
---|
389 | n/a | archive.close() |
---|
390 | n/a | |
---|
391 | n/a | def test_suite(): |
---|
392 | n/a | return unittest.makeSuite(ArchiveUtilTestCase) |
---|
393 | n/a | |
---|
394 | n/a | if __name__ == "__main__": |
---|
395 | n/a | run_unittest(test_suite()) |
---|