blob: f6b047e44b627e5bfa170ecba00be6e7bfd17f49 [file] [log] [blame]
Barry Warsaw7fc2cca2003-01-24 17:34:13 +00001# Copyright (C) 2003 Python Software Foundation
2
3import unittest
4import shutil
5import tempfile
Johannes Gijsbers8e6f2de2004-11-23 09:27:27 +00006import sys
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +00007import stat
Brett Cannon1c3fa182004-06-19 21:11:35 +00008import os
9import os.path
Benjamin Petersonee8712c2008-05-20 21:35:26 +000010from test import support
11from test.support import TESTFN
Tarek Ziadé396fad72010-02-23 05:30:31 +000012from os.path import splitdrive
13from distutils.spawn import find_executable, spawn
14from shutil import (_make_tarball, _make_zipfile, make_archive,
15 register_archive_format, unregister_archive_format,
Tarek Ziadéfb437512010-04-20 08:57:33 +000016 get_archive_formats, Error)
Tarek Ziadé396fad72010-02-23 05:30:31 +000017import tarfile
18import warnings
19
20from test import support
21from test.support import TESTFN, check_warnings, captured_stdout
22
Antoine Pitrou7fff0962009-05-01 21:09:44 +000023TESTFN2 = TESTFN + "2"
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000024
Tarek Ziadé396fad72010-02-23 05:30:31 +000025try:
26 import grp
27 import pwd
28 UID_GID_SUPPORT = True
29except ImportError:
30 UID_GID_SUPPORT = False
31
32try:
33 import zlib
34except ImportError:
35 zlib = None
36
37try:
38 import zipfile
39 ZIP_SUPPORT = True
40except ImportError:
41 ZIP_SUPPORT = find_executable('zip')
42
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000043class TestShutil(unittest.TestCase):
Tarek Ziadé396fad72010-02-23 05:30:31 +000044
45 def setUp(self):
46 super(TestShutil, self).setUp()
47 self.tempdirs = []
48
49 def tearDown(self):
50 super(TestShutil, self).tearDown()
51 while self.tempdirs:
52 d = self.tempdirs.pop()
53 shutil.rmtree(d, os.name in ('nt', 'cygwin'))
54
55 def write_file(self, path, content='xxx'):
56 """Writes a file in the given path.
57
58
59 path can be a string or a sequence.
60 """
61 if isinstance(path, (list, tuple)):
62 path = os.path.join(*path)
63 f = open(path, 'w')
64 try:
65 f.write(content)
66 finally:
67 f.close()
68
69 def mkdtemp(self):
70 """Create a temporary directory that will be cleaned up.
71
72 Returns the path of the directory.
73 """
74 d = tempfile.mkdtemp()
75 self.tempdirs.append(d)
76 return d
Tarek Ziadé5340db32010-04-19 22:30:51 +000077
Barry Warsaw7fc2cca2003-01-24 17:34:13 +000078 def test_rmtree_errors(self):
79 # filename is guaranteed not to exist
80 filename = tempfile.mktemp()
81 self.assertRaises(OSError, shutil.rmtree, filename)
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000082
Johannes Gijsbersb8b09d02004-12-06 20:50:15 +000083 # See bug #1071513 for why we don't run this on cygwin
84 # and bug #1076467 for why we don't run this as root.
85 if (hasattr(os, 'chmod') and sys.platform[:6] != 'cygwin'
Johannes Gijsbers6b220b02004-12-12 15:52:57 +000086 and not (hasattr(os, 'geteuid') and os.geteuid() == 0)):
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000087 def test_on_error(self):
88 self.errorState = 0
89 os.mkdir(TESTFN)
Tim Peters4590c002004-11-01 02:40:52 +000090 self.childpath = os.path.join(TESTFN, 'a')
91 f = open(self.childpath, 'w')
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000092 f.close()
Tim Peters4590c002004-11-01 02:40:52 +000093 old_dir_mode = os.stat(TESTFN).st_mode
94 old_child_mode = os.stat(self.childpath).st_mode
95 # Make unwritable.
96 os.chmod(self.childpath, stat.S_IREAD)
97 os.chmod(TESTFN, stat.S_IREAD)
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +000098
99 shutil.rmtree(TESTFN, onerror=self.check_args_to_onerror)
Johannes Gijsbers8e6f2de2004-11-23 09:27:27 +0000100 # Test whether onerror has actually been called.
Johannes Gijsbersb8b09d02004-12-06 20:50:15 +0000101 self.assertEqual(self.errorState, 2,
102 "Expected call to onerror function did not happen.")
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +0000103
Tim Peters4590c002004-11-01 02:40:52 +0000104 # Make writable again.
105 os.chmod(TESTFN, old_dir_mode)
106 os.chmod(self.childpath, old_child_mode)
107
108 # Clean up.
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +0000109 shutil.rmtree(TESTFN)
110
111 def check_args_to_onerror(self, func, arg, exc):
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000112 # test_rmtree_errors deliberately runs rmtree
113 # on a directory that is chmod 400, which will fail.
114 # This function is run when shutil.rmtree fails.
115 # 99.9% of the time it initially fails to remove
116 # a file in the directory, so the first time through
117 # func is os.remove.
118 # However, some Linux machines running ZFS on
119 # FUSE experienced a failure earlier in the process
120 # at os.listdir. The first failure may legally
121 # be either.
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +0000122 if self.errorState == 0:
Benjamin Peterson25c95f12009-05-08 20:42:26 +0000123 if func is os.remove:
124 self.assertEqual(arg, self.childpath)
125 else:
126 self.assertIs(func, os.listdir,
127 "func must be either os.remove or os.listdir")
128 self.assertEqual(arg, TESTFN)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000129 self.assertTrue(issubclass(exc[0], OSError))
Johannes Gijsbersef5ffc42004-10-31 12:05:31 +0000130 self.errorState = 1
131 else:
132 self.assertEqual(func, os.rmdir)
133 self.assertEqual(arg, TESTFN)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000134 self.assertTrue(issubclass(exc[0], OSError))
Johannes Gijsbers8e6f2de2004-11-23 09:27:27 +0000135 self.errorState = 2
Barry Warsaw7fc2cca2003-01-24 17:34:13 +0000136
Johannes Gijsbersd60e92a2004-09-11 21:26:21 +0000137 def test_rmtree_dont_delete_file(self):
138 # When called on a file instead of a directory, don't delete it.
139 handle, path = tempfile.mkstemp()
140 os.fdopen(handle).close()
141 self.assertRaises(OSError, shutil.rmtree, path)
142 os.remove(path)
143
Tarek Ziadé5340db32010-04-19 22:30:51 +0000144 def _write_data(self, path, data):
145 f = open(path, "w")
146 f.write(data)
147 f.close()
148
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000149 def test_copytree_simple(self):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000150
151 def read_data(path):
152 f = open(path)
153 data = f.read()
154 f.close()
155 return data
156
157 src_dir = tempfile.mkdtemp()
158 dst_dir = os.path.join(tempfile.mkdtemp(), 'destination')
Tarek Ziadé5340db32010-04-19 22:30:51 +0000159 self._write_data(os.path.join(src_dir, 'test.txt'), '123')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000160 os.mkdir(os.path.join(src_dir, 'test_dir'))
Tarek Ziadé5340db32010-04-19 22:30:51 +0000161 self._write_data(os.path.join(src_dir, 'test_dir', 'test.txt'), '456')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000162
163 try:
164 shutil.copytree(src_dir, dst_dir)
165 self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'test.txt')))
166 self.assertTrue(os.path.isdir(os.path.join(dst_dir, 'test_dir')))
167 self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'test_dir',
168 'test.txt')))
169 actual = read_data(os.path.join(dst_dir, 'test.txt'))
170 self.assertEqual(actual, '123')
171 actual = read_data(os.path.join(dst_dir, 'test_dir', 'test.txt'))
172 self.assertEqual(actual, '456')
173 finally:
174 for path in (
175 os.path.join(src_dir, 'test.txt'),
176 os.path.join(dst_dir, 'test.txt'),
177 os.path.join(src_dir, 'test_dir', 'test.txt'),
178 os.path.join(dst_dir, 'test_dir', 'test.txt'),
179 ):
180 if os.path.exists(path):
181 os.remove(path)
Christian Heimese052dd82007-11-20 03:20:04 +0000182 for path in (src_dir,
Antoine Pitrou97c81ef2009-11-04 00:57:15 +0000183 os.path.dirname(dst_dir)
Christian Heimese052dd82007-11-20 03:20:04 +0000184 ):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000185 if os.path.exists(path):
Christian Heimes94140152007-11-20 01:45:17 +0000186 shutil.rmtree(path)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000187
Georg Brandl2ee470f2008-07-16 12:55:28 +0000188 def test_copytree_with_exclude(self):
189
Georg Brandl2ee470f2008-07-16 12:55:28 +0000190 def read_data(path):
191 f = open(path)
192 data = f.read()
193 f.close()
194 return data
195
196 # creating data
197 join = os.path.join
198 exists = os.path.exists
199 src_dir = tempfile.mkdtemp()
Georg Brandl2ee470f2008-07-16 12:55:28 +0000200 try:
Antoine Pitrou97c81ef2009-11-04 00:57:15 +0000201 dst_dir = join(tempfile.mkdtemp(), 'destination')
Tarek Ziadé5340db32010-04-19 22:30:51 +0000202 self._write_data(join(src_dir, 'test.txt'), '123')
203 self._write_data(join(src_dir, 'test.tmp'), '123')
Antoine Pitrou97c81ef2009-11-04 00:57:15 +0000204 os.mkdir(join(src_dir, 'test_dir'))
Tarek Ziadé5340db32010-04-19 22:30:51 +0000205 self._write_data(join(src_dir, 'test_dir', 'test.txt'), '456')
Antoine Pitrou97c81ef2009-11-04 00:57:15 +0000206 os.mkdir(join(src_dir, 'test_dir2'))
Tarek Ziadé5340db32010-04-19 22:30:51 +0000207 self._write_data(join(src_dir, 'test_dir2', 'test.txt'), '456')
Antoine Pitrou97c81ef2009-11-04 00:57:15 +0000208 os.mkdir(join(src_dir, 'test_dir2', 'subdir'))
209 os.mkdir(join(src_dir, 'test_dir2', 'subdir2'))
Tarek Ziadé5340db32010-04-19 22:30:51 +0000210 self._write_data(join(src_dir, 'test_dir2', 'subdir', 'test.txt'),
211 '456')
212 self._write_data(join(src_dir, 'test_dir2', 'subdir2', 'test.py'),
213 '456')
Antoine Pitrou97c81ef2009-11-04 00:57:15 +0000214
215
216 # testing glob-like patterns
217 try:
218 patterns = shutil.ignore_patterns('*.tmp', 'test_dir2')
219 shutil.copytree(src_dir, dst_dir, ignore=patterns)
220 # checking the result: some elements should not be copied
221 self.assertTrue(exists(join(dst_dir, 'test.txt')))
222 self.assertTrue(not exists(join(dst_dir, 'test.tmp')))
223 self.assertTrue(not exists(join(dst_dir, 'test_dir2')))
224 finally:
225 if os.path.exists(dst_dir):
226 shutil.rmtree(dst_dir)
227 try:
228 patterns = shutil.ignore_patterns('*.tmp', 'subdir*')
229 shutil.copytree(src_dir, dst_dir, ignore=patterns)
230 # checking the result: some elements should not be copied
231 self.assertTrue(not exists(join(dst_dir, 'test.tmp')))
232 self.assertTrue(not exists(join(dst_dir, 'test_dir2', 'subdir2')))
233 self.assertTrue(not exists(join(dst_dir, 'test_dir2', 'subdir')))
234 finally:
235 if os.path.exists(dst_dir):
236 shutil.rmtree(dst_dir)
237
238 # testing callable-style
239 try:
240 def _filter(src, names):
241 res = []
242 for name in names:
243 path = os.path.join(src, name)
244
245 if (os.path.isdir(path) and
246 path.split()[-1] == 'subdir'):
247 res.append(name)
248 elif os.path.splitext(path)[-1] in ('.py'):
249 res.append(name)
250 return res
251
252 shutil.copytree(src_dir, dst_dir, ignore=_filter)
253
254 # checking the result: some elements should not be copied
255 self.assertTrue(not exists(join(dst_dir, 'test_dir2', 'subdir2',
256 'test.py')))
257 self.assertTrue(not exists(join(dst_dir, 'test_dir2', 'subdir')))
258
259 finally:
260 if os.path.exists(dst_dir):
261 shutil.rmtree(dst_dir)
Georg Brandl2ee470f2008-07-16 12:55:28 +0000262 finally:
Antoine Pitrou97c81ef2009-11-04 00:57:15 +0000263 shutil.rmtree(src_dir)
264 shutil.rmtree(os.path.dirname(dst_dir))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000265
Johannes Gijsbers46f14592004-08-14 13:30:02 +0000266 if hasattr(os, "symlink"):
267 def test_dont_copy_file_onto_link_to_itself(self):
268 # bug 851123.
269 os.mkdir(TESTFN)
Johannes Gijsbers68128712004-08-14 13:57:08 +0000270 src = os.path.join(TESTFN, 'cheese')
271 dst = os.path.join(TESTFN, 'shop')
Johannes Gijsbers46f14592004-08-14 13:30:02 +0000272 try:
Johannes Gijsbers68128712004-08-14 13:57:08 +0000273 f = open(src, 'w')
Johannes Gijsbers46f14592004-08-14 13:30:02 +0000274 f.write('cheddar')
275 f.close()
Johannes Gijsbers68128712004-08-14 13:57:08 +0000276
277 os.link(src, dst)
278 self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
279 self.assertEqual(open(src,'r').read(), 'cheddar')
280 os.remove(dst)
281
282 # Using `src` here would mean we end up with a symlink pointing
283 # to TESTFN/TESTFN/cheese, while it should point at
284 # TESTFN/cheese.
285 os.symlink('cheese', dst)
286 self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
287 self.assertEqual(open(src,'r').read(), 'cheddar')
288 os.remove(dst)
Johannes Gijsbers46f14592004-08-14 13:30:02 +0000289 finally:
290 try:
291 shutil.rmtree(TESTFN)
292 except OSError:
293 pass
Brett Cannon1c3fa182004-06-19 21:11:35 +0000294
Christian Heimes9bd667a2008-01-20 15:14:11 +0000295 def test_rmtree_on_symlink(self):
296 # bug 1669.
297 os.mkdir(TESTFN)
298 try:
299 src = os.path.join(TESTFN, 'cheese')
300 dst = os.path.join(TESTFN, 'shop')
301 os.mkdir(src)
302 os.symlink(src, dst)
303 self.assertRaises(OSError, shutil.rmtree, dst)
304 finally:
305 shutil.rmtree(TESTFN, ignore_errors=True)
306
Antoine Pitrou7fff0962009-05-01 21:09:44 +0000307 if hasattr(os, "mkfifo"):
308 # Issue #3002: copyfile and copytree block indefinitely on named pipes
309 def test_copyfile_named_pipe(self):
310 os.mkfifo(TESTFN)
311 try:
312 self.assertRaises(shutil.SpecialFileError,
313 shutil.copyfile, TESTFN, TESTFN2)
314 self.assertRaises(shutil.SpecialFileError,
315 shutil.copyfile, __file__, TESTFN)
316 finally:
317 os.remove(TESTFN)
318
319 def test_copytree_named_pipe(self):
320 os.mkdir(TESTFN)
321 try:
322 subdir = os.path.join(TESTFN, "subdir")
323 os.mkdir(subdir)
324 pipe = os.path.join(subdir, "mypipe")
325 os.mkfifo(pipe)
326 try:
327 shutil.copytree(TESTFN, TESTFN2)
328 except shutil.Error as e:
329 errors = e.args[0]
330 self.assertEqual(len(errors), 1)
331 src, dst, error_msg = errors[0]
332 self.assertEqual("`%s` is a named pipe" % pipe, error_msg)
333 else:
334 self.fail("shutil.Error should have been raised")
335 finally:
336 shutil.rmtree(TESTFN, ignore_errors=True)
337 shutil.rmtree(TESTFN2, ignore_errors=True)
338
Tarek Ziadé5340db32010-04-19 22:30:51 +0000339 def test_copytree_special_func(self):
340
341 src_dir = self.mkdtemp()
342 dst_dir = os.path.join(self.mkdtemp(), 'destination')
343 self._write_data(os.path.join(src_dir, 'test.txt'), '123')
344 os.mkdir(os.path.join(src_dir, 'test_dir'))
345 self._write_data(os.path.join(src_dir, 'test_dir', 'test.txt'), '456')
346
347 copied = []
348 def _copy(src, dst):
349 copied.append((src, dst))
350
351 shutil.copytree(src_dir, dst_dir, copy_function=_copy)
352 self.assertEquals(len(copied), 2)
353
Tarek Ziadéfb437512010-04-20 08:57:33 +0000354 def test_copytree_dangling_symlinks(self):
355
356 # a dangling symlink raises an error at the end
357 src_dir = self.mkdtemp()
358 dst_dir = os.path.join(self.mkdtemp(), 'destination')
359 os.symlink('IDONTEXIST', os.path.join(src_dir, 'test.txt'))
360 os.mkdir(os.path.join(src_dir, 'test_dir'))
361 self._write_data(os.path.join(src_dir, 'test_dir', 'test.txt'), '456')
362 self.assertRaises(Error, shutil.copytree, src_dir, dst_dir)
363
364 # a dangling symlink is ignored with the proper flag
365 dst_dir = os.path.join(self.mkdtemp(), 'destination2')
366 shutil.copytree(src_dir, dst_dir, ignore_dangling_symlinks=True)
367 self.assertNotIn('test.txt', os.listdir(dst_dir))
368
369 # a dangling symlink is copied if symlinks=True
370 dst_dir = os.path.join(self.mkdtemp(), 'destination3')
371 shutil.copytree(src_dir, dst_dir, symlinks=True)
372 self.assertIn('test.txt', os.listdir(dst_dir))
373
Tarek Ziadé396fad72010-02-23 05:30:31 +0000374 @unittest.skipUnless(zlib, "requires zlib")
375 def test_make_tarball(self):
376 # creating something to tar
377 tmpdir = self.mkdtemp()
378 self.write_file([tmpdir, 'file1'], 'xxx')
379 self.write_file([tmpdir, 'file2'], 'xxx')
380 os.mkdir(os.path.join(tmpdir, 'sub'))
381 self.write_file([tmpdir, 'sub', 'file3'], 'xxx')
382
383 tmpdir2 = self.mkdtemp()
384 unittest.skipUnless(splitdrive(tmpdir)[0] == splitdrive(tmpdir2)[0],
385 "source and target should be on same drive")
386
387 base_name = os.path.join(tmpdir2, 'archive')
388
389 # working with relative paths to avoid tar warnings
390 old_dir = os.getcwd()
391 os.chdir(tmpdir)
392 try:
393 _make_tarball(splitdrive(base_name)[1], '.')
394 finally:
395 os.chdir(old_dir)
396
397 # check if the compressed tarball was created
398 tarball = base_name + '.tar.gz'
399 self.assertTrue(os.path.exists(tarball))
400
401 # trying an uncompressed one
402 base_name = os.path.join(tmpdir2, 'archive')
403 old_dir = os.getcwd()
404 os.chdir(tmpdir)
405 try:
406 _make_tarball(splitdrive(base_name)[1], '.', compress=None)
407 finally:
408 os.chdir(old_dir)
409 tarball = base_name + '.tar'
410 self.assertTrue(os.path.exists(tarball))
411
412 def _tarinfo(self, path):
413 tar = tarfile.open(path)
414 try:
415 names = tar.getnames()
416 names.sort()
417 return tuple(names)
418 finally:
419 tar.close()
420
421 def _create_files(self):
422 # creating something to tar
423 tmpdir = self.mkdtemp()
424 dist = os.path.join(tmpdir, 'dist')
425 os.mkdir(dist)
426 self.write_file([dist, 'file1'], 'xxx')
427 self.write_file([dist, 'file2'], 'xxx')
428 os.mkdir(os.path.join(dist, 'sub'))
429 self.write_file([dist, 'sub', 'file3'], 'xxx')
430 os.mkdir(os.path.join(dist, 'sub2'))
431 tmpdir2 = self.mkdtemp()
432 base_name = os.path.join(tmpdir2, 'archive')
433 return tmpdir, tmpdir2, base_name
434
435 @unittest.skipUnless(zlib, "Requires zlib")
436 @unittest.skipUnless(find_executable('tar') and find_executable('gzip'),
437 'Need the tar command to run')
438 def test_tarfile_vs_tar(self):
439 tmpdir, tmpdir2, base_name = self._create_files()
440 old_dir = os.getcwd()
441 os.chdir(tmpdir)
442 try:
443 _make_tarball(base_name, 'dist')
444 finally:
445 os.chdir(old_dir)
446
447 # check if the compressed tarball was created
448 tarball = base_name + '.tar.gz'
449 self.assertTrue(os.path.exists(tarball))
450
451 # now create another tarball using `tar`
452 tarball2 = os.path.join(tmpdir, 'archive2.tar.gz')
453 tar_cmd = ['tar', '-cf', 'archive2.tar', 'dist']
454 gzip_cmd = ['gzip', '-f9', 'archive2.tar']
455 old_dir = os.getcwd()
456 os.chdir(tmpdir)
457 try:
458 with captured_stdout() as s:
459 spawn(tar_cmd)
460 spawn(gzip_cmd)
461 finally:
462 os.chdir(old_dir)
463
464 self.assertTrue(os.path.exists(tarball2))
465 # let's compare both tarballs
466 self.assertEquals(self._tarinfo(tarball), self._tarinfo(tarball2))
467
468 # trying an uncompressed one
469 base_name = os.path.join(tmpdir2, 'archive')
470 old_dir = os.getcwd()
471 os.chdir(tmpdir)
472 try:
473 _make_tarball(base_name, 'dist', compress=None)
474 finally:
475 os.chdir(old_dir)
476 tarball = base_name + '.tar'
477 self.assertTrue(os.path.exists(tarball))
478
479 # now for a dry_run
480 base_name = os.path.join(tmpdir2, 'archive')
481 old_dir = os.getcwd()
482 os.chdir(tmpdir)
483 try:
484 _make_tarball(base_name, 'dist', compress=None, dry_run=True)
485 finally:
486 os.chdir(old_dir)
487 tarball = base_name + '.tar'
488 self.assertTrue(os.path.exists(tarball))
489
490 @unittest.skipUnless(find_executable('compress'),
491 'The compress program is required')
492 def test_compress_deprecated(self):
493 tmpdir, tmpdir2, base_name = self._create_files()
494
495 # using compress and testing the PendingDeprecationWarning
496 old_dir = os.getcwd()
497 os.chdir(tmpdir)
498 try:
Florent Xicluna8fbddf12010-03-17 20:29:51 +0000499 with captured_stdout() as s, check_warnings(quiet=False) as w:
500 _make_tarball(base_name, 'dist', compress='compress')
Tarek Ziadé396fad72010-02-23 05:30:31 +0000501 finally:
502 os.chdir(old_dir)
503 tarball = base_name + '.tar.Z'
504 self.assertTrue(os.path.exists(tarball))
Florent Xicluna8fbddf12010-03-17 20:29:51 +0000505 self.assertEqual(len(w.warnings), 1)
Tarek Ziadé396fad72010-02-23 05:30:31 +0000506
507 # same test with dry_run
508 os.remove(tarball)
509 old_dir = os.getcwd()
510 os.chdir(tmpdir)
511 try:
Florent Xicluna8fbddf12010-03-17 20:29:51 +0000512 with captured_stdout() as s, check_warnings(quiet=False) as w:
513 _make_tarball(base_name, 'dist', compress='compress',
514 dry_run=True)
Tarek Ziadé396fad72010-02-23 05:30:31 +0000515 finally:
516 os.chdir(old_dir)
Florent Xicluna8fbddf12010-03-17 20:29:51 +0000517 self.assertFalse(os.path.exists(tarball))
518 self.assertEqual(len(w.warnings), 1)
Tarek Ziadé396fad72010-02-23 05:30:31 +0000519
520 @unittest.skipUnless(zlib, "Requires zlib")
521 @unittest.skipUnless(ZIP_SUPPORT, 'Need zip support to run')
522 def test_make_zipfile(self):
523 # creating something to tar
524 tmpdir = self.mkdtemp()
525 self.write_file([tmpdir, 'file1'], 'xxx')
526 self.write_file([tmpdir, 'file2'], 'xxx')
527
528 tmpdir2 = self.mkdtemp()
529 base_name = os.path.join(tmpdir2, 'archive')
530 _make_zipfile(base_name, tmpdir)
531
532 # check if the compressed tarball was created
533 tarball = base_name + '.zip'
534
535
536 def test_make_archive(self):
537 tmpdir = self.mkdtemp()
538 base_name = os.path.join(tmpdir, 'archive')
539 self.assertRaises(ValueError, make_archive, base_name, 'xxx')
540
541 @unittest.skipUnless(zlib, "Requires zlib")
542 def test_make_archive_owner_group(self):
543 # testing make_archive with owner and group, with various combinations
544 # this works even if there's not gid/uid support
545 if UID_GID_SUPPORT:
546 group = grp.getgrgid(0)[0]
547 owner = pwd.getpwuid(0)[0]
548 else:
549 group = owner = 'root'
550
551 base_dir, root_dir, base_name = self._create_files()
552 base_name = os.path.join(self.mkdtemp() , 'archive')
553 res = make_archive(base_name, 'zip', root_dir, base_dir, owner=owner,
554 group=group)
555 self.assertTrue(os.path.exists(res))
556
557 res = make_archive(base_name, 'zip', root_dir, base_dir)
558 self.assertTrue(os.path.exists(res))
559
560 res = make_archive(base_name, 'tar', root_dir, base_dir,
561 owner=owner, group=group)
562 self.assertTrue(os.path.exists(res))
563
564 res = make_archive(base_name, 'tar', root_dir, base_dir,
565 owner='kjhkjhkjg', group='oihohoh')
566 self.assertTrue(os.path.exists(res))
567
568 @unittest.skipUnless(zlib, "Requires zlib")
569 @unittest.skipUnless(UID_GID_SUPPORT, "Requires grp and pwd support")
570 def test_tarfile_root_owner(self):
571 tmpdir, tmpdir2, base_name = self._create_files()
572 old_dir = os.getcwd()
573 os.chdir(tmpdir)
574 group = grp.getgrgid(0)[0]
575 owner = pwd.getpwuid(0)[0]
576 try:
577 archive_name = _make_tarball(base_name, 'dist', compress=None,
578 owner=owner, group=group)
579 finally:
580 os.chdir(old_dir)
581
582 # check if the compressed tarball was created
583 self.assertTrue(os.path.exists(archive_name))
584
585 # now checks the rights
586 archive = tarfile.open(archive_name)
587 try:
588 for member in archive.getmembers():
589 self.assertEquals(member.uid, 0)
590 self.assertEquals(member.gid, 0)
591 finally:
592 archive.close()
593
594 def test_make_archive_cwd(self):
595 current_dir = os.getcwd()
596 def _breaks(*args, **kw):
597 raise RuntimeError()
598
599 register_archive_format('xxx', _breaks, [], 'xxx file')
600 try:
601 try:
602 make_archive('xxx', 'xxx', root_dir=self.mkdtemp())
603 except Exception:
604 pass
605 self.assertEquals(os.getcwd(), current_dir)
606 finally:
607 unregister_archive_format('xxx')
608
609 def test_register_archive_format(self):
610
611 self.assertRaises(TypeError, register_archive_format, 'xxx', 1)
612 self.assertRaises(TypeError, register_archive_format, 'xxx', lambda: x,
613 1)
614 self.assertRaises(TypeError, register_archive_format, 'xxx', lambda: x,
615 [(1, 2), (1, 2, 3)])
616
617 register_archive_format('xxx', lambda: x, [(1, 2)], 'xxx file')
618 formats = [name for name, params in get_archive_formats()]
619 self.assertIn('xxx', formats)
620
621 unregister_archive_format('xxx')
622 formats = [name for name, params in get_archive_formats()]
623 self.assertNotIn('xxx', formats)
624
Christian Heimes9bd667a2008-01-20 15:14:11 +0000625
Christian Heimesada8c3b2008-03-18 18:26:33 +0000626class TestMove(unittest.TestCase):
627
628 def setUp(self):
629 filename = "foo"
630 self.src_dir = tempfile.mkdtemp()
631 self.dst_dir = tempfile.mkdtemp()
632 self.src_file = os.path.join(self.src_dir, filename)
633 self.dst_file = os.path.join(self.dst_dir, filename)
634 # Try to create a dir in the current directory, hoping that it is
635 # not located on the same filesystem as the system tmp dir.
636 try:
637 self.dir_other_fs = tempfile.mkdtemp(
638 dir=os.path.dirname(__file__))
639 self.file_other_fs = os.path.join(self.dir_other_fs,
640 filename)
641 except OSError:
642 self.dir_other_fs = None
643 with open(self.src_file, "wb") as f:
644 f.write(b"spam")
645
646 def tearDown(self):
647 for d in (self.src_dir, self.dst_dir, self.dir_other_fs):
648 try:
649 if d:
650 shutil.rmtree(d)
651 except:
652 pass
653
654 def _check_move_file(self, src, dst, real_dst):
655 contents = open(src, "rb").read()
656 shutil.move(src, dst)
657 self.assertEqual(contents, open(real_dst, "rb").read())
658 self.assertFalse(os.path.exists(src))
659
660 def _check_move_dir(self, src, dst, real_dst):
661 contents = sorted(os.listdir(src))
662 shutil.move(src, dst)
663 self.assertEqual(contents, sorted(os.listdir(real_dst)))
664 self.assertFalse(os.path.exists(src))
665
666 def test_move_file(self):
667 # Move a file to another location on the same filesystem.
668 self._check_move_file(self.src_file, self.dst_file, self.dst_file)
669
670 def test_move_file_to_dir(self):
671 # Move a file inside an existing dir on the same filesystem.
672 self._check_move_file(self.src_file, self.dst_dir, self.dst_file)
673
674 def test_move_file_other_fs(self):
675 # Move a file to an existing dir on another filesystem.
676 if not self.dir_other_fs:
677 # skip
678 return
679 self._check_move_file(self.src_file, self.file_other_fs,
680 self.file_other_fs)
681
682 def test_move_file_to_dir_other_fs(self):
683 # Move a file to another location on another filesystem.
684 if not self.dir_other_fs:
685 # skip
686 return
687 self._check_move_file(self.src_file, self.dir_other_fs,
688 self.file_other_fs)
689
690 def test_move_dir(self):
691 # Move a dir to another location on the same filesystem.
692 dst_dir = tempfile.mktemp()
693 try:
694 self._check_move_dir(self.src_dir, dst_dir, dst_dir)
695 finally:
696 try:
697 shutil.rmtree(dst_dir)
698 except:
699 pass
700
701 def test_move_dir_other_fs(self):
702 # Move a dir to another location on another filesystem.
703 if not self.dir_other_fs:
704 # skip
705 return
706 dst_dir = tempfile.mktemp(dir=self.dir_other_fs)
707 try:
708 self._check_move_dir(self.src_dir, dst_dir, dst_dir)
709 finally:
710 try:
711 shutil.rmtree(dst_dir)
712 except:
713 pass
714
715 def test_move_dir_to_dir(self):
716 # Move a dir inside an existing dir on the same filesystem.
717 self._check_move_dir(self.src_dir, self.dst_dir,
718 os.path.join(self.dst_dir, os.path.basename(self.src_dir)))
719
720 def test_move_dir_to_dir_other_fs(self):
721 # Move a dir inside an existing dir on another filesystem.
722 if not self.dir_other_fs:
723 # skip
724 return
725 self._check_move_dir(self.src_dir, self.dir_other_fs,
726 os.path.join(self.dir_other_fs, os.path.basename(self.src_dir)))
727
728 def test_existing_file_inside_dest_dir(self):
729 # A file with the same name inside the destination dir already exists.
730 with open(self.dst_file, "wb"):
731 pass
732 self.assertRaises(shutil.Error, shutil.move, self.src_file, self.dst_dir)
733
734 def test_dont_move_dir_in_itself(self):
735 # Moving a dir inside itself raises an Error.
736 dst = os.path.join(self.src_dir, "bar")
737 self.assertRaises(shutil.Error, shutil.move, self.src_dir, dst)
738
Antoine Pitrou0dcc3cd2009-01-29 20:26:59 +0000739 def test_destinsrc_false_negative(self):
740 os.mkdir(TESTFN)
741 try:
742 for src, dst in [('srcdir', 'srcdir/dest')]:
743 src = os.path.join(TESTFN, src)
744 dst = os.path.join(TESTFN, dst)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000745 self.assertTrue(shutil._destinsrc(src, dst),
Benjamin Peterson247a9b82009-02-20 04:09:19 +0000746 msg='_destinsrc() wrongly concluded that '
Antoine Pitrou0dcc3cd2009-01-29 20:26:59 +0000747 'dst (%s) is not in src (%s)' % (dst, src))
748 finally:
749 shutil.rmtree(TESTFN, ignore_errors=True)
Christian Heimesada8c3b2008-03-18 18:26:33 +0000750
Antoine Pitrou0dcc3cd2009-01-29 20:26:59 +0000751 def test_destinsrc_false_positive(self):
752 os.mkdir(TESTFN)
753 try:
754 for src, dst in [('srcdir', 'src/dest'), ('srcdir', 'srcdir.new')]:
755 src = os.path.join(TESTFN, src)
756 dst = os.path.join(TESTFN, dst)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000757 self.assertFalse(shutil._destinsrc(src, dst),
Benjamin Peterson247a9b82009-02-20 04:09:19 +0000758 msg='_destinsrc() wrongly concluded that '
Antoine Pitrou0dcc3cd2009-01-29 20:26:59 +0000759 'dst (%s) is in src (%s)' % (dst, src))
760 finally:
761 shutil.rmtree(TESTFN, ignore_errors=True)
Christian Heimes9bd667a2008-01-20 15:14:11 +0000762
Tarek Ziadé5340db32010-04-19 22:30:51 +0000763
Barry Warsaw7fc2cca2003-01-24 17:34:13 +0000764def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000765 support.run_unittest(TestShutil, TestMove)
Barry Warsaw7fc2cca2003-01-24 17:34:13 +0000766
Barry Warsaw7fc2cca2003-01-24 17:34:13 +0000767if __name__ == '__main__':
Walter Dörwald21d3a322003-05-01 17:45:56 +0000768 test_main()