2003-01-24 13:34:13 -04:00
|
|
|
# Copyright (C) 2003 Python Software Foundation
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
import shutil
|
|
|
|
import tempfile
|
2004-11-23 05:27:27 -04:00
|
|
|
import sys
|
2004-10-31 08:05:31 -04:00
|
|
|
import stat
|
2004-06-19 18:11:35 -03:00
|
|
|
import os
|
|
|
|
import os.path
|
2010-02-23 01:16:41 -04:00
|
|
|
from os.path import splitdrive
|
|
|
|
from distutils.spawn import find_executable, spawn
|
|
|
|
from shutil import (_make_tarball, _make_zipfile, make_archive,
|
|
|
|
register_archive_format, unregister_archive_format,
|
|
|
|
get_archive_formats)
|
|
|
|
import tarfile
|
|
|
|
import warnings
|
|
|
|
|
2003-01-24 13:34:13 -04:00
|
|
|
from test import test_support
|
2010-02-23 01:16:41 -04:00
|
|
|
from test.test_support import TESTFN, check_warnings, captured_stdout
|
|
|
|
|
2009-05-01 17:55:35 -03:00
|
|
|
TESTFN2 = TESTFN + "2"
|
2003-01-24 13:34:13 -04:00
|
|
|
|
2010-02-23 01:16:41 -04:00
|
|
|
try:
|
|
|
|
import grp
|
|
|
|
import pwd
|
|
|
|
UID_GID_SUPPORT = True
|
|
|
|
except ImportError:
|
|
|
|
UID_GID_SUPPORT = False
|
|
|
|
|
|
|
|
try:
|
|
|
|
import zlib
|
|
|
|
except ImportError:
|
|
|
|
zlib = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
import zipfile
|
|
|
|
ZIP_SUPPORT = True
|
|
|
|
except ImportError:
|
|
|
|
ZIP_SUPPORT = find_executable('zip')
|
|
|
|
|
2003-01-24 13:34:13 -04:00
|
|
|
class TestShutil(unittest.TestCase):
|
2010-02-23 01:16:41 -04:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super(TestShutil, self).setUp()
|
|
|
|
self.tempdirs = []
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
super(TestShutil, self).tearDown()
|
|
|
|
while self.tempdirs:
|
|
|
|
d = self.tempdirs.pop()
|
|
|
|
shutil.rmtree(d, os.name in ('nt', 'cygwin'))
|
|
|
|
|
|
|
|
def write_file(self, path, content='xxx'):
|
|
|
|
"""Writes a file in the given path.
|
|
|
|
|
|
|
|
|
|
|
|
path can be a string or a sequence.
|
|
|
|
"""
|
|
|
|
if isinstance(path, (list, tuple)):
|
|
|
|
path = os.path.join(*path)
|
|
|
|
f = open(path, 'w')
|
|
|
|
try:
|
|
|
|
f.write(content)
|
|
|
|
finally:
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
def mkdtemp(self):
|
|
|
|
"""Create a temporary directory that will be cleaned up.
|
|
|
|
|
|
|
|
Returns the path of the directory.
|
|
|
|
"""
|
|
|
|
d = tempfile.mkdtemp()
|
|
|
|
self.tempdirs.append(d)
|
|
|
|
return d
|
2003-01-24 13:34:13 -04:00
|
|
|
def test_rmtree_errors(self):
|
|
|
|
# filename is guaranteed not to exist
|
|
|
|
filename = tempfile.mktemp()
|
|
|
|
self.assertRaises(OSError, shutil.rmtree, filename)
|
2004-10-31 08:05:31 -04:00
|
|
|
|
2004-12-06 16:50:15 -04:00
|
|
|
# See bug #1071513 for why we don't run this on cygwin
|
|
|
|
# and bug #1076467 for why we don't run this as root.
|
|
|
|
if (hasattr(os, 'chmod') and sys.platform[:6] != 'cygwin'
|
2004-12-12 11:52:57 -04:00
|
|
|
and not (hasattr(os, 'geteuid') and os.geteuid() == 0)):
|
2004-10-31 08:05:31 -04:00
|
|
|
def test_on_error(self):
|
|
|
|
self.errorState = 0
|
|
|
|
os.mkdir(TESTFN)
|
2004-10-31 22:40:52 -04:00
|
|
|
self.childpath = os.path.join(TESTFN, 'a')
|
|
|
|
f = open(self.childpath, 'w')
|
2004-10-31 08:05:31 -04:00
|
|
|
f.close()
|
2004-10-31 22:40:52 -04:00
|
|
|
old_dir_mode = os.stat(TESTFN).st_mode
|
|
|
|
old_child_mode = os.stat(self.childpath).st_mode
|
|
|
|
# Make unwritable.
|
|
|
|
os.chmod(self.childpath, stat.S_IREAD)
|
|
|
|
os.chmod(TESTFN, stat.S_IREAD)
|
2004-10-31 08:05:31 -04:00
|
|
|
|
|
|
|
shutil.rmtree(TESTFN, onerror=self.check_args_to_onerror)
|
2004-11-23 05:27:27 -04:00
|
|
|
# Test whether onerror has actually been called.
|
2004-12-06 16:50:15 -04:00
|
|
|
self.assertEqual(self.errorState, 2,
|
|
|
|
"Expected call to onerror function did not happen.")
|
2004-10-31 08:05:31 -04:00
|
|
|
|
2004-10-31 22:40:52 -04:00
|
|
|
# Make writable again.
|
|
|
|
os.chmod(TESTFN, old_dir_mode)
|
|
|
|
os.chmod(self.childpath, old_child_mode)
|
|
|
|
|
|
|
|
# Clean up.
|
2004-10-31 08:05:31 -04:00
|
|
|
shutil.rmtree(TESTFN)
|
|
|
|
|
|
|
|
def check_args_to_onerror(self, func, arg, exc):
|
2009-04-29 19:43:35 -03:00
|
|
|
# test_rmtree_errors deliberately runs rmtree
|
|
|
|
# on a directory that is chmod 400, which will fail.
|
|
|
|
# This function is run when shutil.rmtree fails.
|
|
|
|
# 99.9% of the time it initially fails to remove
|
|
|
|
# a file in the directory, so the first time through
|
|
|
|
# func is os.remove.
|
|
|
|
# However, some Linux machines running ZFS on
|
|
|
|
# FUSE experienced a failure earlier in the process
|
|
|
|
# at os.listdir. The first failure may legally
|
|
|
|
# be either.
|
2004-10-31 08:05:31 -04:00
|
|
|
if self.errorState == 0:
|
2009-04-29 19:43:35 -03:00
|
|
|
if func is os.remove:
|
|
|
|
self.assertEqual(arg, self.childpath)
|
|
|
|
else:
|
|
|
|
self.assertIs(func, os.listdir,
|
|
|
|
"func must be either os.remove or os.listdir")
|
|
|
|
self.assertEqual(arg, TESTFN)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(issubclass(exc[0], OSError))
|
2004-10-31 08:05:31 -04:00
|
|
|
self.errorState = 1
|
|
|
|
else:
|
|
|
|
self.assertEqual(func, os.rmdir)
|
|
|
|
self.assertEqual(arg, TESTFN)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(issubclass(exc[0], OSError))
|
2004-11-23 05:27:27 -04:00
|
|
|
self.errorState = 2
|
2003-01-24 13:34:13 -04:00
|
|
|
|
2004-09-11 18:26:21 -03:00
|
|
|
def test_rmtree_dont_delete_file(self):
|
|
|
|
# When called on a file instead of a directory, don't delete it.
|
|
|
|
handle, path = tempfile.mkstemp()
|
|
|
|
os.fdopen(handle).close()
|
|
|
|
self.assertRaises(OSError, shutil.rmtree, path)
|
|
|
|
os.remove(path)
|
|
|
|
|
2006-07-30 10:00:31 -03:00
|
|
|
def test_copytree_simple(self):
|
2006-08-10 00:01:26 -03:00
|
|
|
def write_data(path, data):
|
|
|
|
f = open(path, "w")
|
|
|
|
f.write(data)
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
def read_data(path):
|
|
|
|
f = open(path)
|
|
|
|
data = f.read()
|
|
|
|
f.close()
|
|
|
|
return data
|
|
|
|
|
2006-07-30 10:00:31 -03:00
|
|
|
src_dir = tempfile.mkdtemp()
|
|
|
|
dst_dir = os.path.join(tempfile.mkdtemp(), 'destination')
|
2006-08-10 00:01:26 -03:00
|
|
|
|
|
|
|
write_data(os.path.join(src_dir, 'test.txt'), '123')
|
|
|
|
|
2006-07-30 10:00:31 -03:00
|
|
|
os.mkdir(os.path.join(src_dir, 'test_dir'))
|
2006-08-10 00:01:26 -03:00
|
|
|
write_data(os.path.join(src_dir, 'test_dir', 'test.txt'), '456')
|
|
|
|
|
2006-07-30 10:00:31 -03:00
|
|
|
try:
|
|
|
|
shutil.copytree(src_dir, dst_dir)
|
|
|
|
self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'test.txt')))
|
|
|
|
self.assertTrue(os.path.isdir(os.path.join(dst_dir, 'test_dir')))
|
2006-08-10 00:01:26 -03:00
|
|
|
self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'test_dir',
|
|
|
|
'test.txt')))
|
|
|
|
actual = read_data(os.path.join(dst_dir, 'test.txt'))
|
|
|
|
self.assertEqual(actual, '123')
|
|
|
|
actual = read_data(os.path.join(dst_dir, 'test_dir', 'test.txt'))
|
|
|
|
self.assertEqual(actual, '456')
|
2006-07-30 10:00:31 -03:00
|
|
|
finally:
|
2006-08-10 00:01:26 -03:00
|
|
|
for path in (
|
|
|
|
os.path.join(src_dir, 'test.txt'),
|
|
|
|
os.path.join(dst_dir, 'test.txt'),
|
|
|
|
os.path.join(src_dir, 'test_dir', 'test.txt'),
|
|
|
|
os.path.join(dst_dir, 'test_dir', 'test.txt'),
|
|
|
|
):
|
|
|
|
if os.path.exists(path):
|
|
|
|
os.remove(path)
|
2007-11-19 23:21:02 -04:00
|
|
|
for path in (src_dir,
|
2009-11-03 20:50:26 -04:00
|
|
|
os.path.dirname(dst_dir)
|
2007-11-19 23:21:02 -04:00
|
|
|
):
|
2006-08-10 00:01:26 -03:00
|
|
|
if os.path.exists(path):
|
2007-11-19 21:48:48 -04:00
|
|
|
shutil.rmtree(path)
|
2006-07-30 22:46:03 -03:00
|
|
|
|
2008-07-05 07:13:36 -03:00
|
|
|
def test_copytree_with_exclude(self):
|
|
|
|
|
|
|
|
def write_data(path, data):
|
|
|
|
f = open(path, "w")
|
|
|
|
f.write(data)
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
def read_data(path):
|
|
|
|
f = open(path)
|
|
|
|
data = f.read()
|
|
|
|
f.close()
|
|
|
|
return data
|
|
|
|
|
|
|
|
# creating data
|
|
|
|
join = os.path.join
|
|
|
|
exists = os.path.exists
|
|
|
|
src_dir = tempfile.mkdtemp()
|
|
|
|
try:
|
2009-11-03 20:50:26 -04:00
|
|
|
dst_dir = join(tempfile.mkdtemp(), 'destination')
|
|
|
|
write_data(join(src_dir, 'test.txt'), '123')
|
|
|
|
write_data(join(src_dir, 'test.tmp'), '123')
|
|
|
|
os.mkdir(join(src_dir, 'test_dir'))
|
|
|
|
write_data(join(src_dir, 'test_dir', 'test.txt'), '456')
|
|
|
|
os.mkdir(join(src_dir, 'test_dir2'))
|
|
|
|
write_data(join(src_dir, 'test_dir2', 'test.txt'), '456')
|
|
|
|
os.mkdir(join(src_dir, 'test_dir2', 'subdir'))
|
|
|
|
os.mkdir(join(src_dir, 'test_dir2', 'subdir2'))
|
|
|
|
write_data(join(src_dir, 'test_dir2', 'subdir', 'test.txt'), '456')
|
|
|
|
write_data(join(src_dir, 'test_dir2', 'subdir2', 'test.py'), '456')
|
|
|
|
|
|
|
|
|
|
|
|
# testing glob-like patterns
|
|
|
|
try:
|
|
|
|
patterns = shutil.ignore_patterns('*.tmp', 'test_dir2')
|
|
|
|
shutil.copytree(src_dir, dst_dir, ignore=patterns)
|
|
|
|
# checking the result: some elements should not be copied
|
|
|
|
self.assertTrue(exists(join(dst_dir, 'test.txt')))
|
|
|
|
self.assertTrue(not exists(join(dst_dir, 'test.tmp')))
|
|
|
|
self.assertTrue(not exists(join(dst_dir, 'test_dir2')))
|
|
|
|
finally:
|
|
|
|
if os.path.exists(dst_dir):
|
|
|
|
shutil.rmtree(dst_dir)
|
|
|
|
try:
|
|
|
|
patterns = shutil.ignore_patterns('*.tmp', 'subdir*')
|
|
|
|
shutil.copytree(src_dir, dst_dir, ignore=patterns)
|
|
|
|
# checking the result: some elements should not be copied
|
|
|
|
self.assertTrue(not exists(join(dst_dir, 'test.tmp')))
|
|
|
|
self.assertTrue(not exists(join(dst_dir, 'test_dir2', 'subdir2')))
|
|
|
|
self.assertTrue(not exists(join(dst_dir, 'test_dir2', 'subdir')))
|
|
|
|
finally:
|
|
|
|
if os.path.exists(dst_dir):
|
|
|
|
shutil.rmtree(dst_dir)
|
2008-07-05 07:13:36 -03:00
|
|
|
|
2009-11-03 20:50:26 -04:00
|
|
|
# testing callable-style
|
|
|
|
try:
|
|
|
|
def _filter(src, names):
|
|
|
|
res = []
|
|
|
|
for name in names:
|
|
|
|
path = os.path.join(src, name)
|
2008-07-05 07:13:36 -03:00
|
|
|
|
2009-11-03 20:50:26 -04:00
|
|
|
if (os.path.isdir(path) and
|
|
|
|
path.split()[-1] == 'subdir'):
|
|
|
|
res.append(name)
|
|
|
|
elif os.path.splitext(path)[-1] in ('.py'):
|
|
|
|
res.append(name)
|
|
|
|
return res
|
2008-07-05 07:13:36 -03:00
|
|
|
|
2009-11-03 20:50:26 -04:00
|
|
|
shutil.copytree(src_dir, dst_dir, ignore=_filter)
|
2008-07-05 07:13:36 -03:00
|
|
|
|
2009-11-03 20:50:26 -04:00
|
|
|
# checking the result: some elements should not be copied
|
|
|
|
self.assertTrue(not exists(join(dst_dir, 'test_dir2', 'subdir2',
|
|
|
|
'test.py')))
|
|
|
|
self.assertTrue(not exists(join(dst_dir, 'test_dir2', 'subdir')))
|
2008-07-05 07:13:36 -03:00
|
|
|
|
2009-11-03 20:50:26 -04:00
|
|
|
finally:
|
|
|
|
if os.path.exists(dst_dir):
|
|
|
|
shutil.rmtree(dst_dir)
|
2008-07-05 07:13:36 -03:00
|
|
|
finally:
|
2009-11-03 20:50:26 -04:00
|
|
|
shutil.rmtree(src_dir)
|
|
|
|
shutil.rmtree(os.path.dirname(dst_dir))
|
2006-07-30 22:46:03 -03:00
|
|
|
|
2004-08-14 10:30:02 -03:00
|
|
|
if hasattr(os, "symlink"):
|
|
|
|
def test_dont_copy_file_onto_link_to_itself(self):
|
|
|
|
# bug 851123.
|
|
|
|
os.mkdir(TESTFN)
|
2004-08-14 10:57:08 -03:00
|
|
|
src = os.path.join(TESTFN, 'cheese')
|
|
|
|
dst = os.path.join(TESTFN, 'shop')
|
2004-08-14 10:30:02 -03:00
|
|
|
try:
|
2004-08-14 10:57:08 -03:00
|
|
|
f = open(src, 'w')
|
2004-08-14 10:30:02 -03:00
|
|
|
f.write('cheddar')
|
|
|
|
f.close()
|
2004-08-14 10:57:08 -03:00
|
|
|
|
|
|
|
os.link(src, dst)
|
|
|
|
self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
|
|
|
|
self.assertEqual(open(src,'r').read(), 'cheddar')
|
|
|
|
os.remove(dst)
|
|
|
|
|
|
|
|
# Using `src` here would mean we end up with a symlink pointing
|
|
|
|
# to TESTFN/TESTFN/cheese, while it should point at
|
|
|
|
# TESTFN/cheese.
|
|
|
|
os.symlink('cheese', dst)
|
|
|
|
self.assertRaises(shutil.Error, shutil.copyfile, src, dst)
|
|
|
|
self.assertEqual(open(src,'r').read(), 'cheddar')
|
|
|
|
os.remove(dst)
|
2004-08-14 10:30:02 -03:00
|
|
|
finally:
|
|
|
|
try:
|
|
|
|
shutil.rmtree(TESTFN)
|
|
|
|
except OSError:
|
|
|
|
pass
|
2004-06-19 18:11:35 -03:00
|
|
|
|
2008-01-20 10:17:42 -04:00
|
|
|
def test_rmtree_on_symlink(self):
|
|
|
|
# bug 1669.
|
|
|
|
os.mkdir(TESTFN)
|
|
|
|
try:
|
|
|
|
src = os.path.join(TESTFN, 'cheese')
|
|
|
|
dst = os.path.join(TESTFN, 'shop')
|
|
|
|
os.mkdir(src)
|
|
|
|
os.symlink(src, dst)
|
|
|
|
self.assertRaises(OSError, shutil.rmtree, dst)
|
|
|
|
finally:
|
|
|
|
shutil.rmtree(TESTFN, ignore_errors=True)
|
|
|
|
|
2009-05-01 17:55:35 -03:00
|
|
|
if hasattr(os, "mkfifo"):
|
|
|
|
# Issue #3002: copyfile and copytree block indefinitely on named pipes
|
|
|
|
def test_copyfile_named_pipe(self):
|
|
|
|
os.mkfifo(TESTFN)
|
|
|
|
try:
|
|
|
|
self.assertRaises(shutil.SpecialFileError,
|
|
|
|
shutil.copyfile, TESTFN, TESTFN2)
|
|
|
|
self.assertRaises(shutil.SpecialFileError,
|
|
|
|
shutil.copyfile, __file__, TESTFN)
|
|
|
|
finally:
|
|
|
|
os.remove(TESTFN)
|
|
|
|
|
|
|
|
def test_copytree_named_pipe(self):
|
|
|
|
os.mkdir(TESTFN)
|
|
|
|
try:
|
|
|
|
subdir = os.path.join(TESTFN, "subdir")
|
|
|
|
os.mkdir(subdir)
|
|
|
|
pipe = os.path.join(subdir, "mypipe")
|
|
|
|
os.mkfifo(pipe)
|
|
|
|
try:
|
|
|
|
shutil.copytree(TESTFN, TESTFN2)
|
|
|
|
except shutil.Error as e:
|
|
|
|
errors = e.args[0]
|
|
|
|
self.assertEqual(len(errors), 1)
|
|
|
|
src, dst, error_msg = errors[0]
|
|
|
|
self.assertEqual("`%s` is a named pipe" % pipe, error_msg)
|
|
|
|
else:
|
|
|
|
self.fail("shutil.Error should have been raised")
|
|
|
|
finally:
|
|
|
|
shutil.rmtree(TESTFN, ignore_errors=True)
|
|
|
|
shutil.rmtree(TESTFN2, ignore_errors=True)
|
|
|
|
|
2010-02-23 01:16:41 -04:00
|
|
|
@unittest.skipUnless(zlib, "requires zlib")
|
|
|
|
def test_make_tarball(self):
|
|
|
|
# creating something to tar
|
|
|
|
tmpdir = self.mkdtemp()
|
|
|
|
self.write_file([tmpdir, 'file1'], 'xxx')
|
|
|
|
self.write_file([tmpdir, 'file2'], 'xxx')
|
|
|
|
os.mkdir(os.path.join(tmpdir, 'sub'))
|
|
|
|
self.write_file([tmpdir, 'sub', 'file3'], 'xxx')
|
|
|
|
|
|
|
|
tmpdir2 = self.mkdtemp()
|
|
|
|
unittest.skipUnless(splitdrive(tmpdir)[0] == splitdrive(tmpdir2)[0],
|
|
|
|
"source and target should be on same drive")
|
|
|
|
|
|
|
|
base_name = os.path.join(tmpdir2, 'archive')
|
|
|
|
|
|
|
|
# working with relative paths to avoid tar warnings
|
|
|
|
old_dir = os.getcwd()
|
|
|
|
os.chdir(tmpdir)
|
|
|
|
try:
|
|
|
|
_make_tarball(splitdrive(base_name)[1], '.')
|
|
|
|
finally:
|
|
|
|
os.chdir(old_dir)
|
|
|
|
|
|
|
|
# check if the compressed tarball was created
|
|
|
|
tarball = base_name + '.tar.gz'
|
|
|
|
self.assertTrue(os.path.exists(tarball))
|
|
|
|
|
|
|
|
# trying an uncompressed one
|
|
|
|
base_name = os.path.join(tmpdir2, 'archive')
|
|
|
|
old_dir = os.getcwd()
|
|
|
|
os.chdir(tmpdir)
|
|
|
|
try:
|
|
|
|
_make_tarball(splitdrive(base_name)[1], '.', compress=None)
|
|
|
|
finally:
|
|
|
|
os.chdir(old_dir)
|
|
|
|
tarball = base_name + '.tar'
|
|
|
|
self.assertTrue(os.path.exists(tarball))
|
|
|
|
|
|
|
|
def _tarinfo(self, path):
|
|
|
|
tar = tarfile.open(path)
|
|
|
|
try:
|
|
|
|
names = tar.getnames()
|
|
|
|
names.sort()
|
|
|
|
return tuple(names)
|
|
|
|
finally:
|
|
|
|
tar.close()
|
|
|
|
|
|
|
|
def _create_files(self):
|
|
|
|
# creating something to tar
|
|
|
|
tmpdir = self.mkdtemp()
|
|
|
|
dist = os.path.join(tmpdir, 'dist')
|
|
|
|
os.mkdir(dist)
|
|
|
|
self.write_file([dist, 'file1'], 'xxx')
|
|
|
|
self.write_file([dist, 'file2'], 'xxx')
|
|
|
|
os.mkdir(os.path.join(dist, 'sub'))
|
|
|
|
self.write_file([dist, 'sub', 'file3'], 'xxx')
|
|
|
|
os.mkdir(os.path.join(dist, 'sub2'))
|
|
|
|
tmpdir2 = self.mkdtemp()
|
|
|
|
base_name = os.path.join(tmpdir2, 'archive')
|
|
|
|
return tmpdir, tmpdir2, base_name
|
|
|
|
|
|
|
|
@unittest.skipUnless(zlib, "Requires zlib")
|
|
|
|
@unittest.skipUnless(find_executable('tar') and find_executable('gzip'),
|
|
|
|
'Need the tar command to run')
|
|
|
|
def test_tarfile_vs_tar(self):
|
|
|
|
tmpdir, tmpdir2, base_name = self._create_files()
|
|
|
|
old_dir = os.getcwd()
|
|
|
|
os.chdir(tmpdir)
|
|
|
|
try:
|
|
|
|
_make_tarball(base_name, 'dist')
|
|
|
|
finally:
|
|
|
|
os.chdir(old_dir)
|
|
|
|
|
|
|
|
# check if the compressed tarball was created
|
|
|
|
tarball = base_name + '.tar.gz'
|
|
|
|
self.assertTrue(os.path.exists(tarball))
|
|
|
|
|
|
|
|
# now create another tarball using `tar`
|
|
|
|
tarball2 = os.path.join(tmpdir, 'archive2.tar.gz')
|
|
|
|
tar_cmd = ['tar', '-cf', 'archive2.tar', 'dist']
|
|
|
|
gzip_cmd = ['gzip', '-f9', 'archive2.tar']
|
|
|
|
old_dir = os.getcwd()
|
|
|
|
os.chdir(tmpdir)
|
|
|
|
try:
|
|
|
|
with captured_stdout() as s:
|
|
|
|
spawn(tar_cmd)
|
|
|
|
spawn(gzip_cmd)
|
|
|
|
finally:
|
|
|
|
os.chdir(old_dir)
|
|
|
|
|
|
|
|
self.assertTrue(os.path.exists(tarball2))
|
|
|
|
# let's compare both tarballs
|
|
|
|
self.assertEquals(self._tarinfo(tarball), self._tarinfo(tarball2))
|
|
|
|
|
|
|
|
# trying an uncompressed one
|
|
|
|
base_name = os.path.join(tmpdir2, 'archive')
|
|
|
|
old_dir = os.getcwd()
|
|
|
|
os.chdir(tmpdir)
|
|
|
|
try:
|
|
|
|
_make_tarball(base_name, 'dist', compress=None)
|
|
|
|
finally:
|
|
|
|
os.chdir(old_dir)
|
|
|
|
tarball = base_name + '.tar'
|
|
|
|
self.assertTrue(os.path.exists(tarball))
|
|
|
|
|
|
|
|
# now for a dry_run
|
|
|
|
base_name = os.path.join(tmpdir2, 'archive')
|
|
|
|
old_dir = os.getcwd()
|
|
|
|
os.chdir(tmpdir)
|
|
|
|
try:
|
|
|
|
_make_tarball(base_name, 'dist', compress=None, dry_run=True)
|
|
|
|
finally:
|
|
|
|
os.chdir(old_dir)
|
|
|
|
tarball = base_name + '.tar'
|
|
|
|
self.assertTrue(os.path.exists(tarball))
|
|
|
|
|
|
|
|
@unittest.skipUnless(find_executable('compress'),
|
|
|
|
'The compress program is required')
|
|
|
|
def test_compress_deprecated(self):
|
|
|
|
tmpdir, tmpdir2, base_name = self._create_files()
|
|
|
|
|
|
|
|
# using compress and testing the PendingDeprecationWarning
|
|
|
|
old_dir = os.getcwd()
|
|
|
|
os.chdir(tmpdir)
|
|
|
|
try:
|
2010-03-17 16:15:56 -03:00
|
|
|
with captured_stdout() as s, check_warnings(quiet=False) as w:
|
|
|
|
_make_tarball(base_name, 'dist', compress='compress')
|
2010-02-23 01:16:41 -04:00
|
|
|
finally:
|
|
|
|
os.chdir(old_dir)
|
|
|
|
tarball = base_name + '.tar.Z'
|
|
|
|
self.assertTrue(os.path.exists(tarball))
|
2010-03-17 16:15:56 -03:00
|
|
|
self.assertEqual(len(w.warnings), 1)
|
2010-02-23 01:16:41 -04:00
|
|
|
|
|
|
|
# same test with dry_run
|
|
|
|
os.remove(tarball)
|
|
|
|
old_dir = os.getcwd()
|
|
|
|
os.chdir(tmpdir)
|
|
|
|
try:
|
2010-03-17 16:15:56 -03:00
|
|
|
with captured_stdout() as s, check_warnings(quiet=False) as w:
|
|
|
|
_make_tarball(base_name, 'dist', compress='compress',
|
|
|
|
dry_run=True)
|
2010-02-23 01:16:41 -04:00
|
|
|
finally:
|
|
|
|
os.chdir(old_dir)
|
2010-03-17 16:15:56 -03:00
|
|
|
self.assertFalse(os.path.exists(tarball))
|
|
|
|
self.assertEqual(len(w.warnings), 1)
|
2010-02-23 01:16:41 -04:00
|
|
|
|
|
|
|
@unittest.skipUnless(zlib, "Requires zlib")
|
|
|
|
@unittest.skipUnless(ZIP_SUPPORT, 'Need zip support to run')
|
|
|
|
def test_make_zipfile(self):
|
|
|
|
# creating something to tar
|
|
|
|
tmpdir = self.mkdtemp()
|
|
|
|
self.write_file([tmpdir, 'file1'], 'xxx')
|
|
|
|
self.write_file([tmpdir, 'file2'], 'xxx')
|
|
|
|
|
|
|
|
tmpdir2 = self.mkdtemp()
|
|
|
|
base_name = os.path.join(tmpdir2, 'archive')
|
|
|
|
_make_zipfile(base_name, tmpdir)
|
|
|
|
|
|
|
|
# check if the compressed tarball was created
|
|
|
|
tarball = base_name + '.zip'
|
|
|
|
|
|
|
|
|
|
|
|
def test_make_archive(self):
|
|
|
|
tmpdir = self.mkdtemp()
|
|
|
|
base_name = os.path.join(tmpdir, 'archive')
|
|
|
|
self.assertRaises(ValueError, make_archive, base_name, 'xxx')
|
|
|
|
|
|
|
|
@unittest.skipUnless(zlib, "Requires zlib")
|
|
|
|
def test_make_archive_owner_group(self):
|
|
|
|
# testing make_archive with owner and group, with various combinations
|
|
|
|
# this works even if there's not gid/uid support
|
|
|
|
if UID_GID_SUPPORT:
|
|
|
|
group = grp.getgrgid(0)[0]
|
|
|
|
owner = pwd.getpwuid(0)[0]
|
|
|
|
else:
|
|
|
|
group = owner = 'root'
|
|
|
|
|
|
|
|
base_dir, root_dir, base_name = self._create_files()
|
|
|
|
base_name = os.path.join(self.mkdtemp() , 'archive')
|
|
|
|
res = make_archive(base_name, 'zip', root_dir, base_dir, owner=owner,
|
|
|
|
group=group)
|
|
|
|
self.assertTrue(os.path.exists(res))
|
|
|
|
|
|
|
|
res = make_archive(base_name, 'zip', root_dir, base_dir)
|
|
|
|
self.assertTrue(os.path.exists(res))
|
|
|
|
|
|
|
|
res = make_archive(base_name, 'tar', root_dir, base_dir,
|
|
|
|
owner=owner, group=group)
|
|
|
|
self.assertTrue(os.path.exists(res))
|
|
|
|
|
|
|
|
res = make_archive(base_name, 'tar', root_dir, base_dir,
|
|
|
|
owner='kjhkjhkjg', group='oihohoh')
|
|
|
|
self.assertTrue(os.path.exists(res))
|
|
|
|
|
|
|
|
@unittest.skipUnless(zlib, "Requires zlib")
|
|
|
|
@unittest.skipUnless(UID_GID_SUPPORT, "Requires grp and pwd support")
|
|
|
|
def test_tarfile_root_owner(self):
|
|
|
|
tmpdir, tmpdir2, base_name = self._create_files()
|
|
|
|
old_dir = os.getcwd()
|
|
|
|
os.chdir(tmpdir)
|
|
|
|
group = grp.getgrgid(0)[0]
|
|
|
|
owner = pwd.getpwuid(0)[0]
|
|
|
|
try:
|
|
|
|
archive_name = _make_tarball(base_name, 'dist', compress=None,
|
|
|
|
owner=owner, group=group)
|
|
|
|
finally:
|
|
|
|
os.chdir(old_dir)
|
|
|
|
|
|
|
|
# check if the compressed tarball was created
|
|
|
|
self.assertTrue(os.path.exists(archive_name))
|
|
|
|
|
|
|
|
# now checks the rights
|
|
|
|
archive = tarfile.open(archive_name)
|
|
|
|
try:
|
|
|
|
for member in archive.getmembers():
|
|
|
|
self.assertEquals(member.uid, 0)
|
|
|
|
self.assertEquals(member.gid, 0)
|
|
|
|
finally:
|
|
|
|
archive.close()
|
|
|
|
|
|
|
|
def test_make_archive_cwd(self):
|
|
|
|
current_dir = os.getcwd()
|
|
|
|
def _breaks(*args, **kw):
|
|
|
|
raise RuntimeError()
|
|
|
|
|
|
|
|
register_archive_format('xxx', _breaks, [], 'xxx file')
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
make_archive('xxx', 'xxx', root_dir=self.mkdtemp())
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
self.assertEquals(os.getcwd(), current_dir)
|
|
|
|
finally:
|
|
|
|
unregister_archive_format('xxx')
|
|
|
|
|
|
|
|
def test_register_archive_format(self):
|
|
|
|
|
|
|
|
self.assertRaises(TypeError, register_archive_format, 'xxx', 1)
|
|
|
|
self.assertRaises(TypeError, register_archive_format, 'xxx', lambda: x,
|
|
|
|
1)
|
|
|
|
self.assertRaises(TypeError, register_archive_format, 'xxx', lambda: x,
|
|
|
|
[(1, 2), (1, 2, 3)])
|
|
|
|
|
|
|
|
register_archive_format('xxx', lambda: x, [(1, 2)], 'xxx file')
|
|
|
|
formats = [name for name, params in get_archive_formats()]
|
|
|
|
self.assertIn('xxx', formats)
|
|
|
|
|
|
|
|
unregister_archive_format('xxx')
|
|
|
|
formats = [name for name, params in get_archive_formats()]
|
|
|
|
self.assertNotIn('xxx', formats)
|
|
|
|
|
2008-01-20 10:17:42 -04:00
|
|
|
|
2008-03-18 14:24:12 -03:00
|
|
|
class TestMove(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
filename = "foo"
|
|
|
|
self.src_dir = tempfile.mkdtemp()
|
|
|
|
self.dst_dir = tempfile.mkdtemp()
|
|
|
|
self.src_file = os.path.join(self.src_dir, filename)
|
|
|
|
self.dst_file = os.path.join(self.dst_dir, filename)
|
|
|
|
# Try to create a dir in the current directory, hoping that it is
|
|
|
|
# not located on the same filesystem as the system tmp dir.
|
|
|
|
try:
|
|
|
|
self.dir_other_fs = tempfile.mkdtemp(
|
|
|
|
dir=os.path.dirname(__file__))
|
|
|
|
self.file_other_fs = os.path.join(self.dir_other_fs,
|
|
|
|
filename)
|
|
|
|
except OSError:
|
|
|
|
self.dir_other_fs = None
|
|
|
|
with open(self.src_file, "wb") as f:
|
|
|
|
f.write("spam")
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
for d in (self.src_dir, self.dst_dir, self.dir_other_fs):
|
|
|
|
try:
|
|
|
|
if d:
|
|
|
|
shutil.rmtree(d)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def _check_move_file(self, src, dst, real_dst):
|
|
|
|
contents = open(src, "rb").read()
|
|
|
|
shutil.move(src, dst)
|
|
|
|
self.assertEqual(contents, open(real_dst, "rb").read())
|
|
|
|
self.assertFalse(os.path.exists(src))
|
|
|
|
|
|
|
|
def _check_move_dir(self, src, dst, real_dst):
|
|
|
|
contents = sorted(os.listdir(src))
|
|
|
|
shutil.move(src, dst)
|
|
|
|
self.assertEqual(contents, sorted(os.listdir(real_dst)))
|
|
|
|
self.assertFalse(os.path.exists(src))
|
|
|
|
|
|
|
|
def test_move_file(self):
|
|
|
|
# Move a file to another location on the same filesystem.
|
|
|
|
self._check_move_file(self.src_file, self.dst_file, self.dst_file)
|
|
|
|
|
|
|
|
def test_move_file_to_dir(self):
|
|
|
|
# Move a file inside an existing dir on the same filesystem.
|
|
|
|
self._check_move_file(self.src_file, self.dst_dir, self.dst_file)
|
|
|
|
|
|
|
|
def test_move_file_other_fs(self):
|
|
|
|
# Move a file to an existing dir on another filesystem.
|
|
|
|
if not self.dir_other_fs:
|
|
|
|
# skip
|
|
|
|
return
|
|
|
|
self._check_move_file(self.src_file, self.file_other_fs,
|
|
|
|
self.file_other_fs)
|
|
|
|
|
|
|
|
def test_move_file_to_dir_other_fs(self):
|
|
|
|
# Move a file to another location on another filesystem.
|
|
|
|
if not self.dir_other_fs:
|
|
|
|
# skip
|
|
|
|
return
|
|
|
|
self._check_move_file(self.src_file, self.dir_other_fs,
|
|
|
|
self.file_other_fs)
|
|
|
|
|
|
|
|
def test_move_dir(self):
|
|
|
|
# Move a dir to another location on the same filesystem.
|
|
|
|
dst_dir = tempfile.mktemp()
|
|
|
|
try:
|
|
|
|
self._check_move_dir(self.src_dir, dst_dir, dst_dir)
|
|
|
|
finally:
|
|
|
|
try:
|
|
|
|
shutil.rmtree(dst_dir)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_move_dir_other_fs(self):
|
|
|
|
# Move a dir to another location on another filesystem.
|
|
|
|
if not self.dir_other_fs:
|
|
|
|
# skip
|
|
|
|
return
|
|
|
|
dst_dir = tempfile.mktemp(dir=self.dir_other_fs)
|
|
|
|
try:
|
|
|
|
self._check_move_dir(self.src_dir, dst_dir, dst_dir)
|
|
|
|
finally:
|
|
|
|
try:
|
|
|
|
shutil.rmtree(dst_dir)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_move_dir_to_dir(self):
|
|
|
|
# Move a dir inside an existing dir on the same filesystem.
|
|
|
|
self._check_move_dir(self.src_dir, self.dst_dir,
|
|
|
|
os.path.join(self.dst_dir, os.path.basename(self.src_dir)))
|
|
|
|
|
|
|
|
def test_move_dir_to_dir_other_fs(self):
|
|
|
|
# Move a dir inside an existing dir on another filesystem.
|
|
|
|
if not self.dir_other_fs:
|
|
|
|
# skip
|
|
|
|
return
|
|
|
|
self._check_move_dir(self.src_dir, self.dir_other_fs,
|
|
|
|
os.path.join(self.dir_other_fs, os.path.basename(self.src_dir)))
|
|
|
|
|
|
|
|
def test_existing_file_inside_dest_dir(self):
|
|
|
|
# A file with the same name inside the destination dir already exists.
|
|
|
|
with open(self.dst_file, "wb"):
|
|
|
|
pass
|
|
|
|
self.assertRaises(shutil.Error, shutil.move, self.src_file, self.dst_dir)
|
|
|
|
|
|
|
|
def test_dont_move_dir_in_itself(self):
|
|
|
|
# Moving a dir inside itself raises an Error.
|
|
|
|
dst = os.path.join(self.src_dir, "bar")
|
|
|
|
self.assertRaises(shutil.Error, shutil.move, self.src_dir, dst)
|
|
|
|
|
2009-01-29 16:19:34 -04:00
|
|
|
def test_destinsrc_false_negative(self):
|
|
|
|
os.mkdir(TESTFN)
|
|
|
|
try:
|
|
|
|
for src, dst in [('srcdir', 'srcdir/dest')]:
|
|
|
|
src = os.path.join(TESTFN, src)
|
|
|
|
dst = os.path.join(TESTFN, dst)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(shutil._destinsrc(src, dst),
|
2009-02-07 15:08:22 -04:00
|
|
|
msg='_destinsrc() wrongly concluded that '
|
2009-01-29 16:19:34 -04:00
|
|
|
'dst (%s) is not in src (%s)' % (dst, src))
|
|
|
|
finally:
|
|
|
|
shutil.rmtree(TESTFN, ignore_errors=True)
|
2008-03-18 14:24:12 -03:00
|
|
|
|
2009-01-29 16:19:34 -04:00
|
|
|
def test_destinsrc_false_positive(self):
|
|
|
|
os.mkdir(TESTFN)
|
|
|
|
try:
|
|
|
|
for src, dst in [('srcdir', 'src/dest'), ('srcdir', 'srcdir.new')]:
|
|
|
|
src = os.path.join(TESTFN, src)
|
|
|
|
dst = os.path.join(TESTFN, dst)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertFalse(shutil._destinsrc(src, dst),
|
2009-02-07 15:08:22 -04:00
|
|
|
msg='_destinsrc() wrongly concluded that '
|
2009-01-29 16:19:34 -04:00
|
|
|
'dst (%s) is in src (%s)' % (dst, src))
|
|
|
|
finally:
|
|
|
|
shutil.rmtree(TESTFN, ignore_errors=True)
|
2008-01-20 10:17:42 -04:00
|
|
|
|
2003-01-24 13:34:13 -04:00
|
|
|
def test_main():
|
2008-03-18 14:24:12 -03:00
|
|
|
test_support.run_unittest(TestShutil, TestMove)
|
2003-01-24 13:34:13 -04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2003-05-01 14:45:56 -03:00
|
|
|
test_main()
|