| 1 |
|
| 2 |
|
| 3 |
import unittest |
| 4 |
import unittest.mock |
| 5 |
import shutil |
| 6 |
import tempfile |
| 7 |
import sys |
| 8 |
import stat |
| 9 |
import os |
| 10 |
import os.path |
| 11 |
import errno |
| 12 |
import functools |
| 13 |
import pathlib |
| 14 |
import subprocess |
| 15 |
import random |
| 16 |
import string |
| 17 |
import contextlib |
| 18 |
import io |
| 19 |
from shutil import (make_archive, |
| 20 |
register_archive_format, unregister_archive_format, |
| 21 |
get_archive_formats, Error, unpack_archive, |
| 22 |
register_unpack_format, RegistryError, |
| 23 |
unregister_unpack_format, get_unpack_formats, |
| 24 |
SameFileError, _GiveupOnFastCopy) |
| 25 |
import tarfile |
| 26 |
import zipfile |
| 27 |
try: |
| 28 |
import posix |
| 29 |
except ImportError: |
| 30 |
posix = None |
| 31 |
|
| 32 |
from test import support |
| 33 |
from test.support import os_helper |
| 34 |
from test.support.os_helper import TESTFN, FakePath |
| 35 |
from test.support import warnings_helper |
| 36 |
|
| 37 |
TESTFN2 = TESTFN + "2" |
| 38 |
TESTFN_SRC = TESTFN + "_SRC" |
| 39 |
TESTFN_DST = TESTFN + "_DST" |
| 40 |
MACOS = sys.platform.startswith("darwin") |
| 41 |
SOLARIS = sys.platform.startswith("sunos") |
| 42 |
AIX = sys.platform[:3] == 'aix' |
| 43 |
try: |
| 44 |
import grp |
| 45 |
import pwd |
| 46 |
UID_GID_SUPPORT = True |
| 47 |
except ImportError: |
| 48 |
UID_GID_SUPPORT = False |
| 49 |
|
| 50 |
try: |
| 51 |
import _winapi |
| 52 |
except ImportError: |
| 53 |
_winapi = None |
| 54 |
|
| 55 |
no_chdir = unittest.mock.patch('os.chdir', |
| 56 |
side_effect=AssertionError("shouldn't call os.chdir()")) |
| 57 |
|
| 58 |
def _fake_rename(*args, **kwargs): |
| 59 |
|
| 60 |
raise OSError(getattr(errno, 'EXDEV', 18), "Invalid cross-device link") |
| 61 |
|
| 62 |
def mock_rename(func): |
| 63 |
@functools.wraps(func) |
| 64 |
def wrap(*args, **kwargs): |
| 65 |
try: |
| 66 |
builtin_rename = os.rename |
| 67 |
os.rename = _fake_rename |
| 68 |
return func(*args, **kwargs) |
| 69 |
finally: |
| 70 |
os.rename = builtin_rename |
| 71 |
return wrap |
| 72 |
|
| 73 |
def write_file(path, content, binary=False): |
| 74 |
"""Write *content* to a file located at *path*. |
| 75 |
|
| 76 |
If *path* is a tuple instead of a string, os.path.join will be used to |
| 77 |
make a path. If *binary* is true, the file will be opened in binary |
| 78 |
mode. |
| 79 |
""" |
| 80 |
if isinstance(path, tuple): |
| 81 |
path = os.path.join(*path) |
| 82 |
mode = 'wb' if binary else 'w' |
| 83 |
encoding = None if binary else "utf-8" |
| 84 |
with open(path, mode, encoding=encoding) as fp: |
| 85 |
fp.write(content) |
| 86 |
|
| 87 |
def write_test_file(path, size): |
| 88 |
"""Create a test file with an arbitrary size and random text content.""" |
| 89 |
def chunks(total, step): |
| 90 |
assert total >= step |
| 91 |
while total > step: |
| 92 |
yield step |
| 93 |
total -= step |
| 94 |
if total: |
| 95 |
yield total |
| 96 |
|
| 97 |
bufsize = min(size, 8192) |
| 98 |
chunk = b"".join([random.choice(string.ascii_letters).encode() |
| 99 |
for i in range(bufsize)]) |
| 100 |
with open(path, 'wb') as f: |
| 101 |
for csize in chunks(size, bufsize): |
| 102 |
f.write(chunk) |
| 103 |
assert os.path.getsize(path) == size |
| 104 |
|
| 105 |
def read_file(path, binary=False): |
| 106 |
"""Return contents from a file located at *path*. |
| 107 |
|
| 108 |
If *path* is a tuple instead of a string, os.path.join will be used to |
| 109 |
make a path. If *binary* is true, the file will be opened in binary |
| 110 |
mode. |
| 111 |
""" |
| 112 |
if isinstance(path, tuple): |
| 113 |
path = os.path.join(*path) |
| 114 |
mode = 'rb' if binary else 'r' |
| 115 |
encoding = None if binary else "utf-8" |
| 116 |
with open(path, mode, encoding=encoding) as fp: |
| 117 |
return fp.read() |
| 118 |
|
| 119 |
def rlistdir(path): |
| 120 |
res = [] |
| 121 |
for name in sorted(os.listdir(path)): |
| 122 |
p = os.path.join(path, name) |
| 123 |
if os.path.isdir(p) and not os.path.islink(p): |
| 124 |
res.append(name + '/') |
| 125 |
for n in rlistdir(p): |
| 126 |
res.append(name + '/' + n) |
| 127 |
else: |
| 128 |
res.append(name) |
| 129 |
return res |
| 130 |
|
| 131 |
def supports_file2file_sendfile(): |
| 132 |
|
| 133 |
if not hasattr(os, "sendfile"): |
| 134 |
return False |
| 135 |
srcname = None |
| 136 |
dstname = None |
| 137 |
try: |
| 138 |
with tempfile.NamedTemporaryFile("wb", dir=os.getcwd(), delete=False) as f: |
| 139 |
srcname = f.name |
| 140 |
f.write(b"0123456789") |
| 141 |
|
| 142 |
with open(srcname, "rb") as src: |
| 143 |
with tempfile.NamedTemporaryFile("wb", dir=os.getcwd(), delete=False) as dst: |
| 144 |
dstname = dst.name |
| 145 |
infd = src.fileno() |
| 146 |
outfd = dst.fileno() |
| 147 |
try: |
| 148 |
os.sendfile(outfd, infd, 0, 2) |
| 149 |
except OSError: |
| 150 |
return False |
| 151 |
else: |
| 152 |
return True |
| 153 |
finally: |
| 154 |
if srcname is not None: |
| 155 |
os_helper.unlink(srcname) |
| 156 |
if dstname is not None: |
| 157 |
os_helper.unlink(dstname) |
| 158 |
|
| 159 |
|
| 160 |
SUPPORTS_SENDFILE = supports_file2file_sendfile() |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
def _maxdataOK(): |
| 167 |
if AIX and sys.maxsize == 2147483647: |
| 168 |
hdrs=subprocess.getoutput("/usr/bin/dump -o %s" % sys.executable) |
| 169 |
maxdata=hdrs.split("\n")[-1].split()[1] |
| 170 |
return int(maxdata,16) >= 0x20000000 |
| 171 |
else: |
| 172 |
return True |
| 173 |
|
| 174 |
|
| 175 |
class BaseTest: |
| 176 |
|
| 177 |
def mkdtemp(self, prefix=None): |
| 178 |
"""Create a temporary directory that will be cleaned up. |
| 179 |
|
| 180 |
Returns the path of the directory. |
| 181 |
""" |
| 182 |
d = tempfile.mkdtemp(prefix=prefix, dir=os.getcwd()) |
| 183 |
self.addCleanup(os_helper.rmtree, d) |
| 184 |
return d |
| 185 |
|
| 186 |
|
| 187 |
class TestRmTree(BaseTest, unittest.TestCase): |
| 188 |
|
| 189 |
def test_rmtree_works_on_bytes(self): |
| 190 |
tmp = self.mkdtemp() |
| 191 |
victim = os.path.join(tmp, 'killme') |
| 192 |
os.mkdir(victim) |
| 193 |
write_file(os.path.join(victim, 'somefile'), 'foo') |
| 194 |
victim = os.fsencode(victim) |
| 195 |
self.assertIsInstance(victim, bytes) |
| 196 |
shutil.rmtree(victim) |
| 197 |
|
| 198 |
@os_helper.skip_unless_symlink |
| 199 |
def test_rmtree_fails_on_symlink(self): |
| 200 |
tmp = self.mkdtemp() |
| 201 |
dir_ = os.path.join(tmp, 'dir') |
| 202 |
os.mkdir(dir_) |
| 203 |
link = os.path.join(tmp, 'link') |
| 204 |
os.symlink(dir_, link) |
| 205 |
self.assertRaises(OSError, shutil.rmtree, link) |
| 206 |
self.assertTrue(os.path.exists(dir_)) |
| 207 |
self.assertTrue(os.path.lexists(link)) |
| 208 |
errors = [] |
| 209 |
def onerror(*args): |
| 210 |
errors.append(args) |
| 211 |
shutil.rmtree(link, onerror=onerror) |
| 212 |
self.assertEqual(len(errors), 1) |
| 213 |
self.assertIs(errors[0][0], os.path.islink) |
| 214 |
self.assertEqual(errors[0][1], link) |
| 215 |
self.assertIsInstance(errors[0][2][1], OSError) |
| 216 |
|
| 217 |
@os_helper.skip_unless_symlink |
| 218 |
def test_rmtree_works_on_symlinks(self): |
| 219 |
tmp = self.mkdtemp() |
| 220 |
dir1 = os.path.join(tmp, 'dir1') |
| 221 |
dir2 = os.path.join(dir1, 'dir2') |
| 222 |
dir3 = os.path.join(tmp, 'dir3') |
| 223 |
for d in dir1, dir2, dir3: |
| 224 |
os.mkdir(d) |
| 225 |
file1 = os.path.join(tmp, 'file1') |
| 226 |
write_file(file1, 'foo') |
| 227 |
link1 = os.path.join(dir1, 'link1') |
| 228 |
os.symlink(dir2, link1) |
| 229 |
link2 = os.path.join(dir1, 'link2') |
| 230 |
os.symlink(dir3, link2) |
| 231 |
link3 = os.path.join(dir1, 'link3') |
| 232 |
os.symlink(file1, link3) |
| 233 |
|
| 234 |
shutil.rmtree(dir1) |
| 235 |
self.assertFalse(os.path.exists(dir1)) |
| 236 |
self.assertTrue(os.path.exists(dir3)) |
| 237 |
self.assertTrue(os.path.exists(file1)) |
| 238 |
|
| 239 |
@unittest.skipUnless(_winapi, 'only relevant on Windows') |
| 240 |
def test_rmtree_fails_on_junctions(self): |
| 241 |
tmp = self.mkdtemp() |
| 242 |
dir_ = os.path.join(tmp, 'dir') |
| 243 |
os.mkdir(dir_) |
| 244 |
link = os.path.join(tmp, 'link') |
| 245 |
_winapi.CreateJunction(dir_, link) |
| 246 |
self.addCleanup(os_helper.unlink, link) |
| 247 |
self.assertRaises(OSError, shutil.rmtree, link) |
| 248 |
self.assertTrue(os.path.exists(dir_)) |
| 249 |
self.assertTrue(os.path.lexists(link)) |
| 250 |
errors = [] |
| 251 |
def onerror(*args): |
| 252 |
errors.append(args) |
| 253 |
shutil.rmtree(link, onerror=onerror) |
| 254 |
self.assertEqual(len(errors), 1) |
| 255 |
self.assertIs(errors[0][0], os.path.islink) |
| 256 |
self.assertEqual(errors[0][1], link) |
| 257 |
self.assertIsInstance(errors[0][2][1], OSError) |
| 258 |
|
| 259 |
@unittest.skipUnless(_winapi, 'only relevant on Windows') |
| 260 |
def test_rmtree_works_on_junctions(self): |
| 261 |
tmp = self.mkdtemp() |
| 262 |
dir1 = os.path.join(tmp, 'dir1') |
| 263 |
dir2 = os.path.join(dir1, 'dir2') |
| 264 |
dir3 = os.path.join(tmp, 'dir3') |
| 265 |
for d in dir1, dir2, dir3: |
| 266 |
os.mkdir(d) |
| 267 |
file1 = os.path.join(tmp, 'file1') |
| 268 |
write_file(file1, 'foo') |
| 269 |
link1 = os.path.join(dir1, 'link1') |
| 270 |
_winapi.CreateJunction(dir2, link1) |
| 271 |
link2 = os.path.join(dir1, 'link2') |
| 272 |
_winapi.CreateJunction(dir3, link2) |
| 273 |
link3 = os.path.join(dir1, 'link3') |
| 274 |
_winapi.CreateJunction(file1, link3) |
| 275 |
|
| 276 |
shutil.rmtree(dir1) |
| 277 |
self.assertFalse(os.path.exists(dir1)) |
| 278 |
self.assertTrue(os.path.exists(dir3)) |
| 279 |
self.assertTrue(os.path.exists(file1)) |
| 280 |
|
| 281 |
def test_rmtree_errors(self): |
| 282 |
|
| 283 |
filename = tempfile.mktemp(dir=self.mkdtemp()) |
| 284 |
self.assertRaises(FileNotFoundError, shutil.rmtree, filename) |
| 285 |
|
| 286 |
shutil.rmtree(filename, ignore_errors=True) |
| 287 |
|
| 288 |
|
| 289 |
tmpdir = self.mkdtemp() |
| 290 |
write_file((tmpdir, "tstfile"), "") |
| 291 |
filename = os.path.join(tmpdir, "tstfile") |
| 292 |
with self.assertRaises(NotADirectoryError) as cm: |
| 293 |
shutil.rmtree(filename) |
| 294 |
self.assertEqual(cm.exception.filename, filename) |
| 295 |
self.assertTrue(os.path.exists(filename)) |
| 296 |
|
| 297 |
shutil.rmtree(filename, ignore_errors=True) |
| 298 |
self.assertTrue(os.path.exists(filename)) |
| 299 |
errors = [] |
| 300 |
def onerror(*args): |
| 301 |
errors.append(args) |
| 302 |
shutil.rmtree(filename, onerror=onerror) |
| 303 |
self.assertEqual(len(errors), 2) |
| 304 |
self.assertIs(errors[0][0], os.scandir) |
| 305 |
self.assertEqual(errors[0][1], filename) |
| 306 |
self.assertIsInstance(errors[0][2][1], NotADirectoryError) |
| 307 |
self.assertEqual(errors[0][2][1].filename, filename) |
| 308 |
self.assertIs(errors[1][0], os.rmdir) |
| 309 |
self.assertEqual(errors[1][1], filename) |
| 310 |
self.assertIsInstance(errors[1][2][1], NotADirectoryError) |
| 311 |
self.assertEqual(errors[1][2][1].filename, filename) |
| 312 |
|
| 313 |
|
| 314 |
@unittest.skipIf(sys.platform[:6] == 'cygwin', |
| 315 |
"This test can't be run on Cygwin (issue #1071513).") |
| 316 |
@os_helper.skip_if_dac_override |
| 317 |
@os_helper.skip_unless_working_chmod |
| 318 |
def test_on_error(self): |
| 319 |
self.errorState = 0 |
| 320 |
os.mkdir(TESTFN) |
| 321 |
self.addCleanup(shutil.rmtree, TESTFN) |
| 322 |
|
| 323 |
self.child_file_path = os.path.join(TESTFN, 'a') |
| 324 |
self.child_dir_path = os.path.join(TESTFN, 'b') |
| 325 |
os_helper.create_empty_file(self.child_file_path) |
| 326 |
os.mkdir(self.child_dir_path) |
| 327 |
old_dir_mode = os.stat(TESTFN).st_mode |
| 328 |
old_child_file_mode = os.stat(self.child_file_path).st_mode |
| 329 |
old_child_dir_mode = os.stat(self.child_dir_path).st_mode |
| 330 |
|
| 331 |
new_mode = stat.S_IREAD|stat.S_IEXEC |
| 332 |
os.chmod(self.child_file_path, new_mode) |
| 333 |
os.chmod(self.child_dir_path, new_mode) |
| 334 |
os.chmod(TESTFN, new_mode) |
| 335 |
|
| 336 |
self.addCleanup(os.chmod, TESTFN, old_dir_mode) |
| 337 |
self.addCleanup(os.chmod, self.child_file_path, old_child_file_mode) |
| 338 |
self.addCleanup(os.chmod, self.child_dir_path, old_child_dir_mode) |
| 339 |
|
| 340 |
shutil.rmtree(TESTFN, onerror=self.check_args_to_onerror) |
| 341 |
|
| 342 |
self.assertEqual(self.errorState, 3, |
| 343 |
"Expected call to onerror function did not happen.") |
| 344 |
|
| 345 |
def check_args_to_onerror(self, func, arg, exc): |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
if self.errorState < 2: |
| 357 |
if func is os.unlink: |
| 358 |
self.assertEqual(arg, self.child_file_path) |
| 359 |
elif func is os.rmdir: |
| 360 |
self.assertEqual(arg, self.child_dir_path) |
| 361 |
else: |
| 362 |
self.assertIs(func, os.listdir) |
| 363 |
self.assertIn(arg, [TESTFN, self.child_dir_path]) |
| 364 |
self.assertTrue(issubclass(exc[0], OSError)) |
| 365 |
self.errorState += 1 |
| 366 |
else: |
| 367 |
self.assertEqual(func, os.rmdir) |
| 368 |
self.assertEqual(arg, TESTFN) |
| 369 |
self.assertTrue(issubclass(exc[0], OSError)) |
| 370 |
self.errorState = 3 |
| 371 |
|
| 372 |
def test_rmtree_does_not_choke_on_failing_lstat(self): |
| 373 |
try: |
| 374 |
orig_lstat = os.lstat |
| 375 |
def raiser(fn, *args, **kwargs): |
| 376 |
if fn != TESTFN: |
| 377 |
raise OSError() |
| 378 |
else: |
| 379 |
return orig_lstat(fn) |
| 380 |
os.lstat = raiser |
| 381 |
|
| 382 |
os.mkdir(TESTFN) |
| 383 |
write_file((TESTFN, 'foo'), 'foo') |
| 384 |
shutil.rmtree(TESTFN) |
| 385 |
finally: |
| 386 |
os.lstat = orig_lstat |
| 387 |
|
| 388 |
def test_rmtree_uses_safe_fd_version_if_available(self): |
| 389 |
_use_fd_functions = ({os.open, os.stat, os.unlink, os.rmdir} <= |
| 390 |
os.supports_dir_fd and |
| 391 |
os.listdir in os.supports_fd and |
| 392 |
os.stat in os.supports_follow_symlinks) |
| 393 |
if _use_fd_functions: |
| 394 |
self.assertTrue(shutil._use_fd_functions) |
| 395 |
self.assertTrue(shutil.rmtree.avoids_symlink_attacks) |
| 396 |
tmp_dir = self.mkdtemp() |
| 397 |
d = os.path.join(tmp_dir, 'a') |
| 398 |
os.mkdir(d) |
| 399 |
try: |
| 400 |
real_rmtree = shutil._rmtree_safe_fd |
| 401 |
class Called(Exception): pass |
| 402 |
def _raiser(*args, **kwargs): |
| 403 |
raise Called |
| 404 |
shutil._rmtree_safe_fd = _raiser |
| 405 |
self.assertRaises(Called, shutil.rmtree, d) |
| 406 |
finally: |
| 407 |
shutil._rmtree_safe_fd = real_rmtree |
| 408 |
else: |
| 409 |
self.assertFalse(shutil._use_fd_functions) |
| 410 |
self.assertFalse(shutil.rmtree.avoids_symlink_attacks) |
| 411 |
|
| 412 |
@unittest.skipUnless(shutil._use_fd_functions, "requires safe rmtree") |
| 413 |
def test_rmtree_fails_on_close(self): |
| 414 |
|
| 415 |
|
| 416 |
tmp = self.mkdtemp() |
| 417 |
dir1 = os.path.join(tmp, 'dir1') |
| 418 |
os.mkdir(dir1) |
| 419 |
dir2 = os.path.join(dir1, 'dir2') |
| 420 |
os.mkdir(dir2) |
| 421 |
def close(fd): |
| 422 |
orig_close(fd) |
| 423 |
nonlocal close_count |
| 424 |
close_count += 1 |
| 425 |
raise OSError |
| 426 |
|
| 427 |
close_count = 0 |
| 428 |
with support.swap_attr(os, 'close', close) as orig_close: |
| 429 |
with self.assertRaises(OSError): |
| 430 |
shutil.rmtree(dir1) |
| 431 |
self.assertTrue(os.path.isdir(dir2)) |
| 432 |
self.assertEqual(close_count, 2) |
| 433 |
|
| 434 |
close_count = 0 |
| 435 |
errors = [] |
| 436 |
def onerror(*args): |
| 437 |
errors.append(args) |
| 438 |
with support.swap_attr(os, 'close', close) as orig_close: |
| 439 |
shutil.rmtree(dir1, onerror=onerror) |
| 440 |
self.assertEqual(len(errors), 2) |
| 441 |
self.assertIs(errors[0][0], close) |
| 442 |
self.assertEqual(errors[0][1], dir2) |
| 443 |
self.assertIs(errors[1][0], close) |
| 444 |
self.assertEqual(errors[1][1], dir1) |
| 445 |
self.assertEqual(close_count, 2) |
| 446 |
|
| 447 |
@unittest.skipUnless(shutil._use_fd_functions, "dir_fd is not supported") |
| 448 |
def test_rmtree_with_dir_fd(self): |
| 449 |
tmp_dir = self.mkdtemp() |
| 450 |
victim = 'killme' |
| 451 |
fullname = os.path.join(tmp_dir, victim) |
| 452 |
dir_fd = os.open(tmp_dir, os.O_RDONLY) |
| 453 |
self.addCleanup(os.close, dir_fd) |
| 454 |
os.mkdir(fullname) |
| 455 |
os.mkdir(os.path.join(fullname, 'subdir')) |
| 456 |
write_file(os.path.join(fullname, 'subdir', 'somefile'), 'foo') |
| 457 |
self.assertTrue(os.path.exists(fullname)) |
| 458 |
shutil.rmtree(victim, dir_fd=dir_fd) |
| 459 |
self.assertFalse(os.path.exists(fullname)) |
| 460 |
|
| 461 |
@unittest.skipIf(shutil._use_fd_functions, "dir_fd is supported") |
| 462 |
def test_rmtree_with_dir_fd_unsupported(self): |
| 463 |
tmp_dir = self.mkdtemp() |
| 464 |
with self.assertRaises(NotImplementedError): |
| 465 |
shutil.rmtree(tmp_dir, dir_fd=0) |
| 466 |
self.assertTrue(os.path.exists(tmp_dir)) |
| 467 |
|
| 468 |
def test_rmtree_dont_delete_file(self): |
| 469 |
|
| 470 |
handle, path = tempfile.mkstemp(dir=self.mkdtemp()) |
| 471 |
os.close(handle) |
| 472 |
self.assertRaises(NotADirectoryError, shutil.rmtree, path) |
| 473 |
os.remove(path) |
| 474 |
|
| 475 |
@os_helper.skip_unless_symlink |
| 476 |
def test_rmtree_on_symlink(self): |
| 477 |
|
| 478 |
os.mkdir(TESTFN) |
| 479 |
try: |
| 480 |
src = os.path.join(TESTFN, 'cheese') |
| 481 |
dst = os.path.join(TESTFN, 'shop') |
| 482 |
os.mkdir(src) |
| 483 |
os.symlink(src, dst) |
| 484 |
self.assertRaises(OSError, shutil.rmtree, dst) |
| 485 |
shutil.rmtree(dst, ignore_errors=True) |
| 486 |
finally: |
| 487 |
shutil.rmtree(TESTFN, ignore_errors=True) |
| 488 |
|
| 489 |
@unittest.skipUnless(_winapi, 'only relevant on Windows') |
| 490 |
def test_rmtree_on_junction(self): |
| 491 |
os.mkdir(TESTFN) |
| 492 |
try: |
| 493 |
src = os.path.join(TESTFN, 'cheese') |
| 494 |
dst = os.path.join(TESTFN, 'shop') |
| 495 |
os.mkdir(src) |
| 496 |
open(os.path.join(src, 'spam'), 'wb').close() |
| 497 |
_winapi.CreateJunction(src, dst) |
| 498 |
self.assertRaises(OSError, shutil.rmtree, dst) |
| 499 |
shutil.rmtree(dst, ignore_errors=True) |
| 500 |
finally: |
| 501 |
shutil.rmtree(TESTFN, ignore_errors=True) |
| 502 |
|
| 503 |
@unittest.skipUnless(hasattr(os, "mkfifo"), 'requires os.mkfifo()') |
| 504 |
@unittest.skipIf(sys.platform == "vxworks", |
| 505 |
"fifo requires special path on VxWorks") |
| 506 |
def test_rmtree_on_named_pipe(self): |
| 507 |
os.mkfifo(TESTFN) |
| 508 |
try: |
| 509 |
with self.assertRaises(NotADirectoryError): |
| 510 |
shutil.rmtree(TESTFN) |
| 511 |
self.assertTrue(os.path.exists(TESTFN)) |
| 512 |
finally: |
| 513 |
os.unlink(TESTFN) |
| 514 |
|
| 515 |
os.mkdir(TESTFN) |
| 516 |
os.mkfifo(os.path.join(TESTFN, 'mypipe')) |
| 517 |
shutil.rmtree(TESTFN) |
| 518 |
self.assertFalse(os.path.exists(TESTFN)) |
| 519 |
|
| 520 |
|
| 521 |
class TestCopyTree(BaseTest, unittest.TestCase): |
| 522 |
|
| 523 |
def test_copytree_simple(self): |
| 524 |
src_dir = self.mkdtemp() |
| 525 |
dst_dir = os.path.join(self.mkdtemp(), 'destination') |
| 526 |
self.addCleanup(shutil.rmtree, src_dir) |
| 527 |
self.addCleanup(shutil.rmtree, os.path.dirname(dst_dir)) |
| 528 |
write_file((src_dir, 'test.txt'), '123') |
| 529 |
os.mkdir(os.path.join(src_dir, 'test_dir')) |
| 530 |
write_file((src_dir, 'test_dir', 'test.txt'), '456') |
| 531 |
|
| 532 |
shutil.copytree(src_dir, dst_dir) |
| 533 |
self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'test.txt'))) |
| 534 |
self.assertTrue(os.path.isdir(os.path.join(dst_dir, 'test_dir'))) |
| 535 |
self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'test_dir', |
| 536 |
'test.txt'))) |
| 537 |
actual = read_file((dst_dir, 'test.txt')) |
| 538 |
self.assertEqual(actual, '123') |
| 539 |
actual = read_file((dst_dir, 'test_dir', 'test.txt')) |
| 540 |
self.assertEqual(actual, '456') |
| 541 |
|
| 542 |
def test_copytree_dirs_exist_ok(self): |
| 543 |
src_dir = self.mkdtemp() |
| 544 |
dst_dir = self.mkdtemp() |
| 545 |
self.addCleanup(shutil.rmtree, src_dir) |
| 546 |
self.addCleanup(shutil.rmtree, dst_dir) |
| 547 |
|
| 548 |
write_file((src_dir, 'nonexisting.txt'), '123') |
| 549 |
os.mkdir(os.path.join(src_dir, 'existing_dir')) |
| 550 |
os.mkdir(os.path.join(dst_dir, 'existing_dir')) |
| 551 |
write_file((dst_dir, 'existing_dir', 'existing.txt'), 'will be replaced') |
| 552 |
write_file((src_dir, 'existing_dir', 'existing.txt'), 'has been replaced') |
| 553 |
|
| 554 |
shutil.copytree(src_dir, dst_dir, dirs_exist_ok=True) |
| 555 |
self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'nonexisting.txt'))) |
| 556 |
self.assertTrue(os.path.isdir(os.path.join(dst_dir, 'existing_dir'))) |
| 557 |
self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'existing_dir', |
| 558 |
'existing.txt'))) |
| 559 |
actual = read_file((dst_dir, 'nonexisting.txt')) |
| 560 |
self.assertEqual(actual, '123') |
| 561 |
actual = read_file((dst_dir, 'existing_dir', 'existing.txt')) |
| 562 |
self.assertEqual(actual, 'has been replaced') |
| 563 |
|
| 564 |
with self.assertRaises(FileExistsError): |
| 565 |
shutil.copytree(src_dir, dst_dir, dirs_exist_ok=False) |
| 566 |
|
| 567 |
@os_helper.skip_unless_symlink |
| 568 |
def test_copytree_symlinks(self): |
| 569 |
tmp_dir = self.mkdtemp() |
| 570 |
src_dir = os.path.join(tmp_dir, 'src') |
| 571 |
dst_dir = os.path.join(tmp_dir, 'dst') |
| 572 |
sub_dir = os.path.join(src_dir, 'sub') |
| 573 |
os.mkdir(src_dir) |
| 574 |
os.mkdir(sub_dir) |
| 575 |
write_file((src_dir, 'file.txt'), 'foo') |
| 576 |
src_link = os.path.join(sub_dir, 'link') |
| 577 |
dst_link = os.path.join(dst_dir, 'sub/link') |
| 578 |
os.symlink(os.path.join(src_dir, 'file.txt'), |
| 579 |
src_link) |
| 580 |
if hasattr(os, 'lchmod'): |
| 581 |
os.lchmod(src_link, stat.S_IRWXU | stat.S_IRWXO) |
| 582 |
if hasattr(os, 'lchflags') and hasattr(stat, 'UF_NODUMP'): |
| 583 |
os.lchflags(src_link, stat.UF_NODUMP) |
| 584 |
src_stat = os.lstat(src_link) |
| 585 |
shutil.copytree(src_dir, dst_dir, symlinks=True) |
| 586 |
self.assertTrue(os.path.islink(os.path.join(dst_dir, 'sub', 'link'))) |
| 587 |
actual = os.readlink(os.path.join(dst_dir, 'sub', 'link')) |
| 588 |
|
| 589 |
|
| 590 |
if os.name == 'nt' and actual.startswith('\\\\?\\'): |
| 591 |
actual = actual[4:] |
| 592 |
self.assertEqual(actual, os.path.join(src_dir, 'file.txt')) |
| 593 |
dst_stat = os.lstat(dst_link) |
| 594 |
if hasattr(os, 'lchmod'): |
| 595 |
self.assertEqual(dst_stat.st_mode, src_stat.st_mode) |
| 596 |
if hasattr(os, 'lchflags'): |
| 597 |
self.assertEqual(dst_stat.st_flags, src_stat.st_flags) |
| 598 |
|
| 599 |
def test_copytree_with_exclude(self): |
| 600 |
|
| 601 |
join = os.path.join |
| 602 |
exists = os.path.exists |
| 603 |
src_dir = self.mkdtemp() |
| 604 |
try: |
| 605 |
dst_dir = join(self.mkdtemp(), 'destination') |
| 606 |
write_file((src_dir, 'test.txt'), '123') |
| 607 |
write_file((src_dir, 'test.tmp'), '123') |
| 608 |
os.mkdir(join(src_dir, 'test_dir')) |
| 609 |
write_file((src_dir, 'test_dir', 'test.txt'), '456') |
| 610 |
os.mkdir(join(src_dir, 'test_dir2')) |
| 611 |
write_file((src_dir, 'test_dir2', 'test.txt'), '456') |
| 612 |
os.mkdir(join(src_dir, 'test_dir2', 'subdir')) |
| 613 |
os.mkdir(join(src_dir, 'test_dir2', 'subdir2')) |
| 614 |
write_file((src_dir, 'test_dir2', 'subdir', 'test.txt'), '456') |
| 615 |
write_file((src_dir, 'test_dir2', 'subdir2', 'test.py'), '456') |
| 616 |
|
| 617 |
|
| 618 |
try: |
| 619 |
patterns = shutil.ignore_patterns('*.tmp', 'test_dir2') |
| 620 |
shutil.copytree(src_dir, dst_dir, ignore=patterns) |
| 621 |
|
| 622 |
self.assertTrue(exists(join(dst_dir, 'test.txt'))) |
| 623 |
self.assertFalse(exists(join(dst_dir, 'test.tmp'))) |
| 624 |
self.assertFalse(exists(join(dst_dir, 'test_dir2'))) |
| 625 |
finally: |
| 626 |
shutil.rmtree(dst_dir) |
| 627 |
try: |
| 628 |
patterns = shutil.ignore_patterns('*.tmp', 'subdir*') |
| 629 |
shutil.copytree(src_dir, dst_dir, ignore=patterns) |
| 630 |
|
| 631 |
self.assertFalse(exists(join(dst_dir, 'test.tmp'))) |
| 632 |
self.assertFalse(exists(join(dst_dir, 'test_dir2', 'subdir2'))) |
| 633 |
self.assertFalse(exists(join(dst_dir, 'test_dir2', 'subdir'))) |
| 634 |
finally: |
| 635 |
shutil.rmtree(dst_dir) |
| 636 |
|
| 637 |
|
| 638 |
try: |
| 639 |
def _filter(src, names): |
| 640 |
res = [] |
| 641 |
for name in names: |
| 642 |
path = os.path.join(src, name) |
| 643 |
|
| 644 |
if (os.path.isdir(path) and |
| 645 |
path.split()[-1] == 'subdir'): |
| 646 |
res.append(name) |
| 647 |
elif os.path.splitext(path)[-1] in ('.py'): |
| 648 |
res.append(name) |
| 649 |
return res |
| 650 |
|
| 651 |
shutil.copytree(src_dir, dst_dir, ignore=_filter) |
| 652 |
|
| 653 |
|
| 654 |
self.assertFalse(exists(join(dst_dir, 'test_dir2', 'subdir2', |
| 655 |
'test.py'))) |
| 656 |
self.assertFalse(exists(join(dst_dir, 'test_dir2', 'subdir'))) |
| 657 |
|
| 658 |
finally: |
| 659 |
shutil.rmtree(dst_dir) |
| 660 |
finally: |
| 661 |
shutil.rmtree(src_dir) |
| 662 |
shutil.rmtree(os.path.dirname(dst_dir)) |
| 663 |
|
| 664 |
def test_copytree_arg_types_of_ignore(self): |
| 665 |
join = os.path.join |
| 666 |
exists = os.path.exists |
| 667 |
|
| 668 |
tmp_dir = self.mkdtemp() |
| 669 |
src_dir = join(tmp_dir, "source") |
| 670 |
|
| 671 |
os.mkdir(join(src_dir)) |
| 672 |
os.mkdir(join(src_dir, 'test_dir')) |
| 673 |
os.mkdir(os.path.join(src_dir, 'test_dir', 'subdir')) |
| 674 |
write_file((src_dir, 'test_dir', 'subdir', 'test.txt'), '456') |
| 675 |
|
| 676 |
invokations = [] |
| 677 |
|
| 678 |
def _ignore(src, names): |
| 679 |
invokations.append(src) |
| 680 |
self.assertIsInstance(src, str) |
| 681 |
self.assertIsInstance(names, list) |
| 682 |
self.assertEqual(len(names), len(set(names))) |
| 683 |
for name in names: |
| 684 |
self.assertIsInstance(name, str) |
| 685 |
return [] |
| 686 |
|
| 687 |
dst_dir = join(self.mkdtemp(), 'destination') |
| 688 |
shutil.copytree(src_dir, dst_dir, ignore=_ignore) |
| 689 |
self.assertTrue(exists(join(dst_dir, 'test_dir', 'subdir', |
| 690 |
'test.txt'))) |
| 691 |
|
| 692 |
dst_dir = join(self.mkdtemp(), 'destination') |
| 693 |
shutil.copytree(pathlib.Path(src_dir), dst_dir, ignore=_ignore) |
| 694 |
self.assertTrue(exists(join(dst_dir, 'test_dir', 'subdir', |
| 695 |
'test.txt'))) |
| 696 |
|
| 697 |
dst_dir = join(self.mkdtemp(), 'destination') |
| 698 |
src_dir_entry = list(os.scandir(tmp_dir))[0] |
| 699 |
self.assertIsInstance(src_dir_entry, os.DirEntry) |
| 700 |
shutil.copytree(src_dir_entry, dst_dir, ignore=_ignore) |
| 701 |
self.assertTrue(exists(join(dst_dir, 'test_dir', 'subdir', |
| 702 |
'test.txt'))) |
| 703 |
|
| 704 |
self.assertEqual(len(invokations), 9) |
| 705 |
|
| 706 |
def test_copytree_retains_permissions(self): |
| 707 |
tmp_dir = self.mkdtemp() |
| 708 |
src_dir = os.path.join(tmp_dir, 'source') |
| 709 |
os.mkdir(src_dir) |
| 710 |
dst_dir = os.path.join(tmp_dir, 'destination') |
| 711 |
self.addCleanup(shutil.rmtree, tmp_dir) |
| 712 |
|
| 713 |
os.chmod(src_dir, 0o777) |
| 714 |
write_file((src_dir, 'permissive.txt'), '123') |
| 715 |
os.chmod(os.path.join(src_dir, 'permissive.txt'), 0o777) |
| 716 |
write_file((src_dir, 'restrictive.txt'), '456') |
| 717 |
os.chmod(os.path.join(src_dir, 'restrictive.txt'), 0o600) |
| 718 |
restrictive_subdir = tempfile.mkdtemp(dir=src_dir) |
| 719 |
self.addCleanup(os_helper.rmtree, restrictive_subdir) |
| 720 |
os.chmod(restrictive_subdir, 0o600) |
| 721 |
|
| 722 |
shutil.copytree(src_dir, dst_dir) |
| 723 |
self.assertEqual(os.stat(src_dir).st_mode, os.stat(dst_dir).st_mode) |
| 724 |
self.assertEqual(os.stat(os.path.join(src_dir, 'permissive.txt')).st_mode, |
| 725 |
os.stat(os.path.join(dst_dir, 'permissive.txt')).st_mode) |
| 726 |
self.assertEqual(os.stat(os.path.join(src_dir, 'restrictive.txt')).st_mode, |
| 727 |
os.stat(os.path.join(dst_dir, 'restrictive.txt')).st_mode) |
| 728 |
restrictive_subdir_dst = os.path.join(dst_dir, |
| 729 |
os.path.split(restrictive_subdir)[1]) |
| 730 |
self.assertEqual(os.stat(restrictive_subdir).st_mode, |
| 731 |
os.stat(restrictive_subdir_dst).st_mode) |
| 732 |
|
| 733 |
@unittest.mock.patch('os.chmod') |
| 734 |
def test_copytree_winerror(self, mock_patch): |
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
src_dir = self.mkdtemp() |
| 739 |
dst_dir = os.path.join(self.mkdtemp(), 'destination') |
| 740 |
self.addCleanup(shutil.rmtree, src_dir) |
| 741 |
self.addCleanup(shutil.rmtree, os.path.dirname(dst_dir)) |
| 742 |
|
| 743 |
mock_patch.side_effect = PermissionError('ka-boom') |
| 744 |
with self.assertRaises(shutil.Error): |
| 745 |
shutil.copytree(src_dir, dst_dir) |
| 746 |
|
| 747 |
def test_copytree_custom_copy_function(self): |
| 748 |
|
| 749 |
def custom_cpfun(a, b): |
| 750 |
flag.append(None) |
| 751 |
self.assertIsInstance(a, str) |
| 752 |
self.assertIsInstance(b, str) |
| 753 |
self.assertEqual(a, os.path.join(src, 'foo')) |
| 754 |
self.assertEqual(b, os.path.join(dst, 'foo')) |
| 755 |
|
| 756 |
flag = [] |
| 757 |
src = self.mkdtemp() |
| 758 |
dst = tempfile.mktemp(dir=self.mkdtemp()) |
| 759 |
with open(os.path.join(src, 'foo'), 'w', encoding='utf-8') as f: |
| 760 |
f.close() |
| 761 |
shutil.copytree(src, dst, copy_function=custom_cpfun) |
| 762 |
self.assertEqual(len(flag), 1) |
| 763 |
|
| 764 |
|
| 765 |
@unittest.skipUnless(hasattr(os, "mkfifo"), 'requires os.mkfifo()') |
| 766 |
@os_helper.skip_unless_symlink |
| 767 |
@unittest.skipIf(sys.platform == "vxworks", |
| 768 |
"fifo requires special path on VxWorks") |
| 769 |
def test_copytree_named_pipe(self): |
| 770 |
os.mkdir(TESTFN) |
| 771 |
try: |
| 772 |
subdir = os.path.join(TESTFN, "subdir") |
| 773 |
os.mkdir(subdir) |
| 774 |
pipe = os.path.join(subdir, "mypipe") |
| 775 |
try: |
| 776 |
os.mkfifo(pipe) |
| 777 |
except PermissionError as e: |
| 778 |
self.skipTest('os.mkfifo(): %s' % e) |
| 779 |
try: |
| 780 |
shutil.copytree(TESTFN, TESTFN2) |
| 781 |
except shutil.Error as e: |
| 782 |
errors = e.args[0] |
| 783 |
self.assertEqual(len(errors), 1) |
| 784 |
src, dst, error_msg = errors[0] |
| 785 |
self.assertEqual("`%s` is a named pipe" % pipe, error_msg) |
| 786 |
else: |
| 787 |
self.fail("shutil.Error should have been raised") |
| 788 |
finally: |
| 789 |
shutil.rmtree(TESTFN, ignore_errors=True) |
| 790 |
shutil.rmtree(TESTFN2, ignore_errors=True) |
| 791 |
|
| 792 |
def test_copytree_special_func(self): |
| 793 |
src_dir = self.mkdtemp() |
| 794 |
dst_dir = os.path.join(self.mkdtemp(), 'destination') |
| 795 |
write_file((src_dir, 'test.txt'), '123') |
| 796 |
os.mkdir(os.path.join(src_dir, 'test_dir')) |
| 797 |
write_file((src_dir, 'test_dir', 'test.txt'), '456') |
| 798 |
|
| 799 |
copied = [] |
| 800 |
def _copy(src, dst): |
| 801 |
copied.append((src, dst)) |
| 802 |
|
| 803 |
shutil.copytree(src_dir, dst_dir, copy_function=_copy) |
| 804 |
self.assertEqual(len(copied), 2) |
| 805 |
|
| 806 |
@os_helper.skip_unless_symlink |
| 807 |
def test_copytree_dangling_symlinks(self): |
| 808 |
src_dir = self.mkdtemp() |
| 809 |
valid_file = os.path.join(src_dir, 'test.txt') |
| 810 |
write_file(valid_file, 'abc') |
| 811 |
dir_a = os.path.join(src_dir, 'dir_a') |
| 812 |
os.mkdir(dir_a) |
| 813 |
for d in src_dir, dir_a: |
| 814 |
os.symlink('IDONTEXIST', os.path.join(d, 'broken')) |
| 815 |
os.symlink(valid_file, os.path.join(d, 'valid')) |
| 816 |
|
| 817 |
|
| 818 |
dst_dir = os.path.join(self.mkdtemp(), 'destination') |
| 819 |
self.assertRaises(Error, shutil.copytree, src_dir, dst_dir) |
| 820 |
|
| 821 |
|
| 822 |
dst_dir = os.path.join(self.mkdtemp(), 'destination2') |
| 823 |
shutil.copytree(src_dir, dst_dir, ignore_dangling_symlinks=True) |
| 824 |
for root, dirs, files in os.walk(dst_dir): |
| 825 |
self.assertNotIn('broken', files) |
| 826 |
self.assertIn('valid', files) |
| 827 |
|
| 828 |
|
| 829 |
dst_dir = os.path.join(self.mkdtemp(), 'destination3') |
| 830 |
shutil.copytree(src_dir, dst_dir, symlinks=True) |
| 831 |
self.assertIn('test.txt', os.listdir(dst_dir)) |
| 832 |
|
| 833 |
@os_helper.skip_unless_symlink |
| 834 |
def test_copytree_symlink_dir(self): |
| 835 |
src_dir = self.mkdtemp() |
| 836 |
dst_dir = os.path.join(self.mkdtemp(), 'destination') |
| 837 |
os.mkdir(os.path.join(src_dir, 'real_dir')) |
| 838 |
with open(os.path.join(src_dir, 'real_dir', 'test.txt'), 'wb'): |
| 839 |
pass |
| 840 |
os.symlink(os.path.join(src_dir, 'real_dir'), |
| 841 |
os.path.join(src_dir, 'link_to_dir'), |
| 842 |
target_is_directory=True) |
| 843 |
|
| 844 |
shutil.copytree(src_dir, dst_dir, symlinks=False) |
| 845 |
self.assertFalse(os.path.islink(os.path.join(dst_dir, 'link_to_dir'))) |
| 846 |
self.assertIn('test.txt', os.listdir(os.path.join(dst_dir, 'link_to_dir'))) |
| 847 |
|
| 848 |
dst_dir = os.path.join(self.mkdtemp(), 'destination2') |
| 849 |
shutil.copytree(src_dir, dst_dir, symlinks=True) |
| 850 |
self.assertTrue(os.path.islink(os.path.join(dst_dir, 'link_to_dir'))) |
| 851 |
self.assertIn('test.txt', os.listdir(os.path.join(dst_dir, 'link_to_dir'))) |
| 852 |
|
| 853 |
def test_copytree_return_value(self): |
| 854 |
|
| 855 |
src_dir = self.mkdtemp() |
| 856 |
dst_dir = src_dir + "dest" |
| 857 |
self.addCleanup(shutil.rmtree, dst_dir, True) |
| 858 |
src = os.path.join(src_dir, 'foo') |
| 859 |
write_file(src, 'foo') |
| 860 |
rv = shutil.copytree(src_dir, dst_dir) |
| 861 |
self.assertEqual(['foo'], os.listdir(rv)) |
| 862 |
|
| 863 |
def test_copytree_subdirectory(self): |
| 864 |
|
| 865 |
base_dir = self.mkdtemp() |
| 866 |
self.addCleanup(shutil.rmtree, base_dir, ignore_errors=True) |
| 867 |
src_dir = os.path.join(base_dir, "t", "pg") |
| 868 |
dst_dir = os.path.join(src_dir, "somevendor", "1.0") |
| 869 |
os.makedirs(src_dir) |
| 870 |
src = os.path.join(src_dir, 'pol') |
| 871 |
write_file(src, 'pol') |
| 872 |
rv = shutil.copytree(src_dir, dst_dir) |
| 873 |
self.assertEqual(['pol'], os.listdir(rv)) |
| 874 |
|
| 875 |
class TestCopy(BaseTest, unittest.TestCase): |
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
@os_helper.skip_unless_symlink |
| 880 |
def test_copymode_follow_symlinks(self): |
| 881 |
tmp_dir = self.mkdtemp() |
| 882 |
src = os.path.join(tmp_dir, 'foo') |
| 883 |
dst = os.path.join(tmp_dir, 'bar') |
| 884 |
src_link = os.path.join(tmp_dir, 'baz') |
| 885 |
dst_link = os.path.join(tmp_dir, 'quux') |
| 886 |
write_file(src, 'foo') |
| 887 |
write_file(dst, 'foo') |
| 888 |
os.symlink(src, src_link) |
| 889 |
os.symlink(dst, dst_link) |
| 890 |
os.chmod(src, stat.S_IRWXU|stat.S_IRWXG) |
| 891 |
|
| 892 |
os.chmod(dst, stat.S_IRWXO) |
| 893 |
self.assertNotEqual(os.stat(src).st_mode, os.stat(dst).st_mode) |
| 894 |
shutil.copymode(src, dst) |
| 895 |
self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode) |
| 896 |
|
| 897 |
|
| 898 |
os.chmod(dst, stat.S_IRWXO) |
| 899 |
shutil.copymode(src_link, dst) |
| 900 |
self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode) |
| 901 |
|
| 902 |
os.chmod(dst, stat.S_IRWXO) |
| 903 |
shutil.copymode(src, dst_link) |
| 904 |
self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode) |
| 905 |
|
| 906 |
os.chmod(dst, stat.S_IRWXO) |
| 907 |
shutil.copymode(src_link, dst_link) |
| 908 |
self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode) |
| 909 |
|
| 910 |
@unittest.skipUnless(hasattr(os, 'lchmod') or os.name == 'nt', 'requires os.lchmod') |
| 911 |
@os_helper.skip_unless_symlink |
| 912 |
def test_copymode_symlink_to_symlink(self): |
| 913 |
_lchmod = os.chmod if os.name == 'nt' else os.lchmod |
| 914 |
tmp_dir = self.mkdtemp() |
| 915 |
src = os.path.join(tmp_dir, 'foo') |
| 916 |
dst = os.path.join(tmp_dir, 'bar') |
| 917 |
src_link = os.path.join(tmp_dir, 'baz') |
| 918 |
dst_link = os.path.join(tmp_dir, 'quux') |
| 919 |
write_file(src, 'foo') |
| 920 |
write_file(dst, 'foo') |
| 921 |
os.symlink(src, src_link) |
| 922 |
os.symlink(dst, dst_link) |
| 923 |
os.chmod(src, stat.S_IRWXU|stat.S_IRWXG) |
| 924 |
os.chmod(dst, stat.S_IRWXU) |
| 925 |
_lchmod(src_link, stat.S_IRWXO|stat.S_IRWXG) |
| 926 |
|
| 927 |
_lchmod(dst_link, stat.S_IRWXO) |
| 928 |
old_mode = os.stat(dst).st_mode |
| 929 |
shutil.copymode(src_link, dst_link, follow_symlinks=False) |
| 930 |
self.assertEqual(os.lstat(src_link).st_mode, |
| 931 |
os.lstat(dst_link).st_mode) |
| 932 |
self.assertEqual(os.stat(dst).st_mode, old_mode) |
| 933 |
|
| 934 |
_lchmod(dst_link, stat.S_IRWXO) |
| 935 |
shutil.copymode(src_link, dst, follow_symlinks=False) |
| 936 |
self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode) |
| 937 |
|
| 938 |
_lchmod(dst_link, stat.S_IRWXO) |
| 939 |
shutil.copymode(src, dst_link, follow_symlinks=False) |
| 940 |
self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode) |
| 941 |
|
| 942 |
@unittest.skipIf(hasattr(os, 'lchmod'), 'requires os.lchmod to be missing') |
| 943 |
@os_helper.skip_unless_symlink |
| 944 |
def test_copymode_symlink_to_symlink_wo_lchmod(self): |
| 945 |
tmp_dir = self.mkdtemp() |
| 946 |
src = os.path.join(tmp_dir, 'foo') |
| 947 |
dst = os.path.join(tmp_dir, 'bar') |
| 948 |
src_link = os.path.join(tmp_dir, 'baz') |
| 949 |
dst_link = os.path.join(tmp_dir, 'quux') |
| 950 |
write_file(src, 'foo') |
| 951 |
write_file(dst, 'foo') |
| 952 |
os.symlink(src, src_link) |
| 953 |
os.symlink(dst, dst_link) |
| 954 |
shutil.copymode(src_link, dst_link, follow_symlinks=False) |
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
@os_helper.skip_unless_symlink |
| 959 |
def test_copystat_symlinks(self): |
| 960 |
tmp_dir = self.mkdtemp() |
| 961 |
src = os.path.join(tmp_dir, 'foo') |
| 962 |
dst = os.path.join(tmp_dir, 'bar') |
| 963 |
src_link = os.path.join(tmp_dir, 'baz') |
| 964 |
dst_link = os.path.join(tmp_dir, 'qux') |
| 965 |
write_file(src, 'foo') |
| 966 |
src_stat = os.stat(src) |
| 967 |
os.utime(src, (src_stat.st_atime, |
| 968 |
src_stat.st_mtime - 42.0)) |
| 969 |
write_file(dst, 'bar') |
| 970 |
self.assertNotEqual(os.stat(src).st_mtime, os.stat(dst).st_mtime) |
| 971 |
os.symlink(src, src_link) |
| 972 |
os.symlink(dst, dst_link) |
| 973 |
if hasattr(os, 'lchmod'): |
| 974 |
os.lchmod(src_link, stat.S_IRWXO) |
| 975 |
elif os.name == 'nt': |
| 976 |
os.chmod(src_link, stat.S_IRWXO) |
| 977 |
if hasattr(os, 'lchflags') and hasattr(stat, 'UF_NODUMP'): |
| 978 |
os.lchflags(src_link, stat.UF_NODUMP) |
| 979 |
src_link_stat = os.lstat(src_link) |
| 980 |
|
| 981 |
if hasattr(os, 'lchmod') or os.name == 'nt': |
| 982 |
shutil.copystat(src_link, dst_link, follow_symlinks=True) |
| 983 |
self.assertNotEqual(src_link_stat.st_mode, os.stat(dst).st_mode) |
| 984 |
|
| 985 |
shutil.copystat(src_link, dst_link, follow_symlinks=False) |
| 986 |
dst_link_stat = os.lstat(dst_link) |
| 987 |
if os.utime in os.supports_follow_symlinks: |
| 988 |
for attr in 'st_atime', 'st_mtime': |
| 989 |
|
| 990 |
self.assertLessEqual(getattr(src_link_stat, attr), |
| 991 |
getattr(dst_link_stat, attr) + 1) |
| 992 |
if hasattr(os, 'lchmod') or os.name == 'nt': |
| 993 |
self.assertEqual(src_link_stat.st_mode, dst_link_stat.st_mode) |
| 994 |
if hasattr(os, 'lchflags') and hasattr(src_link_stat, 'st_flags'): |
| 995 |
self.assertEqual(src_link_stat.st_flags, dst_link_stat.st_flags) |
| 996 |
|
| 997 |
shutil.copystat(src_link, dst, follow_symlinks=False) |
| 998 |
self.assertTrue(abs(os.stat(src).st_mtime - os.stat(dst).st_mtime) < |
| 999 |
00000.1) |
| 1000 |
|
| 1001 |
@unittest.skipUnless(hasattr(os, 'chflags') and |
| 1002 |
hasattr(errno, 'EOPNOTSUPP') and |
| 1003 |
hasattr(errno, 'ENOTSUP'), |
| 1004 |
"requires os.chflags, EOPNOTSUPP & ENOTSUP") |
| 1005 |
def test_copystat_handles_harmless_chflags_errors(self): |
| 1006 |
tmpdir = self.mkdtemp() |
| 1007 |
file1 = os.path.join(tmpdir, 'file1') |
| 1008 |
file2 = os.path.join(tmpdir, 'file2') |
| 1009 |
write_file(file1, 'xxx') |
| 1010 |
write_file(file2, 'xxx') |
| 1011 |
|
| 1012 |
def make_chflags_raiser(err): |
| 1013 |
ex = OSError() |
| 1014 |
|
| 1015 |
def _chflags_raiser(path, flags, *, follow_symlinks=True): |
| 1016 |
ex.errno = err |
| 1017 |
raise ex |
| 1018 |
return _chflags_raiser |
| 1019 |
old_chflags = os.chflags |
| 1020 |
try: |
| 1021 |
for err in errno.EOPNOTSUPP, errno.ENOTSUP: |
| 1022 |
os.chflags = make_chflags_raiser(err) |
| 1023 |
shutil.copystat(file1, file2) |
| 1024 |
|
| 1025 |
os.chflags = make_chflags_raiser(errno.EOPNOTSUPP + errno.ENOTSUP) |
| 1026 |
self.assertRaises(OSError, shutil.copystat, file1, file2) |
| 1027 |
finally: |
| 1028 |
os.chflags = old_chflags |
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
@os_helper.skip_unless_xattr |
| 1033 |
def test_copyxattr(self): |
| 1034 |
tmp_dir = self.mkdtemp() |
| 1035 |
src = os.path.join(tmp_dir, 'foo') |
| 1036 |
write_file(src, 'foo') |
| 1037 |
dst = os.path.join(tmp_dir, 'bar') |
| 1038 |
write_file(dst, 'bar') |
| 1039 |
|
| 1040 |
|
| 1041 |
shutil._copyxattr(src, dst) |
| 1042 |
|
| 1043 |
os.setxattr(src, 'user.foo', b'42') |
| 1044 |
os.setxattr(src, 'user.bar', b'43') |
| 1045 |
shutil._copyxattr(src, dst) |
| 1046 |
self.assertEqual(sorted(os.listxattr(src)), sorted(os.listxattr(dst))) |
| 1047 |
self.assertEqual( |
| 1048 |
os.getxattr(src, 'user.foo'), |
| 1049 |
os.getxattr(dst, 'user.foo')) |
| 1050 |
|
| 1051 |
os.remove(dst) |
| 1052 |
write_file(dst, 'bar') |
| 1053 |
os_error = OSError(errno.EPERM, 'EPERM') |
| 1054 |
|
| 1055 |
def _raise_on_user_foo(fname, attr, val, **kwargs): |
| 1056 |
if attr == 'user.foo': |
| 1057 |
raise os_error |
| 1058 |
else: |
| 1059 |
orig_setxattr(fname, attr, val, **kwargs) |
| 1060 |
try: |
| 1061 |
orig_setxattr = os.setxattr |
| 1062 |
os.setxattr = _raise_on_user_foo |
| 1063 |
shutil._copyxattr(src, dst) |
| 1064 |
self.assertIn('user.bar', os.listxattr(dst)) |
| 1065 |
finally: |
| 1066 |
os.setxattr = orig_setxattr |
| 1067 |
|
| 1068 |
def _raise_on_src(fname, *, follow_symlinks=True): |
| 1069 |
if fname == src: |
| 1070 |
raise OSError(errno.ENOTSUP, 'Operation not supported') |
| 1071 |
return orig_listxattr(fname, follow_symlinks=follow_symlinks) |
| 1072 |
try: |
| 1073 |
orig_listxattr = os.listxattr |
| 1074 |
os.listxattr = _raise_on_src |
| 1075 |
shutil._copyxattr(src, dst) |
| 1076 |
finally: |
| 1077 |
os.listxattr = orig_listxattr |
| 1078 |
|
| 1079 |
|
| 1080 |
src = os.path.join(tmp_dir, 'the_original') |
| 1081 |
srcro = os.path.join(tmp_dir, 'the_original_ro') |
| 1082 |
write_file(src, src) |
| 1083 |
write_file(srcro, srcro) |
| 1084 |
os.setxattr(src, 'user.the_value', b'fiddly') |
| 1085 |
os.setxattr(srcro, 'user.the_value', b'fiddly') |
| 1086 |
os.chmod(srcro, 0o444) |
| 1087 |
dst = os.path.join(tmp_dir, 'the_copy') |
| 1088 |
dstro = os.path.join(tmp_dir, 'the_copy_ro') |
| 1089 |
write_file(dst, dst) |
| 1090 |
write_file(dstro, dstro) |
| 1091 |
shutil.copystat(src, dst) |
| 1092 |
shutil.copystat(srcro, dstro) |
| 1093 |
self.assertEqual(os.getxattr(dst, 'user.the_value'), b'fiddly') |
| 1094 |
self.assertEqual(os.getxattr(dstro, 'user.the_value'), b'fiddly') |
| 1095 |
|
| 1096 |
@os_helper.skip_unless_symlink |
| 1097 |
@os_helper.skip_unless_xattr |
| 1098 |
@os_helper.skip_unless_dac_override |
| 1099 |
def test_copyxattr_symlinks(self): |
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
tmp_dir = self.mkdtemp() |
| 1104 |
src = os.path.join(tmp_dir, 'foo') |
| 1105 |
src_link = os.path.join(tmp_dir, 'baz') |
| 1106 |
write_file(src, 'foo') |
| 1107 |
os.symlink(src, src_link) |
| 1108 |
os.setxattr(src, 'trusted.foo', b'42') |
| 1109 |
os.setxattr(src_link, 'trusted.foo', b'43', follow_symlinks=False) |
| 1110 |
dst = os.path.join(tmp_dir, 'bar') |
| 1111 |
dst_link = os.path.join(tmp_dir, 'qux') |
| 1112 |
write_file(dst, 'bar') |
| 1113 |
os.symlink(dst, dst_link) |
| 1114 |
shutil._copyxattr(src_link, dst_link, follow_symlinks=False) |
| 1115 |
self.assertEqual(os.getxattr(dst_link, 'trusted.foo', follow_symlinks=False), b'43') |
| 1116 |
self.assertRaises(OSError, os.getxattr, dst, 'trusted.foo') |
| 1117 |
shutil._copyxattr(src_link, dst, follow_symlinks=False) |
| 1118 |
self.assertEqual(os.getxattr(dst, 'trusted.foo'), b'43') |
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
def _copy_file(self, method): |
| 1123 |
fname = 'test.txt' |
| 1124 |
tmpdir = self.mkdtemp() |
| 1125 |
write_file((tmpdir, fname), 'xxx') |
| 1126 |
file1 = os.path.join(tmpdir, fname) |
| 1127 |
tmpdir2 = self.mkdtemp() |
| 1128 |
method(file1, tmpdir2) |
| 1129 |
file2 = os.path.join(tmpdir2, fname) |
| 1130 |
return (file1, file2) |
| 1131 |
|
| 1132 |
def test_copy(self): |
| 1133 |
|
| 1134 |
file1, file2 = self._copy_file(shutil.copy) |
| 1135 |
self.assertTrue(os.path.exists(file2)) |
| 1136 |
self.assertEqual(os.stat(file1).st_mode, os.stat(file2).st_mode) |
| 1137 |
|
| 1138 |
@os_helper.skip_unless_symlink |
| 1139 |
def test_copy_symlinks(self): |
| 1140 |
tmp_dir = self.mkdtemp() |
| 1141 |
src = os.path.join(tmp_dir, 'foo') |
| 1142 |
dst = os.path.join(tmp_dir, 'bar') |
| 1143 |
src_link = os.path.join(tmp_dir, 'baz') |
| 1144 |
write_file(src, 'foo') |
| 1145 |
os.symlink(src, src_link) |
| 1146 |
if hasattr(os, 'lchmod'): |
| 1147 |
os.lchmod(src_link, stat.S_IRWXU | stat.S_IRWXO) |
| 1148 |
|
| 1149 |
shutil.copy(src_link, dst, follow_symlinks=True) |
| 1150 |
self.assertFalse(os.path.islink(dst)) |
| 1151 |
self.assertEqual(read_file(src), read_file(dst)) |
| 1152 |
os.remove(dst) |
| 1153 |
|
| 1154 |
shutil.copy(src_link, dst, follow_symlinks=False) |
| 1155 |
self.assertTrue(os.path.islink(dst)) |
| 1156 |
self.assertEqual(os.readlink(dst), os.readlink(src_link)) |
| 1157 |
if hasattr(os, 'lchmod'): |
| 1158 |
self.assertEqual(os.lstat(src_link).st_mode, |
| 1159 |
os.lstat(dst).st_mode) |
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
@unittest.skipUnless(hasattr(os, 'utime'), 'requires os.utime') |
| 1164 |
def test_copy2(self): |
| 1165 |
|
| 1166 |
|
| 1167 |
file1, file2 = self._copy_file(shutil.copy2) |
| 1168 |
self.assertTrue(os.path.exists(file2)) |
| 1169 |
file1_stat = os.stat(file1) |
| 1170 |
file2_stat = os.stat(file2) |
| 1171 |
self.assertEqual(file1_stat.st_mode, file2_stat.st_mode) |
| 1172 |
for attr in 'st_atime', 'st_mtime': |
| 1173 |
|
| 1174 |
self.assertLessEqual(getattr(file1_stat, attr), |
| 1175 |
getattr(file2_stat, attr) + 1) |
| 1176 |
if hasattr(os, 'chflags') and hasattr(file1_stat, 'st_flags'): |
| 1177 |
self.assertEqual(getattr(file1_stat, 'st_flags'), |
| 1178 |
getattr(file2_stat, 'st_flags')) |
| 1179 |
|
| 1180 |
@os_helper.skip_unless_symlink |
| 1181 |
def test_copy2_symlinks(self): |
| 1182 |
tmp_dir = self.mkdtemp() |
| 1183 |
src = os.path.join(tmp_dir, 'foo') |
| 1184 |
dst = os.path.join(tmp_dir, 'bar') |
| 1185 |
src_link = os.path.join(tmp_dir, 'baz') |
| 1186 |
write_file(src, 'foo') |
| 1187 |
os.symlink(src, src_link) |
| 1188 |
if hasattr(os, 'lchmod'): |
| 1189 |
os.lchmod(src_link, stat.S_IRWXU | stat.S_IRWXO) |
| 1190 |
if hasattr(os, 'lchflags') and hasattr(stat, 'UF_NODUMP'): |
| 1191 |
os.lchflags(src_link, stat.UF_NODUMP) |
| 1192 |
src_stat = os.stat(src) |
| 1193 |
src_link_stat = os.lstat(src_link) |
| 1194 |
|
| 1195 |
shutil.copy2(src_link, dst, follow_symlinks=True) |
| 1196 |
self.assertFalse(os.path.islink(dst)) |
| 1197 |
self.assertEqual(read_file(src), read_file(dst)) |
| 1198 |
os.remove(dst) |
| 1199 |
|
| 1200 |
shutil.copy2(src_link, dst, follow_symlinks=False) |
| 1201 |
self.assertTrue(os.path.islink(dst)) |
| 1202 |
self.assertEqual(os.readlink(dst), os.readlink(src_link)) |
| 1203 |
dst_stat = os.lstat(dst) |
| 1204 |
if os.utime in os.supports_follow_symlinks: |
| 1205 |
for attr in 'st_atime', 'st_mtime': |
| 1206 |
|
| 1207 |
self.assertLessEqual(getattr(src_link_stat, attr), |
| 1208 |
getattr(dst_stat, attr) + 1) |
| 1209 |
if hasattr(os, 'lchmod'): |
| 1210 |
self.assertEqual(src_link_stat.st_mode, dst_stat.st_mode) |
| 1211 |
self.assertNotEqual(src_stat.st_mode, dst_stat.st_mode) |
| 1212 |
if hasattr(os, 'lchflags') and hasattr(src_link_stat, 'st_flags'): |
| 1213 |
self.assertEqual(src_link_stat.st_flags, dst_stat.st_flags) |
| 1214 |
|
| 1215 |
@os_helper.skip_unless_xattr |
| 1216 |
def test_copy2_xattr(self): |
| 1217 |
tmp_dir = self.mkdtemp() |
| 1218 |
src = os.path.join(tmp_dir, 'foo') |
| 1219 |
dst = os.path.join(tmp_dir, 'bar') |
| 1220 |
write_file(src, 'foo') |
| 1221 |
os.setxattr(src, 'user.foo', b'42') |
| 1222 |
shutil.copy2(src, dst) |
| 1223 |
self.assertEqual( |
| 1224 |
os.getxattr(src, 'user.foo'), |
| 1225 |
os.getxattr(dst, 'user.foo')) |
| 1226 |
os.remove(dst) |
| 1227 |
|
| 1228 |
def test_copy_return_value(self): |
| 1229 |
|
| 1230 |
for fn in (shutil.copy, shutil.copy2): |
| 1231 |
src_dir = self.mkdtemp() |
| 1232 |
dst_dir = self.mkdtemp() |
| 1233 |
src = os.path.join(src_dir, 'foo') |
| 1234 |
write_file(src, 'foo') |
| 1235 |
rv = fn(src, dst_dir) |
| 1236 |
self.assertEqual(rv, os.path.join(dst_dir, 'foo')) |
| 1237 |
rv = fn(src, os.path.join(dst_dir, 'bar')) |
| 1238 |
self.assertEqual(rv, os.path.join(dst_dir, 'bar')) |
| 1239 |
|
| 1240 |
def test_copy_dir(self): |
| 1241 |
self._test_copy_dir(shutil.copy) |
| 1242 |
|
| 1243 |
def test_copy2_dir(self): |
| 1244 |
self._test_copy_dir(shutil.copy2) |
| 1245 |
|
| 1246 |
def _test_copy_dir(self, copy_func): |
| 1247 |
src_dir = self.mkdtemp() |
| 1248 |
src_file = os.path.join(src_dir, 'foo') |
| 1249 |
dir2 = self.mkdtemp() |
| 1250 |
dst = os.path.join(src_dir, 'does_not_exist/') |
| 1251 |
write_file(src_file, 'foo') |
| 1252 |
if sys.platform == "win32": |
| 1253 |
err = PermissionError |
| 1254 |
else: |
| 1255 |
err = IsADirectoryError |
| 1256 |
self.assertRaises(err, copy_func, dir2, src_dir) |
| 1257 |
|
| 1258 |
|
| 1259 |
self.assertRaises(err, copy_func, dir2, dst) |
| 1260 |
copy_func(src_file, dir2) |
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
@os_helper.skip_unless_symlink |
| 1265 |
def test_copyfile_symlinks(self): |
| 1266 |
tmp_dir = self.mkdtemp() |
| 1267 |
src = os.path.join(tmp_dir, 'src') |
| 1268 |
dst = os.path.join(tmp_dir, 'dst') |
| 1269 |
dst_link = os.path.join(tmp_dir, 'dst_link') |
| 1270 |
link = os.path.join(tmp_dir, 'link') |
| 1271 |
write_file(src, 'foo') |
| 1272 |
os.symlink(src, link) |
| 1273 |
|
| 1274 |
shutil.copyfile(link, dst_link, follow_symlinks=False) |
| 1275 |
self.assertTrue(os.path.islink(dst_link)) |
| 1276 |
self.assertEqual(os.readlink(link), os.readlink(dst_link)) |
| 1277 |
|
| 1278 |
shutil.copyfile(link, dst) |
| 1279 |
self.assertFalse(os.path.islink(dst)) |
| 1280 |
|
| 1281 |
@unittest.skipUnless(hasattr(os, 'link'), 'requires os.link') |
| 1282 |
def test_dont_copy_file_onto_link_to_itself(self): |
| 1283 |
|
| 1284 |
os.mkdir(TESTFN) |
| 1285 |
src = os.path.join(TESTFN, 'cheese') |
| 1286 |
dst = os.path.join(TESTFN, 'shop') |
| 1287 |
try: |
| 1288 |
with open(src, 'w', encoding='utf-8') as f: |
| 1289 |
f.write('cheddar') |
| 1290 |
try: |
| 1291 |
os.link(src, dst) |
| 1292 |
except PermissionError as e: |
| 1293 |
self.skipTest('os.link(): %s' % e) |
| 1294 |
self.assertRaises(shutil.SameFileError, shutil.copyfile, src, dst) |
| 1295 |
with open(src, 'r', encoding='utf-8') as f: |
| 1296 |
self.assertEqual(f.read(), 'cheddar') |
| 1297 |
os.remove(dst) |
| 1298 |
finally: |
| 1299 |
shutil.rmtree(TESTFN, ignore_errors=True) |
| 1300 |
|
| 1301 |
@os_helper.skip_unless_symlink |
| 1302 |
def test_dont_copy_file_onto_symlink_to_itself(self): |
| 1303 |
|
| 1304 |
os.mkdir(TESTFN) |
| 1305 |
src = os.path.join(TESTFN, 'cheese') |
| 1306 |
dst = os.path.join(TESTFN, 'shop') |
| 1307 |
try: |
| 1308 |
with open(src, 'w', encoding='utf-8') as f: |
| 1309 |
f.write('cheddar') |
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
os.symlink('cheese', dst) |
| 1314 |
self.assertRaises(shutil.SameFileError, shutil.copyfile, src, dst) |
| 1315 |
with open(src, 'r', encoding='utf-8') as f: |
| 1316 |
self.assertEqual(f.read(), 'cheddar') |
| 1317 |
os.remove(dst) |
| 1318 |
finally: |
| 1319 |
shutil.rmtree(TESTFN, ignore_errors=True) |
| 1320 |
|
| 1321 |
|
| 1322 |
@unittest.skipUnless(hasattr(os, "mkfifo"), 'requires os.mkfifo()') |
| 1323 |
@unittest.skipIf(sys.platform == "vxworks", |
| 1324 |
"fifo requires special path on VxWorks") |
| 1325 |
def test_copyfile_named_pipe(self): |
| 1326 |
try: |
| 1327 |
os.mkfifo(TESTFN) |
| 1328 |
except PermissionError as e: |
| 1329 |
self.skipTest('os.mkfifo(): %s' % e) |
| 1330 |
try: |
| 1331 |
self.assertRaises(shutil.SpecialFileError, |
| 1332 |
shutil.copyfile, TESTFN, TESTFN2) |
| 1333 |
self.assertRaises(shutil.SpecialFileError, |
| 1334 |
shutil.copyfile, __file__, TESTFN) |
| 1335 |
finally: |
| 1336 |
os.remove(TESTFN) |
| 1337 |
|
| 1338 |
def test_copyfile_return_value(self): |
| 1339 |
|
| 1340 |
src_dir = self.mkdtemp() |
| 1341 |
dst_dir = self.mkdtemp() |
| 1342 |
dst_file = os.path.join(dst_dir, 'bar') |
| 1343 |
src_file = os.path.join(src_dir, 'foo') |
| 1344 |
write_file(src_file, 'foo') |
| 1345 |
rv = shutil.copyfile(src_file, dst_file) |
| 1346 |
self.assertTrue(os.path.exists(rv)) |
| 1347 |
self.assertEqual(read_file(src_file), read_file(dst_file)) |
| 1348 |
|
| 1349 |
def test_copyfile_same_file(self): |
| 1350 |
|
| 1351 |
|
| 1352 |
src_dir = self.mkdtemp() |
| 1353 |
src_file = os.path.join(src_dir, 'foo') |
| 1354 |
write_file(src_file, 'foo') |
| 1355 |
self.assertRaises(SameFileError, shutil.copyfile, src_file, src_file) |
| 1356 |
|
| 1357 |
self.assertRaises(Error, shutil.copyfile, src_file, src_file) |
| 1358 |
|
| 1359 |
self.assertEqual(read_file(src_file), 'foo') |
| 1360 |
|
| 1361 |
@unittest.skipIf(MACOS or SOLARIS or _winapi, 'On MACOS, Solaris and Windows the errors are not confusing (though different)') |
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
@unittest.skipIf(AIX, 'Not valid on AIX, see gh-92670') |
| 1366 |
def test_copyfile_nonexistent_dir(self): |
| 1367 |
|
| 1368 |
src_dir = self.mkdtemp() |
| 1369 |
src_file = os.path.join(src_dir, 'foo') |
| 1370 |
dst = os.path.join(src_dir, 'does_not_exist/') |
| 1371 |
write_file(src_file, 'foo') |
| 1372 |
self.assertRaises(FileNotFoundError, shutil.copyfile, src_file, dst) |
| 1373 |
|
| 1374 |
def test_copyfile_copy_dir(self): |
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
src_dir = self.mkdtemp() |
| 1379 |
src_file = os.path.join(src_dir, 'foo') |
| 1380 |
dir2 = self.mkdtemp() |
| 1381 |
dst = os.path.join(src_dir, 'does_not_exist/') |
| 1382 |
write_file(src_file, 'foo') |
| 1383 |
if sys.platform == "win32": |
| 1384 |
err = PermissionError |
| 1385 |
else: |
| 1386 |
err = IsADirectoryError |
| 1387 |
|
| 1388 |
self.assertRaises(err, shutil.copyfile, src_dir, dst) |
| 1389 |
self.assertRaises(err, shutil.copyfile, src_file, src_dir) |
| 1390 |
self.assertRaises(err, shutil.copyfile, dir2, src_dir) |
| 1391 |
|
| 1392 |
|
| 1393 |
class TestArchives(BaseTest, unittest.TestCase): |
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
@support.requires_zlib() |
| 1398 |
def test_make_tarball(self): |
| 1399 |
|
| 1400 |
root_dir, base_dir = self._create_files('') |
| 1401 |
|
| 1402 |
tmpdir2 = self.mkdtemp() |
| 1403 |
|
| 1404 |
os.rmdir(tmpdir2) |
| 1405 |
|
| 1406 |
work_dir = os.path.dirname(tmpdir2) |
| 1407 |
rel_base_name = os.path.join(os.path.basename(tmpdir2), 'archive') |
| 1408 |
|
| 1409 |
with os_helper.change_cwd(work_dir), no_chdir: |
| 1410 |
base_name = os.path.abspath(rel_base_name) |
| 1411 |
tarball = make_archive(rel_base_name, 'gztar', root_dir, '.') |
| 1412 |
|
| 1413 |
|
| 1414 |
self.assertEqual(tarball, base_name + '.tar.gz') |
| 1415 |
self.assertTrue(os.path.isfile(tarball)) |
| 1416 |
self.assertTrue(tarfile.is_tarfile(tarball)) |
| 1417 |
with tarfile.open(tarball, 'r:gz') as tf: |
| 1418 |
self.assertCountEqual(tf.getnames(), |
| 1419 |
['.', './sub', './sub2', |
| 1420 |
'./file1', './file2', './sub/file3']) |
| 1421 |
|
| 1422 |
|
| 1423 |
with os_helper.change_cwd(work_dir), no_chdir: |
| 1424 |
tarball = make_archive(rel_base_name, 'tar', root_dir, '.') |
| 1425 |
self.assertEqual(tarball, base_name + '.tar') |
| 1426 |
self.assertTrue(os.path.isfile(tarball)) |
| 1427 |
self.assertTrue(tarfile.is_tarfile(tarball)) |
| 1428 |
with tarfile.open(tarball, 'r') as tf: |
| 1429 |
self.assertCountEqual(tf.getnames(), |
| 1430 |
['.', './sub', './sub2', |
| 1431 |
'./file1', './file2', './sub/file3']) |
| 1432 |
|
| 1433 |
def _tarinfo(self, path): |
| 1434 |
with tarfile.open(path) as tar: |
| 1435 |
names = tar.getnames() |
| 1436 |
names.sort() |
| 1437 |
return tuple(names) |
| 1438 |
|
| 1439 |
def _create_files(self, base_dir='dist'): |
| 1440 |
|
| 1441 |
root_dir = self.mkdtemp() |
| 1442 |
dist = os.path.join(root_dir, base_dir) |
| 1443 |
os.makedirs(dist, exist_ok=True) |
| 1444 |
write_file((dist, 'file1'), 'xxx') |
| 1445 |
write_file((dist, 'file2'), 'xxx') |
| 1446 |
os.mkdir(os.path.join(dist, 'sub')) |
| 1447 |
write_file((dist, 'sub', 'file3'), 'xxx') |
| 1448 |
os.mkdir(os.path.join(dist, 'sub2')) |
| 1449 |
if base_dir: |
| 1450 |
write_file((root_dir, 'outer'), 'xxx') |
| 1451 |
return root_dir, base_dir |
| 1452 |
|
| 1453 |
@support.requires_zlib() |
| 1454 |
@unittest.skipUnless(shutil.which('tar'), |
| 1455 |
'Need the tar command to run') |
| 1456 |
def test_tarfile_vs_tar(self): |
| 1457 |
root_dir, base_dir = self._create_files() |
| 1458 |
base_name = os.path.join(self.mkdtemp(), 'archive') |
| 1459 |
with no_chdir: |
| 1460 |
tarball = make_archive(base_name, 'gztar', root_dir, base_dir) |
| 1461 |
|
| 1462 |
|
| 1463 |
self.assertEqual(tarball, base_name + '.tar.gz') |
| 1464 |
self.assertTrue(os.path.isfile(tarball)) |
| 1465 |
|
| 1466 |
|
| 1467 |
tarball2 = os.path.join(root_dir, 'archive2.tar') |
| 1468 |
tar_cmd = ['tar', '-cf', 'archive2.tar', base_dir] |
| 1469 |
if sys.platform == 'darwin': |
| 1470 |
|
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
import platform |
| 1478 |
if int(platform.mac_ver()[0].split('.')[0]) >= 11: |
| 1479 |
tar_cmd.insert(1, '--no-mac-metadata') |
| 1480 |
subprocess.check_call(tar_cmd, cwd=root_dir, |
| 1481 |
stdout=subprocess.DEVNULL) |
| 1482 |
|
| 1483 |
self.assertTrue(os.path.isfile(tarball2)) |
| 1484 |
|
| 1485 |
self.assertEqual(self._tarinfo(tarball), self._tarinfo(tarball2)) |
| 1486 |
|
| 1487 |
|
| 1488 |
with no_chdir: |
| 1489 |
tarball = make_archive(base_name, 'tar', root_dir, base_dir) |
| 1490 |
self.assertEqual(tarball, base_name + '.tar') |
| 1491 |
self.assertTrue(os.path.isfile(tarball)) |
| 1492 |
|
| 1493 |
|
| 1494 |
with no_chdir: |
| 1495 |
tarball = make_archive(base_name, 'tar', root_dir, base_dir, |
| 1496 |
dry_run=True) |
| 1497 |
self.assertEqual(tarball, base_name + '.tar') |
| 1498 |
self.assertTrue(os.path.isfile(tarball)) |
| 1499 |
|
| 1500 |
@support.requires_zlib() |
| 1501 |
def test_make_zipfile(self): |
| 1502 |
|
| 1503 |
root_dir, base_dir = self._create_files() |
| 1504 |
|
| 1505 |
tmpdir2 = self.mkdtemp() |
| 1506 |
|
| 1507 |
os.rmdir(tmpdir2) |
| 1508 |
|
| 1509 |
work_dir = os.path.dirname(tmpdir2) |
| 1510 |
rel_base_name = os.path.join(os.path.basename(tmpdir2), 'archive') |
| 1511 |
|
| 1512 |
with os_helper.change_cwd(work_dir), no_chdir: |
| 1513 |
base_name = os.path.abspath(rel_base_name) |
| 1514 |
res = make_archive(rel_base_name, 'zip', root_dir) |
| 1515 |
|
| 1516 |
self.assertEqual(res, base_name + '.zip') |
| 1517 |
self.assertTrue(os.path.isfile(res)) |
| 1518 |
self.assertTrue(zipfile.is_zipfile(res)) |
| 1519 |
with zipfile.ZipFile(res) as zf: |
| 1520 |
self.assertCountEqual(zf.namelist(), |
| 1521 |
['dist/', 'dist/sub/', 'dist/sub2/', |
| 1522 |
'dist/file1', 'dist/file2', 'dist/sub/file3', |
| 1523 |
'outer']) |
| 1524 |
|
| 1525 |
with os_helper.change_cwd(work_dir), no_chdir: |
| 1526 |
base_name = os.path.abspath(rel_base_name) |
| 1527 |
res = make_archive(rel_base_name, 'zip', root_dir, base_dir) |
| 1528 |
|
| 1529 |
self.assertEqual(res, base_name + '.zip') |
| 1530 |
self.assertTrue(os.path.isfile(res)) |
| 1531 |
self.assertTrue(zipfile.is_zipfile(res)) |
| 1532 |
with zipfile.ZipFile(res) as zf: |
| 1533 |
self.assertCountEqual(zf.namelist(), |
| 1534 |
['dist/', 'dist/sub/', 'dist/sub2/', |
| 1535 |
'dist/file1', 'dist/file2', 'dist/sub/file3']) |
| 1536 |
|
| 1537 |
@support.requires_zlib() |
| 1538 |
@unittest.skipUnless(shutil.which('zip'), |
| 1539 |
'Need the zip command to run') |
| 1540 |
def test_zipfile_vs_zip(self): |
| 1541 |
root_dir, base_dir = self._create_files() |
| 1542 |
base_name = os.path.join(self.mkdtemp(), 'archive') |
| 1543 |
with no_chdir: |
| 1544 |
archive = make_archive(base_name, 'zip', root_dir, base_dir) |
| 1545 |
|
| 1546 |
|
| 1547 |
self.assertEqual(archive, base_name + '.zip') |
| 1548 |
self.assertTrue(os.path.isfile(archive)) |
| 1549 |
|
| 1550 |
|
| 1551 |
archive2 = os.path.join(root_dir, 'archive2.zip') |
| 1552 |
zip_cmd = ['zip', '-q', '-r', 'archive2.zip', base_dir] |
| 1553 |
subprocess.check_call(zip_cmd, cwd=root_dir, |
| 1554 |
stdout=subprocess.DEVNULL) |
| 1555 |
|
| 1556 |
self.assertTrue(os.path.isfile(archive2)) |
| 1557 |
|
| 1558 |
with zipfile.ZipFile(archive) as zf: |
| 1559 |
names = zf.namelist() |
| 1560 |
with zipfile.ZipFile(archive2) as zf: |
| 1561 |
names2 = zf.namelist() |
| 1562 |
self.assertEqual(sorted(names), sorted(names2)) |
| 1563 |
|
| 1564 |
@support.requires_zlib() |
| 1565 |
@unittest.skipUnless(shutil.which('unzip'), |
| 1566 |
'Need the unzip command to run') |
| 1567 |
def test_unzip_zipfile(self): |
| 1568 |
root_dir, base_dir = self._create_files() |
| 1569 |
base_name = os.path.join(self.mkdtemp(), 'archive') |
| 1570 |
with no_chdir: |
| 1571 |
archive = make_archive(base_name, 'zip', root_dir, base_dir) |
| 1572 |
|
| 1573 |
|
| 1574 |
self.assertEqual(archive, base_name + '.zip') |
| 1575 |
self.assertTrue(os.path.isfile(archive)) |
| 1576 |
|
| 1577 |
|
| 1578 |
zip_cmd = ['unzip', '-t', archive] |
| 1579 |
with os_helper.change_cwd(root_dir): |
| 1580 |
try: |
| 1581 |
subprocess.check_output(zip_cmd, stderr=subprocess.STDOUT) |
| 1582 |
except subprocess.CalledProcessError as exc: |
| 1583 |
details = exc.output.decode(errors="replace") |
| 1584 |
if 'unrecognized option: t' in details: |
| 1585 |
self.skipTest("unzip doesn't support -t") |
| 1586 |
msg = "{}\n\n**Unzip Output**\n{}" |
| 1587 |
self.fail(msg.format(exc, details)) |
| 1588 |
|
| 1589 |
def test_make_archive(self): |
| 1590 |
tmpdir = self.mkdtemp() |
| 1591 |
base_name = os.path.join(tmpdir, 'archive') |
| 1592 |
self.assertRaises(ValueError, make_archive, base_name, 'xxx') |
| 1593 |
|
| 1594 |
@support.requires_zlib() |
| 1595 |
def test_make_archive_owner_group(self): |
| 1596 |
|
| 1597 |
|
| 1598 |
if UID_GID_SUPPORT: |
| 1599 |
group = grp.getgrgid(0)[0] |
| 1600 |
owner = pwd.getpwuid(0)[0] |
| 1601 |
else: |
| 1602 |
group = owner = 'root' |
| 1603 |
|
| 1604 |
root_dir, base_dir = self._create_files() |
| 1605 |
base_name = os.path.join(self.mkdtemp(), 'archive') |
| 1606 |
res = make_archive(base_name, 'zip', root_dir, base_dir, owner=owner, |
| 1607 |
group=group) |
| 1608 |
self.assertTrue(os.path.isfile(res)) |
| 1609 |
|
| 1610 |
res = make_archive(base_name, 'zip', root_dir, base_dir) |
| 1611 |
self.assertTrue(os.path.isfile(res)) |
| 1612 |
|
| 1613 |
res = make_archive(base_name, 'tar', root_dir, base_dir, |
| 1614 |
owner=owner, group=group) |
| 1615 |
self.assertTrue(os.path.isfile(res)) |
| 1616 |
|
| 1617 |
res = make_archive(base_name, 'tar', root_dir, base_dir, |
| 1618 |
owner='kjhkjhkjg', group='oihohoh') |
| 1619 |
self.assertTrue(os.path.isfile(res)) |
| 1620 |
|
| 1621 |
|
| 1622 |
@support.requires_zlib() |
| 1623 |
@unittest.skipUnless(UID_GID_SUPPORT, "Requires grp and pwd support") |
| 1624 |
def test_tarfile_root_owner(self): |
| 1625 |
root_dir, base_dir = self._create_files() |
| 1626 |
base_name = os.path.join(self.mkdtemp(), 'archive') |
| 1627 |
group = grp.getgrgid(0)[0] |
| 1628 |
owner = pwd.getpwuid(0)[0] |
| 1629 |
with os_helper.change_cwd(root_dir), no_chdir: |
| 1630 |
archive_name = make_archive(base_name, 'gztar', root_dir, 'dist', |
| 1631 |
owner=owner, group=group) |
| 1632 |
|
| 1633 |
|
| 1634 |
self.assertTrue(os.path.isfile(archive_name)) |
| 1635 |
|
| 1636 |
|
| 1637 |
archive = tarfile.open(archive_name) |
| 1638 |
try: |
| 1639 |
for member in archive.getmembers(): |
| 1640 |
self.assertEqual(member.uid, 0) |
| 1641 |
self.assertEqual(member.gid, 0) |
| 1642 |
finally: |
| 1643 |
archive.close() |
| 1644 |
|
| 1645 |
def test_make_archive_cwd(self): |
| 1646 |
current_dir = os.getcwd() |
| 1647 |
root_dir = self.mkdtemp() |
| 1648 |
def _breaks(*args, **kw): |
| 1649 |
raise RuntimeError() |
| 1650 |
dirs = [] |
| 1651 |
def _chdir(path): |
| 1652 |
dirs.append(path) |
| 1653 |
orig_chdir(path) |
| 1654 |
|
| 1655 |
register_archive_format('xxx', _breaks, [], 'xxx file') |
| 1656 |
try: |
| 1657 |
with support.swap_attr(os, 'chdir', _chdir) as orig_chdir: |
| 1658 |
try: |
| 1659 |
make_archive('xxx', 'xxx', root_dir=root_dir) |
| 1660 |
except Exception: |
| 1661 |
pass |
| 1662 |
self.assertEqual(os.getcwd(), current_dir) |
| 1663 |
self.assertEqual(dirs, [root_dir, current_dir]) |
| 1664 |
finally: |
| 1665 |
unregister_archive_format('xxx') |
| 1666 |
|
| 1667 |
def test_make_tarfile_in_curdir(self): |
| 1668 |
|
| 1669 |
root_dir = self.mkdtemp() |
| 1670 |
with os_helper.change_cwd(root_dir), no_chdir: |
| 1671 |
self.assertEqual(make_archive('test', 'tar'), 'test.tar') |
| 1672 |
self.assertTrue(os.path.isfile('test.tar')) |
| 1673 |
|
| 1674 |
@support.requires_zlib() |
| 1675 |
def test_make_zipfile_in_curdir(self): |
| 1676 |
|
| 1677 |
root_dir = self.mkdtemp() |
| 1678 |
with os_helper.change_cwd(root_dir), no_chdir: |
| 1679 |
self.assertEqual(make_archive('test', 'zip'), 'test.zip') |
| 1680 |
self.assertTrue(os.path.isfile('test.zip')) |
| 1681 |
|
| 1682 |
def test_register_archive_format(self): |
| 1683 |
|
| 1684 |
self.assertRaises(TypeError, register_archive_format, 'xxx', 1) |
| 1685 |
self.assertRaises(TypeError, register_archive_format, 'xxx', lambda: x, |
| 1686 |
1) |
| 1687 |
self.assertRaises(TypeError, register_archive_format, 'xxx', lambda: x, |
| 1688 |
[(1, 2), (1, 2, 3)]) |
| 1689 |
|
| 1690 |
register_archive_format('xxx', lambda: x, [(1, 2)], 'xxx file') |
| 1691 |
formats = [name for name, params in get_archive_formats()] |
| 1692 |
self.assertIn('xxx', formats) |
| 1693 |
|
| 1694 |
unregister_archive_format('xxx') |
| 1695 |
formats = [name for name, params in get_archive_formats()] |
| 1696 |
self.assertNotIn('xxx', formats) |
| 1697 |
|
| 1698 |
def test_make_tarfile_rootdir_nodir(self): |
| 1699 |
|
| 1700 |
self.addCleanup(os_helper.unlink, f'{TESTFN}.tar') |
| 1701 |
for dry_run in (False, True): |
| 1702 |
with self.subTest(dry_run=dry_run): |
| 1703 |
tmp_dir = self.mkdtemp() |
| 1704 |
nonexisting_file = os.path.join(tmp_dir, 'nonexisting') |
| 1705 |
with self.assertRaises(FileNotFoundError) as cm: |
| 1706 |
make_archive(TESTFN, 'tar', nonexisting_file, dry_run=dry_run) |
| 1707 |
self.assertEqual(cm.exception.errno, errno.ENOENT) |
| 1708 |
self.assertEqual(cm.exception.filename, nonexisting_file) |
| 1709 |
self.assertFalse(os.path.exists(f'{TESTFN}.tar')) |
| 1710 |
|
| 1711 |
tmp_fd, tmp_file = tempfile.mkstemp(dir=tmp_dir) |
| 1712 |
os.close(tmp_fd) |
| 1713 |
with self.assertRaises(NotADirectoryError) as cm: |
| 1714 |
make_archive(TESTFN, 'tar', tmp_file, dry_run=dry_run) |
| 1715 |
self.assertEqual(cm.exception.errno, errno.ENOTDIR) |
| 1716 |
self.assertEqual(cm.exception.filename, tmp_file) |
| 1717 |
self.assertFalse(os.path.exists(f'{TESTFN}.tar')) |
| 1718 |
|
| 1719 |
@support.requires_zlib() |
| 1720 |
def test_make_zipfile_rootdir_nodir(self): |
| 1721 |
|
| 1722 |
self.addCleanup(os_helper.unlink, f'{TESTFN}.zip') |
| 1723 |
for dry_run in (False, True): |
| 1724 |
with self.subTest(dry_run=dry_run): |
| 1725 |
tmp_dir = self.mkdtemp() |
| 1726 |
nonexisting_file = os.path.join(tmp_dir, 'nonexisting') |
| 1727 |
with self.assertRaises(FileNotFoundError) as cm: |
| 1728 |
make_archive(TESTFN, 'zip', nonexisting_file, dry_run=dry_run) |
| 1729 |
self.assertEqual(cm.exception.errno, errno.ENOENT) |
| 1730 |
self.assertEqual(cm.exception.filename, nonexisting_file) |
| 1731 |
self.assertFalse(os.path.exists(f'{TESTFN}.zip')) |
| 1732 |
|
| 1733 |
tmp_fd, tmp_file = tempfile.mkstemp(dir=tmp_dir) |
| 1734 |
os.close(tmp_fd) |
| 1735 |
with self.assertRaises(NotADirectoryError) as cm: |
| 1736 |
make_archive(TESTFN, 'zip', tmp_file, dry_run=dry_run) |
| 1737 |
self.assertEqual(cm.exception.errno, errno.ENOTDIR) |
| 1738 |
self.assertEqual(cm.exception.filename, tmp_file) |
| 1739 |
self.assertFalse(os.path.exists(f'{TESTFN}.zip')) |
| 1740 |
|
| 1741 |
|
| 1742 |
|
| 1743 |
def check_unpack_archive(self, format, **kwargs): |
| 1744 |
self.check_unpack_archive_with_converter( |
| 1745 |
format, lambda path: path, **kwargs) |
| 1746 |
self.check_unpack_archive_with_converter( |
| 1747 |
format, pathlib.Path, **kwargs) |
| 1748 |
self.check_unpack_archive_with_converter(format, FakePath, **kwargs) |
| 1749 |
|
| 1750 |
def check_unpack_archive_with_converter(self, format, converter, **kwargs): |
| 1751 |
root_dir, base_dir = self._create_files() |
| 1752 |
expected = rlistdir(root_dir) |
| 1753 |
expected.remove('outer') |
| 1754 |
|
| 1755 |
base_name = os.path.join(self.mkdtemp(), 'archive') |
| 1756 |
filename = make_archive(base_name, format, root_dir, base_dir) |
| 1757 |
|
| 1758 |
|
| 1759 |
tmpdir2 = self.mkdtemp() |
| 1760 |
unpack_archive(converter(filename), converter(tmpdir2), **kwargs) |
| 1761 |
self.assertEqual(rlistdir(tmpdir2), expected) |
| 1762 |
|
| 1763 |
|
| 1764 |
tmpdir3 = self.mkdtemp() |
| 1765 |
unpack_archive(converter(filename), converter(tmpdir3), format=format, |
| 1766 |
**kwargs) |
| 1767 |
self.assertEqual(rlistdir(tmpdir3), expected) |
| 1768 |
|
| 1769 |
with self.assertRaises(shutil.ReadError): |
| 1770 |
unpack_archive(converter(TESTFN), **kwargs) |
| 1771 |
with self.assertRaises(ValueError): |
| 1772 |
unpack_archive(converter(TESTFN), format='xxx', **kwargs) |
| 1773 |
|
| 1774 |
def check_unpack_tarball(self, format): |
| 1775 |
self.check_unpack_archive(format, filter='fully_trusted') |
| 1776 |
self.check_unpack_archive(format, filter='data') |
| 1777 |
with warnings_helper.check_no_warnings(self): |
| 1778 |
self.check_unpack_archive(format) |
| 1779 |
|
| 1780 |
def test_unpack_archive_tar(self): |
| 1781 |
self.check_unpack_tarball('tar') |
| 1782 |
|
| 1783 |
@support.requires_zlib() |
| 1784 |
def test_unpack_archive_gztar(self): |
| 1785 |
self.check_unpack_tarball('gztar') |
| 1786 |
|
| 1787 |
@support.requires_bz2() |
| 1788 |
def test_unpack_archive_bztar(self): |
| 1789 |
self.check_unpack_tarball('bztar') |
| 1790 |
|
| 1791 |
@support.requires_lzma() |
| 1792 |
@unittest.skipIf(AIX and not _maxdataOK(), "AIX MAXDATA must be 0x20000000 or larger") |
| 1793 |
def test_unpack_archive_xztar(self): |
| 1794 |
self.check_unpack_tarball('xztar') |
| 1795 |
|
| 1796 |
@support.requires_zlib() |
| 1797 |
def test_unpack_archive_zip(self): |
| 1798 |
self.check_unpack_archive('zip') |
| 1799 |
with self.assertRaises(TypeError): |
| 1800 |
self.check_unpack_archive('zip', filter='data') |
| 1801 |
|
| 1802 |
def test_unpack_registry(self): |
| 1803 |
|
| 1804 |
formats = get_unpack_formats() |
| 1805 |
|
| 1806 |
def _boo(filename, extract_dir, extra): |
| 1807 |
self.assertEqual(extra, 1) |
| 1808 |
self.assertEqual(filename, 'stuff.boo') |
| 1809 |
self.assertEqual(extract_dir, 'xx') |
| 1810 |
|
| 1811 |
register_unpack_format('Boo', ['.boo', '.b2'], _boo, [('extra', 1)]) |
| 1812 |
unpack_archive('stuff.boo', 'xx') |
| 1813 |
|
| 1814 |
|
| 1815 |
self.assertRaises(RegistryError, register_unpack_format, 'Boo2', |
| 1816 |
['.boo'], _boo) |
| 1817 |
|
| 1818 |
|
| 1819 |
unregister_unpack_format('Boo') |
| 1820 |
register_unpack_format('Boo2', ['.boo'], _boo) |
| 1821 |
self.assertIn(('Boo2', ['.boo'], ''), get_unpack_formats()) |
| 1822 |
self.assertNotIn(('Boo', ['.boo'], ''), get_unpack_formats()) |
| 1823 |
|
| 1824 |
|
| 1825 |
unregister_unpack_format('Boo2') |
| 1826 |
self.assertEqual(get_unpack_formats(), formats) |
| 1827 |
|
| 1828 |
|
| 1829 |
class TestMisc(BaseTest, unittest.TestCase): |
| 1830 |
|
| 1831 |
@unittest.skipUnless(hasattr(shutil, 'disk_usage'), |
| 1832 |
"disk_usage not available on this platform") |
| 1833 |
def test_disk_usage(self): |
| 1834 |
usage = shutil.disk_usage(os.path.dirname(__file__)) |
| 1835 |
for attr in ('total', 'used', 'free'): |
| 1836 |
self.assertIsInstance(getattr(usage, attr), int) |
| 1837 |
self.assertGreater(usage.total, 0) |
| 1838 |
self.assertGreater(usage.used, 0) |
| 1839 |
self.assertGreaterEqual(usage.free, 0) |
| 1840 |
self.assertGreaterEqual(usage.total, usage.used) |
| 1841 |
self.assertGreater(usage.total, usage.free) |
| 1842 |
|
| 1843 |
|
| 1844 |
shutil.disk_usage(__file__) |
| 1845 |
|
| 1846 |
@unittest.skipUnless(UID_GID_SUPPORT, "Requires grp and pwd support") |
| 1847 |
@unittest.skipUnless(hasattr(os, 'chown'), 'requires os.chown') |
| 1848 |
def test_chown(self): |
| 1849 |
dirname = self.mkdtemp() |
| 1850 |
filename = tempfile.mktemp(dir=dirname) |
| 1851 |
write_file(filename, 'testing chown function') |
| 1852 |
|
| 1853 |
with self.assertRaises(ValueError): |
| 1854 |
shutil.chown(filename) |
| 1855 |
|
| 1856 |
with self.assertRaises(LookupError): |
| 1857 |
shutil.chown(filename, user='non-existing username') |
| 1858 |
|
| 1859 |
with self.assertRaises(LookupError): |
| 1860 |
shutil.chown(filename, group='non-existing groupname') |
| 1861 |
|
| 1862 |
with self.assertRaises(TypeError): |
| 1863 |
shutil.chown(filename, b'spam') |
| 1864 |
|
| 1865 |
with self.assertRaises(TypeError): |
| 1866 |
shutil.chown(filename, 3.14) |
| 1867 |
|
| 1868 |
uid = os.getuid() |
| 1869 |
gid = os.getgid() |
| 1870 |
|
| 1871 |
def check_chown(path, uid=None, gid=None): |
| 1872 |
s = os.stat(filename) |
| 1873 |
if uid is not None: |
| 1874 |
self.assertEqual(uid, s.st_uid) |
| 1875 |
if gid is not None: |
| 1876 |
self.assertEqual(gid, s.st_gid) |
| 1877 |
|
| 1878 |
shutil.chown(filename, uid, gid) |
| 1879 |
check_chown(filename, uid, gid) |
| 1880 |
shutil.chown(filename, uid) |
| 1881 |
check_chown(filename, uid) |
| 1882 |
shutil.chown(filename, user=uid) |
| 1883 |
check_chown(filename, uid) |
| 1884 |
shutil.chown(filename, group=gid) |
| 1885 |
check_chown(filename, gid=gid) |
| 1886 |
|
| 1887 |
shutil.chown(dirname, uid, gid) |
| 1888 |
check_chown(dirname, uid, gid) |
| 1889 |
shutil.chown(dirname, uid) |
| 1890 |
check_chown(dirname, uid) |
| 1891 |
shutil.chown(dirname, user=uid) |
| 1892 |
check_chown(dirname, uid) |
| 1893 |
shutil.chown(dirname, group=gid) |
| 1894 |
check_chown(dirname, gid=gid) |
| 1895 |
|
| 1896 |
try: |
| 1897 |
user = pwd.getpwuid(uid)[0] |
| 1898 |
group = grp.getgrgid(gid)[0] |
| 1899 |
except KeyError: |
| 1900 |
|
| 1901 |
pass |
| 1902 |
else: |
| 1903 |
shutil.chown(filename, user, group) |
| 1904 |
check_chown(filename, uid, gid) |
| 1905 |
shutil.chown(dirname, user, group) |
| 1906 |
check_chown(dirname, uid, gid) |
| 1907 |
|
| 1908 |
|
| 1909 |
class TestWhich(BaseTest, unittest.TestCase): |
| 1910 |
|
| 1911 |
def setUp(self): |
| 1912 |
self.temp_dir = self.mkdtemp(prefix="Tmp") |
| 1913 |
|
| 1914 |
|
| 1915 |
self.temp_file = tempfile.NamedTemporaryFile(dir=self.temp_dir, |
| 1916 |
prefix="Tmp", |
| 1917 |
suffix=".Exe") |
| 1918 |
os.chmod(self.temp_file.name, stat.S_IXUSR) |
| 1919 |
self.addCleanup(self.temp_file.close) |
| 1920 |
self.dir, self.file = os.path.split(self.temp_file.name) |
| 1921 |
self.env_path = self.dir |
| 1922 |
self.curdir = os.curdir |
| 1923 |
self.ext = ".EXE" |
| 1924 |
|
| 1925 |
def test_basic(self): |
| 1926 |
|
| 1927 |
rv = shutil.which(self.file, path=self.dir) |
| 1928 |
self.assertEqual(rv, self.temp_file.name) |
| 1929 |
|
| 1930 |
def test_absolute_cmd(self): |
| 1931 |
|
| 1932 |
|
| 1933 |
rv = shutil.which(self.temp_file.name, path=self.temp_dir) |
| 1934 |
self.assertEqual(rv, self.temp_file.name) |
| 1935 |
|
| 1936 |
def test_relative_cmd(self): |
| 1937 |
|
| 1938 |
|
| 1939 |
base_dir, tail_dir = os.path.split(self.dir) |
| 1940 |
relpath = os.path.join(tail_dir, self.file) |
| 1941 |
with os_helper.change_cwd(path=base_dir): |
| 1942 |
rv = shutil.which(relpath, path=self.temp_dir) |
| 1943 |
self.assertEqual(rv, relpath) |
| 1944 |
|
| 1945 |
with os_helper.change_cwd(path=self.dir): |
| 1946 |
rv = shutil.which(relpath, path=base_dir) |
| 1947 |
self.assertIsNone(rv) |
| 1948 |
|
| 1949 |
def test_cwd(self): |
| 1950 |
|
| 1951 |
base_dir = os.path.dirname(self.dir) |
| 1952 |
with os_helper.change_cwd(path=self.dir): |
| 1953 |
rv = shutil.which(self.file, path=base_dir) |
| 1954 |
if sys.platform == "win32": |
| 1955 |
|
| 1956 |
self.assertEqual(rv, os.path.join(self.curdir, self.file)) |
| 1957 |
else: |
| 1958 |
|
| 1959 |
self.assertIsNone(rv) |
| 1960 |
|
| 1961 |
@os_helper.skip_if_dac_override |
| 1962 |
def test_non_matching_mode(self): |
| 1963 |
|
| 1964 |
os.chmod(self.temp_file.name, stat.S_IREAD) |
| 1965 |
if os.access(self.temp_file.name, os.W_OK): |
| 1966 |
self.skipTest("can't set the file read-only") |
| 1967 |
rv = shutil.which(self.file, path=self.dir, mode=os.W_OK) |
| 1968 |
self.assertIsNone(rv) |
| 1969 |
|
| 1970 |
def test_relative_path(self): |
| 1971 |
base_dir, tail_dir = os.path.split(self.dir) |
| 1972 |
with os_helper.change_cwd(path=base_dir): |
| 1973 |
rv = shutil.which(self.file, path=tail_dir) |
| 1974 |
self.assertEqual(rv, os.path.join(tail_dir, self.file)) |
| 1975 |
|
| 1976 |
def test_nonexistent_file(self): |
| 1977 |
|
| 1978 |
rv = shutil.which("foo.exe", path=self.dir) |
| 1979 |
self.assertIsNone(rv) |
| 1980 |
|
| 1981 |
@unittest.skipUnless(sys.platform == "win32", |
| 1982 |
"pathext check is Windows-only") |
| 1983 |
def test_pathext_checking(self): |
| 1984 |
|
| 1985 |
|
| 1986 |
rv = shutil.which(self.file[:-4], path=self.dir) |
| 1987 |
self.assertEqual(rv, self.temp_file.name[:-4] + self.ext) |
| 1988 |
|
| 1989 |
def test_environ_path(self): |
| 1990 |
with os_helper.EnvironmentVarGuard() as env: |
| 1991 |
env['PATH'] = self.env_path |
| 1992 |
rv = shutil.which(self.file) |
| 1993 |
self.assertEqual(rv, self.temp_file.name) |
| 1994 |
|
| 1995 |
def test_environ_path_empty(self): |
| 1996 |
|
| 1997 |
with os_helper.EnvironmentVarGuard() as env: |
| 1998 |
env['PATH'] = '' |
| 1999 |
with unittest.mock.patch('os.confstr', return_value=self.dir, \ |
| 2000 |
create=True), \ |
| 2001 |
support.swap_attr(os, 'defpath', self.dir), \ |
| 2002 |
os_helper.change_cwd(self.dir): |
| 2003 |
rv = shutil.which(self.file) |
| 2004 |
self.assertIsNone(rv) |
| 2005 |
|
| 2006 |
def test_environ_path_cwd(self): |
| 2007 |
expected_cwd = os.path.basename(self.temp_file.name) |
| 2008 |
if sys.platform == "win32": |
| 2009 |
curdir = os.curdir |
| 2010 |
if isinstance(expected_cwd, bytes): |
| 2011 |
curdir = os.fsencode(curdir) |
| 2012 |
expected_cwd = os.path.join(curdir, expected_cwd) |
| 2013 |
|
| 2014 |
|
| 2015 |
with os_helper.EnvironmentVarGuard() as env: |
| 2016 |
env['PATH'] = os.pathsep |
| 2017 |
with unittest.mock.patch('os.confstr', return_value=self.dir, \ |
| 2018 |
create=True), \ |
| 2019 |
support.swap_attr(os, 'defpath', self.dir): |
| 2020 |
rv = shutil.which(self.file) |
| 2021 |
self.assertIsNone(rv) |
| 2022 |
|
| 2023 |
|
| 2024 |
with os_helper.change_cwd(self.dir): |
| 2025 |
rv = shutil.which(self.file) |
| 2026 |
self.assertEqual(rv, expected_cwd) |
| 2027 |
|
| 2028 |
def test_environ_path_missing(self): |
| 2029 |
with os_helper.EnvironmentVarGuard() as env: |
| 2030 |
env.pop('PATH', None) |
| 2031 |
|
| 2032 |
|
| 2033 |
with unittest.mock.patch('os.confstr', side_effect=ValueError, \ |
| 2034 |
create=True), \ |
| 2035 |
support.swap_attr(os, 'defpath', self.dir): |
| 2036 |
rv = shutil.which(self.file) |
| 2037 |
self.assertEqual(rv, self.temp_file.name) |
| 2038 |
|
| 2039 |
|
| 2040 |
with unittest.mock.patch('os.confstr', return_value=self.dir, \ |
| 2041 |
create=True), \ |
| 2042 |
support.swap_attr(os, 'defpath', ''): |
| 2043 |
rv = shutil.which(self.file) |
| 2044 |
self.assertEqual(rv, self.temp_file.name) |
| 2045 |
|
| 2046 |
def test_empty_path(self): |
| 2047 |
base_dir = os.path.dirname(self.dir) |
| 2048 |
with os_helper.change_cwd(path=self.dir), \ |
| 2049 |
os_helper.EnvironmentVarGuard() as env: |
| 2050 |
env['PATH'] = self.env_path |
| 2051 |
rv = shutil.which(self.file, path='') |
| 2052 |
self.assertIsNone(rv) |
| 2053 |
|
| 2054 |
def test_empty_path_no_PATH(self): |
| 2055 |
with os_helper.EnvironmentVarGuard() as env: |
| 2056 |
env.pop('PATH', None) |
| 2057 |
rv = shutil.which(self.file) |
| 2058 |
self.assertIsNone(rv) |
| 2059 |
|
| 2060 |
@unittest.skipUnless(sys.platform == "win32", 'test specific to Windows') |
| 2061 |
def test_pathext(self): |
| 2062 |
ext = ".xyz" |
| 2063 |
temp_filexyz = tempfile.NamedTemporaryFile(dir=self.temp_dir, |
| 2064 |
prefix="Tmp2", suffix=ext) |
| 2065 |
os.chmod(temp_filexyz.name, stat.S_IXUSR) |
| 2066 |
self.addCleanup(temp_filexyz.close) |
| 2067 |
|
| 2068 |
|
| 2069 |
program = os.path.basename(temp_filexyz.name) |
| 2070 |
program = os.path.splitext(program)[0] |
| 2071 |
|
| 2072 |
with os_helper.EnvironmentVarGuard() as env: |
| 2073 |
env['PATHEXT'] = ext |
| 2074 |
rv = shutil.which(program, path=self.temp_dir) |
| 2075 |
self.assertEqual(rv, temp_filexyz.name) |
| 2076 |
|
| 2077 |
|
| 2078 |
@unittest.skipUnless(sys.platform == "win32", 'test specific to Windows') |
| 2079 |
def test_pathext_with_empty_str(self): |
| 2080 |
ext = ".xyz" |
| 2081 |
temp_filexyz = tempfile.NamedTemporaryFile(dir=self.temp_dir, |
| 2082 |
prefix="Tmp2", suffix=ext) |
| 2083 |
self.addCleanup(temp_filexyz.close) |
| 2084 |
|
| 2085 |
|
| 2086 |
program = os.path.basename(temp_filexyz.name) |
| 2087 |
program = os.path.splitext(program)[0] |
| 2088 |
|
| 2089 |
with os_helper.EnvironmentVarGuard() as env: |
| 2090 |
env['PATHEXT'] = f"{ext};" |
| 2091 |
rv = shutil.which(program, path=self.temp_dir) |
| 2092 |
self.assertEqual(rv, temp_filexyz.name) |
| 2093 |
|
| 2094 |
|
| 2095 |
class TestWhichBytes(TestWhich): |
| 2096 |
def setUp(self): |
| 2097 |
TestWhich.setUp(self) |
| 2098 |
self.dir = os.fsencode(self.dir) |
| 2099 |
self.file = os.fsencode(self.file) |
| 2100 |
self.temp_file.name = os.fsencode(self.temp_file.name) |
| 2101 |
self.curdir = os.fsencode(self.curdir) |
| 2102 |
self.ext = os.fsencode(self.ext) |
| 2103 |
|
| 2104 |
|
| 2105 |
class TestMove(BaseTest, unittest.TestCase): |
| 2106 |
|
| 2107 |
def setUp(self): |
| 2108 |
filename = "foo" |
| 2109 |
self.src_dir = self.mkdtemp() |
| 2110 |
self.dst_dir = self.mkdtemp() |
| 2111 |
self.src_file = os.path.join(self.src_dir, filename) |
| 2112 |
self.dst_file = os.path.join(self.dst_dir, filename) |
| 2113 |
with open(self.src_file, "wb") as f: |
| 2114 |
f.write(b"spam") |
| 2115 |
|
| 2116 |
def _check_move_file(self, src, dst, real_dst): |
| 2117 |
with open(src, "rb") as f: |
| 2118 |
contents = f.read() |
| 2119 |
shutil.move(src, dst) |
| 2120 |
with open(real_dst, "rb") as f: |
| 2121 |
self.assertEqual(contents, f.read()) |
| 2122 |
self.assertFalse(os.path.exists(src)) |
| 2123 |
|
| 2124 |
def _check_move_dir(self, src, dst, real_dst): |
| 2125 |
contents = sorted(os.listdir(src)) |
| 2126 |
shutil.move(src, dst) |
| 2127 |
self.assertEqual(contents, sorted(os.listdir(real_dst))) |
| 2128 |
self.assertFalse(os.path.exists(src)) |
| 2129 |
|
| 2130 |
def test_move_file(self): |
| 2131 |
|
| 2132 |
self._check_move_file(self.src_file, self.dst_file, self.dst_file) |
| 2133 |
|
| 2134 |
def test_move_file_to_dir(self): |
| 2135 |
|
| 2136 |
self._check_move_file(self.src_file, self.dst_dir, self.dst_file) |
| 2137 |
|
| 2138 |
def test_move_file_to_dir_pathlike_src(self): |
| 2139 |
|
| 2140 |
src = pathlib.Path(self.src_file) |
| 2141 |
self._check_move_file(src, self.dst_dir, self.dst_file) |
| 2142 |
|
| 2143 |
def test_move_file_to_dir_pathlike_dst(self): |
| 2144 |
|
| 2145 |
dst = pathlib.Path(self.dst_dir) |
| 2146 |
self._check_move_file(self.src_file, dst, self.dst_file) |
| 2147 |
|
| 2148 |
@mock_rename |
| 2149 |
def test_move_file_other_fs(self): |
| 2150 |
|
| 2151 |
self.test_move_file() |
| 2152 |
|
| 2153 |
@mock_rename |
| 2154 |
def test_move_file_to_dir_other_fs(self): |
| 2155 |
|
| 2156 |
self.test_move_file_to_dir() |
| 2157 |
|
| 2158 |
def test_move_dir(self): |
| 2159 |
|
| 2160 |
dst_dir = tempfile.mktemp(dir=self.mkdtemp()) |
| 2161 |
try: |
| 2162 |
self._check_move_dir(self.src_dir, dst_dir, dst_dir) |
| 2163 |
finally: |
| 2164 |
os_helper.rmtree(dst_dir) |
| 2165 |
|
| 2166 |
@mock_rename |
| 2167 |
def test_move_dir_other_fs(self): |
| 2168 |
|
| 2169 |
self.test_move_dir() |
| 2170 |
|
| 2171 |
def test_move_dir_to_dir(self): |
| 2172 |
|
| 2173 |
self._check_move_dir(self.src_dir, self.dst_dir, |
| 2174 |
os.path.join(self.dst_dir, os.path.basename(self.src_dir))) |
| 2175 |
|
| 2176 |
@mock_rename |
| 2177 |
def test_move_dir_to_dir_other_fs(self): |
| 2178 |
|
| 2179 |
self.test_move_dir_to_dir() |
| 2180 |
|
| 2181 |
def test_move_dir_sep_to_dir(self): |
| 2182 |
self._check_move_dir(self.src_dir + os.path.sep, self.dst_dir, |
| 2183 |
os.path.join(self.dst_dir, os.path.basename(self.src_dir))) |
| 2184 |
|
| 2185 |
@unittest.skipUnless(os.path.altsep, 'requires os.path.altsep') |
| 2186 |
def test_move_dir_altsep_to_dir(self): |
| 2187 |
self._check_move_dir(self.src_dir + os.path.altsep, self.dst_dir, |
| 2188 |
os.path.join(self.dst_dir, os.path.basename(self.src_dir))) |
| 2189 |
|
| 2190 |
def test_existing_file_inside_dest_dir(self): |
| 2191 |
|
| 2192 |
with open(self.dst_file, "wb"): |
| 2193 |
pass |
| 2194 |
self.assertRaises(shutil.Error, shutil.move, self.src_file, self.dst_dir) |
| 2195 |
|
| 2196 |
def test_dont_move_dir_in_itself(self): |
| 2197 |
|
| 2198 |
dst = os.path.join(self.src_dir, "bar") |
| 2199 |
self.assertRaises(shutil.Error, shutil.move, self.src_dir, dst) |
| 2200 |
|
| 2201 |
def test_destinsrc_false_negative(self): |
| 2202 |
os.mkdir(TESTFN) |
| 2203 |
try: |
| 2204 |
for src, dst in [('srcdir', 'srcdir/dest')]: |
| 2205 |
src = os.path.join(TESTFN, src) |
| 2206 |
dst = os.path.join(TESTFN, dst) |
| 2207 |
self.assertTrue(shutil._destinsrc(src, dst), |
| 2208 |
msg='_destinsrc() wrongly concluded that ' |
| 2209 |
'dst (%s) is not in src (%s)' % (dst, src)) |
| 2210 |
finally: |
| 2211 |
os_helper.rmtree(TESTFN) |
| 2212 |
|
| 2213 |
def test_destinsrc_false_positive(self): |
| 2214 |
os.mkdir(TESTFN) |
| 2215 |
try: |
| 2216 |
for src, dst in [('srcdir', 'src/dest'), ('srcdir', 'srcdir.new')]: |
| 2217 |
src = os.path.join(TESTFN, src) |
| 2218 |
dst = os.path.join(TESTFN, dst) |
| 2219 |
self.assertFalse(shutil._destinsrc(src, dst), |
| 2220 |
msg='_destinsrc() wrongly concluded that ' |
| 2221 |
'dst (%s) is in src (%s)' % (dst, src)) |
| 2222 |
finally: |
| 2223 |
os_helper.rmtree(TESTFN) |
| 2224 |
|
| 2225 |
@os_helper.skip_unless_symlink |
| 2226 |
@mock_rename |
| 2227 |
def test_move_file_symlink(self): |
| 2228 |
dst = os.path.join(self.src_dir, 'bar') |
| 2229 |
os.symlink(self.src_file, dst) |
| 2230 |
shutil.move(dst, self.dst_file) |
| 2231 |
self.assertTrue(os.path.islink(self.dst_file)) |
| 2232 |
self.assertTrue(os.path.samefile(self.src_file, self.dst_file)) |
| 2233 |
|
| 2234 |
@os_helper.skip_unless_symlink |
| 2235 |
@mock_rename |
| 2236 |
def test_move_file_symlink_to_dir(self): |
| 2237 |
filename = "bar" |
| 2238 |
dst = os.path.join(self.src_dir, filename) |
| 2239 |
os.symlink(self.src_file, dst) |
| 2240 |
shutil.move(dst, self.dst_dir) |
| 2241 |
final_link = os.path.join(self.dst_dir, filename) |
| 2242 |
self.assertTrue(os.path.islink(final_link)) |
| 2243 |
self.assertTrue(os.path.samefile(self.src_file, final_link)) |
| 2244 |
|
| 2245 |
@os_helper.skip_unless_symlink |
| 2246 |
@mock_rename |
| 2247 |
def test_move_dangling_symlink(self): |
| 2248 |
src = os.path.join(self.src_dir, 'baz') |
| 2249 |
dst = os.path.join(self.src_dir, 'bar') |
| 2250 |
os.symlink(src, dst) |
| 2251 |
dst_link = os.path.join(self.dst_dir, 'quux') |
| 2252 |
shutil.move(dst, dst_link) |
| 2253 |
self.assertTrue(os.path.islink(dst_link)) |
| 2254 |
self.assertEqual(os.path.realpath(src), os.path.realpath(dst_link)) |
| 2255 |
|
| 2256 |
@os_helper.skip_unless_symlink |
| 2257 |
@mock_rename |
| 2258 |
def test_move_dir_symlink(self): |
| 2259 |
src = os.path.join(self.src_dir, 'baz') |
| 2260 |
dst = os.path.join(self.src_dir, 'bar') |
| 2261 |
os.mkdir(src) |
| 2262 |
os.symlink(src, dst) |
| 2263 |
dst_link = os.path.join(self.dst_dir, 'quux') |
| 2264 |
shutil.move(dst, dst_link) |
| 2265 |
self.assertTrue(os.path.islink(dst_link)) |
| 2266 |
self.assertTrue(os.path.samefile(src, dst_link)) |
| 2267 |
|
| 2268 |
def test_move_return_value(self): |
| 2269 |
rv = shutil.move(self.src_file, self.dst_dir) |
| 2270 |
self.assertEqual(rv, |
| 2271 |
os.path.join(self.dst_dir, os.path.basename(self.src_file))) |
| 2272 |
|
| 2273 |
def test_move_as_rename_return_value(self): |
| 2274 |
rv = shutil.move(self.src_file, os.path.join(self.dst_dir, 'bar')) |
| 2275 |
self.assertEqual(rv, os.path.join(self.dst_dir, 'bar')) |
| 2276 |
|
| 2277 |
@mock_rename |
| 2278 |
def test_move_file_special_function(self): |
| 2279 |
moved = [] |
| 2280 |
def _copy(src, dst): |
| 2281 |
moved.append((src, dst)) |
| 2282 |
shutil.move(self.src_file, self.dst_dir, copy_function=_copy) |
| 2283 |
self.assertEqual(len(moved), 1) |
| 2284 |
|
| 2285 |
@mock_rename |
| 2286 |
def test_move_dir_special_function(self): |
| 2287 |
moved = [] |
| 2288 |
def _copy(src, dst): |
| 2289 |
moved.append((src, dst)) |
| 2290 |
os_helper.create_empty_file(os.path.join(self.src_dir, 'child')) |
| 2291 |
os_helper.create_empty_file(os.path.join(self.src_dir, 'child1')) |
| 2292 |
shutil.move(self.src_dir, self.dst_dir, copy_function=_copy) |
| 2293 |
self.assertEqual(len(moved), 3) |
| 2294 |
|
| 2295 |
def test_move_dir_caseinsensitive(self): |
| 2296 |
|
| 2297 |
|
| 2298 |
|
| 2299 |
self.src_dir = self.mkdtemp() |
| 2300 |
dst_dir = os.path.join( |
| 2301 |
os.path.dirname(self.src_dir), |
| 2302 |
os.path.basename(self.src_dir).upper()) |
| 2303 |
self.assertNotEqual(self.src_dir, dst_dir) |
| 2304 |
|
| 2305 |
try: |
| 2306 |
shutil.move(self.src_dir, dst_dir) |
| 2307 |
self.assertTrue(os.path.isdir(dst_dir)) |
| 2308 |
finally: |
| 2309 |
os.rmdir(dst_dir) |
| 2310 |
|
| 2311 |
|
| 2312 |
|
| 2313 |
@mock_rename |
| 2314 |
def _test_move_symlink_to_dir_into_dir(self, dst): |
| 2315 |
src = os.path.join(self.src_dir, 'linktodir') |
| 2316 |
dst_link = os.path.join(self.dst_dir, 'linktodir') |
| 2317 |
os.symlink(self.dst_dir, src, target_is_directory=True) |
| 2318 |
shutil.move(src, dst) |
| 2319 |
self.assertTrue(os.path.islink(dst_link)) |
| 2320 |
self.assertTrue(os.path.samefile(self.dst_dir, dst_link)) |
| 2321 |
self.assertFalse(os.path.exists(src)) |
| 2322 |
|
| 2323 |
|
| 2324 |
|
| 2325 |
os.symlink(self.dst_dir, src, target_is_directory=True) |
| 2326 |
with self.assertRaises(shutil.Error): |
| 2327 |
shutil.move(src, dst) |
| 2328 |
self.assertTrue(os.path.samefile(self.dst_dir, dst_link)) |
| 2329 |
self.assertTrue(os.path.exists(src)) |
| 2330 |
|
| 2331 |
@os_helper.skip_unless_symlink |
| 2332 |
def test_move_symlink_to_dir_into_dir(self): |
| 2333 |
self._test_move_symlink_to_dir_into_dir(self.dst_dir) |
| 2334 |
|
| 2335 |
@os_helper.skip_unless_symlink |
| 2336 |
def test_move_symlink_to_dir_into_symlink_to_dir(self): |
| 2337 |
dst = os.path.join(self.src_dir, 'otherlinktodir') |
| 2338 |
os.symlink(self.dst_dir, dst, target_is_directory=True) |
| 2339 |
self._test_move_symlink_to_dir_into_dir(dst) |
| 2340 |
|
| 2341 |
@os_helper.skip_unless_dac_override |
| 2342 |
@unittest.skipUnless(hasattr(os, 'lchflags') |
| 2343 |
and hasattr(stat, 'SF_IMMUTABLE') |
| 2344 |
and hasattr(stat, 'UF_OPAQUE'), |
| 2345 |
'requires lchflags') |
| 2346 |
def test_move_dir_permission_denied(self): |
| 2347 |
|
| 2348 |
|
| 2349 |
try: |
| 2350 |
os.mkdir(TESTFN_SRC) |
| 2351 |
os.lchflags(TESTFN_SRC, stat.SF_IMMUTABLE) |
| 2352 |
|
| 2353 |
|
| 2354 |
|
| 2355 |
self.assertRaises(PermissionError, shutil.move, TESTFN_SRC, TESTFN_DST) |
| 2356 |
self.assertFalse(TESTFN_DST in os.listdir()) |
| 2357 |
|
| 2358 |
|
| 2359 |
os.lchflags(TESTFN_SRC, stat.UF_OPAQUE) |
| 2360 |
os_helper.create_empty_file(os.path.join(TESTFN_SRC, 'child')) |
| 2361 |
os.lchflags(TESTFN_SRC, stat.SF_IMMUTABLE) |
| 2362 |
|
| 2363 |
|
| 2364 |
|
| 2365 |
self.assertRaises(PermissionError, shutil.move, TESTFN_SRC, TESTFN_DST) |
| 2366 |
self.assertFalse(TESTFN_DST in os.listdir()) |
| 2367 |
finally: |
| 2368 |
if os.path.exists(TESTFN_SRC): |
| 2369 |
os.lchflags(TESTFN_SRC, stat.UF_OPAQUE) |
| 2370 |
os_helper.rmtree(TESTFN_SRC) |
| 2371 |
if os.path.exists(TESTFN_DST): |
| 2372 |
os.lchflags(TESTFN_DST, stat.UF_OPAQUE) |
| 2373 |
os_helper.rmtree(TESTFN_DST) |
| 2374 |
|
| 2375 |
|
| 2376 |
class TestCopyFile(unittest.TestCase): |
| 2377 |
|
| 2378 |
class Faux(object): |
| 2379 |
_entered = False |
| 2380 |
_exited_with = None |
| 2381 |
_raised = False |
| 2382 |
def __init__(self, raise_in_exit=False, suppress_at_exit=True): |
| 2383 |
self._raise_in_exit = raise_in_exit |
| 2384 |
self._suppress_at_exit = suppress_at_exit |
| 2385 |
def read(self, *args): |
| 2386 |
return '' |
| 2387 |
def __enter__(self): |
| 2388 |
self._entered = True |
| 2389 |
def __exit__(self, exc_type, exc_val, exc_tb): |
| 2390 |
self._exited_with = exc_type, exc_val, exc_tb |
| 2391 |
if self._raise_in_exit: |
| 2392 |
self._raised = True |
| 2393 |
raise OSError("Cannot close") |
| 2394 |
return self._suppress_at_exit |
| 2395 |
|
| 2396 |
def test_w_source_open_fails(self): |
| 2397 |
def _open(filename, mode='r'): |
| 2398 |
if filename == 'srcfile': |
| 2399 |
raise OSError('Cannot open "srcfile"') |
| 2400 |
assert 0 |
| 2401 |
|
| 2402 |
with support.swap_attr(shutil, 'open', _open): |
| 2403 |
with self.assertRaises(OSError): |
| 2404 |
shutil.copyfile('srcfile', 'destfile') |
| 2405 |
|
| 2406 |
@unittest.skipIf(MACOS, "skipped on macOS") |
| 2407 |
def test_w_dest_open_fails(self): |
| 2408 |
srcfile = self.Faux() |
| 2409 |
|
| 2410 |
def _open(filename, mode='r'): |
| 2411 |
if filename == 'srcfile': |
| 2412 |
return srcfile |
| 2413 |
if filename == 'destfile': |
| 2414 |
raise OSError('Cannot open "destfile"') |
| 2415 |
assert 0 |
| 2416 |
|
| 2417 |
with support.swap_attr(shutil, 'open', _open): |
| 2418 |
shutil.copyfile('srcfile', 'destfile') |
| 2419 |
self.assertTrue(srcfile._entered) |
| 2420 |
self.assertTrue(srcfile._exited_with[0] is OSError) |
| 2421 |
self.assertEqual(srcfile._exited_with[1].args, |
| 2422 |
('Cannot open "destfile"',)) |
| 2423 |
|
| 2424 |
@unittest.skipIf(MACOS, "skipped on macOS") |
| 2425 |
def test_w_dest_close_fails(self): |
| 2426 |
srcfile = self.Faux() |
| 2427 |
destfile = self.Faux(True) |
| 2428 |
|
| 2429 |
def _open(filename, mode='r'): |
| 2430 |
if filename == 'srcfile': |
| 2431 |
return srcfile |
| 2432 |
if filename == 'destfile': |
| 2433 |
return destfile |
| 2434 |
assert 0 |
| 2435 |
|
| 2436 |
with support.swap_attr(shutil, 'open', _open): |
| 2437 |
shutil.copyfile('srcfile', 'destfile') |
| 2438 |
self.assertTrue(srcfile._entered) |
| 2439 |
self.assertTrue(destfile._entered) |
| 2440 |
self.assertTrue(destfile._raised) |
| 2441 |
self.assertTrue(srcfile._exited_with[0] is OSError) |
| 2442 |
self.assertEqual(srcfile._exited_with[1].args, |
| 2443 |
('Cannot close',)) |
| 2444 |
|
| 2445 |
@unittest.skipIf(MACOS, "skipped on macOS") |
| 2446 |
def test_w_source_close_fails(self): |
| 2447 |
|
| 2448 |
srcfile = self.Faux(True) |
| 2449 |
destfile = self.Faux() |
| 2450 |
|
| 2451 |
def _open(filename, mode='r'): |
| 2452 |
if filename == 'srcfile': |
| 2453 |
return srcfile |
| 2454 |
if filename == 'destfile': |
| 2455 |
return destfile |
| 2456 |
assert 0 |
| 2457 |
|
| 2458 |
with support.swap_attr(shutil, 'open', _open): |
| 2459 |
with self.assertRaises(OSError): |
| 2460 |
shutil.copyfile('srcfile', 'destfile') |
| 2461 |
self.assertTrue(srcfile._entered) |
| 2462 |
self.assertTrue(destfile._entered) |
| 2463 |
self.assertFalse(destfile._raised) |
| 2464 |
self.assertTrue(srcfile._exited_with[0] is None) |
| 2465 |
self.assertTrue(srcfile._raised) |
| 2466 |
|
| 2467 |
|
| 2468 |
class TestCopyFileObj(unittest.TestCase): |
| 2469 |
FILESIZE = 2 * 1024 * 1024 |
| 2470 |
|
| 2471 |
@classmethod |
| 2472 |
def setUpClass(cls): |
| 2473 |
write_test_file(TESTFN, cls.FILESIZE) |
| 2474 |
|
| 2475 |
@classmethod |
| 2476 |
def tearDownClass(cls): |
| 2477 |
os_helper.unlink(TESTFN) |
| 2478 |
os_helper.unlink(TESTFN2) |
| 2479 |
|
| 2480 |
def tearDown(self): |
| 2481 |
os_helper.unlink(TESTFN2) |
| 2482 |
|
| 2483 |
@contextlib.contextmanager |
| 2484 |
def get_files(self): |
| 2485 |
with open(TESTFN, "rb") as src: |
| 2486 |
with open(TESTFN2, "wb") as dst: |
| 2487 |
yield (src, dst) |
| 2488 |
|
| 2489 |
def assert_files_eq(self, src, dst): |
| 2490 |
with open(src, 'rb') as fsrc: |
| 2491 |
with open(dst, 'rb') as fdst: |
| 2492 |
self.assertEqual(fsrc.read(), fdst.read()) |
| 2493 |
|
| 2494 |
def test_content(self): |
| 2495 |
with self.get_files() as (src, dst): |
| 2496 |
shutil.copyfileobj(src, dst) |
| 2497 |
self.assert_files_eq(TESTFN, TESTFN2) |
| 2498 |
|
| 2499 |
def test_file_not_closed(self): |
| 2500 |
with self.get_files() as (src, dst): |
| 2501 |
shutil.copyfileobj(src, dst) |
| 2502 |
assert not src.closed |
| 2503 |
assert not dst.closed |
| 2504 |
|
| 2505 |
def test_file_offset(self): |
| 2506 |
with self.get_files() as (src, dst): |
| 2507 |
shutil.copyfileobj(src, dst) |
| 2508 |
self.assertEqual(src.tell(), self.FILESIZE) |
| 2509 |
self.assertEqual(dst.tell(), self.FILESIZE) |
| 2510 |
|
| 2511 |
@unittest.skipIf(os.name != 'nt', "Windows only") |
| 2512 |
def test_win_impl(self): |
| 2513 |
|
| 2514 |
with unittest.mock.patch("shutil._copyfileobj_readinto") as m: |
| 2515 |
shutil.copyfile(TESTFN, TESTFN2) |
| 2516 |
assert m.called |
| 2517 |
|
| 2518 |
|
| 2519 |
self.assertEqual(m.call_args[0][2], 1 * 1024 * 1024) |
| 2520 |
|
| 2521 |
|
| 2522 |
|
| 2523 |
with tempfile.NamedTemporaryFile(dir=os.getcwd(), delete=False) as f: |
| 2524 |
f.write(b'foo') |
| 2525 |
fname = f.name |
| 2526 |
self.addCleanup(os_helper.unlink, fname) |
| 2527 |
with unittest.mock.patch("shutil._copyfileobj_readinto") as m: |
| 2528 |
shutil.copyfile(fname, TESTFN2) |
| 2529 |
self.assertEqual(m.call_args[0][2], 3) |
| 2530 |
|
| 2531 |
|
| 2532 |
with tempfile.NamedTemporaryFile(dir=os.getcwd(), delete=False) as f: |
| 2533 |
pass |
| 2534 |
fname = f.name |
| 2535 |
self.addCleanup(os_helper.unlink, fname) |
| 2536 |
with unittest.mock.patch("shutil._copyfileobj_readinto") as m: |
| 2537 |
shutil.copyfile(fname, TESTFN2) |
| 2538 |
assert not m.called |
| 2539 |
self.assert_files_eq(fname, TESTFN2) |
| 2540 |
|
| 2541 |
|
| 2542 |
class _ZeroCopyFileTest(object): |
| 2543 |
"""Tests common to all zero-copy APIs.""" |
| 2544 |
FILESIZE = (10 * 1024 * 1024) |
| 2545 |
FILEDATA = b"" |
| 2546 |
PATCHPOINT = "" |
| 2547 |
|
| 2548 |
@classmethod |
| 2549 |
def setUpClass(cls): |
| 2550 |
write_test_file(TESTFN, cls.FILESIZE) |
| 2551 |
with open(TESTFN, 'rb') as f: |
| 2552 |
cls.FILEDATA = f.read() |
| 2553 |
assert len(cls.FILEDATA) == cls.FILESIZE |
| 2554 |
|
| 2555 |
@classmethod |
| 2556 |
def tearDownClass(cls): |
| 2557 |
os_helper.unlink(TESTFN) |
| 2558 |
|
| 2559 |
def tearDown(self): |
| 2560 |
os_helper.unlink(TESTFN2) |
| 2561 |
|
| 2562 |
@contextlib.contextmanager |
| 2563 |
def get_files(self): |
| 2564 |
with open(TESTFN, "rb") as src: |
| 2565 |
with open(TESTFN2, "wb") as dst: |
| 2566 |
yield (src, dst) |
| 2567 |
|
| 2568 |
def zerocopy_fun(self, *args, **kwargs): |
| 2569 |
raise NotImplementedError("must be implemented in subclass") |
| 2570 |
|
| 2571 |
def reset(self): |
| 2572 |
self.tearDown() |
| 2573 |
self.tearDownClass() |
| 2574 |
self.setUpClass() |
| 2575 |
self.setUp() |
| 2576 |
|
| 2577 |
|
| 2578 |
|
| 2579 |
def test_regular_copy(self): |
| 2580 |
with self.get_files() as (src, dst): |
| 2581 |
self.zerocopy_fun(src, dst) |
| 2582 |
self.assertEqual(read_file(TESTFN2, binary=True), self.FILEDATA) |
| 2583 |
|
| 2584 |
with self.get_files() as (src, dst): |
| 2585 |
with unittest.mock.patch('shutil.copyfileobj') as m: |
| 2586 |
shutil.copyfile(TESTFN, TESTFN2) |
| 2587 |
assert not m.called |
| 2588 |
|
| 2589 |
def test_same_file(self): |
| 2590 |
self.addCleanup(self.reset) |
| 2591 |
with self.get_files() as (src, dst): |
| 2592 |
with self.assertRaises((OSError, _GiveupOnFastCopy)): |
| 2593 |
self.zerocopy_fun(src, src) |
| 2594 |
|
| 2595 |
self.assertEqual(read_file(TESTFN, binary=True), self.FILEDATA) |
| 2596 |
|
| 2597 |
def test_non_existent_src(self): |
| 2598 |
name = tempfile.mktemp(dir=os.getcwd()) |
| 2599 |
with self.assertRaises(FileNotFoundError) as cm: |
| 2600 |
shutil.copyfile(name, "new") |
| 2601 |
self.assertEqual(cm.exception.filename, name) |
| 2602 |
|
| 2603 |
def test_empty_file(self): |
| 2604 |
srcname = TESTFN + 'src' |
| 2605 |
dstname = TESTFN + 'dst' |
| 2606 |
self.addCleanup(lambda: os_helper.unlink(srcname)) |
| 2607 |
self.addCleanup(lambda: os_helper.unlink(dstname)) |
| 2608 |
with open(srcname, "wb"): |
| 2609 |
pass |
| 2610 |
|
| 2611 |
with open(srcname, "rb") as src: |
| 2612 |
with open(dstname, "wb") as dst: |
| 2613 |
self.zerocopy_fun(src, dst) |
| 2614 |
|
| 2615 |
self.assertEqual(read_file(dstname, binary=True), b"") |
| 2616 |
|
| 2617 |
def test_unhandled_exception(self): |
| 2618 |
with unittest.mock.patch(self.PATCHPOINT, |
| 2619 |
side_effect=ZeroDivisionError): |
| 2620 |
self.assertRaises(ZeroDivisionError, |
| 2621 |
shutil.copyfile, TESTFN, TESTFN2) |
| 2622 |
|
| 2623 |
def test_exception_on_first_call(self): |
| 2624 |
|
| 2625 |
|
| 2626 |
|
| 2627 |
with unittest.mock.patch(self.PATCHPOINT, |
| 2628 |
side_effect=OSError(errno.EINVAL, "yo")): |
| 2629 |
with self.get_files() as (src, dst): |
| 2630 |
with self.assertRaises(_GiveupOnFastCopy): |
| 2631 |
self.zerocopy_fun(src, dst) |
| 2632 |
|
| 2633 |
def test_filesystem_full(self): |
| 2634 |
|
| 2635 |
|
| 2636 |
with unittest.mock.patch(self.PATCHPOINT, |
| 2637 |
side_effect=OSError(errno.ENOSPC, "yo")): |
| 2638 |
with self.get_files() as (src, dst): |
| 2639 |
self.assertRaises(OSError, self.zerocopy_fun, src, dst) |
| 2640 |
|
| 2641 |
|
| 2642 |
@unittest.skipIf(not SUPPORTS_SENDFILE, 'os.sendfile() not supported') |
| 2643 |
class TestZeroCopySendfile(_ZeroCopyFileTest, unittest.TestCase): |
| 2644 |
PATCHPOINT = "os.sendfile" |
| 2645 |
|
| 2646 |
def zerocopy_fun(self, fsrc, fdst): |
| 2647 |
return shutil._fastcopy_sendfile(fsrc, fdst) |
| 2648 |
|
| 2649 |
def test_non_regular_file_src(self): |
| 2650 |
with io.BytesIO(self.FILEDATA) as src: |
| 2651 |
with open(TESTFN2, "wb") as dst: |
| 2652 |
with self.assertRaises(_GiveupOnFastCopy): |
| 2653 |
self.zerocopy_fun(src, dst) |
| 2654 |
shutil.copyfileobj(src, dst) |
| 2655 |
|
| 2656 |
self.assertEqual(read_file(TESTFN2, binary=True), self.FILEDATA) |
| 2657 |
|
| 2658 |
def test_non_regular_file_dst(self): |
| 2659 |
with open(TESTFN, "rb") as src: |
| 2660 |
with io.BytesIO() as dst: |
| 2661 |
with self.assertRaises(_GiveupOnFastCopy): |
| 2662 |
self.zerocopy_fun(src, dst) |
| 2663 |
shutil.copyfileobj(src, dst) |
| 2664 |
dst.seek(0) |
| 2665 |
self.assertEqual(dst.read(), self.FILEDATA) |
| 2666 |
|
| 2667 |
def test_exception_on_second_call(self): |
| 2668 |
def sendfile(*args, **kwargs): |
| 2669 |
if not flag: |
| 2670 |
flag.append(None) |
| 2671 |
return orig_sendfile(*args, **kwargs) |
| 2672 |
else: |
| 2673 |
raise OSError(errno.EBADF, "yo") |
| 2674 |
|
| 2675 |
flag = [] |
| 2676 |
orig_sendfile = os.sendfile |
| 2677 |
with unittest.mock.patch('os.sendfile', create=True, |
| 2678 |
side_effect=sendfile): |
| 2679 |
with self.get_files() as (src, dst): |
| 2680 |
with self.assertRaises(OSError) as cm: |
| 2681 |
shutil._fastcopy_sendfile(src, dst) |
| 2682 |
assert flag |
| 2683 |
self.assertEqual(cm.exception.errno, errno.EBADF) |
| 2684 |
|
| 2685 |
def test_cant_get_size(self): |
| 2686 |
|
| 2687 |
|
| 2688 |
|
| 2689 |
with unittest.mock.patch('os.fstat', side_effect=OSError) as m: |
| 2690 |
with self.get_files() as (src, dst): |
| 2691 |
shutil._fastcopy_sendfile(src, dst) |
| 2692 |
assert m.called |
| 2693 |
self.assertEqual(read_file(TESTFN2, binary=True), self.FILEDATA) |
| 2694 |
|
| 2695 |
def test_small_chunks(self): |
| 2696 |
|
| 2697 |
|
| 2698 |
|
| 2699 |
|
| 2700 |
mock = unittest.mock.Mock() |
| 2701 |
mock.st_size = 65536 + 1 |
| 2702 |
with unittest.mock.patch('os.fstat', return_value=mock) as m: |
| 2703 |
with self.get_files() as (src, dst): |
| 2704 |
shutil._fastcopy_sendfile(src, dst) |
| 2705 |
assert m.called |
| 2706 |
self.assertEqual(read_file(TESTFN2, binary=True), self.FILEDATA) |
| 2707 |
|
| 2708 |
def test_big_chunk(self): |
| 2709 |
|
| 2710 |
|
| 2711 |
|
| 2712 |
|
| 2713 |
mock = unittest.mock.Mock() |
| 2714 |
mock.st_size = self.FILESIZE + (100 * 1024 * 1024) |
| 2715 |
with unittest.mock.patch('os.fstat', return_value=mock) as m: |
| 2716 |
with self.get_files() as (src, dst): |
| 2717 |
shutil._fastcopy_sendfile(src, dst) |
| 2718 |
assert m.called |
| 2719 |
self.assertEqual(read_file(TESTFN2, binary=True), self.FILEDATA) |
| 2720 |
|
| 2721 |
def test_blocksize_arg(self): |
| 2722 |
with unittest.mock.patch('os.sendfile', |
| 2723 |
side_effect=ZeroDivisionError) as m: |
| 2724 |
self.assertRaises(ZeroDivisionError, |
| 2725 |
shutil.copyfile, TESTFN, TESTFN2) |
| 2726 |
blocksize = m.call_args[0][3] |
| 2727 |
|
| 2728 |
|
| 2729 |
self.assertEqual(blocksize, os.path.getsize(TESTFN)) |
| 2730 |
|
| 2731 |
os_helper.unlink(TESTFN2) |
| 2732 |
write_file(TESTFN2, b"hello", binary=True) |
| 2733 |
self.addCleanup(os_helper.unlink, TESTFN2 + '3') |
| 2734 |
self.assertRaises(ZeroDivisionError, |
| 2735 |
shutil.copyfile, TESTFN2, TESTFN2 + '3') |
| 2736 |
blocksize = m.call_args[0][3] |
| 2737 |
self.assertEqual(blocksize, 2 ** 23) |
| 2738 |
|
| 2739 |
def test_file2file_not_supported(self): |
| 2740 |
|
| 2741 |
|
| 2742 |
|
| 2743 |
assert shutil._USE_CP_SENDFILE |
| 2744 |
try: |
| 2745 |
with unittest.mock.patch( |
| 2746 |
self.PATCHPOINT, |
| 2747 |
side_effect=OSError(errno.ENOTSOCK, "yo")) as m: |
| 2748 |
with self.get_files() as (src, dst): |
| 2749 |
with self.assertRaises(_GiveupOnFastCopy): |
| 2750 |
shutil._fastcopy_sendfile(src, dst) |
| 2751 |
assert m.called |
| 2752 |
assert not shutil._USE_CP_SENDFILE |
| 2753 |
|
| 2754 |
with unittest.mock.patch(self.PATCHPOINT) as m: |
| 2755 |
shutil.copyfile(TESTFN, TESTFN2) |
| 2756 |
assert not m.called |
| 2757 |
finally: |
| 2758 |
shutil._USE_CP_SENDFILE = True |
| 2759 |
|
| 2760 |
|
| 2761 |
@unittest.skipIf(not MACOS, 'macOS only') |
| 2762 |
class TestZeroCopyMACOS(_ZeroCopyFileTest, unittest.TestCase): |
| 2763 |
PATCHPOINT = "posix._fcopyfile" |
| 2764 |
|
| 2765 |
def zerocopy_fun(self, src, dst): |
| 2766 |
return shutil._fastcopy_fcopyfile(src, dst, posix._COPYFILE_DATA) |
| 2767 |
|
| 2768 |
|
| 2769 |
class TestGetTerminalSize(unittest.TestCase): |
| 2770 |
def test_does_not_crash(self): |
| 2771 |
"""Check if get_terminal_size() returns a meaningful value. |
| 2772 |
|
| 2773 |
There's no easy portable way to actually check the size of the |
| 2774 |
terminal, so let's check if it returns something sensible instead. |
| 2775 |
""" |
| 2776 |
size = shutil.get_terminal_size() |
| 2777 |
self.assertGreaterEqual(size.columns, 0) |
| 2778 |
self.assertGreaterEqual(size.lines, 0) |
| 2779 |
|
| 2780 |
def test_os_environ_first(self): |
| 2781 |
"Check if environment variables have precedence" |
| 2782 |
|
| 2783 |
with os_helper.EnvironmentVarGuard() as env: |
| 2784 |
env['COLUMNS'] = '777' |
| 2785 |
del env['LINES'] |
| 2786 |
size = shutil.get_terminal_size() |
| 2787 |
self.assertEqual(size.columns, 777) |
| 2788 |
|
| 2789 |
with os_helper.EnvironmentVarGuard() as env: |
| 2790 |
del env['COLUMNS'] |
| 2791 |
env['LINES'] = '888' |
| 2792 |
size = shutil.get_terminal_size() |
| 2793 |
self.assertEqual(size.lines, 888) |
| 2794 |
|
| 2795 |
def test_bad_environ(self): |
| 2796 |
with os_helper.EnvironmentVarGuard() as env: |
| 2797 |
env['COLUMNS'] = 'xxx' |
| 2798 |
env['LINES'] = 'yyy' |
| 2799 |
size = shutil.get_terminal_size() |
| 2800 |
self.assertGreaterEqual(size.columns, 0) |
| 2801 |
self.assertGreaterEqual(size.lines, 0) |
| 2802 |
|
| 2803 |
@unittest.skipUnless(os.isatty(sys.__stdout__.fileno()), "not on tty") |
| 2804 |
@unittest.skipUnless(hasattr(os, 'get_terminal_size'), |
| 2805 |
'need os.get_terminal_size()') |
| 2806 |
def test_stty_match(self): |
| 2807 |
"""Check if stty returns the same results ignoring env |
| 2808 |
|
| 2809 |
This test will fail if stdin and stdout are connected to |
| 2810 |
different terminals with different sizes. Nevertheless, such |
| 2811 |
situations should be pretty rare. |
| 2812 |
""" |
| 2813 |
try: |
| 2814 |
size = subprocess.check_output(['stty', 'size']).decode().split() |
| 2815 |
except (FileNotFoundError, PermissionError, |
| 2816 |
subprocess.CalledProcessError): |
| 2817 |
self.skipTest("stty invocation failed") |
| 2818 |
expected = (int(size[1]), int(size[0])) |
| 2819 |
|
| 2820 |
with os_helper.EnvironmentVarGuard() as env: |
| 2821 |
del env['LINES'] |
| 2822 |
del env['COLUMNS'] |
| 2823 |
actual = shutil.get_terminal_size() |
| 2824 |
|
| 2825 |
self.assertEqual(expected, actual) |
| 2826 |
|
| 2827 |
@unittest.skipIf(support.is_wasi, "WASI has no /dev/null") |
| 2828 |
def test_fallback(self): |
| 2829 |
with os_helper.EnvironmentVarGuard() as env: |
| 2830 |
del env['LINES'] |
| 2831 |
del env['COLUMNS'] |
| 2832 |
|
| 2833 |
|
| 2834 |
with support.swap_attr(sys, '__stdout__', None): |
| 2835 |
size = shutil.get_terminal_size(fallback=(10, 20)) |
| 2836 |
self.assertEqual(size.columns, 10) |
| 2837 |
self.assertEqual(size.lines, 20) |
| 2838 |
|
| 2839 |
|
| 2840 |
|
| 2841 |
with open(os.devnull, 'w', encoding='utf-8') as f, \ |
| 2842 |
support.swap_attr(sys, '__stdout__', f): |
| 2843 |
size = shutil.get_terminal_size(fallback=(30, 40)) |
| 2844 |
self.assertEqual(size.columns, 30) |
| 2845 |
self.assertEqual(size.lines, 40) |
| 2846 |
|
| 2847 |
|
| 2848 |
class PublicAPITests(unittest.TestCase): |
| 2849 |
"""Ensures that the correct values are exposed in the public API.""" |
| 2850 |
|
| 2851 |
def test_module_all_attribute(self): |
| 2852 |
self.assertTrue(hasattr(shutil, '__all__')) |
| 2853 |
target_api = ['copyfileobj', 'copyfile', 'copymode', 'copystat', |
| 2854 |
'copy', 'copy2', 'copytree', 'move', 'rmtree', 'Error', |
| 2855 |
'SpecialFileError', 'ExecError', 'make_archive', |
| 2856 |
'get_archive_formats', 'register_archive_format', |
| 2857 |
'unregister_archive_format', 'get_unpack_formats', |
| 2858 |
'register_unpack_format', 'unregister_unpack_format', |
| 2859 |
'unpack_archive', 'ignore_patterns', 'chown', 'which', |
| 2860 |
'get_terminal_size', 'SameFileError'] |
| 2861 |
if hasattr(os, 'statvfs') or os.name == 'nt': |
| 2862 |
target_api.append('disk_usage') |
| 2863 |
self.assertEqual(set(shutil.__all__), set(target_api)) |
| 2864 |
|
| 2865 |
|
| 2866 |
if __name__ == '__main__': |
| 2867 |
unittest.main() |