| 1 |
# Copyright (C) 2003 Python Software Foundation |
| 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 subprocess |
| 14 |
import random |
| 15 |
import string |
| 16 |
import contextlib |
| 17 |
import io |
| 18 |
from shutil import (make_archive, |
| 19 |
register_archive_format, unregister_archive_format, |
| 20 |
get_archive_formats, Error, unpack_archive, |
| 21 |
register_unpack_format, RegistryError, |
| 22 |
unregister_unpack_format, get_unpack_formats, |
| 23 |
SameFileError, _GiveupOnFastCopy) |
| 24 |
import tarfile |
| 25 |
import zipfile |
| 26 |
try: |
| 27 |
import posix |
| 28 |
except ImportError: |
| 29 |
posix = None |
| 30 |
|
| 31 |
from test import support |
| 32 |
from test.support import os_helper |
| 33 |
from test.support.os_helper import TESTFN, FakePath |
| 34 |
|
| 35 |
TESTFN2 = TESTFN + "2" |
| 36 |
TESTFN_SRC = TESTFN + "_SRC" |
| 37 |
TESTFN_DST = TESTFN + "_DST" |
| 38 |
MACOS = sys.platform.startswith("darwin") |
| 39 |
SOLARIS = sys.platform.startswith("sunos") |
| 40 |
AIX = sys.platform[:3] == 'aix' |
| 41 |
try: |
| 42 |
import grp |
| 43 |
import pwd |
| 44 |
UID_GID_SUPPORT = True |
| 45 |
except ImportError: |
| 46 |
UID_GID_SUPPORT = False |
| 47 |
|
| 48 |
try: |
| 49 |
import _winapi |
| 50 |
except ImportError: |
| 51 |
_winapi = None |
| 52 |
|
| 53 |
no_chdir = unittest.mock.patch('os.chdir', |
| 54 |
side_effect=AssertionError("shouldn't call os.chdir()")) |
| 55 |
|
| 56 |
def _fake_rename(*args, **kwargs): |
| 57 |
# Pretend the destination path is on a different filesystem. |
| 58 |
raise OSError(getattr(errno, 'EXDEV', 18), "Invalid cross-device link") |
| 59 |
|
| 60 |
def mock_rename(func): |
| 61 |
@functools.wraps(func) |
| 62 |
def wrap(*args, **kwargs): |
| 63 |
try: |
| 64 |
builtin_rename = os.rename |
| 65 |
os.rename = _fake_rename |
| 66 |
return func(*args, **kwargs) |
| 67 |
finally: |
| 68 |
os.rename = builtin_rename |
| 69 |
return wrap |
| 70 |
|
| 71 |
def create_file(path, content=b''): |
| 72 |
"""Write *content* to a file located at *path*. |
| 73 |
|
| 74 |
If *path* is a tuple instead of a string, os.path.join will be used to |
| 75 |
make a path. |
| 76 |
""" |
| 77 |
if isinstance(path, tuple): |
| 78 |
path = os.path.join(*path) |
| 79 |
if isinstance(content, str): |
| 80 |
content = content.encode() |
| 81 |
with open(path, 'xb') as fp: |
| 82 |
fp.write(content) |
| 83 |
|
| 84 |
def write_test_file(path, size): |
| 85 |
"""Create a test file with an arbitrary size and random text content.""" |
| 86 |
def chunks(total, step): |
| 87 |
assert total >= step |
| 88 |
while total > step: |
| 89 |
yield step |
| 90 |
total -= step |
| 91 |
if total: |
| 92 |
yield total |
| 93 |
|
| 94 |
bufsize = min(size, 8192) |
| 95 |
chunk = b"".join([random.choice(string.ascii_letters).encode() |
| 96 |
for i in range(bufsize)]) |
| 97 |
with open(path, 'wb') as f: |
| 98 |
for csize in chunks(size, bufsize): |
| 99 |
f.write(chunk) |
| 100 |
assert os.path.getsize(path) == size |
| 101 |
|
| 102 |
def read_file(path, binary=False): |
| 103 |
"""Return contents from a file located at *path*. |
| 104 |
|
| 105 |
If *path* is a tuple instead of a string, os.path.join will be used to |
| 106 |
make a path. If *binary* is true, the file will be opened in binary |
| 107 |
mode. |
| 108 |
""" |
| 109 |
if isinstance(path, tuple): |
| 110 |
path = os.path.join(*path) |
| 111 |
mode = 'rb' if binary else 'r' |
| 112 |
encoding = None if binary else "utf-8" |
| 113 |
with open(path, mode, encoding=encoding) as fp: |
| 114 |
return fp.read() |
| 115 |
|
| 116 |
def rlistdir(path): |
| 117 |
res = [] |
| 118 |
for name in sorted(os.listdir(path)): |
| 119 |
p = os.path.join(path, name) |
| 120 |
if os.path.isdir(p) and not os.path.islink(p): |
| 121 |
res.append(name + '/') |
| 122 |
for n in rlistdir(p): |
| 123 |
res.append(name + '/' + n) |
| 124 |
else: |
| 125 |
res.append(name) |
| 126 |
return res |
| 127 |
|
| 128 |
def supports_file2file_sendfile(): |
| 129 |
# ...apparently Linux and Solaris are the only ones |
| 130 |
if not hasattr(os, "sendfile"): |
| 131 |
return False |
| 132 |
srcname = None |
| 133 |
dstname = None |
| 134 |
try: |
| 135 |
with tempfile.NamedTemporaryFile("wb", dir=os.getcwd(), delete=False) as f: |
| 136 |
srcname = f.name |
| 137 |
f.write(b"0123456789") |
| 138 |
|
| 139 |
with open(srcname, "rb") as src: |
| 140 |
with tempfile.NamedTemporaryFile("wb", dir=os.getcwd(), delete=False) as dst: |
| 141 |
dstname = dst.name |
| 142 |
infd = src.fileno() |
| 143 |
outfd = dst.fileno() |
| 144 |
try: |
| 145 |
os.sendfile(outfd, infd, 0, 2) |
| 146 |
except OSError: |
| 147 |
return False |
| 148 |
else: |
| 149 |
return True |
| 150 |
finally: |
| 151 |
if srcname is not None: |
| 152 |
os_helper.unlink(srcname) |
| 153 |
if dstname is not None: |
| 154 |
os_helper.unlink(dstname) |
| 155 |
|
| 156 |
|
| 157 |
SUPPORTS_SENDFILE = supports_file2file_sendfile() |
| 158 |
|
| 159 |
# AIX 32-bit mode, by default, lacks enough memory for the xz/lzma compiler test |
| 160 |
# The AIX command 'dump -o program' gives XCOFF header information |
| 161 |
# The second word of the last line in the maxdata value |
| 162 |
# when 32-bit maxdata must be greater than 0x1000000 for the xz test to succeed |
| 163 |
def _maxdataOK(): |
| 164 |
if AIX and sys.maxsize == 2147483647: |
| 165 |
hdrs=subprocess.getoutput("/usr/bin/dump -o %s" % sys.executable) |
| 166 |
maxdata=hdrs.split("\n")[-1].split()[1] |
| 167 |
return int(maxdata,16) >= 0x20000000 |
| 168 |
else: |
| 169 |
return True |
| 170 |
|
| 171 |
|
| 172 |
class BaseTest: |
| 173 |
|
| 174 |
def mkdtemp(self, prefix=None): |
| 175 |
"""Create a temporary directory that will be cleaned up. |
| 176 |
|
| 177 |
Returns the path of the directory. |
| 178 |
""" |
| 179 |
d = tempfile.mkdtemp(prefix=prefix, dir=os.getcwd()) |
| 180 |
self.addCleanup(os_helper.rmtree, d) |
| 181 |
return d |
| 182 |
|
| 183 |
|
| 184 |
class TestRmTree(BaseTest, unittest.TestCase): |
| 185 |
|
| 186 |
def test_rmtree_works_on_bytes(self): |
| 187 |
tmp = self.mkdtemp() |
| 188 |
victim = os.path.join(tmp, 'killme') |
| 189 |
os.mkdir(victim) |
| 190 |
create_file(os.path.join(victim, 'somefile'), 'foo') |
| 191 |
victim = os.fsencode(victim) |
| 192 |
self.assertIsInstance(victim, bytes) |
| 193 |
shutil.rmtree(victim) |
| 194 |
|
| 195 |
@os_helper.skip_unless_symlink |
| 196 |
def test_rmtree_fails_on_symlink_onerror(self): |
| 197 |
tmp = self.mkdtemp() |
| 198 |
dir_ = os.path.join(tmp, 'dir') |
| 199 |
os.mkdir(dir_) |
| 200 |
link = os.path.join(tmp, 'link') |
| 201 |
os.symlink(dir_, link) |
| 202 |
self.assertRaises(OSError, shutil.rmtree, link) |
| 203 |
self.assertTrue(os.path.exists(dir_)) |
| 204 |
self.assertTrue(os.path.lexists(link)) |
| 205 |
errors = [] |
| 206 |
def onerror(*args): |
| 207 |
errors.append(args) |
| 208 |
shutil.rmtree(link, onerror=onerror) |
| 209 |
self.assertEqual(len(errors), 1) |
| 210 |
self.assertIs(errors[0][0], os.path.islink) |
| 211 |
self.assertEqual(errors[0][1], link) |
| 212 |
self.assertIsInstance(errors[0][2][1], OSError) |
| 213 |
|
| 214 |
@os_helper.skip_unless_symlink |
| 215 |
def test_rmtree_fails_on_symlink_onexc(self): |
| 216 |
tmp = self.mkdtemp() |
| 217 |
dir_ = os.path.join(tmp, 'dir') |
| 218 |
os.mkdir(dir_) |
| 219 |
link = os.path.join(tmp, 'link') |
| 220 |
os.symlink(dir_, link) |
| 221 |
self.assertRaises(OSError, shutil.rmtree, link) |
| 222 |
self.assertTrue(os.path.exists(dir_)) |
| 223 |
self.assertTrue(os.path.lexists(link)) |
| 224 |
errors = [] |
| 225 |
def onexc(*args): |
| 226 |
errors.append(args) |
| 227 |
shutil.rmtree(link, onexc=onexc) |
| 228 |
self.assertEqual(len(errors), 1) |
| 229 |
self.assertIs(errors[0][0], os.path.islink) |
| 230 |
self.assertEqual(errors[0][1], link) |
| 231 |
self.assertIsInstance(errors[0][2], OSError) |
| 232 |
|
| 233 |
@os_helper.skip_unless_symlink |
| 234 |
def test_rmtree_works_on_symlinks(self): |
| 235 |
tmp = self.mkdtemp() |
| 236 |
dir1 = os.path.join(tmp, 'dir1') |
| 237 |
dir2 = os.path.join(dir1, 'dir2') |
| 238 |
dir3 = os.path.join(tmp, 'dir3') |
| 239 |
for d in dir1, dir2, dir3: |
| 240 |
os.mkdir(d) |
| 241 |
file1 = os.path.join(tmp, 'file1') |
| 242 |
create_file(file1, 'foo') |
| 243 |
link1 = os.path.join(dir1, 'link1') |
| 244 |
os.symlink(dir2, link1) |
| 245 |
link2 = os.path.join(dir1, 'link2') |
| 246 |
os.symlink(dir3, link2) |
| 247 |
link3 = os.path.join(dir1, 'link3') |
| 248 |
os.symlink(file1, link3) |
| 249 |
# make sure symlinks are removed but not followed |
| 250 |
shutil.rmtree(dir1) |
| 251 |
self.assertFalse(os.path.exists(dir1)) |
| 252 |
self.assertTrue(os.path.exists(dir3)) |
| 253 |
self.assertTrue(os.path.exists(file1)) |
| 254 |
|
| 255 |
@unittest.skipUnless(_winapi, 'only relevant on Windows') |
| 256 |
def test_rmtree_fails_on_junctions_onerror(self): |
| 257 |
tmp = self.mkdtemp() |
| 258 |
dir_ = os.path.join(tmp, 'dir') |
| 259 |
os.mkdir(dir_) |
| 260 |
link = os.path.join(tmp, 'link') |
| 261 |
_winapi.CreateJunction(dir_, link) |
| 262 |
self.addCleanup(os_helper.unlink, link) |
| 263 |
self.assertRaises(OSError, shutil.rmtree, link) |
| 264 |
self.assertTrue(os.path.exists(dir_)) |
| 265 |
self.assertTrue(os.path.lexists(link)) |
| 266 |
errors = [] |
| 267 |
def onerror(*args): |
| 268 |
errors.append(args) |
| 269 |
shutil.rmtree(link, onerror=onerror) |
| 270 |
self.assertEqual(len(errors), 1) |
| 271 |
self.assertIs(errors[0][0], os.path.islink) |
| 272 |
self.assertEqual(errors[0][1], link) |
| 273 |
self.assertIsInstance(errors[0][2][1], OSError) |
| 274 |
|
| 275 |
@unittest.skipUnless(_winapi, 'only relevant on Windows') |
| 276 |
def test_rmtree_fails_on_junctions_onexc(self): |
| 277 |
tmp = self.mkdtemp() |
| 278 |
dir_ = os.path.join(tmp, 'dir') |
| 279 |
os.mkdir(dir_) |
| 280 |
link = os.path.join(tmp, 'link') |
| 281 |
_winapi.CreateJunction(dir_, link) |
| 282 |
self.addCleanup(os_helper.unlink, link) |
| 283 |
self.assertRaises(OSError, shutil.rmtree, link) |
| 284 |
self.assertTrue(os.path.exists(dir_)) |
| 285 |
self.assertTrue(os.path.lexists(link)) |
| 286 |
errors = [] |
| 287 |
def onexc(*args): |
| 288 |
errors.append(args) |
| 289 |
shutil.rmtree(link, onexc=onexc) |
| 290 |
self.assertEqual(len(errors), 1) |
| 291 |
self.assertIs(errors[0][0], os.path.islink) |
| 292 |
self.assertEqual(errors[0][1], link) |
| 293 |
self.assertIsInstance(errors[0][2], OSError) |
| 294 |
|
| 295 |
@unittest.skipUnless(_winapi, 'only relevant on Windows') |
| 296 |
def test_rmtree_works_on_junctions(self): |
| 297 |
tmp = self.mkdtemp() |
| 298 |
dir1 = os.path.join(tmp, 'dir1') |
| 299 |
dir2 = os.path.join(dir1, 'dir2') |
| 300 |
dir3 = os.path.join(tmp, 'dir3') |
| 301 |
for d in dir1, dir2, dir3: |
| 302 |
os.mkdir(d) |
| 303 |
file1 = os.path.join(tmp, 'file1') |
| 304 |
create_file(file1, 'foo') |
| 305 |
link1 = os.path.join(dir1, 'link1') |
| 306 |
_winapi.CreateJunction(dir2, link1) |
| 307 |
link2 = os.path.join(dir1, 'link2') |
| 308 |
_winapi.CreateJunction(dir3, link2) |
| 309 |
link3 = os.path.join(dir1, 'link3') |
| 310 |
_winapi.CreateJunction(file1, link3) |
| 311 |
# make sure junctions are removed but not followed |
| 312 |
shutil.rmtree(dir1) |
| 313 |
self.assertFalse(os.path.exists(dir1)) |
| 314 |
self.assertTrue(os.path.exists(dir3)) |
| 315 |
self.assertTrue(os.path.exists(file1)) |
| 316 |
|
| 317 |
def test_rmtree_errors(self): |
| 318 |
# filename is guaranteed not to exist |
| 319 |
filename = tempfile.mktemp(dir=self.mkdtemp()) |
| 320 |
self.assertRaises(FileNotFoundError, shutil.rmtree, filename) |
| 321 |
# test that ignore_errors option is honored |
| 322 |
shutil.rmtree(filename, ignore_errors=True) |
| 323 |
|
| 324 |
# existing file |
| 325 |
tmpdir = self.mkdtemp() |
| 326 |
filename = os.path.join(tmpdir, "tstfile") |
| 327 |
create_file(filename) |
| 328 |
with self.assertRaises(NotADirectoryError) as cm: |
| 329 |
shutil.rmtree(filename) |
| 330 |
self.assertEqual(cm.exception.filename, filename) |
| 331 |
self.assertTrue(os.path.exists(filename)) |
| 332 |
# test that ignore_errors option is honored |
| 333 |
shutil.rmtree(filename, ignore_errors=True) |
| 334 |
self.assertTrue(os.path.exists(filename)) |
| 335 |
|
| 336 |
self.assertRaises(TypeError, shutil.rmtree, None) |
| 337 |
self.assertRaises(TypeError, shutil.rmtree, None, ignore_errors=True) |
| 338 |
exc = TypeError if shutil.rmtree.avoids_symlink_attacks else NotImplementedError |
| 339 |
with self.assertRaises(exc): |
| 340 |
shutil.rmtree(filename, dir_fd='invalid') |
| 341 |
with self.assertRaises(exc): |
| 342 |
shutil.rmtree(filename, dir_fd='invalid', ignore_errors=True) |
| 343 |
|
| 344 |
def test_rmtree_errors_onerror(self): |
| 345 |
tmpdir = self.mkdtemp() |
| 346 |
filename = os.path.join(tmpdir, "tstfile") |
| 347 |
create_file(filename) |
| 348 |
errors = [] |
| 349 |
def onerror(*args): |
| 350 |
errors.append(args) |
| 351 |
shutil.rmtree(filename, onerror=onerror) |
| 352 |
self.assertEqual(len(errors), 2) |
| 353 |
self.assertIs(errors[0][0], os.scandir) |
| 354 |
self.assertEqual(errors[0][1], filename) |
| 355 |
self.assertIsInstance(errors[0][2][1], NotADirectoryError) |
| 356 |
self.assertEqual(errors[0][2][1].filename, filename) |
| 357 |
self.assertIs(errors[1][0], os.rmdir) |
| 358 |
self.assertEqual(errors[1][1], filename) |
| 359 |
self.assertIsInstance(errors[1][2][1], NotADirectoryError) |
| 360 |
self.assertEqual(errors[1][2][1].filename, filename) |
| 361 |
|
| 362 |
def test_rmtree_errors_onexc(self): |
| 363 |
tmpdir = self.mkdtemp() |
| 364 |
filename = os.path.join(tmpdir, "tstfile") |
| 365 |
create_file(filename) |
| 366 |
errors = [] |
| 367 |
def onexc(*args): |
| 368 |
errors.append(args) |
| 369 |
shutil.rmtree(filename, onexc=onexc) |
| 370 |
self.assertEqual(len(errors), 2) |
| 371 |
self.assertIs(errors[0][0], os.scandir) |
| 372 |
self.assertEqual(errors[0][1], filename) |
| 373 |
self.assertIsInstance(errors[0][2], NotADirectoryError) |
| 374 |
self.assertEqual(errors[0][2].filename, filename) |
| 375 |
self.assertIs(errors[1][0], os.rmdir) |
| 376 |
self.assertEqual(errors[1][1], filename) |
| 377 |
self.assertIsInstance(errors[1][2], NotADirectoryError) |
| 378 |
self.assertEqual(errors[1][2].filename, filename) |
| 379 |
|
| 380 |
@unittest.skipIf(sys.platform[:6] == 'cygwin', |
| 381 |
"This test can't be run on Cygwin (issue #1071513).") |
| 382 |
@os_helper.skip_if_dac_override |
| 383 |
@os_helper.skip_unless_working_chmod |
| 384 |
def test_on_error(self): |
| 385 |
self.errorState = 0 |
| 386 |
os.mkdir(TESTFN) |
| 387 |
self.addCleanup(shutil.rmtree, TESTFN) |
| 388 |
|
| 389 |
self.child_file_path = os.path.join(TESTFN, 'a') |
| 390 |
self.child_dir_path = os.path.join(TESTFN, 'b') |
| 391 |
os_helper.create_empty_file(self.child_file_path) |
| 392 |
os.mkdir(self.child_dir_path) |
| 393 |
old_dir_mode = os.stat(TESTFN).st_mode |
| 394 |
old_child_file_mode = os.stat(self.child_file_path).st_mode |
| 395 |
old_child_dir_mode = os.stat(self.child_dir_path).st_mode |
| 396 |
# Make unwritable. |
| 397 |
new_mode = stat.S_IREAD|stat.S_IEXEC |
| 398 |
os.chmod(self.child_file_path, new_mode) |
| 399 |
os.chmod(self.child_dir_path, new_mode) |
| 400 |
os.chmod(TESTFN, new_mode) |
| 401 |
|
| 402 |
self.addCleanup(os.chmod, TESTFN, old_dir_mode) |
| 403 |
self.addCleanup(os.chmod, self.child_file_path, old_child_file_mode) |
| 404 |
self.addCleanup(os.chmod, self.child_dir_path, old_child_dir_mode) |
| 405 |
|
| 406 |
shutil.rmtree(TESTFN, onerror=self.check_args_to_onerror) |
| 407 |
# Test whether onerror has actually been called. |
| 408 |
self.assertEqual(self.errorState, 3, |
| 409 |
"Expected call to onerror function did not happen.") |
| 410 |
|
| 411 |
def check_args_to_onerror(self, func, arg, exc): |
| 412 |
# test_rmtree_errors deliberately runs rmtree |
| 413 |
# on a directory that is chmod 500, which will fail. |
| 414 |
# This function is run when shutil.rmtree fails. |
| 415 |
# 99.9% of the time it initially fails to remove |
| 416 |
# a file in the directory, so the first time through |
| 417 |
# func is os.remove. |
| 418 |
# However, some Linux machines running ZFS on |
| 419 |
# FUSE experienced a failure earlier in the process |
| 420 |
# at os.listdir. The first failure may legally |
| 421 |
# be either. |
| 422 |
if self.errorState < 2: |
| 423 |
if func is os.unlink: |
| 424 |
self.assertEqual(arg, self.child_file_path) |
| 425 |
elif func is os.rmdir: |
| 426 |
self.assertEqual(arg, self.child_dir_path) |
| 427 |
else: |
| 428 |
self.assertIs(func, os.listdir) |
| 429 |
self.assertIn(arg, [TESTFN, self.child_dir_path]) |
| 430 |
self.assertIsSubclass(exc[0], OSError) |
| 431 |
self.errorState += 1 |
| 432 |
else: |
| 433 |
self.assertEqual(func, os.rmdir) |
| 434 |
self.assertEqual(arg, TESTFN) |
| 435 |
self.assertIsSubclass(exc[0], OSError) |
| 436 |
self.errorState = 3 |
| 437 |
|
| 438 |
@unittest.skipIf(sys.platform[:6] == 'cygwin', |
| 439 |
"This test can't be run on Cygwin (issue #1071513).") |
| 440 |
@os_helper.skip_if_dac_override |
| 441 |
@os_helper.skip_unless_working_chmod |
| 442 |
def test_on_exc(self): |
| 443 |
self.errorState = 0 |
| 444 |
os.mkdir(TESTFN) |
| 445 |
self.addCleanup(shutil.rmtree, TESTFN) |
| 446 |
|
| 447 |
self.child_file_path = os.path.join(TESTFN, 'a') |
| 448 |
self.child_dir_path = os.path.join(TESTFN, 'b') |
| 449 |
os_helper.create_empty_file(self.child_file_path) |
| 450 |
os.mkdir(self.child_dir_path) |
| 451 |
old_dir_mode = os.stat(TESTFN).st_mode |
| 452 |
old_child_file_mode = os.stat(self.child_file_path).st_mode |
| 453 |
old_child_dir_mode = os.stat(self.child_dir_path).st_mode |
| 454 |
# Make unwritable. |
| 455 |
new_mode = stat.S_IREAD|stat.S_IEXEC |
| 456 |
os.chmod(self.child_file_path, new_mode) |
| 457 |
os.chmod(self.child_dir_path, new_mode) |
| 458 |
os.chmod(TESTFN, new_mode) |
| 459 |
|
| 460 |
self.addCleanup(os.chmod, TESTFN, old_dir_mode) |
| 461 |
self.addCleanup(os.chmod, self.child_file_path, old_child_file_mode) |
| 462 |
self.addCleanup(os.chmod, self.child_dir_path, old_child_dir_mode) |
| 463 |
|
| 464 |
shutil.rmtree(TESTFN, onexc=self.check_args_to_onexc) |
| 465 |
# Test whether onexc has actually been called. |
| 466 |
self.assertEqual(self.errorState, 3, |
| 467 |
"Expected call to onexc function did not happen.") |
| 468 |
|
| 469 |
def check_args_to_onexc(self, func, arg, exc): |
| 470 |
# test_rmtree_errors deliberately runs rmtree |
| 471 |
# on a directory that is chmod 500, which will fail. |
| 472 |
# This function is run when shutil.rmtree fails. |
| 473 |
# 99.9% of the time it initially fails to remove |
| 474 |
# a file in the directory, so the first time through |
| 475 |
# func is os.remove. |
| 476 |
# However, some Linux machines running ZFS on |
| 477 |
# FUSE experienced a failure earlier in the process |
| 478 |
# at os.listdir. The first failure may legally |
| 479 |
# be either. |
| 480 |
if self.errorState < 2: |
| 481 |
if func is os.unlink: |
| 482 |
self.assertEqual(arg, self.child_file_path) |
| 483 |
elif func is os.rmdir: |
| 484 |
self.assertEqual(arg, self.child_dir_path) |
| 485 |
else: |
| 486 |
self.assertIs(func, os.listdir) |
| 487 |
self.assertIn(arg, [TESTFN, self.child_dir_path]) |
| 488 |
self.assertTrue(isinstance(exc, OSError)) |
| 489 |
self.errorState += 1 |
| 490 |
else: |
| 491 |
self.assertEqual(func, os.rmdir) |
| 492 |
self.assertEqual(arg, TESTFN) |
| 493 |
self.assertTrue(isinstance(exc, OSError)) |
| 494 |
self.errorState = 3 |
| 495 |
|
| 496 |
@unittest.skipIf(sys.platform[:6] == 'cygwin', |
| 497 |
"This test can't be run on Cygwin (issue #1071513).") |
| 498 |
@os_helper.skip_if_dac_override |
| 499 |
@os_helper.skip_unless_working_chmod |
| 500 |
def test_both_onerror_and_onexc(self): |
| 501 |
onerror_called = False |
| 502 |
onexc_called = False |
| 503 |
|
| 504 |
def onerror(*args): |
| 505 |
nonlocal onerror_called |
| 506 |
onerror_called = True |
| 507 |
|
| 508 |
def onexc(*args): |
| 509 |
nonlocal onexc_called |
| 510 |
onexc_called = True |
| 511 |
|
| 512 |
os.mkdir(TESTFN) |
| 513 |
self.addCleanup(shutil.rmtree, TESTFN) |
| 514 |
|
| 515 |
self.child_file_path = os.path.join(TESTFN, 'a') |
| 516 |
self.child_dir_path = os.path.join(TESTFN, 'b') |
| 517 |
os_helper.create_empty_file(self.child_file_path) |
| 518 |
os.mkdir(self.child_dir_path) |
| 519 |
old_dir_mode = os.stat(TESTFN).st_mode |
| 520 |
old_child_file_mode = os.stat(self.child_file_path).st_mode |
| 521 |
old_child_dir_mode = os.stat(self.child_dir_path).st_mode |
| 522 |
# Make unwritable. |
| 523 |
new_mode = stat.S_IREAD|stat.S_IEXEC |
| 524 |
os.chmod(self.child_file_path, new_mode) |
| 525 |
os.chmod(self.child_dir_path, new_mode) |
| 526 |
os.chmod(TESTFN, new_mode) |
| 527 |
|
| 528 |
self.addCleanup(os.chmod, TESTFN, old_dir_mode) |
| 529 |
self.addCleanup(os.chmod, self.child_file_path, old_child_file_mode) |
| 530 |
self.addCleanup(os.chmod, self.child_dir_path, old_child_dir_mode) |
| 531 |
|
| 532 |
shutil.rmtree(TESTFN, onerror=onerror, onexc=onexc) |
| 533 |
self.assertTrue(onexc_called) |
| 534 |
self.assertFalse(onerror_called) |
| 535 |
|
| 536 |
def test_rmtree_does_not_choke_on_failing_lstat(self): |
| 537 |
try: |
| 538 |
orig_lstat = os.lstat |
| 539 |
def raiser(fn, *args, **kwargs): |
| 540 |
if fn != TESTFN: |
| 541 |
raise OSError() |
| 542 |
else: |
| 543 |
return orig_lstat(fn) |
| 544 |
os.lstat = raiser |
| 545 |
|
| 546 |
os.mkdir(TESTFN) |
| 547 |
create_file((TESTFN, 'foo'), 'foo') |
| 548 |
shutil.rmtree(TESTFN) |
| 549 |
finally: |
| 550 |
os.lstat = orig_lstat |
| 551 |
|
| 552 |
def test_rmtree_uses_safe_fd_version_if_available(self): |
| 553 |
_use_fd_functions = ({os.open, os.stat, os.unlink, os.rmdir} <= |
| 554 |
os.supports_dir_fd and |
| 555 |
os.listdir in os.supports_fd and |
| 556 |
os.stat in os.supports_follow_symlinks) |
| 557 |
if _use_fd_functions: |
| 558 |
self.assertTrue(shutil.rmtree.avoids_symlink_attacks) |
| 559 |
tmp_dir = self.mkdtemp() |
| 560 |
d = os.path.join(tmp_dir, 'a') |
| 561 |
os.mkdir(d) |
| 562 |
try: |
| 563 |
real_open = os.open |
| 564 |
class Called(Exception): pass |
| 565 |
def _raiser(*args, **kwargs): |
| 566 |
raise Called |
| 567 |
os.open = _raiser |
| 568 |
self.assertRaises(Called, shutil.rmtree, d) |
| 569 |
finally: |
| 570 |
os.open = real_open |
| 571 |
else: |
| 572 |
self.assertFalse(shutil.rmtree.avoids_symlink_attacks) |
| 573 |
|
| 574 |
@unittest.skipUnless(shutil.rmtree.avoids_symlink_attacks, "requires safe rmtree") |
| 575 |
def test_rmtree_fails_on_close(self): |
| 576 |
# Test that the error handler is called for failed os.close() and that |
| 577 |
# os.close() is only called once for a file descriptor. |
| 578 |
tmp = self.mkdtemp() |
| 579 |
dir1 = os.path.join(tmp, 'dir1') |
| 580 |
os.mkdir(dir1) |
| 581 |
dir2 = os.path.join(dir1, 'dir2') |
| 582 |
os.mkdir(dir2) |
| 583 |
def close(fd): |
| 584 |
orig_close(fd) |
| 585 |
nonlocal close_count |
| 586 |
close_count += 1 |
| 587 |
raise OSError |
| 588 |
|
| 589 |
close_count = 0 |
| 590 |
with support.swap_attr(os, 'close', close) as orig_close: |
| 591 |
with self.assertRaises(OSError): |
| 592 |
shutil.rmtree(dir1) |
| 593 |
self.assertTrue(os.path.isdir(dir2)) |
| 594 |
self.assertEqual(close_count, 2) |
| 595 |
|
| 596 |
close_count = 0 |
| 597 |
errors = [] |
| 598 |
def onexc(*args): |
| 599 |
errors.append(args) |
| 600 |
with support.swap_attr(os, 'close', close) as orig_close: |
| 601 |
shutil.rmtree(dir1, onexc=onexc) |
| 602 |
self.assertEqual(len(errors), 2) |
| 603 |
self.assertIs(errors[0][0], close) |
| 604 |
self.assertEqual(errors[0][1], dir2) |
| 605 |
self.assertIs(errors[1][0], close) |
| 606 |
self.assertEqual(errors[1][1], dir1) |
| 607 |
self.assertEqual(close_count, 2) |
| 608 |
|
| 609 |
@unittest.skipUnless(shutil.rmtree.avoids_symlink_attacks, "dir_fd is not supported") |
| 610 |
def test_rmtree_with_dir_fd(self): |
| 611 |
tmp_dir = self.mkdtemp() |
| 612 |
victim = 'killme' |
| 613 |
fullname = os.path.join(tmp_dir, victim) |
| 614 |
dir_fd = os.open(tmp_dir, os.O_RDONLY) |
| 615 |
self.addCleanup(os.close, dir_fd) |
| 616 |
os.mkdir(fullname) |
| 617 |
os.mkdir(os.path.join(fullname, 'subdir')) |
| 618 |
create_file(os.path.join(fullname, 'subdir', 'somefile'), 'foo') |
| 619 |
self.assertTrue(os.path.exists(fullname)) |
| 620 |
shutil.rmtree(victim, dir_fd=dir_fd) |
| 621 |
self.assertFalse(os.path.exists(fullname)) |
| 622 |
|
| 623 |
@unittest.skipIf(shutil.rmtree.avoids_symlink_attacks, "dir_fd is supported") |
| 624 |
def test_rmtree_with_dir_fd_unsupported(self): |
| 625 |
tmp_dir = self.mkdtemp() |
| 626 |
with self.assertRaises(NotImplementedError): |
| 627 |
shutil.rmtree(tmp_dir, dir_fd=0) |
| 628 |
self.assertTrue(os.path.exists(tmp_dir)) |
| 629 |
|
| 630 |
def test_rmtree_dont_delete_file(self): |
| 631 |
# When called on a file instead of a directory, don't delete it. |
| 632 |
handle, path = tempfile.mkstemp(dir=self.mkdtemp()) |
| 633 |
os.close(handle) |
| 634 |
self.assertRaises(NotADirectoryError, shutil.rmtree, path) |
| 635 |
os.remove(path) |
| 636 |
|
| 637 |
@os_helper.skip_unless_symlink |
| 638 |
def test_rmtree_on_symlink(self): |
| 639 |
# bug 1669. |
| 640 |
os.mkdir(TESTFN) |
| 641 |
try: |
| 642 |
src = os.path.join(TESTFN, 'cheese') |
| 643 |
dst = os.path.join(TESTFN, 'shop') |
| 644 |
os.mkdir(src) |
| 645 |
os.symlink(src, dst) |
| 646 |
self.assertRaises(OSError, shutil.rmtree, dst) |
| 647 |
shutil.rmtree(dst, ignore_errors=True) |
| 648 |
finally: |
| 649 |
shutil.rmtree(TESTFN, ignore_errors=True) |
| 650 |
|
| 651 |
@unittest.skipUnless(_winapi, 'only relevant on Windows') |
| 652 |
def test_rmtree_on_junction(self): |
| 653 |
os.mkdir(TESTFN) |
| 654 |
try: |
| 655 |
src = os.path.join(TESTFN, 'cheese') |
| 656 |
dst = os.path.join(TESTFN, 'shop') |
| 657 |
os.mkdir(src) |
| 658 |
create_file(os.path.join(src, 'spam')) |
| 659 |
_winapi.CreateJunction(src, dst) |
| 660 |
self.assertRaises(OSError, shutil.rmtree, dst) |
| 661 |
shutil.rmtree(dst, ignore_errors=True) |
| 662 |
finally: |
| 663 |
shutil.rmtree(TESTFN, ignore_errors=True) |
| 664 |
|
| 665 |
@unittest.skipUnless(hasattr(os, "mkfifo"), 'requires os.mkfifo()') |
| 666 |
@unittest.skipIf(sys.platform == "vxworks", |
| 667 |
"fifo requires special path on VxWorks") |
| 668 |
def test_rmtree_on_named_pipe(self): |
| 669 |
os.mkfifo(TESTFN) |
| 670 |
try: |
| 671 |
with self.assertRaises(NotADirectoryError): |
| 672 |
shutil.rmtree(TESTFN) |
| 673 |
self.assertTrue(os.path.exists(TESTFN)) |
| 674 |
finally: |
| 675 |
os.unlink(TESTFN) |
| 676 |
|
| 677 |
os.mkdir(TESTFN) |
| 678 |
os.mkfifo(os.path.join(TESTFN, 'mypipe')) |
| 679 |
shutil.rmtree(TESTFN) |
| 680 |
self.assertFalse(os.path.exists(TESTFN)) |
| 681 |
|
| 682 |
@unittest.skipIf(sys.platform[:6] == 'cygwin', |
| 683 |
"This test can't be run on Cygwin (issue #1071513).") |
| 684 |
@os_helper.skip_if_dac_override |
| 685 |
@os_helper.skip_unless_working_chmod |
| 686 |
def test_rmtree_deleted_race_condition(self): |
| 687 |
# bpo-37260 |
| 688 |
# |
| 689 |
# Test that a file or a directory deleted after it is enumerated |
| 690 |
# by scandir() but before unlink() or rmdr() is called doesn't |
| 691 |
# generate any errors. |
| 692 |
def _onexc(fn, path, exc): |
| 693 |
assert fn in (os.rmdir, os.unlink) |
| 694 |
if not isinstance(exc, PermissionError): |
| 695 |
raise |
| 696 |
# Make the parent and the children writeable. |
| 697 |
for p, mode in zip(paths, old_modes): |
| 698 |
os.chmod(p, mode) |
| 699 |
# Remove other dirs except one. |
| 700 |
keep = next(p for p in dirs if p != path) |
| 701 |
for p in dirs: |
| 702 |
if p != keep: |
| 703 |
os.rmdir(p) |
| 704 |
# Remove other files except one. |
| 705 |
keep = next(p for p in files if p != path) |
| 706 |
for p in files: |
| 707 |
if p != keep: |
| 708 |
os.unlink(p) |
| 709 |
|
| 710 |
os.mkdir(TESTFN) |
| 711 |
paths = [TESTFN] + [os.path.join(TESTFN, f'child{i}') |
| 712 |
for i in range(6)] |
| 713 |
dirs = paths[1::2] |
| 714 |
files = paths[2::2] |
| 715 |
for path in dirs: |
| 716 |
os.mkdir(path) |
| 717 |
for path in files: |
| 718 |
create_file(path) |
| 719 |
|
| 720 |
old_modes = [os.stat(path).st_mode for path in paths] |
| 721 |
|
| 722 |
# Make the parent and the children non-writeable. |
| 723 |
new_mode = stat.S_IREAD|stat.S_IEXEC |
| 724 |
for path in reversed(paths): |
| 725 |
os.chmod(path, new_mode) |
| 726 |
|
| 727 |
try: |
| 728 |
shutil.rmtree(TESTFN, onexc=_onexc) |
| 729 |
except: |
| 730 |
# Test failed, so cleanup artifacts. |
| 731 |
for path, mode in zip(paths, old_modes): |
| 732 |
try: |
| 733 |
os.chmod(path, mode) |
| 734 |
except OSError: |
| 735 |
pass |
| 736 |
shutil.rmtree(TESTFN) |
| 737 |
raise |
| 738 |
|
| 739 |
def test_rmtree_above_recursion_limit(self): |
| 740 |
recursion_limit = 40 |
| 741 |
# directory_depth > recursion_limit |
| 742 |
directory_depth = recursion_limit + 10 |
| 743 |
base = os.path.join(TESTFN, *(['d'] * directory_depth)) |
| 744 |
os.makedirs(base) |
| 745 |
|
| 746 |
with support.infinite_recursion(recursion_limit): |
| 747 |
shutil.rmtree(TESTFN) |
| 748 |
|
| 749 |
|
| 750 |
class TestCopyTree(BaseTest, unittest.TestCase): |
| 751 |
|
| 752 |
def test_copytree_simple(self): |
| 753 |
src_dir = self.mkdtemp() |
| 754 |
dst_dir = os.path.join(self.mkdtemp(), 'destination') |
| 755 |
self.addCleanup(shutil.rmtree, src_dir) |
| 756 |
self.addCleanup(shutil.rmtree, os.path.dirname(dst_dir)) |
| 757 |
create_file((src_dir, 'test.txt'), '123') |
| 758 |
os.mkdir(os.path.join(src_dir, 'test_dir')) |
| 759 |
create_file((src_dir, 'test_dir', 'test.txt'), '456') |
| 760 |
|
| 761 |
shutil.copytree(src_dir, dst_dir) |
| 762 |
self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'test.txt'))) |
| 763 |
self.assertTrue(os.path.isdir(os.path.join(dst_dir, 'test_dir'))) |
| 764 |
self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'test_dir', |
| 765 |
'test.txt'))) |
| 766 |
actual = read_file((dst_dir, 'test.txt')) |
| 767 |
self.assertEqual(actual, '123') |
| 768 |
actual = read_file((dst_dir, 'test_dir', 'test.txt')) |
| 769 |
self.assertEqual(actual, '456') |
| 770 |
|
| 771 |
def test_copytree_dirs_exist_ok(self): |
| 772 |
src_dir = self.mkdtemp() |
| 773 |
dst_dir = self.mkdtemp() |
| 774 |
self.addCleanup(shutil.rmtree, src_dir) |
| 775 |
self.addCleanup(shutil.rmtree, dst_dir) |
| 776 |
|
| 777 |
create_file((src_dir, 'nonexisting.txt'), '123') |
| 778 |
os.mkdir(os.path.join(src_dir, 'existing_dir')) |
| 779 |
os.mkdir(os.path.join(dst_dir, 'existing_dir')) |
| 780 |
create_file((dst_dir, 'existing_dir', 'existing.txt'), 'will be replaced') |
| 781 |
create_file((src_dir, 'existing_dir', 'existing.txt'), 'has been replaced') |
| 782 |
|
| 783 |
shutil.copytree(src_dir, dst_dir, dirs_exist_ok=True) |
| 784 |
self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'nonexisting.txt'))) |
| 785 |
self.assertTrue(os.path.isdir(os.path.join(dst_dir, 'existing_dir'))) |
| 786 |
self.assertTrue(os.path.isfile(os.path.join(dst_dir, 'existing_dir', |
| 787 |
'existing.txt'))) |
| 788 |
actual = read_file((dst_dir, 'nonexisting.txt')) |
| 789 |
self.assertEqual(actual, '123') |
| 790 |
actual = read_file((dst_dir, 'existing_dir', 'existing.txt')) |
| 791 |
self.assertEqual(actual, 'has been replaced') |
| 792 |
|
| 793 |
with self.assertRaises(FileExistsError): |
| 794 |
shutil.copytree(src_dir, dst_dir, dirs_exist_ok=False) |
| 795 |
|
| 796 |
@os_helper.skip_unless_symlink |
| 797 |
def test_copytree_symlinks(self): |
| 798 |
tmp_dir = self.mkdtemp() |
| 799 |
src_dir = os.path.join(tmp_dir, 'src') |
| 800 |
dst_dir = os.path.join(tmp_dir, 'dst') |
| 801 |
sub_dir = os.path.join(src_dir, 'sub') |
| 802 |
os.mkdir(src_dir) |
| 803 |
os.mkdir(sub_dir) |
| 804 |
create_file((src_dir, 'file.txt'), 'foo') |
| 805 |
src_link = os.path.join(sub_dir, 'link') |
| 806 |
dst_link = os.path.join(dst_dir, 'sub/link') |
| 807 |
os.symlink(os.path.join(src_dir, 'file.txt'), |
| 808 |
src_link) |
| 809 |
if hasattr(os, 'lchmod'): |
| 810 |
os.lchmod(src_link, stat.S_IRWXU | stat.S_IRWXO) |
| 811 |
if hasattr(os, 'lchflags') and hasattr(stat, 'UF_NODUMP'): |
| 812 |
os.lchflags(src_link, stat.UF_NODUMP) |
| 813 |
src_stat = os.lstat(src_link) |
| 814 |
shutil.copytree(src_dir, dst_dir, symlinks=True) |
| 815 |
self.assertTrue(os.path.islink(os.path.join(dst_dir, 'sub', 'link'))) |
| 816 |
actual = os.readlink(os.path.join(dst_dir, 'sub', 'link')) |
| 817 |
# Bad practice to blindly strip the prefix as it may be required to |
| 818 |
# correctly refer to the file, but we're only comparing paths here. |
| 819 |
if os.name == 'nt' and actual.startswith('\\\\?\\'): |
| 820 |
actual = actual[4:] |
| 821 |
self.assertEqual(actual, os.path.join(src_dir, 'file.txt')) |
| 822 |
dst_stat = os.lstat(dst_link) |
| 823 |
if hasattr(os, 'lchmod'): |
| 824 |
self.assertEqual(dst_stat.st_mode, src_stat.st_mode) |
| 825 |
if hasattr(os, 'lchflags'): |
| 826 |
self.assertEqual(dst_stat.st_flags, src_stat.st_flags) |
| 827 |
|
| 828 |
def test_copytree_with_exclude(self): |
| 829 |
# creating data |
| 830 |
join = os.path.join |
| 831 |
exists = os.path.exists |
| 832 |
src_dir = self.mkdtemp() |
| 833 |
try: |
| 834 |
dst_dir = join(self.mkdtemp(), 'destination') |
| 835 |
create_file((src_dir, 'test.txt'), '123') |
| 836 |
create_file((src_dir, 'test.tmp'), '123') |
| 837 |
os.mkdir(join(src_dir, 'test_dir')) |
| 838 |
create_file((src_dir, 'test_dir', 'test.txt'), '456') |
| 839 |
os.mkdir(join(src_dir, 'test_dir2')) |
| 840 |
create_file((src_dir, 'test_dir2', 'test.txt'), '456') |
| 841 |
os.mkdir(join(src_dir, 'test_dir2', 'subdir')) |
| 842 |
os.mkdir(join(src_dir, 'test_dir2', 'subdir2')) |
| 843 |
create_file((src_dir, 'test_dir2', 'subdir', 'test.txt'), '456') |
| 844 |
create_file((src_dir, 'test_dir2', 'subdir2', 'test.py'), '456') |
| 845 |
|
| 846 |
# testing glob-like patterns |
| 847 |
try: |
| 848 |
patterns = shutil.ignore_patterns('*.tmp', 'test_dir2') |
| 849 |
shutil.copytree(src_dir, dst_dir, ignore=patterns) |
| 850 |
# checking the result: some elements should not be copied |
| 851 |
self.assertTrue(exists(join(dst_dir, 'test.txt'))) |
| 852 |
self.assertFalse(exists(join(dst_dir, 'test.tmp'))) |
| 853 |
self.assertFalse(exists(join(dst_dir, 'test_dir2'))) |
| 854 |
finally: |
| 855 |
shutil.rmtree(dst_dir) |
| 856 |
try: |
| 857 |
patterns = shutil.ignore_patterns('*.tmp', 'subdir*') |
| 858 |
shutil.copytree(src_dir, dst_dir, ignore=patterns) |
| 859 |
# checking the result: some elements should not be copied |
| 860 |
self.assertFalse(exists(join(dst_dir, 'test.tmp'))) |
| 861 |
self.assertFalse(exists(join(dst_dir, 'test_dir2', 'subdir2'))) |
| 862 |
self.assertFalse(exists(join(dst_dir, 'test_dir2', 'subdir'))) |
| 863 |
finally: |
| 864 |
shutil.rmtree(dst_dir) |
| 865 |
|
| 866 |
# testing callable-style |
| 867 |
try: |
| 868 |
def _filter(src, names): |
| 869 |
res = [] |
| 870 |
for name in names: |
| 871 |
path = os.path.join(src, name) |
| 872 |
|
| 873 |
if (os.path.isdir(path) and |
| 874 |
path.split()[-1] == 'subdir'): |
| 875 |
res.append(name) |
| 876 |
elif os.path.splitext(path)[-1] in ('.py'): |
| 877 |
res.append(name) |
| 878 |
return res |
| 879 |
|
| 880 |
shutil.copytree(src_dir, dst_dir, ignore=_filter) |
| 881 |
|
| 882 |
# checking the result: some elements should not be copied |
| 883 |
self.assertFalse(exists(join(dst_dir, 'test_dir2', 'subdir2', |
| 884 |
'test.py'))) |
| 885 |
self.assertFalse(exists(join(dst_dir, 'test_dir2', 'subdir'))) |
| 886 |
|
| 887 |
finally: |
| 888 |
shutil.rmtree(dst_dir) |
| 889 |
finally: |
| 890 |
shutil.rmtree(src_dir) |
| 891 |
shutil.rmtree(os.path.dirname(dst_dir)) |
| 892 |
|
| 893 |
def test_copytree_arg_types_of_ignore(self): |
| 894 |
join = os.path.join |
| 895 |
exists = os.path.exists |
| 896 |
|
| 897 |
tmp_dir = self.mkdtemp() |
| 898 |
src_dir = join(tmp_dir, "source") |
| 899 |
|
| 900 |
os.mkdir(join(src_dir)) |
| 901 |
os.mkdir(join(src_dir, 'test_dir')) |
| 902 |
os.mkdir(os.path.join(src_dir, 'test_dir', 'subdir')) |
| 903 |
create_file((src_dir, 'test_dir', 'subdir', 'test.txt'), '456') |
| 904 |
|
| 905 |
invocations = [] |
| 906 |
|
| 907 |
def _ignore(src, names): |
| 908 |
invocations.append(src) |
| 909 |
self.assertIsInstance(src, str) |
| 910 |
self.assertIsInstance(names, list) |
| 911 |
self.assertEqual(len(names), len(set(names))) |
| 912 |
for name in names: |
| 913 |
self.assertIsInstance(name, str) |
| 914 |
return [] |
| 915 |
|
| 916 |
dst_dir = join(self.mkdtemp(), 'destination') |
| 917 |
shutil.copytree(src_dir, dst_dir, ignore=_ignore) |
| 918 |
self.assertTrue(exists(join(dst_dir, 'test_dir', 'subdir', |
| 919 |
'test.txt'))) |
| 920 |
|
| 921 |
dst_dir = join(self.mkdtemp(), 'destination') |
| 922 |
shutil.copytree(FakePath(src_dir), dst_dir, ignore=_ignore) |
| 923 |
self.assertTrue(exists(join(dst_dir, 'test_dir', 'subdir', |
| 924 |
'test.txt'))) |
| 925 |
|
| 926 |
dst_dir = join(self.mkdtemp(), 'destination') |
| 927 |
src_dir_entry = list(os.scandir(tmp_dir))[0] |
| 928 |
self.assertIsInstance(src_dir_entry, os.DirEntry) |
| 929 |
shutil.copytree(src_dir_entry, dst_dir, ignore=_ignore) |
| 930 |
self.assertTrue(exists(join(dst_dir, 'test_dir', 'subdir', |
| 931 |
'test.txt'))) |
| 932 |
|
| 933 |
self.assertEqual(len(invocations), 9) |
| 934 |
|
| 935 |
def test_copytree_retains_permissions(self): |
| 936 |
tmp_dir = self.mkdtemp() |
| 937 |
src_dir = os.path.join(tmp_dir, 'source') |
| 938 |
os.mkdir(src_dir) |
| 939 |
dst_dir = os.path.join(tmp_dir, 'destination') |
| 940 |
self.addCleanup(shutil.rmtree, tmp_dir) |
| 941 |
|
| 942 |
os.chmod(src_dir, 0o777) |
| 943 |
create_file((src_dir, 'permissive.txt'), '123') |
| 944 |
os.chmod(os.path.join(src_dir, 'permissive.txt'), 0o777) |
| 945 |
create_file((src_dir, 'restrictive.txt'), '456') |
| 946 |
os.chmod(os.path.join(src_dir, 'restrictive.txt'), 0o600) |
| 947 |
restrictive_subdir = tempfile.mkdtemp(dir=src_dir) |
| 948 |
self.addCleanup(os_helper.rmtree, restrictive_subdir) |
| 949 |
os.chmod(restrictive_subdir, 0o600) |
| 950 |
|
| 951 |
shutil.copytree(src_dir, dst_dir) |
| 952 |
self.assertEqual(os.stat(src_dir).st_mode, os.stat(dst_dir).st_mode) |
| 953 |
self.assertEqual(os.stat(os.path.join(src_dir, 'permissive.txt')).st_mode, |
| 954 |
os.stat(os.path.join(dst_dir, 'permissive.txt')).st_mode) |
| 955 |
self.assertEqual(os.stat(os.path.join(src_dir, 'restrictive.txt')).st_mode, |
| 956 |
os.stat(os.path.join(dst_dir, 'restrictive.txt')).st_mode) |
| 957 |
restrictive_subdir_dst = os.path.join(dst_dir, |
| 958 |
os.path.split(restrictive_subdir)[1]) |
| 959 |
self.assertEqual(os.stat(restrictive_subdir).st_mode, |
| 960 |
os.stat(restrictive_subdir_dst).st_mode) |
| 961 |
|
| 962 |
@unittest.mock.patch('os.chmod') |
| 963 |
def test_copytree_winerror(self, mock_patch): |
| 964 |
# When copying to VFAT, copystat() raises OSError. On Windows, the |
| 965 |
# exception object has a meaningful 'winerror' attribute, but not |
| 966 |
# on other operating systems. Do not assume 'winerror' is set. |
| 967 |
src_dir = self.mkdtemp() |
| 968 |
dst_dir = os.path.join(self.mkdtemp(), 'destination') |
| 969 |
self.addCleanup(shutil.rmtree, src_dir) |
| 970 |
self.addCleanup(shutil.rmtree, os.path.dirname(dst_dir)) |
| 971 |
|
| 972 |
mock_patch.side_effect = PermissionError('ka-boom') |
| 973 |
with self.assertRaises(shutil.Error): |
| 974 |
shutil.copytree(src_dir, dst_dir) |
| 975 |
|
| 976 |
def test_copytree_custom_copy_function(self): |
| 977 |
# See: https://bugs.python.org/issue35648 |
| 978 |
def custom_cpfun(a, b): |
| 979 |
flag.append(None) |
| 980 |
self.assertIsInstance(a, str) |
| 981 |
self.assertIsInstance(b, str) |
| 982 |
self.assertEqual(a, os.path.join(src, 'foo')) |
| 983 |
self.assertEqual(b, os.path.join(dst, 'foo')) |
| 984 |
|
| 985 |
flag = [] |
| 986 |
src = self.mkdtemp() |
| 987 |
dst = tempfile.mktemp(dir=self.mkdtemp()) |
| 988 |
create_file(os.path.join(src, 'foo')) |
| 989 |
shutil.copytree(src, dst, copy_function=custom_cpfun) |
| 990 |
self.assertEqual(len(flag), 1) |
| 991 |
|
| 992 |
# Issue #3002: copyfile and copytree block indefinitely on named pipes |
| 993 |
@unittest.skipUnless(hasattr(os, "mkfifo"), 'requires os.mkfifo()') |
| 994 |
@os_helper.skip_unless_symlink |
| 995 |
@unittest.skipIf(sys.platform == "vxworks", |
| 996 |
"fifo requires special path on VxWorks") |
| 997 |
def test_copytree_named_pipe(self): |
| 998 |
os.mkdir(TESTFN) |
| 999 |
try: |
| 1000 |
subdir = os.path.join(TESTFN, "subdir") |
| 1001 |
os.mkdir(subdir) |
| 1002 |
pipe = os.path.join(subdir, "mypipe") |
| 1003 |
try: |
| 1004 |
os.mkfifo(pipe) |
| 1005 |
except PermissionError as e: |
| 1006 |
self.skipTest('os.mkfifo(): %s' % e) |
| 1007 |
try: |
| 1008 |
shutil.copytree(TESTFN, TESTFN2) |
| 1009 |
except shutil.Error as e: |
| 1010 |
errors = e.args[0] |
| 1011 |
self.assertEqual(len(errors), 1) |
| 1012 |
src, dst, error_msg = errors[0] |
| 1013 |
self.assertEqual("`%s` is a named pipe" % pipe, error_msg) |
| 1014 |
else: |
| 1015 |
self.fail("shutil.Error should have been raised") |
| 1016 |
finally: |
| 1017 |
shutil.rmtree(TESTFN, ignore_errors=True) |
| 1018 |
shutil.rmtree(TESTFN2, ignore_errors=True) |
| 1019 |
|
| 1020 |
def test_copytree_special_func(self): |
| 1021 |
src_dir = self.mkdtemp() |
| 1022 |
dst_dir = os.path.join(self.mkdtemp(), 'destination') |
| 1023 |
create_file((src_dir, 'test.txt'), '123') |
| 1024 |
os.mkdir(os.path.join(src_dir, 'test_dir')) |
| 1025 |
create_file((src_dir, 'test_dir', 'test.txt'), '456') |
| 1026 |
|
| 1027 |
copied = [] |
| 1028 |
def _copy(src, dst): |
| 1029 |
copied.append((src, dst)) |
| 1030 |
|
| 1031 |
shutil.copytree(src_dir, dst_dir, copy_function=_copy) |
| 1032 |
self.assertEqual(len(copied), 2) |
| 1033 |
|
| 1034 |
@os_helper.skip_unless_symlink |
| 1035 |
def test_copytree_dangling_symlinks(self): |
| 1036 |
src_dir = self.mkdtemp() |
| 1037 |
valid_file = os.path.join(src_dir, 'test.txt') |
| 1038 |
create_file(valid_file, 'abc') |
| 1039 |
dir_a = os.path.join(src_dir, 'dir_a') |
| 1040 |
os.mkdir(dir_a) |
| 1041 |
for d in src_dir, dir_a: |
| 1042 |
os.symlink('IDONTEXIST', os.path.join(d, 'broken')) |
| 1043 |
os.symlink(valid_file, os.path.join(d, 'valid')) |
| 1044 |
|
| 1045 |
# A dangling symlink should raise an error. |
| 1046 |
dst_dir = os.path.join(self.mkdtemp(), 'destination') |
| 1047 |
self.assertRaises(Error, shutil.copytree, src_dir, dst_dir) |
| 1048 |
|
| 1049 |
# Dangling symlinks should be ignored with the proper flag. |
| 1050 |
dst_dir = os.path.join(self.mkdtemp(), 'destination2') |
| 1051 |
shutil.copytree(src_dir, dst_dir, ignore_dangling_symlinks=True) |
| 1052 |
for root, dirs, files in os.walk(dst_dir): |
| 1053 |
self.assertNotIn('broken', files) |
| 1054 |
self.assertIn('valid', files) |
| 1055 |
|
| 1056 |
# a dangling symlink is copied if symlinks=True |
| 1057 |
dst_dir = os.path.join(self.mkdtemp(), 'destination3') |
| 1058 |
shutil.copytree(src_dir, dst_dir, symlinks=True) |
| 1059 |
self.assertIn('test.txt', os.listdir(dst_dir)) |
| 1060 |
|
| 1061 |
@os_helper.skip_unless_symlink |
| 1062 |
def test_copytree_symlink_dir(self): |
| 1063 |
src_dir = self.mkdtemp() |
| 1064 |
dst_dir = os.path.join(self.mkdtemp(), 'destination') |
| 1065 |
os.mkdir(os.path.join(src_dir, 'real_dir')) |
| 1066 |
create_file(os.path.join(src_dir, 'real_dir', 'test.txt')) |
| 1067 |
os.symlink(os.path.join(src_dir, 'real_dir'), |
| 1068 |
os.path.join(src_dir, 'link_to_dir'), |
| 1069 |
target_is_directory=True) |
| 1070 |
|
| 1071 |
shutil.copytree(src_dir, dst_dir, symlinks=False) |
| 1072 |
self.assertFalse(os.path.islink(os.path.join(dst_dir, 'link_to_dir'))) |
| 1073 |
self.assertIn('test.txt', os.listdir(os.path.join(dst_dir, 'link_to_dir'))) |
| 1074 |
|
| 1075 |
dst_dir = os.path.join(self.mkdtemp(), 'destination2') |
| 1076 |
shutil.copytree(src_dir, dst_dir, symlinks=True) |
| 1077 |
self.assertTrue(os.path.islink(os.path.join(dst_dir, 'link_to_dir'))) |
| 1078 |
self.assertIn('test.txt', os.listdir(os.path.join(dst_dir, 'link_to_dir'))) |
| 1079 |
|
| 1080 |
def test_copytree_return_value(self): |
| 1081 |
# copytree returns its destination path. |
| 1082 |
src_dir = self.mkdtemp() |
| 1083 |
dst_dir = src_dir + "dest" |
| 1084 |
self.addCleanup(shutil.rmtree, dst_dir, True) |
| 1085 |
src = os.path.join(src_dir, 'foo') |
| 1086 |
create_file(src, 'foo') |
| 1087 |
rv = shutil.copytree(src_dir, dst_dir) |
| 1088 |
self.assertEqual(['foo'], os.listdir(rv)) |
| 1089 |
|
| 1090 |
def test_copytree_subdirectory(self): |
| 1091 |
# copytree where dst is a subdirectory of src, see Issue 38688 |
| 1092 |
base_dir = self.mkdtemp() |
| 1093 |
self.addCleanup(shutil.rmtree, base_dir, ignore_errors=True) |
| 1094 |
src_dir = os.path.join(base_dir, "t", "pg") |
| 1095 |
dst_dir = os.path.join(src_dir, "somevendor", "1.0") |
| 1096 |
os.makedirs(src_dir) |
| 1097 |
src = os.path.join(src_dir, 'pol') |
| 1098 |
create_file(src, 'pol') |
| 1099 |
rv = shutil.copytree(src_dir, dst_dir) |
| 1100 |
self.assertEqual(['pol'], os.listdir(rv)) |
| 1101 |
|
| 1102 |
class TestCopy(BaseTest, unittest.TestCase): |
| 1103 |
|
| 1104 |
### shutil.copymode |
| 1105 |
|
| 1106 |
@os_helper.skip_unless_symlink |
| 1107 |
def test_copymode_follow_symlinks(self): |
| 1108 |
tmp_dir = self.mkdtemp() |
| 1109 |
src = os.path.join(tmp_dir, 'foo') |
| 1110 |
dst = os.path.join(tmp_dir, 'bar') |
| 1111 |
src_link = os.path.join(tmp_dir, 'baz') |
| 1112 |
dst_link = os.path.join(tmp_dir, 'quux') |
| 1113 |
create_file(src, 'foo') |
| 1114 |
create_file(dst, 'foo') |
| 1115 |
os.symlink(src, src_link) |
| 1116 |
os.symlink(dst, dst_link) |
| 1117 |
os.chmod(src, stat.S_IRWXU|stat.S_IRWXG) |
| 1118 |
# file to file |
| 1119 |
os.chmod(dst, stat.S_IRWXO) |
| 1120 |
self.assertNotEqual(os.stat(src).st_mode, os.stat(dst).st_mode) |
| 1121 |
shutil.copymode(src, dst) |
| 1122 |
self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode) |
| 1123 |
# On Windows, os.chmod does not follow symlinks (issue #15411) |
| 1124 |
# follow src link |
| 1125 |
os.chmod(dst, stat.S_IRWXO) |
| 1126 |
shutil.copymode(src_link, dst) |
| 1127 |
self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode) |
| 1128 |
# follow dst link |
| 1129 |
os.chmod(dst, stat.S_IRWXO) |
| 1130 |
shutil.copymode(src, dst_link) |
| 1131 |
self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode) |
| 1132 |
# follow both links |
| 1133 |
os.chmod(dst, stat.S_IRWXO) |
| 1134 |
shutil.copymode(src_link, dst_link) |
| 1135 |
self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode) |
| 1136 |
|
| 1137 |
@unittest.skipUnless(hasattr(os, 'lchmod'), 'requires os.lchmod') |
| 1138 |
@os_helper.skip_unless_symlink |
| 1139 |
def test_copymode_symlink_to_symlink(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 |
dst_link = os.path.join(tmp_dir, 'quux') |
| 1145 |
create_file(src, 'foo') |
| 1146 |
create_file(dst, 'foo') |
| 1147 |
os.symlink(src, src_link) |
| 1148 |
os.symlink(dst, dst_link) |
| 1149 |
os.chmod(src, stat.S_IRWXU|stat.S_IRWXG) |
| 1150 |
os.chmod(dst, stat.S_IRWXU) |
| 1151 |
os.lchmod(src_link, stat.S_IRWXO|stat.S_IRWXG) |
| 1152 |
# link to link |
| 1153 |
os.lchmod(dst_link, stat.S_IRWXO) |
| 1154 |
old_mode = os.stat(dst).st_mode |
| 1155 |
shutil.copymode(src_link, dst_link, follow_symlinks=False) |
| 1156 |
self.assertEqual(os.lstat(src_link).st_mode, |
| 1157 |
os.lstat(dst_link).st_mode) |
| 1158 |
self.assertEqual(os.stat(dst).st_mode, old_mode) |
| 1159 |
# src link - use chmod |
| 1160 |
os.lchmod(dst_link, stat.S_IRWXO) |
| 1161 |
shutil.copymode(src_link, dst, follow_symlinks=False) |
| 1162 |
self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode) |
| 1163 |
# dst link - use chmod |
| 1164 |
os.lchmod(dst_link, stat.S_IRWXO) |
| 1165 |
shutil.copymode(src, dst_link, follow_symlinks=False) |
| 1166 |
self.assertEqual(os.stat(src).st_mode, os.stat(dst).st_mode) |
| 1167 |
|
| 1168 |
@unittest.skipIf(hasattr(os, 'lchmod'), 'requires os.lchmod to be missing') |
| 1169 |
@os_helper.skip_unless_symlink |
| 1170 |
def test_copymode_symlink_to_symlink_wo_lchmod(self): |
| 1171 |
tmp_dir = self.mkdtemp() |
| 1172 |
src = os.path.join(tmp_dir, 'foo') |
| 1173 |
dst = os.path.join(tmp_dir, 'bar') |
| 1174 |
src_link = os.path.join(tmp_dir, 'baz') |
| 1175 |
dst_link = os.path.join(tmp_dir, 'quux') |
| 1176 |
create_file(src, 'foo') |
| 1177 |
create_file(dst, 'foo') |
| 1178 |
os.symlink(src, src_link) |
| 1179 |
os.symlink(dst, dst_link) |
| 1180 |
shutil.copymode(src_link, dst_link, follow_symlinks=False) # silent fail |
| 1181 |
|
| 1182 |
### shutil.copystat |
| 1183 |
|
| 1184 |
@os_helper.skip_unless_symlink |
| 1185 |
def test_copystat_symlinks(self): |
| 1186 |
tmp_dir = self.mkdtemp() |
| 1187 |
src = os.path.join(tmp_dir, 'foo') |
| 1188 |
dst = os.path.join(tmp_dir, 'bar') |
| 1189 |
src_link = os.path.join(tmp_dir, 'baz') |
| 1190 |
dst_link = os.path.join(tmp_dir, 'qux') |
| 1191 |
create_file(src, 'foo') |
| 1192 |
src_stat = os.stat(src) |
| 1193 |
os.utime(src, (src_stat.st_atime, |
| 1194 |
src_stat.st_mtime - 42.0)) # ensure different mtimes |
| 1195 |
create_file(dst, 'bar') |
| 1196 |
self.assertNotEqual(os.stat(src).st_mtime, os.stat(dst).st_mtime) |
| 1197 |
os.symlink(src, src_link) |
| 1198 |
os.symlink(dst, dst_link) |
| 1199 |
if hasattr(os, 'lchmod'): |
| 1200 |
os.lchmod(src_link, stat.S_IRWXO) |
| 1201 |
if hasattr(os, 'lchflags') and hasattr(stat, 'UF_NODUMP'): |
| 1202 |
os.lchflags(src_link, stat.UF_NODUMP) |
| 1203 |
src_link_stat = os.lstat(src_link) |
| 1204 |
# follow |
| 1205 |
if hasattr(os, 'lchmod'): |
| 1206 |
shutil.copystat(src_link, dst_link, follow_symlinks=True) |
| 1207 |
self.assertNotEqual(src_link_stat.st_mode, os.stat(dst).st_mode) |
| 1208 |
# don't follow |
| 1209 |
shutil.copystat(src_link, dst_link, follow_symlinks=False) |
| 1210 |
dst_link_stat = os.lstat(dst_link) |
| 1211 |
if os.utime in os.supports_follow_symlinks: |
| 1212 |
for attr in 'st_atime', 'st_mtime': |
| 1213 |
# The modification times may be truncated in the new file. |
| 1214 |
self.assertLessEqual(getattr(src_link_stat, attr), |
| 1215 |
getattr(dst_link_stat, attr) + 1) |
| 1216 |
if hasattr(os, 'lchmod'): |
| 1217 |
self.assertEqual(src_link_stat.st_mode, dst_link_stat.st_mode) |
| 1218 |
if hasattr(os, 'lchflags') and hasattr(src_link_stat, 'st_flags'): |
| 1219 |
self.assertEqual(src_link_stat.st_flags, dst_link_stat.st_flags) |
| 1220 |
# tell to follow but dst is not a link |
| 1221 |
shutil.copystat(src_link, dst, follow_symlinks=False) |
| 1222 |
self.assertTrue(abs(os.stat(src).st_mtime - os.stat(dst).st_mtime) < |
| 1223 |
00000.1) |
| 1224 |
|
| 1225 |
@unittest.skipUnless(hasattr(os, 'chflags') and |
| 1226 |
hasattr(errno, 'EOPNOTSUPP') and |
| 1227 |
hasattr(errno, 'ENOTSUP'), |
| 1228 |
"requires os.chflags, EOPNOTSUPP & ENOTSUP") |
| 1229 |
def test_copystat_handles_harmless_chflags_errors(self): |
| 1230 |
tmpdir = self.mkdtemp() |
| 1231 |
file1 = os.path.join(tmpdir, 'file1') |
| 1232 |
file2 = os.path.join(tmpdir, 'file2') |
| 1233 |
create_file(file1, 'xxx') |
| 1234 |
create_file(file2, 'xxx') |
| 1235 |
|
| 1236 |
def make_chflags_raiser(err): |
| 1237 |
ex = OSError() |
| 1238 |
|
| 1239 |
def _chflags_raiser(path, flags, *, follow_symlinks=True): |
| 1240 |
ex.errno = err |
| 1241 |
raise ex |
| 1242 |
return _chflags_raiser |
| 1243 |
old_chflags = os.chflags |
| 1244 |
try: |
| 1245 |
for err in errno.EOPNOTSUPP, errno.ENOTSUP: |
| 1246 |
os.chflags = make_chflags_raiser(err) |
| 1247 |
shutil.copystat(file1, file2) |
| 1248 |
# assert others errors break it |
| 1249 |
os.chflags = make_chflags_raiser(errno.EOPNOTSUPP + errno.ENOTSUP) |
| 1250 |
self.assertRaises(OSError, shutil.copystat, file1, file2) |
| 1251 |
finally: |
| 1252 |
os.chflags = old_chflags |
| 1253 |
|
| 1254 |
### shutil.copyxattr |
| 1255 |
|
| 1256 |
@os_helper.skip_unless_xattr |
| 1257 |
def test_copyxattr(self): |
| 1258 |
tmp_dir = self.mkdtemp() |
| 1259 |
src = os.path.join(tmp_dir, 'foo') |
| 1260 |
create_file(src, 'foo') |
| 1261 |
dst = os.path.join(tmp_dir, 'bar') |
| 1262 |
create_file(dst, 'bar') |
| 1263 |
|
| 1264 |
# no xattr == no problem |
| 1265 |
shutil._copyxattr(src, dst) |
| 1266 |
# common case |
| 1267 |
os.setxattr(src, 'user.foo', b'42') |
| 1268 |
os.setxattr(src, 'user.bar', b'43') |
| 1269 |
shutil._copyxattr(src, dst) |
| 1270 |
self.assertEqual(sorted(os.listxattr(src)), sorted(os.listxattr(dst))) |
| 1271 |
self.assertEqual( |
| 1272 |
os.getxattr(src, 'user.foo'), |
| 1273 |
os.getxattr(dst, 'user.foo')) |
| 1274 |
# check errors don't affect other attrs |
| 1275 |
os.remove(dst) |
| 1276 |
create_file(dst, 'bar') |
| 1277 |
os_error = OSError(errno.EPERM, 'EPERM') |
| 1278 |
|
| 1279 |
def _raise_on_user_foo(fname, attr, val, **kwargs): |
| 1280 |
if attr == 'user.foo': |
| 1281 |
raise os_error |
| 1282 |
else: |
| 1283 |
orig_setxattr(fname, attr, val, **kwargs) |
| 1284 |
try: |
| 1285 |
orig_setxattr = os.setxattr |
| 1286 |
os.setxattr = _raise_on_user_foo |
| 1287 |
shutil._copyxattr(src, dst) |
| 1288 |
self.assertIn('user.bar', os.listxattr(dst)) |
| 1289 |
finally: |
| 1290 |
os.setxattr = orig_setxattr |
| 1291 |
# the source filesystem not supporting xattrs should be ok, too. |
| 1292 |
def _raise_on_src(fname, *, follow_symlinks=True): |
| 1293 |
if fname == src: |
| 1294 |
raise OSError(errno.ENOTSUP, 'Operation not supported') |
| 1295 |
return orig_listxattr(fname, follow_symlinks=follow_symlinks) |
| 1296 |
try: |
| 1297 |
orig_listxattr = os.listxattr |
| 1298 |
os.listxattr = _raise_on_src |
| 1299 |
shutil._copyxattr(src, dst) |
| 1300 |
finally: |
| 1301 |
os.listxattr = orig_listxattr |
| 1302 |
|
| 1303 |
# test that shutil.copystat copies xattrs |
| 1304 |
src = os.path.join(tmp_dir, 'the_original') |
| 1305 |
srcro = os.path.join(tmp_dir, 'the_original_ro') |
| 1306 |
create_file(src, src) |
| 1307 |
create_file(srcro, srcro) |
| 1308 |
os.setxattr(src, 'user.the_value', b'fiddly') |
| 1309 |
os.setxattr(srcro, 'user.the_value', b'fiddly') |
| 1310 |
os.chmod(srcro, 0o444) |
| 1311 |
dst = os.path.join(tmp_dir, 'the_copy') |
| 1312 |
dstro = os.path.join(tmp_dir, 'the_copy_ro') |
| 1313 |
create_file(dst, dst) |
| 1314 |
create_file(dstro, dstro) |
| 1315 |
shutil.copystat(src, dst) |
| 1316 |
shutil.copystat(srcro, dstro) |
| 1317 |
self.assertEqual(os.getxattr(dst, 'user.the_value'), b'fiddly') |
| 1318 |
self.assertEqual(os.getxattr(dstro, 'user.the_value'), b'fiddly') |
| 1319 |
|
| 1320 |
@os_helper.skip_unless_symlink |
| 1321 |
@os_helper.skip_unless_xattr |
| 1322 |
@os_helper.skip_unless_dac_override |
| 1323 |
def test_copyxattr_symlinks(self): |
| 1324 |
# On Linux, it's only possible to access non-user xattr for symlinks; |
| 1325 |
# which in turn require root privileges. This test should be expanded |
| 1326 |
# as soon as other platforms gain support for extended attributes. |
| 1327 |
tmp_dir = self.mkdtemp() |
| 1328 |
src = os.path.join(tmp_dir, 'foo') |
| 1329 |
src_link = os.path.join(tmp_dir, 'baz') |
| 1330 |
create_file(src, 'foo') |
| 1331 |
os.symlink(src, src_link) |
| 1332 |
os.setxattr(src, 'trusted.foo', b'42') |
| 1333 |
os.setxattr(src_link, 'trusted.foo', b'43', follow_symlinks=False) |
| 1334 |
dst = os.path.join(tmp_dir, 'bar') |
| 1335 |
dst_link = os.path.join(tmp_dir, 'qux') |
| 1336 |
create_file(dst, 'bar') |
| 1337 |
os.symlink(dst, dst_link) |
| 1338 |
shutil._copyxattr(src_link, dst_link, follow_symlinks=False) |
| 1339 |
self.assertEqual(os.getxattr(dst_link, 'trusted.foo', follow_symlinks=False), b'43') |
| 1340 |
self.assertRaises(OSError, os.getxattr, dst, 'trusted.foo') |
| 1341 |
shutil._copyxattr(src_link, dst, follow_symlinks=False) |
| 1342 |
self.assertEqual(os.getxattr(dst, 'trusted.foo'), b'43') |
| 1343 |
|
| 1344 |
### shutil.copy |
| 1345 |
|
| 1346 |
def _copy_file(self, method): |
| 1347 |
fname = 'test.txt' |
| 1348 |
tmpdir = self.mkdtemp() |
| 1349 |
create_file((tmpdir, fname), 'xxx') |
| 1350 |
file1 = os.path.join(tmpdir, fname) |
| 1351 |
tmpdir2 = self.mkdtemp() |
| 1352 |
method(file1, tmpdir2) |
| 1353 |
file2 = os.path.join(tmpdir2, fname) |
| 1354 |
return (file1, file2) |
| 1355 |
|
| 1356 |
def test_copy(self): |
| 1357 |
# Ensure that the copied file exists and has the same mode bits. |
| 1358 |
file1, file2 = self._copy_file(shutil.copy) |
| 1359 |
self.assertTrue(os.path.exists(file2)) |
| 1360 |
self.assertEqual(os.stat(file1).st_mode, os.stat(file2).st_mode) |
| 1361 |
|
| 1362 |
@os_helper.skip_unless_symlink |
| 1363 |
def test_copy_symlinks(self): |
| 1364 |
tmp_dir = self.mkdtemp() |
| 1365 |
src = os.path.join(tmp_dir, 'foo') |
| 1366 |
dst = os.path.join(tmp_dir, 'bar') |
| 1367 |
src_link = os.path.join(tmp_dir, 'baz') |
| 1368 |
create_file(src, 'foo') |
| 1369 |
os.symlink(src, src_link) |
| 1370 |
if hasattr(os, 'lchmod'): |
| 1371 |
os.lchmod(src_link, stat.S_IRWXU | stat.S_IRWXO) |
| 1372 |
# don't follow |
| 1373 |
shutil.copy(src_link, dst, follow_symlinks=True) |
| 1374 |
self.assertFalse(os.path.islink(dst)) |
| 1375 |
self.assertEqual(read_file(src), read_file(dst)) |
| 1376 |
os.remove(dst) |
| 1377 |
# follow |
| 1378 |
shutil.copy(src_link, dst, follow_symlinks=False) |
| 1379 |
self.assertTrue(os.path.islink(dst)) |
| 1380 |
self.assertEqual(os.readlink(dst), os.readlink(src_link)) |
| 1381 |
if hasattr(os, 'lchmod'): |
| 1382 |
self.assertEqual(os.lstat(src_link).st_mode, |
| 1383 |
os.lstat(dst).st_mode) |
| 1384 |
|
| 1385 |
### shutil.copy2 |
| 1386 |
|
| 1387 |
@unittest.skipUnless(hasattr(os, 'utime'), 'requires os.utime') |
| 1388 |
def test_copy2(self): |
| 1389 |
# Ensure that the copied file exists and has the same mode and |
| 1390 |
# modification time bits. |
| 1391 |
file1, file2 = self._copy_file(shutil.copy2) |
| 1392 |
self.assertTrue(os.path.exists(file2)) |
| 1393 |
file1_stat = os.stat(file1) |
| 1394 |
file2_stat = os.stat(file2) |
| 1395 |
self.assertEqual(file1_stat.st_mode, file2_stat.st_mode) |
| 1396 |
for attr in 'st_atime', 'st_mtime': |
| 1397 |
# The modification times may be truncated in the new file. |
| 1398 |
self.assertLessEqual(getattr(file1_stat, attr), |
| 1399 |
getattr(file2_stat, attr) + 1) |
| 1400 |
if hasattr(os, 'chflags') and hasattr(file1_stat, 'st_flags'): |
| 1401 |
self.assertEqual(getattr(file1_stat, 'st_flags'), |
| 1402 |
getattr(file2_stat, 'st_flags')) |
| 1403 |
|
| 1404 |
@os_helper.skip_unless_symlink |
| 1405 |
def test_copy2_symlinks(self): |
| 1406 |
tmp_dir = self.mkdtemp() |
| 1407 |
src = os.path.join(tmp_dir, 'foo') |
| 1408 |
dst = os.path.join(tmp_dir, 'bar') |
| 1409 |
src_link = os.path.join(tmp_dir, 'baz') |
| 1410 |
create_file(src, 'foo') |
| 1411 |
os.symlink(src, src_link) |
| 1412 |
if hasattr(os, 'lchmod'): |
| 1413 |
os.lchmod(src_link, stat.S_IRWXU | stat.S_IRWXO) |
| 1414 |
if hasattr(os, 'lchflags') and hasattr(stat, 'UF_NODUMP'): |
| 1415 |
os.lchflags(src_link, stat.UF_NODUMP) |
| 1416 |
src_stat = os.stat(src) |
| 1417 |
src_link_stat = os.lstat(src_link) |
| 1418 |
# follow |
| 1419 |
shutil.copy2(src_link, dst, follow_symlinks=True) |
| 1420 |
self.assertFalse(os.path.islink(dst)) |
| 1421 |
self.assertEqual(read_file(src), read_file(dst)) |
| 1422 |
os.remove(dst) |
| 1423 |
# don't follow |
| 1424 |
shutil.copy2(src_link, dst, follow_symlinks=False) |
| 1425 |
self.assertTrue(os.path.islink(dst)) |
| 1426 |
self.assertEqual(os.readlink(dst), os.readlink(src_link)) |
| 1427 |
dst_stat = os.lstat(dst) |
| 1428 |
if os.utime in os.supports_follow_symlinks: |
| 1429 |
for attr in 'st_atime', 'st_mtime': |
| 1430 |
# The modification times may be truncated in the new file. |
| 1431 |
self.assertLessEqual(getattr(src_link_stat, attr), |
| 1432 |
getattr(dst_stat, attr) + 1) |
| 1433 |
if hasattr(os, 'lchmod'): |
| 1434 |
self.assertEqual(src_link_stat.st_mode, dst_stat.st_mode) |
| 1435 |
self.assertNotEqual(src_stat.st_mode, dst_stat.st_mode) |
| 1436 |
if hasattr(os, 'lchflags') and hasattr(src_link_stat, 'st_flags'): |
| 1437 |
self.assertEqual(src_link_stat.st_flags, dst_stat.st_flags) |
| 1438 |
|
| 1439 |
@os_helper.skip_unless_xattr |
| 1440 |
def test_copy2_xattr(self): |
| 1441 |
tmp_dir = self.mkdtemp() |
| 1442 |
src = os.path.join(tmp_dir, 'foo') |
| 1443 |
dst = os.path.join(tmp_dir, 'bar') |
| 1444 |
create_file(src, 'foo') |
| 1445 |
os.setxattr(src, 'user.foo', b'42') |
| 1446 |
shutil.copy2(src, dst) |
| 1447 |
self.assertEqual( |
| 1448 |
os.getxattr(src, 'user.foo'), |
| 1449 |
os.getxattr(dst, 'user.foo')) |
| 1450 |
os.remove(dst) |
| 1451 |
|
| 1452 |
def test_copy_return_value(self): |
| 1453 |
# copy and copy2 both return their destination path. |
| 1454 |
for fn in (shutil.copy, shutil.copy2): |
| 1455 |
src_dir = self.mkdtemp() |
| 1456 |
dst_dir = self.mkdtemp() |
| 1457 |
src = os.path.join(src_dir, 'foo') |
| 1458 |
create_file(src, 'foo') |
| 1459 |
rv = fn(src, dst_dir) |
| 1460 |
self.assertEqual(rv, os.path.join(dst_dir, 'foo')) |
| 1461 |
rv = fn(src, os.path.join(dst_dir, 'bar')) |
| 1462 |
self.assertEqual(rv, os.path.join(dst_dir, 'bar')) |
| 1463 |
|
| 1464 |
def test_copy_dir(self): |
| 1465 |
self._test_copy_dir(shutil.copy) |
| 1466 |
|
| 1467 |
def test_copy2_dir(self): |
| 1468 |
self._test_copy_dir(shutil.copy2) |
| 1469 |
|
| 1470 |
def _test_copy_dir(self, copy_func): |
| 1471 |
src_dir = self.mkdtemp() |
| 1472 |
src_file = os.path.join(src_dir, 'foo') |
| 1473 |
dir2 = self.mkdtemp() |
| 1474 |
dst = os.path.join(src_dir, 'does_not_exist/') |
| 1475 |
create_file(src_file, 'foo') |
| 1476 |
if sys.platform == "win32": |
| 1477 |
err = PermissionError |
| 1478 |
else: |
| 1479 |
err = IsADirectoryError |
| 1480 |
self.assertRaises(err, copy_func, dir2, src_dir) |
| 1481 |
|
| 1482 |
# raise *err* because of src rather than FileNotFoundError because of dst |
| 1483 |
self.assertRaises(err, copy_func, dir2, dst) |
| 1484 |
copy_func(src_file, dir2) # should not raise exceptions |
| 1485 |
|
| 1486 |
### shutil.copyfile |
| 1487 |
|
| 1488 |
@os_helper.skip_unless_symlink |
| 1489 |
def test_copyfile_symlinks(self): |
| 1490 |
tmp_dir = self.mkdtemp() |
| 1491 |
src = os.path.join(tmp_dir, 'src') |
| 1492 |
dst = os.path.join(tmp_dir, 'dst') |
| 1493 |
dst_link = os.path.join(tmp_dir, 'dst_link') |
| 1494 |
link = os.path.join(tmp_dir, 'link') |
| 1495 |
create_file(src, 'foo') |
| 1496 |
os.symlink(src, link) |
| 1497 |
# don't follow |
| 1498 |
shutil.copyfile(link, dst_link, follow_symlinks=False) |
| 1499 |
self.assertTrue(os.path.islink(dst_link)) |
| 1500 |
self.assertEqual(os.readlink(link), os.readlink(dst_link)) |
| 1501 |
# follow |
| 1502 |
shutil.copyfile(link, dst) |
| 1503 |
self.assertFalse(os.path.islink(dst)) |
| 1504 |
|
| 1505 |
@unittest.skipUnless(hasattr(os, 'link'), 'requires os.link') |
| 1506 |
def test_dont_copy_file_onto_link_to_itself(self): |
| 1507 |
# bug 851123. |
| 1508 |
os.mkdir(TESTFN) |
| 1509 |
src = os.path.join(TESTFN, 'cheese') |
| 1510 |
dst = os.path.join(TESTFN, 'shop') |
| 1511 |
try: |
| 1512 |
create_file(src, 'cheddar') |
| 1513 |
try: |
| 1514 |
os.link(src, dst) |
| 1515 |
except PermissionError as e: |
| 1516 |
self.skipTest('os.link(): %s' % e) |
| 1517 |
self.assertRaises(shutil.SameFileError, shutil.copyfile, src, dst) |
| 1518 |
with open(src, 'r', encoding='utf-8') as f: |
| 1519 |
self.assertEqual(f.read(), 'cheddar') |
| 1520 |
os.remove(dst) |
| 1521 |
finally: |
| 1522 |
shutil.rmtree(TESTFN, ignore_errors=True) |
| 1523 |
|
| 1524 |
@os_helper.skip_unless_symlink |
| 1525 |
def test_dont_copy_file_onto_symlink_to_itself(self): |
| 1526 |
# bug 851123. |
| 1527 |
os.mkdir(TESTFN) |
| 1528 |
src = os.path.join(TESTFN, 'cheese') |
| 1529 |
dst = os.path.join(TESTFN, 'shop') |
| 1530 |
try: |
| 1531 |
create_file(src, 'cheddar') |
| 1532 |
# Using `src` here would mean we end up with a symlink pointing |
| 1533 |
# to TESTFN/TESTFN/cheese, while it should point at |
| 1534 |
# TESTFN/cheese. |
| 1535 |
os.symlink('cheese', dst) |
| 1536 |
self.assertRaises(shutil.SameFileError, shutil.copyfile, src, dst) |
| 1537 |
with open(src, 'r', encoding='utf-8') as f: |
| 1538 |
self.assertEqual(f.read(), 'cheddar') |
| 1539 |
os.remove(dst) |
| 1540 |
finally: |
| 1541 |
shutil.rmtree(TESTFN, ignore_errors=True) |
| 1542 |
|
| 1543 |
# Issue #3002: copyfile and copytree block indefinitely on named pipes |
| 1544 |
@unittest.skipUnless(hasattr(os, "mkfifo"), 'requires os.mkfifo()') |
| 1545 |
@unittest.skipIf(sys.platform == "vxworks", |
| 1546 |
"fifo requires special path on VxWorks") |
| 1547 |
def test_copyfile_named_pipe(self): |
| 1548 |
try: |
| 1549 |
os.mkfifo(TESTFN) |
| 1550 |
except PermissionError as e: |
| 1551 |
self.skipTest('os.mkfifo(): %s' % e) |
| 1552 |
try: |
| 1553 |
self.assertRaises(shutil.SpecialFileError, |
| 1554 |
shutil.copyfile, TESTFN, TESTFN2) |
| 1555 |
self.assertRaises(shutil.SpecialFileError, |
| 1556 |
shutil.copyfile, __file__, TESTFN) |
| 1557 |
finally: |
| 1558 |
os.remove(TESTFN) |
| 1559 |
|
| 1560 |
def test_copyfile_return_value(self): |
| 1561 |
# copytree returns its destination path. |
| 1562 |
src_dir = self.mkdtemp() |
| 1563 |
dst_dir = self.mkdtemp() |
| 1564 |
dst_file = os.path.join(dst_dir, 'bar') |
| 1565 |
src_file = os.path.join(src_dir, 'foo') |
| 1566 |
create_file(src_file, 'foo') |
| 1567 |
rv = shutil.copyfile(src_file, dst_file) |
| 1568 |
self.assertTrue(os.path.exists(rv)) |
| 1569 |
self.assertEqual(read_file(src_file), read_file(dst_file)) |
| 1570 |
|
| 1571 |
def test_copyfile_same_file(self): |
| 1572 |
# copyfile() should raise SameFileError if the source and destination |
| 1573 |
# are the same. |
| 1574 |
src_dir = self.mkdtemp() |
| 1575 |
src_file = os.path.join(src_dir, 'foo') |
| 1576 |
create_file(src_file, 'foo') |
| 1577 |
self.assertRaises(SameFileError, shutil.copyfile, src_file, src_file) |
| 1578 |
# But Error should work too, to stay backward compatible. |
| 1579 |
self.assertRaises(Error, shutil.copyfile, src_file, src_file) |
| 1580 |
# Make sure file is not corrupted. |
| 1581 |
self.assertEqual(read_file(src_file), 'foo') |
| 1582 |
|
| 1583 |
@unittest.skipIf(MACOS or SOLARIS or _winapi, 'On MACOS, Solaris and Windows the errors are not confusing (though different)') |
| 1584 |
# gh-92670: The test uses a trailing slash to force the OS consider |
| 1585 |
# the path as a directory, but on AIX the trailing slash has no effect |
| 1586 |
# and is considered as a file. |
| 1587 |
@unittest.skipIf(AIX, 'Not valid on AIX, see gh-92670') |
| 1588 |
def test_copyfile_nonexistent_dir(self): |
| 1589 |
# Issue 43219 |
| 1590 |
src_dir = self.mkdtemp() |
| 1591 |
src_file = os.path.join(src_dir, 'foo') |
| 1592 |
dst = os.path.join(src_dir, 'does_not_exist/') |
| 1593 |
create_file(src_file, 'foo') |
| 1594 |
self.assertRaises(FileNotFoundError, shutil.copyfile, src_file, dst) |
| 1595 |
|
| 1596 |
def test_copyfile_copy_dir(self): |
| 1597 |
# Issue 45234 |
| 1598 |
# test copy() and copyfile() raising proper exceptions when src and/or |
| 1599 |
# dst are directories |
| 1600 |
src_dir = self.mkdtemp() |
| 1601 |
src_file = os.path.join(src_dir, 'foo') |
| 1602 |
dir2 = self.mkdtemp() |
| 1603 |
dst = os.path.join(src_dir, 'does_not_exist/') |
| 1604 |
create_file(src_file, 'foo') |
| 1605 |
if sys.platform == "win32": |
| 1606 |
err = PermissionError |
| 1607 |
else: |
| 1608 |
err = IsADirectoryError |
| 1609 |
|
| 1610 |
self.assertRaises(err, shutil.copyfile, src_dir, dst) |
| 1611 |
self.assertRaises(err, shutil.copyfile, src_file, src_dir) |
| 1612 |
self.assertRaises(err, shutil.copyfile, dir2, src_dir) |
| 1613 |
|
| 1614 |
|
| 1615 |
class TestArchives(BaseTest, unittest.TestCase): |
| 1616 |
|
| 1617 |
### shutil.make_archive |
| 1618 |
|
| 1619 |
def _tarinfo(self, path): |
| 1620 |
with tarfile.open(path) as tar: |
| 1621 |
names = tar.getnames() |
| 1622 |
names.sort() |
| 1623 |
return tuple(names) |
| 1624 |
|
| 1625 |
def _create_files(self, base_dir='dist'): |
| 1626 |
# creating something to tar |
| 1627 |
root_dir = self.mkdtemp() |
| 1628 |
dist = os.path.join(root_dir, base_dir) |
| 1629 |
os.makedirs(dist, exist_ok=True) |
| 1630 |
create_file((dist, 'file1'), 'xxx') |
| 1631 |
create_file((dist, 'file2'), 'xxx') |
| 1632 |
os.mkdir(os.path.join(dist, 'sub')) |
| 1633 |
create_file((dist, 'sub', 'file3'), 'xxx') |
| 1634 |
os.mkdir(os.path.join(dist, 'sub2')) |
| 1635 |
if base_dir: |
| 1636 |
create_file((root_dir, 'outer'), 'xxx') |
| 1637 |
return root_dir, base_dir |
| 1638 |
|
| 1639 |
@support.requires_zlib() |
| 1640 |
def test_make_tarfile(self): |
| 1641 |
root_dir, base_dir = self._create_files() |
| 1642 |
# Test without base_dir. |
| 1643 |
with os_helper.temp_cwd(), no_chdir: |
| 1644 |
base_name = os.path.join('dst', 'archive') |
| 1645 |
archive = make_archive(base_name, 'tar', root_dir) |
| 1646 |
# check if the compressed tarball was created |
| 1647 |
self.assertEqual(archive, os.path.abspath(base_name) + '.tar') |
| 1648 |
self.assertTrue(os.path.isfile(archive)) |
| 1649 |
self.assertTrue(tarfile.is_tarfile(archive)) |
| 1650 |
with tarfile.open(archive, 'r') as tf: |
| 1651 |
self.assertCountEqual(tf.getnames(), |
| 1652 |
['.', './dist', './dist/sub', './dist/sub2', |
| 1653 |
'./dist/file1', './dist/file2', './dist/sub/file3', |
| 1654 |
'./outer']) |
| 1655 |
|
| 1656 |
# Test with base_dir. |
| 1657 |
with os_helper.temp_cwd(), no_chdir: |
| 1658 |
base_name = os.path.join('dst2', 'archive') |
| 1659 |
archive = make_archive(base_name, 'tar', root_dir, base_dir) |
| 1660 |
self.assertEqual(archive, os.path.abspath(base_name) + '.tar') |
| 1661 |
# check if the uncompressed tarball was created |
| 1662 |
self.assertTrue(os.path.isfile(archive)) |
| 1663 |
self.assertTrue(tarfile.is_tarfile(archive)) |
| 1664 |
with tarfile.open(archive, 'r') as tf: |
| 1665 |
self.assertCountEqual(tf.getnames(), |
| 1666 |
['dist', 'dist/sub', 'dist/sub2', |
| 1667 |
'dist/file1', 'dist/file2', 'dist/sub/file3']) |
| 1668 |
|
| 1669 |
# Test with multi-component base_dir. |
| 1670 |
with os_helper.temp_cwd(), no_chdir: |
| 1671 |
base_name = os.path.join('dst3', 'archive') |
| 1672 |
archive = make_archive(base_name, 'tar', root_dir, |
| 1673 |
os.path.join(base_dir, 'sub')) |
| 1674 |
self.assertEqual(archive, os.path.abspath(base_name) + '.tar') |
| 1675 |
self.assertTrue(os.path.isfile(archive)) |
| 1676 |
self.assertTrue(tarfile.is_tarfile(archive)) |
| 1677 |
with tarfile.open(archive, 'r') as tf: |
| 1678 |
self.assertCountEqual(tf.getnames(), |
| 1679 |
['dist/sub', 'dist/sub/file3']) |
| 1680 |
|
| 1681 |
@support.requires_zlib() |
| 1682 |
def test_make_tarfile_without_rootdir(self): |
| 1683 |
root_dir, base_dir = self._create_files() |
| 1684 |
# Test without base_dir. |
| 1685 |
base_name = os.path.join(self.mkdtemp(), 'dst', 'archive') |
| 1686 |
base_name = os.path.relpath(base_name, root_dir) |
| 1687 |
with os_helper.change_cwd(root_dir), no_chdir: |
| 1688 |
archive = make_archive(base_name, 'gztar') |
| 1689 |
self.assertEqual(archive, base_name + '.tar.gz') |
| 1690 |
self.assertTrue(os.path.isfile(archive)) |
| 1691 |
self.assertTrue(tarfile.is_tarfile(archive)) |
| 1692 |
with tarfile.open(archive, 'r:gz') as tf: |
| 1693 |
self.assertCountEqual(tf.getnames(), |
| 1694 |
['.', './dist', './dist/sub', './dist/sub2', |
| 1695 |
'./dist/file1', './dist/file2', './dist/sub/file3', |
| 1696 |
'./outer']) |
| 1697 |
|
| 1698 |
# Test with base_dir. |
| 1699 |
with os_helper.change_cwd(root_dir), no_chdir: |
| 1700 |
base_name = os.path.join('dst', 'archive') |
| 1701 |
archive = make_archive(base_name, 'tar', base_dir=base_dir) |
| 1702 |
self.assertEqual(archive, base_name + '.tar') |
| 1703 |
self.assertTrue(os.path.isfile(archive)) |
| 1704 |
self.assertTrue(tarfile.is_tarfile(archive)) |
| 1705 |
with tarfile.open(archive, 'r') as tf: |
| 1706 |
self.assertCountEqual(tf.getnames(), |
| 1707 |
['dist', 'dist/sub', 'dist/sub2', |
| 1708 |
'dist/file1', 'dist/file2', 'dist/sub/file3']) |
| 1709 |
|
| 1710 |
def test_make_tarfile_with_explicit_curdir(self): |
| 1711 |
# Test with base_dir=os.curdir. |
| 1712 |
root_dir, base_dir = self._create_files() |
| 1713 |
with os_helper.temp_cwd(), no_chdir: |
| 1714 |
base_name = os.path.join('dst', 'archive') |
| 1715 |
archive = make_archive(base_name, 'tar', root_dir, os.curdir) |
| 1716 |
self.assertEqual(archive, os.path.abspath(base_name) + '.tar') |
| 1717 |
self.assertTrue(os.path.isfile(archive)) |
| 1718 |
self.assertTrue(tarfile.is_tarfile(archive)) |
| 1719 |
with tarfile.open(archive, 'r') as tf: |
| 1720 |
self.assertCountEqual(tf.getnames(), |
| 1721 |
['.', './dist', './dist/sub', './dist/sub2', |
| 1722 |
'./dist/file1', './dist/file2', './dist/sub/file3', |
| 1723 |
'./outer']) |
| 1724 |
|
| 1725 |
@support.requires_zlib() |
| 1726 |
@unittest.skipUnless(shutil.which('tar'), |
| 1727 |
'Need the tar command to run') |
| 1728 |
def test_tarfile_vs_tar(self): |
| 1729 |
root_dir, base_dir = self._create_files() |
| 1730 |
base_name = os.path.join(self.mkdtemp(), 'archive') |
| 1731 |
with no_chdir: |
| 1732 |
tarball = make_archive(base_name, 'gztar', root_dir, base_dir) |
| 1733 |
|
| 1734 |
# check if the compressed tarball was created |
| 1735 |
self.assertEqual(tarball, base_name + '.tar.gz') |
| 1736 |
self.assertTrue(os.path.isfile(tarball)) |
| 1737 |
|
| 1738 |
# now create another tarball using `tar` |
| 1739 |
tarball2 = os.path.join(root_dir, 'archive2.tar') |
| 1740 |
tar_cmd = ['tar', '-cf', 'archive2.tar', base_dir] |
| 1741 |
if sys.platform == 'darwin': |
| 1742 |
# macOS tar can include extended attributes, |
| 1743 |
# ACLs and other mac specific metadata into the |
| 1744 |
# archive (an recentish version of the OS). |
| 1745 |
# |
| 1746 |
# This feature can be disabled with the |
| 1747 |
# '--no-mac-metadata' option on macOS 11 or |
| 1748 |
# later. |
| 1749 |
import platform |
| 1750 |
if int(platform.mac_ver()[0].split('.')[0]) >= 11: |
| 1751 |
tar_cmd.insert(1, '--no-mac-metadata') |
| 1752 |
subprocess.check_call(tar_cmd, cwd=root_dir, |
| 1753 |
stdout=subprocess.DEVNULL) |
| 1754 |
|
| 1755 |
self.assertTrue(os.path.isfile(tarball2)) |
| 1756 |
# let's compare both tarballs |
| 1757 |
self.assertEqual(self._tarinfo(tarball), self._tarinfo(tarball2)) |
| 1758 |
|
| 1759 |
# trying an uncompressed one |
| 1760 |
with no_chdir: |
| 1761 |
tarball = make_archive(base_name, 'tar', root_dir, base_dir) |
| 1762 |
self.assertEqual(tarball, base_name + '.tar') |
| 1763 |
self.assertTrue(os.path.isfile(tarball)) |
| 1764 |
|
| 1765 |
# now for a dry_run |
| 1766 |
with no_chdir: |
| 1767 |
tarball = make_archive(base_name, 'tar', root_dir, base_dir, |
| 1768 |
dry_run=True) |
| 1769 |
self.assertEqual(tarball, base_name + '.tar') |
| 1770 |
self.assertTrue(os.path.isfile(tarball)) |
| 1771 |
|
| 1772 |
@support.requires_zlib() |
| 1773 |
def test_make_zipfile(self): |
| 1774 |
root_dir, base_dir = self._create_files() |
| 1775 |
# Test without base_dir. |
| 1776 |
with os_helper.temp_cwd(), no_chdir: |
| 1777 |
base_name = os.path.join('dst', 'archive') |
| 1778 |
archive = make_archive(base_name, 'zip', root_dir) |
| 1779 |
self.assertEqual(archive, os.path.abspath(base_name) + '.zip') |
| 1780 |
self.assertTrue(os.path.isfile(archive)) |
| 1781 |
self.assertTrue(zipfile.is_zipfile(archive)) |
| 1782 |
with zipfile.ZipFile(archive) as zf: |
| 1783 |
self.assertCountEqual(zf.namelist(), |
| 1784 |
['dist/', 'dist/sub/', 'dist/sub2/', |
| 1785 |
'dist/file1', 'dist/file2', 'dist/sub/file3', |
| 1786 |
'outer']) |
| 1787 |
|
| 1788 |
# Test with base_dir. |
| 1789 |
with os_helper.temp_cwd(), no_chdir: |
| 1790 |
base_name = os.path.join('dst2', 'archive') |
| 1791 |
archive = make_archive(base_name, 'zip', root_dir, base_dir) |
| 1792 |
self.assertEqual(archive, os.path.abspath(base_name) + '.zip') |
| 1793 |
self.assertTrue(os.path.isfile(archive)) |
| 1794 |
self.assertTrue(zipfile.is_zipfile(archive)) |
| 1795 |
with zipfile.ZipFile(archive) as zf: |
| 1796 |
self.assertCountEqual(zf.namelist(), |
| 1797 |
['dist/', 'dist/sub/', 'dist/sub2/', |
| 1798 |
'dist/file1', 'dist/file2', 'dist/sub/file3']) |
| 1799 |
|
| 1800 |
# Test with multi-component base_dir. |
| 1801 |
with os_helper.temp_cwd(), no_chdir: |
| 1802 |
base_name = os.path.join('dst3', 'archive') |
| 1803 |
archive = make_archive(base_name, 'zip', root_dir, |
| 1804 |
os.path.join(base_dir, 'sub')) |
| 1805 |
self.assertEqual(archive, os.path.abspath(base_name) + '.zip') |
| 1806 |
self.assertTrue(os.path.isfile(archive)) |
| 1807 |
self.assertTrue(zipfile.is_zipfile(archive)) |
| 1808 |
with zipfile.ZipFile(archive) as zf: |
| 1809 |
self.assertCountEqual(zf.namelist(), |
| 1810 |
['dist/sub/', 'dist/sub/file3']) |
| 1811 |
|
| 1812 |
@support.requires_zlib() |
| 1813 |
def test_make_zipfile_without_rootdir(self): |
| 1814 |
root_dir, base_dir = self._create_files() |
| 1815 |
# Test without base_dir. |
| 1816 |
base_name = os.path.join(self.mkdtemp(), 'dst', 'archive') |
| 1817 |
base_name = os.path.relpath(base_name, root_dir) |
| 1818 |
with os_helper.change_cwd(root_dir), no_chdir: |
| 1819 |
archive = make_archive(base_name, 'zip') |
| 1820 |
self.assertEqual(archive, base_name + '.zip') |
| 1821 |
self.assertTrue(os.path.isfile(archive)) |
| 1822 |
self.assertTrue(zipfile.is_zipfile(archive)) |
| 1823 |
with zipfile.ZipFile(archive) as zf: |
| 1824 |
self.assertCountEqual(zf.namelist(), |
| 1825 |
['dist/', 'dist/sub/', 'dist/sub2/', |
| 1826 |
'dist/file1', 'dist/file2', 'dist/sub/file3', |
| 1827 |
'outer']) |
| 1828 |
|
| 1829 |
# Test with base_dir. |
| 1830 |
root_dir, base_dir = self._create_files() |
| 1831 |
with os_helper.change_cwd(root_dir), no_chdir: |
| 1832 |
base_name = os.path.join('dst', 'archive') |
| 1833 |
archive = make_archive(base_name, 'zip', base_dir=base_dir) |
| 1834 |
self.assertEqual(archive, base_name + '.zip') |
| 1835 |
self.assertTrue(os.path.isfile(archive)) |
| 1836 |
self.assertTrue(zipfile.is_zipfile(archive)) |
| 1837 |
with zipfile.ZipFile(archive) as zf: |
| 1838 |
self.assertCountEqual(zf.namelist(), |
| 1839 |
['dist/', 'dist/sub/', 'dist/sub2/', |
| 1840 |
'dist/file1', 'dist/file2', 'dist/sub/file3']) |
| 1841 |
|
| 1842 |
@support.requires_zlib() |
| 1843 |
def test_make_zipfile_with_explicit_curdir(self): |
| 1844 |
# Test with base_dir=os.curdir. |
| 1845 |
root_dir, base_dir = self._create_files() |
| 1846 |
with os_helper.temp_cwd(), no_chdir: |
| 1847 |
base_name = os.path.join('dst', 'archive') |
| 1848 |
archive = make_archive(base_name, 'zip', root_dir, os.curdir) |
| 1849 |
self.assertEqual(archive, os.path.abspath(base_name) + '.zip') |
| 1850 |
self.assertTrue(os.path.isfile(archive)) |
| 1851 |
self.assertTrue(zipfile.is_zipfile(archive)) |
| 1852 |
with zipfile.ZipFile(archive) as zf: |
| 1853 |
self.assertCountEqual(zf.namelist(), |
| 1854 |
['dist/', 'dist/sub/', 'dist/sub2/', |
| 1855 |
'dist/file1', 'dist/file2', 'dist/sub/file3', |
| 1856 |
'outer']) |
| 1857 |
|
| 1858 |
@support.requires_zlib() |
| 1859 |
@unittest.skipUnless(shutil.which('zip'), |
| 1860 |
'Need the zip command to run') |
| 1861 |
def test_zipfile_vs_zip(self): |
| 1862 |
root_dir, base_dir = self._create_files() |
| 1863 |
base_name = os.path.join(self.mkdtemp(), 'archive') |
| 1864 |
with no_chdir: |
| 1865 |
archive = make_archive(base_name, 'zip', root_dir, base_dir) |
| 1866 |
|
| 1867 |
# check if ZIP file was created |
| 1868 |
self.assertEqual(archive, base_name + '.zip') |
| 1869 |
self.assertTrue(os.path.isfile(archive)) |
| 1870 |
|
| 1871 |
# now create another ZIP file using `zip` |
| 1872 |
archive2 = os.path.join(root_dir, 'archive2.zip') |
| 1873 |
zip_cmd = ['zip', '-q', '-r', 'archive2.zip', base_dir] |
| 1874 |
subprocess.check_call(zip_cmd, cwd=root_dir, |
| 1875 |
stdout=subprocess.DEVNULL) |
| 1876 |
|
| 1877 |
self.assertTrue(os.path.isfile(archive2)) |
| 1878 |
# let's compare both ZIP files |
| 1879 |
with zipfile.ZipFile(archive) as zf: |
| 1880 |
names = zf.namelist() |
| 1881 |
with zipfile.ZipFile(archive2) as zf: |
| 1882 |
names2 = zf.namelist() |
| 1883 |
self.assertEqual(sorted(names), sorted(names2)) |
| 1884 |
|
| 1885 |
@support.requires_zlib() |
| 1886 |
@unittest.skipUnless(shutil.which('unzip'), |
| 1887 |
'Need the unzip command to run') |
| 1888 |
def test_unzip_zipfile(self): |
| 1889 |
root_dir, base_dir = self._create_files() |
| 1890 |
base_name = os.path.join(self.mkdtemp(), 'archive') |
| 1891 |
with no_chdir: |
| 1892 |
archive = make_archive(base_name, 'zip', root_dir, base_dir) |
| 1893 |
|
| 1894 |
# check if ZIP file was created |
| 1895 |
self.assertEqual(archive, base_name + '.zip') |
| 1896 |
self.assertTrue(os.path.isfile(archive)) |
| 1897 |
|
| 1898 |
# now check the ZIP file using `unzip -t` |
| 1899 |
zip_cmd = ['unzip', '-t', archive] |
| 1900 |
with os_helper.change_cwd(root_dir): |
| 1901 |
try: |
| 1902 |
subprocess.check_output(zip_cmd, stderr=subprocess.STDOUT) |
| 1903 |
except subprocess.CalledProcessError as exc: |
| 1904 |
details = exc.output.decode(errors="replace") |
| 1905 |
if any(message in details for message in [ |
| 1906 |
'unrecognized option: t', # BusyBox |
| 1907 |
'invalid option -- t', # Android |
| 1908 |
]): |
| 1909 |
self.skipTest("unzip doesn't support -t") |
| 1910 |
msg = "{}\n\n**Unzip Output**\n{}" |
| 1911 |
self.fail(msg.format(exc, details)) |
| 1912 |
|
| 1913 |
def test_make_archive(self): |
| 1914 |
tmpdir = self.mkdtemp() |
| 1915 |
base_name = os.path.join(tmpdir, 'archive') |
| 1916 |
self.assertRaises(ValueError, make_archive, base_name, 'xxx') |
| 1917 |
|
| 1918 |
@support.requires_zlib() |
| 1919 |
def test_make_archive_owner_group(self): |
| 1920 |
# testing make_archive with owner and group, with various combinations |
| 1921 |
# this works even if there's not gid/uid support |
| 1922 |
if UID_GID_SUPPORT: |
| 1923 |
group = grp.getgrgid(0)[0] |
| 1924 |
owner = pwd.getpwuid(0)[0] |
| 1925 |
else: |
| 1926 |
group = owner = 'root' |
| 1927 |
|
| 1928 |
root_dir, base_dir = self._create_files() |
| 1929 |
base_name = os.path.join(self.mkdtemp(), 'archive') |
| 1930 |
res = make_archive(base_name, 'zip', root_dir, base_dir, owner=owner, |
| 1931 |
group=group) |
| 1932 |
self.assertTrue(os.path.isfile(res)) |
| 1933 |
|
| 1934 |
res = make_archive(base_name, 'zip', root_dir, base_dir) |
| 1935 |
self.assertTrue(os.path.isfile(res)) |
| 1936 |
|
| 1937 |
res = make_archive(base_name, 'tar', root_dir, base_dir, |
| 1938 |
owner=owner, group=group) |
| 1939 |
self.assertTrue(os.path.isfile(res)) |
| 1940 |
|
| 1941 |
res = make_archive(base_name, 'tar', root_dir, base_dir, |
| 1942 |
owner='kjhkjhkjg', group='oihohoh') |
| 1943 |
self.assertTrue(os.path.isfile(res)) |
| 1944 |
|
| 1945 |
|
| 1946 |
@support.requires_zlib() |
| 1947 |
@unittest.skipUnless(UID_GID_SUPPORT, "Requires grp and pwd support") |
| 1948 |
def test_tarfile_root_owner(self): |
| 1949 |
root_dir, base_dir = self._create_files() |
| 1950 |
base_name = os.path.join(self.mkdtemp(), 'archive') |
| 1951 |
group = grp.getgrgid(0)[0] |
| 1952 |
owner = pwd.getpwuid(0)[0] |
| 1953 |
with os_helper.change_cwd(root_dir), no_chdir: |
| 1954 |
archive_name = make_archive(base_name, 'gztar', root_dir, 'dist', |
| 1955 |
owner=owner, group=group) |
| 1956 |
|
| 1957 |
# check if the compressed tarball was created |
| 1958 |
self.assertTrue(os.path.isfile(archive_name)) |
| 1959 |
|
| 1960 |
# now checks the rights |
| 1961 |
archive = tarfile.open(archive_name) |
| 1962 |
try: |
| 1963 |
for member in archive.getmembers(): |
| 1964 |
self.assertEqual(member.uid, 0) |
| 1965 |
self.assertEqual(member.gid, 0) |
| 1966 |
finally: |
| 1967 |
archive.close() |
| 1968 |
|
| 1969 |
def test_make_archive_cwd_default(self): |
| 1970 |
current_dir = os.getcwd() |
| 1971 |
def archiver(base_name, base_dir, **kw): |
| 1972 |
self.assertNotIn('root_dir', kw) |
| 1973 |
self.assertEqual(base_name, 'basename') |
| 1974 |
self.assertEqual(os.getcwd(), current_dir) |
| 1975 |
raise RuntimeError() |
| 1976 |
|
| 1977 |
register_archive_format('xxx', archiver, [], 'xxx file') |
| 1978 |
try: |
| 1979 |
with no_chdir: |
| 1980 |
with self.assertRaises(RuntimeError): |
| 1981 |
make_archive('basename', 'xxx') |
| 1982 |
self.assertEqual(os.getcwd(), current_dir) |
| 1983 |
finally: |
| 1984 |
unregister_archive_format('xxx') |
| 1985 |
|
| 1986 |
def test_make_archive_cwd(self): |
| 1987 |
current_dir = os.getcwd() |
| 1988 |
root_dir = self.mkdtemp() |
| 1989 |
def archiver(base_name, base_dir, **kw): |
| 1990 |
self.assertNotIn('root_dir', kw) |
| 1991 |
self.assertEqual(base_name, os.path.join(current_dir, 'basename')) |
| 1992 |
self.assertEqual(os.getcwd(), root_dir) |
| 1993 |
raise RuntimeError() |
| 1994 |
dirs = [] |
| 1995 |
def _chdir(path): |
| 1996 |
dirs.append(path) |
| 1997 |
orig_chdir(path) |
| 1998 |
|
| 1999 |
register_archive_format('xxx', archiver, [], 'xxx file') |
| 2000 |
try: |
| 2001 |
with support.swap_attr(os, 'chdir', _chdir) as orig_chdir: |
| 2002 |
with self.assertRaises(RuntimeError): |
| 2003 |
make_archive('basename', 'xxx', root_dir=root_dir) |
| 2004 |
self.assertEqual(os.getcwd(), current_dir) |
| 2005 |
self.assertEqual(dirs, [root_dir, current_dir]) |
| 2006 |
finally: |
| 2007 |
unregister_archive_format('xxx') |
| 2008 |
|
| 2009 |
def test_make_archive_cwd_supports_root_dir(self): |
| 2010 |
current_dir = os.getcwd() |
| 2011 |
root_dir = self.mkdtemp() |
| 2012 |
def archiver(base_name, base_dir, **kw): |
| 2013 |
self.assertEqual(base_name, 'basename') |
| 2014 |
self.assertEqual(kw['root_dir'], root_dir) |
| 2015 |
self.assertEqual(os.getcwd(), current_dir) |
| 2016 |
raise RuntimeError() |
| 2017 |
archiver.supports_root_dir = True |
| 2018 |
|
| 2019 |
register_archive_format('xxx', archiver, [], 'xxx file') |
| 2020 |
try: |
| 2021 |
with no_chdir: |
| 2022 |
with self.assertRaises(RuntimeError): |
| 2023 |
make_archive('basename', 'xxx', root_dir=root_dir) |
| 2024 |
self.assertEqual(os.getcwd(), current_dir) |
| 2025 |
finally: |
| 2026 |
unregister_archive_format('xxx') |
| 2027 |
|
| 2028 |
def test_make_tarfile_in_curdir(self): |
| 2029 |
# Issue #21280: Test with the archive in the current directory. |
| 2030 |
root_dir = self.mkdtemp() |
| 2031 |
with os_helper.change_cwd(root_dir), no_chdir: |
| 2032 |
# root_dir must be None, so the archive path is relative. |
| 2033 |
self.assertEqual(make_archive('test', 'tar'), 'test.tar') |
| 2034 |
self.assertTrue(os.path.isfile('test.tar')) |
| 2035 |
|
| 2036 |
@support.requires_zlib() |
| 2037 |
def test_make_zipfile_in_curdir(self): |
| 2038 |
# Issue #21280: Test with the archive in the current directory. |
| 2039 |
root_dir = self.mkdtemp() |
| 2040 |
with os_helper.change_cwd(root_dir), no_chdir: |
| 2041 |
# root_dir must be None, so the archive path is relative. |
| 2042 |
self.assertEqual(make_archive('test', 'zip'), 'test.zip') |
| 2043 |
self.assertTrue(os.path.isfile('test.zip')) |
| 2044 |
|
| 2045 |
def test_register_archive_format(self): |
| 2046 |
|
| 2047 |
self.assertRaises(TypeError, register_archive_format, 'xxx', 1) |
| 2048 |
self.assertRaises(TypeError, register_archive_format, 'xxx', lambda: x, |
| 2049 |
1) |
| 2050 |
self.assertRaises(TypeError, register_archive_format, 'xxx', lambda: x, |
| 2051 |
[(1, 2), (1, 2, 3)]) |
| 2052 |
|
| 2053 |
register_archive_format('xxx', lambda: x, [(1, 2)], 'xxx file') |
| 2054 |
formats = [name for name, params in get_archive_formats()] |
| 2055 |
self.assertIn('xxx', formats) |
| 2056 |
|
| 2057 |
unregister_archive_format('xxx') |
| 2058 |
formats = [name for name, params in get_archive_formats()] |
| 2059 |
self.assertNotIn('xxx', formats) |
| 2060 |
|
| 2061 |
def test_make_tarfile_rootdir_nodir(self): |
| 2062 |
# GH-99203: Test with root_dir is not a real directory. |
| 2063 |
self.addCleanup(os_helper.unlink, f'{TESTFN}.tar') |
| 2064 |
for dry_run in (False, True): |
| 2065 |
with self.subTest(dry_run=dry_run): |
| 2066 |
# root_dir does not exist. |
| 2067 |
tmp_dir = self.mkdtemp() |
| 2068 |
nonexisting_file = os.path.join(tmp_dir, 'nonexisting') |
| 2069 |
with self.assertRaises(FileNotFoundError) as cm: |
| 2070 |
make_archive(TESTFN, 'tar', nonexisting_file, dry_run=dry_run) |
| 2071 |
self.assertEqual(cm.exception.errno, errno.ENOENT) |
| 2072 |
self.assertEqual(cm.exception.filename, nonexisting_file) |
| 2073 |
self.assertFalse(os.path.exists(f'{TESTFN}.tar')) |
| 2074 |
|
| 2075 |
# root_dir is a file. |
| 2076 |
tmp_fd, tmp_file = tempfile.mkstemp(dir=tmp_dir) |
| 2077 |
os.close(tmp_fd) |
| 2078 |
with self.assertRaises(NotADirectoryError) as cm: |
| 2079 |
make_archive(TESTFN, 'tar', tmp_file, dry_run=dry_run) |
| 2080 |
self.assertEqual(cm.exception.errno, errno.ENOTDIR) |
| 2081 |
self.assertEqual(cm.exception.filename, tmp_file) |
| 2082 |
self.assertFalse(os.path.exists(f'{TESTFN}.tar')) |
| 2083 |
|
| 2084 |
@support.requires_zlib() |
| 2085 |
def test_make_zipfile_rootdir_nodir(self): |
| 2086 |
# GH-99203: Test with root_dir is not a real directory. |
| 2087 |
self.addCleanup(os_helper.unlink, f'{TESTFN}.zip') |
| 2088 |
for dry_run in (False, True): |
| 2089 |
with self.subTest(dry_run=dry_run): |
| 2090 |
# root_dir does not exist. |
| 2091 |
tmp_dir = self.mkdtemp() |
| 2092 |
nonexisting_file = os.path.join(tmp_dir, 'nonexisting') |
| 2093 |
with self.assertRaises(FileNotFoundError) as cm: |
| 2094 |
make_archive(TESTFN, 'zip', nonexisting_file, dry_run=dry_run) |
| 2095 |
self.assertEqual(cm.exception.errno, errno.ENOENT) |
| 2096 |
self.assertEqual(cm.exception.filename, nonexisting_file) |
| 2097 |
self.assertFalse(os.path.exists(f'{TESTFN}.zip')) |
| 2098 |
|
| 2099 |
# root_dir is a file. |
| 2100 |
tmp_fd, tmp_file = tempfile.mkstemp(dir=tmp_dir) |
| 2101 |
os.close(tmp_fd) |
| 2102 |
with self.assertRaises(NotADirectoryError) as cm: |
| 2103 |
make_archive(TESTFN, 'zip', tmp_file, dry_run=dry_run) |
| 2104 |
self.assertEqual(cm.exception.errno, errno.ENOTDIR) |
| 2105 |
self.assertEqual(cm.exception.filename, tmp_file) |
| 2106 |
self.assertFalse(os.path.exists(f'{TESTFN}.zip')) |
| 2107 |
|
| 2108 |
### shutil.unpack_archive |
| 2109 |
|
| 2110 |
def check_unpack_archive(self, format, **kwargs): |
| 2111 |
self.check_unpack_archive_with_converter( |
| 2112 |
format, lambda path: path, **kwargs) |
| 2113 |
self.check_unpack_archive_with_converter(format, FakePath, **kwargs) |
| 2114 |
|
| 2115 |
def check_unpack_archive_with_converter(self, format, converter, **kwargs): |
| 2116 |
root_dir, base_dir = self._create_files() |
| 2117 |
expected = rlistdir(root_dir) |
| 2118 |
expected.remove('outer') |
| 2119 |
|
| 2120 |
base_name = os.path.join(self.mkdtemp(), 'archive') |
| 2121 |
filename = make_archive(base_name, format, root_dir, base_dir) |
| 2122 |
|
| 2123 |
# let's try to unpack it now |
| 2124 |
tmpdir2 = self.mkdtemp() |
| 2125 |
unpack_archive(converter(filename), converter(tmpdir2), **kwargs) |
| 2126 |
self.assertEqual(rlistdir(tmpdir2), expected) |
| 2127 |
|
| 2128 |
# and again, this time with the format specified |
| 2129 |
tmpdir3 = self.mkdtemp() |
| 2130 |
unpack_archive(converter(filename), converter(tmpdir3), format=format, |
| 2131 |
**kwargs) |
| 2132 |
self.assertEqual(rlistdir(tmpdir3), expected) |
| 2133 |
|
| 2134 |
with self.assertRaises(shutil.ReadError): |
| 2135 |
unpack_archive(converter(TESTFN), **kwargs) |
| 2136 |
with self.assertRaises(ValueError): |
| 2137 |
unpack_archive(converter(TESTFN), format='xxx', **kwargs) |
| 2138 |
|
| 2139 |
def check_unpack_tarball(self, format): |
| 2140 |
self.check_unpack_archive(format, filter='fully_trusted') |
| 2141 |
self.check_unpack_archive(format, filter='data') |
| 2142 |
|
| 2143 |
def test_unpack_archive_tar(self): |
| 2144 |
self.check_unpack_tarball('tar') |
| 2145 |
|
| 2146 |
@support.requires_zlib() |
| 2147 |
def test_unpack_archive_gztar(self): |
| 2148 |
self.check_unpack_tarball('gztar') |
| 2149 |
|
| 2150 |
@support.requires_bz2() |
| 2151 |
def test_unpack_archive_bztar(self): |
| 2152 |
self.check_unpack_tarball('bztar') |
| 2153 |
|
| 2154 |
@support.requires_zstd() |
| 2155 |
def test_unpack_archive_zstdtar(self): |
| 2156 |
self.check_unpack_tarball('zstdtar') |
| 2157 |
|
| 2158 |
@support.requires_lzma() |
| 2159 |
@unittest.skipIf(AIX and not _maxdataOK(), "AIX MAXDATA must be 0x20000000 or larger") |
| 2160 |
def test_unpack_archive_xztar(self): |
| 2161 |
self.check_unpack_tarball('xztar') |
| 2162 |
|
| 2163 |
@support.requires_zlib() |
| 2164 |
def test_unpack_archive_zip(self): |
| 2165 |
self.check_unpack_archive('zip') |
| 2166 |
with self.assertRaises(TypeError): |
| 2167 |
self.check_unpack_archive('zip', filter='data') |
| 2168 |
|
| 2169 |
def test_unpack_archive_zip_badpaths(self): |
| 2170 |
srcdir = self.mkdtemp() |
| 2171 |
zipname = os.path.join(srcdir, 'test.zip') |
| 2172 |
abspath = os.path.join(srcdir, 'abspath') |
| 2173 |
with zipfile.ZipFile(zipname, 'w') as zf: |
| 2174 |
zf.writestr(abspath, 'badfile') |
| 2175 |
zf.writestr(os.sep + abspath, 'badfile') |
| 2176 |
zf.writestr('/abspath', 'badfile') |
| 2177 |
zf.writestr('C:/abspath', 'badfile') |
| 2178 |
zf.writestr('D:\\abspath', 'badfile') |
| 2179 |
zf.writestr('E:abspath', 'badfile') |
| 2180 |
zf.writestr('F:/G:/abspath', 'badfile') |
| 2181 |
zf.writestr('//server/share/abspath', 'badfile') |
| 2182 |
zf.writestr('\\\\server2\\share\\abspath', 'badfile') |
| 2183 |
zf.writestr('../relpath', 'badfile') |
| 2184 |
zf.writestr(os.pardir + os.sep + 'relpath2', 'badfile') |
| 2185 |
zf.writestr('good/file', 'goodfile') |
| 2186 |
zf.writestr('good..file', 'goodfile') |
| 2187 |
|
| 2188 |
dstdir = os.path.join(self.mkdtemp(), 'dst') |
| 2189 |
unpack_archive(zipname, dstdir) |
| 2190 |
self.assertTrue(os.path.isfile(os.path.join(dstdir, 'good', 'file'))) |
| 2191 |
self.assertTrue(os.path.isfile(os.path.join(dstdir, 'good..file'))) |
| 2192 |
self.assertFalse(os.path.exists(abspath)) |
| 2193 |
self.assertFalse(os.path.exists(os.path.join(dstdir, 'abspath'))) |
| 2194 |
self.assertFalse(os.path.exists(os.path.join(dstdir, 'G_'))) |
| 2195 |
self.assertFalse(os.path.exists(os.path.join(dstdir, 'server'))) |
| 2196 |
if os.name != 'nt': |
| 2197 |
self.assertTrue(os.path.isfile(os.path.join(dstdir, 'C:', 'abspath'))) |
| 2198 |
self.assertTrue(os.path.isfile(os.path.join(dstdir, 'D:\\abspath'))) |
| 2199 |
self.assertTrue(os.path.isfile(os.path.join(dstdir, 'E:abspath'))) |
| 2200 |
self.assertTrue(os.path.isfile(os.path.join(dstdir, 'F:', 'G:', 'abspath'))) |
| 2201 |
self.assertTrue(os.path.isfile(os.path.join(dstdir, '\\\\server2\\share\\abspath'))) |
| 2202 |
if os.pardir == '..': |
| 2203 |
self.assertFalse(os.path.exists(os.path.join(dstdir, '..', 'relpath'))) |
| 2204 |
self.assertFalse(os.path.exists(os.path.join(dstdir, 'relpath'))) |
| 2205 |
else: |
| 2206 |
self.assertTrue(os.path.isfile(os.path.join(dstdir, '..', 'relpath'))) |
| 2207 |
self.assertFalse(os.path.exists(os.path.join(dstdir, os.pardir, 'relpath2'))) |
| 2208 |
self.assertFalse(os.path.exists(os.path.join(dstdir, 'relpath2'))) |
| 2209 |
|
| 2210 |
dstdir2 = os.path.join(self.mkdtemp(), 'dst') |
| 2211 |
os.mkdir(dstdir2) |
| 2212 |
with os_helper.change_cwd(dstdir2): |
| 2213 |
unpack_archive(zipname, '') |
| 2214 |
self.assertTrue(os.path.isfile(os.path.join('good', 'file'))) |
| 2215 |
self.assertTrue(os.path.isfile('good..file')) |
| 2216 |
self.assertFalse(os.path.exists(abspath)) |
| 2217 |
self.assertFalse(os.path.exists('abspath')) |
| 2218 |
self.assertFalse(os.path.exists('C_')) |
| 2219 |
self.assertFalse(os.path.exists('server')) |
| 2220 |
if os.name != 'nt': |
| 2221 |
self.assertTrue(os.path.isfile(os.path.join('C:', 'abspath'))) |
| 2222 |
self.assertTrue(os.path.isfile('D:\\abspath')) |
| 2223 |
self.assertTrue(os.path.isfile('E:abspath')) |
| 2224 |
self.assertTrue(os.path.isfile(os.path.join('F:', 'G:', 'abspath'))) |
| 2225 |
self.assertTrue(os.path.isfile('\\\\server2\\share\\abspath')) |
| 2226 |
if os.pardir == '..': |
| 2227 |
self.assertFalse(os.path.exists(os.path.join('..', 'relpath'))) |
| 2228 |
self.assertFalse(os.path.exists('relpath')) |
| 2229 |
else: |
| 2230 |
self.assertTrue(os.path.isfile(os.path.join('..', 'relpath'))) |
| 2231 |
self.assertFalse(os.path.exists(os.path.join(os.pardir, 'relpath2'))) |
| 2232 |
self.assertFalse(os.path.exists('relpath2')) |
| 2233 |
|
| 2234 |
def test_unpack_registry(self): |
| 2235 |
|
| 2236 |
formats = get_unpack_formats() |
| 2237 |
|
| 2238 |
def _boo(filename, extract_dir, extra): |
| 2239 |
self.assertEqual(extra, 1) |
| 2240 |
self.assertEqual(filename, 'stuff.boo') |
| 2241 |
self.assertEqual(extract_dir, 'xx') |
| 2242 |
|
| 2243 |
register_unpack_format('Boo', ['.boo', '.b2'], _boo, [('extra', 1)]) |
| 2244 |
unpack_archive('stuff.boo', 'xx') |
| 2245 |
|
| 2246 |
# trying to register a .boo unpacker again |
| 2247 |
self.assertRaises(RegistryError, register_unpack_format, 'Boo2', |
| 2248 |
['.boo'], _boo) |
| 2249 |
|
| 2250 |
# should work now |
| 2251 |
unregister_unpack_format('Boo') |
| 2252 |
register_unpack_format('Boo2', ['.boo'], _boo) |
| 2253 |
self.assertIn(('Boo2', ['.boo'], ''), get_unpack_formats()) |
| 2254 |
self.assertNotIn(('Boo', ['.boo'], ''), get_unpack_formats()) |
| 2255 |
|
| 2256 |
# let's leave a clean state |
| 2257 |
unregister_unpack_format('Boo2') |
| 2258 |
self.assertEqual(get_unpack_formats(), formats) |
| 2259 |
|
| 2260 |
|
| 2261 |
class TestMisc(BaseTest, unittest.TestCase): |
| 2262 |
|
| 2263 |
@unittest.skipUnless(hasattr(shutil, 'disk_usage'), |
| 2264 |
"disk_usage not available on this platform") |
| 2265 |
def test_disk_usage(self): |
| 2266 |
usage = shutil.disk_usage(os.path.dirname(__file__)) |
| 2267 |
for attr in ('total', 'used', 'free'): |
| 2268 |
self.assertIsInstance(getattr(usage, attr), int) |
| 2269 |
self.assertGreater(usage.total, 0) |
| 2270 |
self.assertGreater(usage.used, 0) |
| 2271 |
self.assertGreaterEqual(usage.free, 0) |
| 2272 |
self.assertGreaterEqual(usage.total, usage.used) |
| 2273 |
self.assertGreater(usage.total, usage.free) |
| 2274 |
|
| 2275 |
# bpo-32557: Check that disk_usage() also accepts a filename |
| 2276 |
shutil.disk_usage(__file__) |
| 2277 |
|
| 2278 |
@unittest.skipUnless(UID_GID_SUPPORT, "Requires grp and pwd support") |
| 2279 |
@unittest.skipUnless(hasattr(os, 'chown'), 'requires os.chown') |
| 2280 |
def test_chown(self): |
| 2281 |
dirname = self.mkdtemp() |
| 2282 |
filename = tempfile.mktemp(dir=dirname) |
| 2283 |
linkname = os.path.join(dirname, "chown_link") |
| 2284 |
create_file(filename, 'testing chown function') |
| 2285 |
os.symlink(filename, linkname) |
| 2286 |
|
| 2287 |
with self.assertRaises(ValueError): |
| 2288 |
shutil.chown(filename) |
| 2289 |
|
| 2290 |
with self.assertRaises(LookupError): |
| 2291 |
shutil.chown(filename, user='non-existing username') |
| 2292 |
|
| 2293 |
with self.assertRaises(LookupError): |
| 2294 |
shutil.chown(filename, group='non-existing groupname') |
| 2295 |
|
| 2296 |
with self.assertRaises(TypeError): |
| 2297 |
shutil.chown(filename, b'spam') |
| 2298 |
|
| 2299 |
with self.assertRaises(TypeError): |
| 2300 |
shutil.chown(filename, 3.14) |
| 2301 |
|
| 2302 |
uid = os.getuid() |
| 2303 |
gid = os.getgid() |
| 2304 |
|
| 2305 |
def check_chown(path, uid=None, gid=None): |
| 2306 |
s = os.stat(path) |
| 2307 |
if uid is not None: |
| 2308 |
self.assertEqual(uid, s.st_uid) |
| 2309 |
if gid is not None: |
| 2310 |
self.assertEqual(gid, s.st_gid) |
| 2311 |
|
| 2312 |
shutil.chown(filename, uid, gid) |
| 2313 |
check_chown(filename, uid, gid) |
| 2314 |
shutil.chown(filename, uid) |
| 2315 |
check_chown(filename, uid) |
| 2316 |
shutil.chown(filename, user=uid) |
| 2317 |
check_chown(filename, uid) |
| 2318 |
shutil.chown(filename, group=gid) |
| 2319 |
check_chown(filename, gid=gid) |
| 2320 |
|
| 2321 |
shutil.chown(dirname, uid, gid) |
| 2322 |
check_chown(dirname, uid, gid) |
| 2323 |
shutil.chown(dirname, uid) |
| 2324 |
check_chown(dirname, uid) |
| 2325 |
shutil.chown(dirname, user=uid) |
| 2326 |
check_chown(dirname, uid) |
| 2327 |
shutil.chown(dirname, group=gid) |
| 2328 |
check_chown(dirname, gid=gid) |
| 2329 |
|
| 2330 |
try: |
| 2331 |
user = pwd.getpwuid(uid)[0] |
| 2332 |
group = grp.getgrgid(gid)[0] |
| 2333 |
except KeyError: |
| 2334 |
# On some systems uid/gid cannot be resolved. |
| 2335 |
pass |
| 2336 |
else: |
| 2337 |
shutil.chown(filename, user, group) |
| 2338 |
check_chown(filename, uid, gid) |
| 2339 |
shutil.chown(dirname, user, group) |
| 2340 |
check_chown(dirname, uid, gid) |
| 2341 |
|
| 2342 |
dirfd = os.open(dirname, os.O_RDONLY) |
| 2343 |
self.addCleanup(os.close, dirfd) |
| 2344 |
basename = os.path.basename(filename) |
| 2345 |
baselinkname = os.path.basename(linkname) |
| 2346 |
shutil.chown(basename, uid, gid, dir_fd=dirfd) |
| 2347 |
check_chown(filename, uid, gid) |
| 2348 |
shutil.chown(basename, uid, dir_fd=dirfd) |
| 2349 |
check_chown(filename, uid) |
| 2350 |
shutil.chown(basename, group=gid, dir_fd=dirfd) |
| 2351 |
check_chown(filename, gid=gid) |
| 2352 |
shutil.chown(basename, uid, gid, dir_fd=dirfd, follow_symlinks=True) |
| 2353 |
check_chown(filename, uid, gid) |
| 2354 |
shutil.chown(basename, uid, gid, dir_fd=dirfd, follow_symlinks=False) |
| 2355 |
check_chown(filename, uid, gid) |
| 2356 |
shutil.chown(linkname, uid, follow_symlinks=True) |
| 2357 |
check_chown(filename, uid) |
| 2358 |
shutil.chown(baselinkname, group=gid, dir_fd=dirfd, follow_symlinks=False) |
| 2359 |
check_chown(filename, gid=gid) |
| 2360 |
shutil.chown(baselinkname, uid, gid, dir_fd=dirfd, follow_symlinks=True) |
| 2361 |
check_chown(filename, uid, gid) |
| 2362 |
|
| 2363 |
with self.assertRaises(TypeError): |
| 2364 |
shutil.chown(filename, uid, dir_fd=dirname) |
| 2365 |
|
| 2366 |
with self.assertRaises(FileNotFoundError): |
| 2367 |
shutil.chown('missingfile', uid, gid, dir_fd=dirfd) |
| 2368 |
|
| 2369 |
with self.assertRaises(ValueError): |
| 2370 |
shutil.chown(filename, dir_fd=dirfd) |
| 2371 |
|
| 2372 |
|
| 2373 |
@support.requires_subprocess() |
| 2374 |
class TestWhich(BaseTest, unittest.TestCase): |
| 2375 |
|
| 2376 |
def setUp(self): |
| 2377 |
temp_dir = self.mkdtemp(prefix="Tmp") |
| 2378 |
base_dir = os.path.join(temp_dir, TESTFN + '-basedir') |
| 2379 |
os.mkdir(base_dir) |
| 2380 |
self.dir = os.path.join(base_dir, TESTFN + '-dir') |
| 2381 |
os.mkdir(self.dir) |
| 2382 |
self.other_dir = os.path.join(base_dir, TESTFN + '-dir2') |
| 2383 |
os.mkdir(self.other_dir) |
| 2384 |
# Give the temp_file an ".exe" suffix for all. |
| 2385 |
# It's needed on Windows and not harmful on other platforms. |
| 2386 |
self.file = TESTFN + '.Exe' |
| 2387 |
self.filepath = os.path.join(self.dir, self.file) |
| 2388 |
self.create_file(self.filepath) |
| 2389 |
self.env_path = self.dir |
| 2390 |
self.curdir = os.curdir |
| 2391 |
self.ext = ".EXE" |
| 2392 |
|
| 2393 |
to_text_type = staticmethod(os.fsdecode) |
| 2394 |
|
| 2395 |
def create_file(self, path): |
| 2396 |
create_file(path) |
| 2397 |
os.chmod(path, 0o755) |
| 2398 |
|
| 2399 |
def assertNormEqual(self, actual, expected): |
| 2400 |
self.assertEqual(os.path.normcase(actual), os.path.normcase(expected)) |
| 2401 |
|
| 2402 |
def test_basic(self): |
| 2403 |
# Given an EXE in a directory, it should be returned. |
| 2404 |
rv = shutil.which(self.file, path=self.dir) |
| 2405 |
self.assertEqual(rv, self.filepath) |
| 2406 |
|
| 2407 |
def test_absolute_cmd(self): |
| 2408 |
# When given the fully qualified path to an executable that exists, |
| 2409 |
# it should be returned. |
| 2410 |
rv = shutil.which(self.filepath, path=self.other_dir) |
| 2411 |
self.assertEqual(rv, self.filepath) |
| 2412 |
|
| 2413 |
def test_relative_cmd(self): |
| 2414 |
# When given the relative path with a directory part to an executable |
| 2415 |
# that exists, it should be returned. |
| 2416 |
base_dir, tail_dir = os.path.split(self.dir) |
| 2417 |
relpath = os.path.join(tail_dir, self.file) |
| 2418 |
with os_helper.change_cwd(path=base_dir): |
| 2419 |
rv = shutil.which(relpath, path=self.other_dir) |
| 2420 |
self.assertEqual(rv, relpath) |
| 2421 |
# But it shouldn't be searched in PATH directories (issue #16957). |
| 2422 |
with os_helper.change_cwd(path=self.dir): |
| 2423 |
rv = shutil.which(relpath, path=base_dir) |
| 2424 |
self.assertIsNone(rv) |
| 2425 |
|
| 2426 |
@unittest.skipUnless(sys.platform != "win32", |
| 2427 |
"test is for non win32") |
| 2428 |
def test_cwd_non_win32(self): |
| 2429 |
# Issue #16957 |
| 2430 |
with os_helper.change_cwd(path=self.dir): |
| 2431 |
rv = shutil.which(self.file, path=self.other_dir) |
| 2432 |
# non-win32: shouldn't match in the current directory. |
| 2433 |
self.assertIsNone(rv) |
| 2434 |
|
| 2435 |
@unittest.skipUnless(sys.platform == "win32", |
| 2436 |
"test is for win32") |
| 2437 |
def test_cwd_win32(self): |
| 2438 |
base_dir = os.path.dirname(self.dir) |
| 2439 |
with os_helper.change_cwd(path=self.dir): |
| 2440 |
with unittest.mock.patch('shutil._win_path_needs_curdir', return_value=True): |
| 2441 |
rv = shutil.which(self.file, path=self.other_dir) |
| 2442 |
# Current directory implicitly on PATH |
| 2443 |
self.assertEqual(rv, os.path.join(self.curdir, self.file)) |
| 2444 |
with unittest.mock.patch('shutil._win_path_needs_curdir', return_value=False): |
| 2445 |
rv = shutil.which(self.file, path=self.other_dir) |
| 2446 |
# Current directory not on PATH |
| 2447 |
self.assertIsNone(rv) |
| 2448 |
|
| 2449 |
@unittest.skipUnless(sys.platform == "win32", |
| 2450 |
"test is for win32") |
| 2451 |
def test_cwd_win32_added_before_all_other_path(self): |
| 2452 |
other_file_path = os.path.join(self.other_dir, self.file) |
| 2453 |
self.create_file(other_file_path) |
| 2454 |
with unittest.mock.patch('shutil._win_path_needs_curdir', return_value=True): |
| 2455 |
with os_helper.change_cwd(path=self.dir): |
| 2456 |
rv = shutil.which(self.file, path=self.other_dir) |
| 2457 |
self.assertEqual(rv, os.path.join(self.curdir, self.file)) |
| 2458 |
with os_helper.change_cwd(path=self.other_dir): |
| 2459 |
rv = shutil.which(self.file, path=self.dir) |
| 2460 |
self.assertEqual(rv, os.path.join(self.curdir, self.file)) |
| 2461 |
|
| 2462 |
@os_helper.skip_if_dac_override |
| 2463 |
def test_non_matching_mode(self): |
| 2464 |
# Set the file read-only and ask for writeable files. |
| 2465 |
os.chmod(self.filepath, stat.S_IREAD) |
| 2466 |
if os.access(self.filepath, os.W_OK): |
| 2467 |
self.skipTest("can't set the file read-only") |
| 2468 |
rv = shutil.which(self.file, path=self.dir, mode=os.W_OK) |
| 2469 |
self.assertIsNone(rv) |
| 2470 |
|
| 2471 |
def test_relative_path(self): |
| 2472 |
base_dir, tail_dir = os.path.split(self.dir) |
| 2473 |
with os_helper.change_cwd(path=base_dir): |
| 2474 |
rv = shutil.which(self.file, path=tail_dir) |
| 2475 |
self.assertEqual(rv, os.path.join(tail_dir, self.file)) |
| 2476 |
|
| 2477 |
def test_nonexistent_file(self): |
| 2478 |
# Return None when no matching executable file is found on the path. |
| 2479 |
rv = shutil.which("foo.exe", path=self.dir) |
| 2480 |
self.assertIsNone(rv) |
| 2481 |
|
| 2482 |
@unittest.skipUnless(sys.platform == "win32", |
| 2483 |
"pathext check is Windows-only") |
| 2484 |
def test_pathext_checking(self): |
| 2485 |
# Ask for the file without the ".exe" extension, then ensure that |
| 2486 |
# it gets found properly with the extension. |
| 2487 |
rv = shutil.which(self.file[:-4], path=self.dir) |
| 2488 |
self.assertEqual(rv, self.filepath[:-4] + self.ext) |
| 2489 |
|
| 2490 |
def test_environ_path(self): |
| 2491 |
with os_helper.EnvironmentVarGuard() as env: |
| 2492 |
env['PATH'] = self.env_path |
| 2493 |
rv = shutil.which(self.file) |
| 2494 |
self.assertEqual(rv, self.filepath) |
| 2495 |
|
| 2496 |
def test_environ_path_empty(self): |
| 2497 |
# PATH='': no match |
| 2498 |
with os_helper.EnvironmentVarGuard() as env: |
| 2499 |
env['PATH'] = '' |
| 2500 |
with unittest.mock.patch('os.confstr', return_value=self.dir, \ |
| 2501 |
create=True), \ |
| 2502 |
support.swap_attr(os, 'defpath', self.dir), \ |
| 2503 |
os_helper.change_cwd(self.dir): |
| 2504 |
rv = shutil.which(self.file) |
| 2505 |
self.assertIsNone(rv) |
| 2506 |
|
| 2507 |
def test_environ_path_cwd(self): |
| 2508 |
expected_cwd = self.file |
| 2509 |
if sys.platform == "win32": |
| 2510 |
expected_cwd = os.path.join(self.curdir, expected_cwd) |
| 2511 |
|
| 2512 |
# PATH=':': explicitly looks in the current directory |
| 2513 |
with os_helper.EnvironmentVarGuard() as env: |
| 2514 |
env['PATH'] = os.pathsep |
| 2515 |
with unittest.mock.patch('os.confstr', return_value=self.dir, \ |
| 2516 |
create=True), \ |
| 2517 |
support.swap_attr(os, 'defpath', self.dir): |
| 2518 |
rv = shutil.which(self.file) |
| 2519 |
self.assertIsNone(rv) |
| 2520 |
|
| 2521 |
# look in current directory |
| 2522 |
with os_helper.change_cwd(self.dir): |
| 2523 |
rv = shutil.which(self.file) |
| 2524 |
self.assertEqual(rv, expected_cwd) |
| 2525 |
|
| 2526 |
def test_environ_path_missing(self): |
| 2527 |
with os_helper.EnvironmentVarGuard() as env: |
| 2528 |
del env['PATH'] |
| 2529 |
|
| 2530 |
# without confstr |
| 2531 |
with unittest.mock.patch('os.confstr', side_effect=ValueError, \ |
| 2532 |
create=True), \ |
| 2533 |
support.swap_attr(os, 'defpath', self.dir): |
| 2534 |
rv = shutil.which(self.file) |
| 2535 |
self.assertEqual(rv, self.filepath) |
| 2536 |
|
| 2537 |
# with confstr |
| 2538 |
with unittest.mock.patch('os.confstr', return_value=self.dir, \ |
| 2539 |
create=True), \ |
| 2540 |
support.swap_attr(os, 'defpath', ''): |
| 2541 |
rv = shutil.which(self.file) |
| 2542 |
self.assertEqual(rv, self.filepath) |
| 2543 |
|
| 2544 |
def test_empty_path(self): |
| 2545 |
base_dir = os.path.dirname(self.dir) |
| 2546 |
with os_helper.change_cwd(path=self.dir), \ |
| 2547 |
os_helper.EnvironmentVarGuard() as env: |
| 2548 |
env['PATH'] = self.env_path |
| 2549 |
rv = shutil.which(self.file, path='') |
| 2550 |
self.assertIsNone(rv) |
| 2551 |
|
| 2552 |
def test_empty_path_no_PATH(self): |
| 2553 |
with os_helper.EnvironmentVarGuard() as env: |
| 2554 |
del env['PATH'] |
| 2555 |
rv = shutil.which(self.file) |
| 2556 |
self.assertIsNone(rv) |
| 2557 |
|
| 2558 |
@unittest.skipUnless(sys.platform == "win32", 'test specific to Windows') |
| 2559 |
def test_pathext(self): |
| 2560 |
ext = '.xyz' |
| 2561 |
cmd = self.to_text_type(TESTFN2) |
| 2562 |
cmdext = cmd + self.to_text_type(ext) |
| 2563 |
filepath = os.path.join(self.dir, cmdext) |
| 2564 |
self.create_file(filepath) |
| 2565 |
with os_helper.EnvironmentVarGuard() as env: |
| 2566 |
env['PATHEXT'] = ext |
| 2567 |
self.assertEqual(shutil.which(cmd, path=self.dir), filepath) |
| 2568 |
self.assertEqual(shutil.which(cmdext, path=self.dir), filepath) |
| 2569 |
|
| 2570 |
# Issue 40592: See https://bugs.python.org/issue40592 |
| 2571 |
@unittest.skipUnless(sys.platform == "win32", 'test specific to Windows') |
| 2572 |
def test_pathext_with_empty_str(self): |
| 2573 |
ext = '.xyz' |
| 2574 |
cmd = self.to_text_type(TESTFN2) |
| 2575 |
cmdext = cmd + self.to_text_type(ext) |
| 2576 |
filepath = os.path.join(self.dir, cmdext) |
| 2577 |
self.create_file(filepath) |
| 2578 |
with os_helper.EnvironmentVarGuard() as env: |
| 2579 |
env['PATHEXT'] = ext + ';' # note the ; |
| 2580 |
self.assertEqual(shutil.which(cmd, path=self.dir), filepath) |
| 2581 |
self.assertEqual(shutil.which(cmdext, path=self.dir), filepath) |
| 2582 |
|
| 2583 |
@unittest.skipUnless(sys.platform == "win32", 'test specific to Windows') |
| 2584 |
def test_pathext_with_multidot_extension(self): |
| 2585 |
ext = '.foo.bar' |
| 2586 |
cmd = self.to_text_type(TESTFN2) |
| 2587 |
cmdext = cmd + self.to_text_type(ext) |
| 2588 |
filepath = os.path.join(self.dir, cmdext) |
| 2589 |
self.create_file(filepath) |
| 2590 |
with os_helper.EnvironmentVarGuard() as env: |
| 2591 |
env['PATHEXT'] = ext |
| 2592 |
self.assertEqual(shutil.which(cmd, path=self.dir), filepath) |
| 2593 |
self.assertEqual(shutil.which(cmdext, path=self.dir), filepath) |
| 2594 |
|
| 2595 |
@unittest.skipUnless(sys.platform == "win32", 'test specific to Windows') |
| 2596 |
def test_pathext_with_null_extension(self): |
| 2597 |
cmd = self.to_text_type(TESTFN2) |
| 2598 |
cmddot = cmd + self.to_text_type('.') |
| 2599 |
filepath = os.path.join(self.dir, cmd) |
| 2600 |
self.create_file(filepath) |
| 2601 |
with os_helper.EnvironmentVarGuard() as env: |
| 2602 |
env['PATHEXT'] = '.xyz' |
| 2603 |
self.assertIsNone(shutil.which(cmd, path=self.dir)) |
| 2604 |
self.assertIsNone(shutil.which(cmddot, path=self.dir)) |
| 2605 |
env['PATHEXT'] = '.xyz;.' # note the . |
| 2606 |
self.assertEqual(shutil.which(cmd, path=self.dir), filepath) |
| 2607 |
self.assertEqual(shutil.which(cmddot, path=self.dir), |
| 2608 |
filepath + self.to_text_type('.')) |
| 2609 |
env['PATHEXT'] = '.xyz;..' # multiple dots |
| 2610 |
self.assertEqual(shutil.which(cmd, path=self.dir), filepath) |
| 2611 |
self.assertEqual(shutil.which(cmddot, path=self.dir), |
| 2612 |
filepath + self.to_text_type('.')) |
| 2613 |
|
| 2614 |
@unittest.skipUnless(sys.platform == "win32", 'test specific to Windows') |
| 2615 |
def test_pathext_extension_ends_with_dot(self): |
| 2616 |
ext = '.xyz' |
| 2617 |
cmd = self.to_text_type(TESTFN2) |
| 2618 |
cmdext = cmd + self.to_text_type(ext) |
| 2619 |
dot = self.to_text_type('.') |
| 2620 |
filepath = os.path.join(self.dir, cmdext) |
| 2621 |
self.create_file(filepath) |
| 2622 |
with os_helper.EnvironmentVarGuard() as env: |
| 2623 |
env['PATHEXT'] = ext + '.' |
| 2624 |
self.assertEqual(shutil.which(cmd, path=self.dir), filepath) # cmd.exe hangs here |
| 2625 |
self.assertEqual(shutil.which(cmdext, path=self.dir), filepath) |
| 2626 |
self.assertIsNone(shutil.which(cmd + dot, path=self.dir)) |
| 2627 |
self.assertIsNone(shutil.which(cmdext + dot, path=self.dir)) |
| 2628 |
|
| 2629 |
# See GH-75586 |
| 2630 |
@unittest.skipUnless(sys.platform == "win32", 'test specific to Windows') |
| 2631 |
def test_pathext_applied_on_files_in_path(self): |
| 2632 |
ext = '.xyz' |
| 2633 |
cmd = self.to_text_type(TESTFN2) |
| 2634 |
cmdext = cmd + self.to_text_type(ext) |
| 2635 |
filepath = os.path.join(self.dir, cmdext) |
| 2636 |
self.create_file(filepath) |
| 2637 |
with os_helper.EnvironmentVarGuard() as env: |
| 2638 |
env["PATH"] = os.fsdecode(self.dir) |
| 2639 |
env["PATHEXT"] = ext |
| 2640 |
self.assertEqual(shutil.which(cmd), filepath) |
| 2641 |
self.assertEqual(shutil.which(cmdext), filepath) |
| 2642 |
|
| 2643 |
# See GH-75586 |
| 2644 |
@unittest.skipUnless(sys.platform == "win32", 'test specific to Windows') |
| 2645 |
def test_win_path_needs_curdir(self): |
| 2646 |
with unittest.mock.patch('_winapi.NeedCurrentDirectoryForExePath', return_value=True) as need_curdir_mock: |
| 2647 |
self.assertTrue(shutil._win_path_needs_curdir('dontcare', os.X_OK)) |
| 2648 |
need_curdir_mock.assert_called_once_with('dontcare') |
| 2649 |
need_curdir_mock.reset_mock() |
| 2650 |
self.assertTrue(shutil._win_path_needs_curdir('dontcare', 0)) |
| 2651 |
need_curdir_mock.assert_not_called() |
| 2652 |
|
| 2653 |
with unittest.mock.patch('_winapi.NeedCurrentDirectoryForExePath', return_value=False) as need_curdir_mock: |
| 2654 |
self.assertFalse(shutil._win_path_needs_curdir('dontcare', os.X_OK)) |
| 2655 |
need_curdir_mock.assert_called_once_with('dontcare') |
| 2656 |
|
| 2657 |
@unittest.skipUnless(sys.platform == "win32", 'test specific to Windows') |
| 2658 |
def test_same_dir_with_pathext_extension(self): |
| 2659 |
cmd = self.file # with .exe extension |
| 2660 |
# full match |
| 2661 |
self.assertNormEqual(shutil.which(cmd, path=self.dir), self.filepath) |
| 2662 |
self.assertNormEqual(shutil.which(cmd, path=self.dir, mode=os.F_OK), |
| 2663 |
self.filepath) |
| 2664 |
|
| 2665 |
cmd2 = cmd + self.to_text_type('.com') # with .exe.com extension |
| 2666 |
other_file_path = os.path.join(self.dir, cmd2) |
| 2667 |
self.create_file(other_file_path) |
| 2668 |
|
| 2669 |
# full match |
| 2670 |
self.assertNormEqual(shutil.which(cmd, path=self.dir), self.filepath) |
| 2671 |
self.assertNormEqual(shutil.which(cmd, path=self.dir, mode=os.F_OK), |
| 2672 |
self.filepath) |
| 2673 |
self.assertNormEqual(shutil.which(cmd2, path=self.dir), other_file_path) |
| 2674 |
self.assertNormEqual(shutil.which(cmd2, path=self.dir, mode=os.F_OK), |
| 2675 |
other_file_path) |
| 2676 |
|
| 2677 |
@unittest.skipUnless(sys.platform == "win32", 'test specific to Windows') |
| 2678 |
def test_same_dir_without_pathext_extension(self): |
| 2679 |
cmd = self.file[:-4] # without .exe extension |
| 2680 |
# pathext match |
| 2681 |
self.assertNormEqual(shutil.which(cmd, path=self.dir), self.filepath) |
| 2682 |
self.assertNormEqual(shutil.which(cmd, path=self.dir, mode=os.F_OK), |
| 2683 |
self.filepath) |
| 2684 |
|
| 2685 |
# without extension |
| 2686 |
other_file_path = os.path.join(self.dir, cmd) |
| 2687 |
self.create_file(other_file_path) |
| 2688 |
|
| 2689 |
# pathext match if mode contains X_OK |
| 2690 |
self.assertNormEqual(shutil.which(cmd, path=self.dir), self.filepath) |
| 2691 |
# full match |
| 2692 |
self.assertNormEqual(shutil.which(cmd, path=self.dir, mode=os.F_OK), |
| 2693 |
other_file_path) |
| 2694 |
self.assertNormEqual(shutil.which(self.file, path=self.dir), self.filepath) |
| 2695 |
self.assertNormEqual(shutil.which(self.file, path=self.dir, mode=os.F_OK), |
| 2696 |
self.filepath) |
| 2697 |
|
| 2698 |
@unittest.skipUnless(sys.platform == "win32", 'test specific to Windows') |
| 2699 |
def test_dir_order_with_pathext_extension(self): |
| 2700 |
cmd = self.file # with .exe extension |
| 2701 |
search_path = os.pathsep.join([os.fsdecode(self.other_dir), |
| 2702 |
os.fsdecode(self.dir)]) |
| 2703 |
# full match in the second directory |
| 2704 |
self.assertNormEqual(shutil.which(cmd, path=search_path), self.filepath) |
| 2705 |
self.assertNormEqual(shutil.which(cmd, path=search_path, mode=os.F_OK), |
| 2706 |
self.filepath) |
| 2707 |
|
| 2708 |
cmd2 = cmd + self.to_text_type('.com') # with .exe.com extension |
| 2709 |
other_file_path = os.path.join(self.other_dir, cmd2) |
| 2710 |
self.create_file(other_file_path) |
| 2711 |
|
| 2712 |
# pathext match in the first directory |
| 2713 |
self.assertNormEqual(shutil.which(cmd, path=search_path), other_file_path) |
| 2714 |
self.assertNormEqual(shutil.which(cmd, path=search_path, mode=os.F_OK), |
| 2715 |
other_file_path) |
| 2716 |
# full match in the first directory |
| 2717 |
self.assertNormEqual(shutil.which(cmd2, path=search_path), other_file_path) |
| 2718 |
self.assertNormEqual(shutil.which(cmd2, path=search_path, mode=os.F_OK), |
| 2719 |
other_file_path) |
| 2720 |
|
| 2721 |
# full match in the first directory |
| 2722 |
search_path = os.pathsep.join([os.fsdecode(self.dir), |
| 2723 |
os.fsdecode(self.other_dir)]) |
| 2724 |
self.assertEqual(shutil.which(cmd, path=search_path), self.filepath) |
| 2725 |
self.assertEqual(shutil.which(cmd, path=search_path, mode=os.F_OK), |
| 2726 |
self.filepath) |
| 2727 |
|
| 2728 |
@unittest.skipUnless(sys.platform == "win32", 'test specific to Windows') |
| 2729 |
def test_dir_order_without_pathext_extension(self): |
| 2730 |
cmd = self.file[:-4] # without .exe extension |
| 2731 |
search_path = os.pathsep.join([os.fsdecode(self.other_dir), |
| 2732 |
os.fsdecode(self.dir)]) |
| 2733 |
# pathext match in the second directory |
| 2734 |
self.assertNormEqual(shutil.which(cmd, path=search_path), self.filepath) |
| 2735 |
self.assertNormEqual(shutil.which(cmd, path=search_path, mode=os.F_OK), |
| 2736 |
self.filepath) |
| 2737 |
|
| 2738 |
# without extension |
| 2739 |
other_file_path = os.path.join(self.other_dir, cmd) |
| 2740 |
self.create_file(other_file_path) |
| 2741 |
|
| 2742 |
# pathext match in the second directory |
| 2743 |
self.assertNormEqual(shutil.which(cmd, path=search_path), self.filepath) |
| 2744 |
# full match in the first directory |
| 2745 |
self.assertNormEqual(shutil.which(cmd, path=search_path, mode=os.F_OK), |
| 2746 |
other_file_path) |
| 2747 |
# full match in the second directory |
| 2748 |
self.assertNormEqual(shutil.which(self.file, path=search_path), self.filepath) |
| 2749 |
self.assertNormEqual(shutil.which(self.file, path=search_path, mode=os.F_OK), |
| 2750 |
self.filepath) |
| 2751 |
|
| 2752 |
# pathext match in the first directory |
| 2753 |
search_path = os.pathsep.join([os.fsdecode(self.dir), |
| 2754 |
os.fsdecode(self.other_dir)]) |
| 2755 |
self.assertNormEqual(shutil.which(cmd, path=search_path), self.filepath) |
| 2756 |
self.assertNormEqual(shutil.which(cmd, path=search_path, mode=os.F_OK), |
| 2757 |
self.filepath) |
| 2758 |
|
| 2759 |
|
| 2760 |
class TestWhichBytes(TestWhich): |
| 2761 |
def setUp(self): |
| 2762 |
TestWhich.setUp(self) |
| 2763 |
self.dir = os.fsencode(self.dir) |
| 2764 |
self.file = os.fsencode(self.file) |
| 2765 |
self.filepath = os.fsencode(self.filepath) |
| 2766 |
self.other_dir = os.fsencode(self.other_dir) |
| 2767 |
self.curdir = os.fsencode(self.curdir) |
| 2768 |
self.ext = os.fsencode(self.ext) |
| 2769 |
|
| 2770 |
to_text_type = staticmethod(os.fsencode) |
| 2771 |
|
| 2772 |
|
| 2773 |
class TestMove(BaseTest, unittest.TestCase): |
| 2774 |
|
| 2775 |
def setUp(self): |
| 2776 |
filename = "foo" |
| 2777 |
self.src_dir = self.mkdtemp() |
| 2778 |
self.dst_dir = self.mkdtemp() |
| 2779 |
self.src_file = os.path.join(self.src_dir, filename) |
| 2780 |
self.dst_file = os.path.join(self.dst_dir, filename) |
| 2781 |
create_file(self.src_file, b"spam") |
| 2782 |
|
| 2783 |
def _check_move_file(self, src, dst, real_dst): |
| 2784 |
with open(src, "rb") as f: |
| 2785 |
contents = f.read() |
| 2786 |
shutil.move(src, dst) |
| 2787 |
with open(real_dst, "rb") as f: |
| 2788 |
self.assertEqual(contents, f.read()) |
| 2789 |
self.assertFalse(os.path.exists(src)) |
| 2790 |
|
| 2791 |
def _check_move_dir(self, src, dst, real_dst): |
| 2792 |
contents = sorted(os.listdir(src)) |
| 2793 |
shutil.move(src, dst) |
| 2794 |
self.assertEqual(contents, sorted(os.listdir(real_dst))) |
| 2795 |
self.assertFalse(os.path.exists(src)) |
| 2796 |
|
| 2797 |
def test_move_file(self): |
| 2798 |
# Move a file to another location on the same filesystem. |
| 2799 |
self._check_move_file(self.src_file, self.dst_file, self.dst_file) |
| 2800 |
|
| 2801 |
def test_move_file_to_dir(self): |
| 2802 |
# Move a file inside an existing dir on the same filesystem. |
| 2803 |
self._check_move_file(self.src_file, self.dst_dir, self.dst_file) |
| 2804 |
|
| 2805 |
def test_move_file_to_dir_pathlike_src(self): |
| 2806 |
# Move a pathlike file to another location on the same filesystem. |
| 2807 |
src = FakePath(self.src_file) |
| 2808 |
self._check_move_file(src, self.dst_dir, self.dst_file) |
| 2809 |
|
| 2810 |
def test_move_file_to_dir_pathlike_dst(self): |
| 2811 |
# Move a file to another pathlike location on the same filesystem. |
| 2812 |
dst = FakePath(self.dst_dir) |
| 2813 |
self._check_move_file(self.src_file, dst, self.dst_file) |
| 2814 |
|
| 2815 |
@mock_rename |
| 2816 |
def test_move_file_other_fs(self): |
| 2817 |
# Move a file to an existing dir on another filesystem. |
| 2818 |
self.test_move_file() |
| 2819 |
|
| 2820 |
@mock_rename |
| 2821 |
def test_move_file_to_dir_other_fs(self): |
| 2822 |
# Move a file to another location on another filesystem. |
| 2823 |
self.test_move_file_to_dir() |
| 2824 |
|
| 2825 |
def test_move_dir(self): |
| 2826 |
# Move a dir to another location on the same filesystem. |
| 2827 |
dst_dir = tempfile.mktemp(dir=self.mkdtemp()) |
| 2828 |
try: |
| 2829 |
self._check_move_dir(self.src_dir, dst_dir, dst_dir) |
| 2830 |
finally: |
| 2831 |
os_helper.rmtree(dst_dir) |
| 2832 |
|
| 2833 |
@mock_rename |
| 2834 |
def test_move_dir_other_fs(self): |
| 2835 |
# Move a dir to another location on another filesystem. |
| 2836 |
self.test_move_dir() |
| 2837 |
|
| 2838 |
def test_move_dir_to_dir(self): |
| 2839 |
# Move a dir inside an existing dir on the same filesystem. |
| 2840 |
self._check_move_dir(self.src_dir, self.dst_dir, |
| 2841 |
os.path.join(self.dst_dir, os.path.basename(self.src_dir))) |
| 2842 |
|
| 2843 |
@mock_rename |
| 2844 |
def test_move_dir_to_dir_other_fs(self): |
| 2845 |
# Move a dir inside an existing dir on another filesystem. |
| 2846 |
self.test_move_dir_to_dir() |
| 2847 |
|
| 2848 |
def test_move_dir_sep_to_dir(self): |
| 2849 |
self._check_move_dir(self.src_dir + os.path.sep, self.dst_dir, |
| 2850 |
os.path.join(self.dst_dir, os.path.basename(self.src_dir))) |
| 2851 |
|
| 2852 |
@unittest.skipUnless(os.path.altsep, 'requires os.path.altsep') |
| 2853 |
def test_move_dir_altsep_to_dir(self): |
| 2854 |
self._check_move_dir(self.src_dir + os.path.altsep, self.dst_dir, |
| 2855 |
os.path.join(self.dst_dir, os.path.basename(self.src_dir))) |
| 2856 |
|
| 2857 |
def test_existing_file_inside_dest_dir(self): |
| 2858 |
# A file with the same name inside the destination dir already exists. |
| 2859 |
create_file(self.dst_file) |
| 2860 |
self.assertRaises(shutil.Error, shutil.move, self.src_file, self.dst_dir) |
| 2861 |
|
| 2862 |
def test_dont_move_dir_in_itself(self): |
| 2863 |
# Moving a dir inside itself raises an Error. |
| 2864 |
dst = os.path.join(self.src_dir, "bar") |
| 2865 |
self.assertRaises(shutil.Error, shutil.move, self.src_dir, dst) |
| 2866 |
|
| 2867 |
def test_destinsrc_false_negative(self): |
| 2868 |
os.mkdir(TESTFN) |
| 2869 |
try: |
| 2870 |
for src, dst in [('srcdir', 'srcdir/dest')]: |
| 2871 |
src = os.path.join(TESTFN, src) |
| 2872 |
dst = os.path.join(TESTFN, dst) |
| 2873 |
self.assertTrue(shutil._destinsrc(src, dst), |
| 2874 |
msg='_destinsrc() wrongly concluded that ' |
| 2875 |
'dst (%s) is not in src (%s)' % (dst, src)) |
| 2876 |
finally: |
| 2877 |
os_helper.rmtree(TESTFN) |
| 2878 |
|
| 2879 |
def test_destinsrc_false_positive(self): |
| 2880 |
os.mkdir(TESTFN) |
| 2881 |
try: |
| 2882 |
for src, dst in [('srcdir', 'src/dest'), ('srcdir', 'srcdir.new')]: |
| 2883 |
src = os.path.join(TESTFN, src) |
| 2884 |
dst = os.path.join(TESTFN, dst) |
| 2885 |
self.assertFalse(shutil._destinsrc(src, dst), |
| 2886 |
msg='_destinsrc() wrongly concluded that ' |
| 2887 |
'dst (%s) is in src (%s)' % (dst, src)) |
| 2888 |
finally: |
| 2889 |
os_helper.rmtree(TESTFN) |
| 2890 |
|
| 2891 |
@os_helper.skip_unless_symlink |
| 2892 |
@mock_rename |
| 2893 |
def test_move_file_symlink(self): |
| 2894 |
dst = os.path.join(self.src_dir, 'bar') |
| 2895 |
os.symlink(self.src_file, dst) |
| 2896 |
shutil.move(dst, self.dst_file) |
| 2897 |
self.assertTrue(os.path.islink(self.dst_file)) |
| 2898 |
self.assertTrue(os.path.samefile(self.src_file, self.dst_file)) |
| 2899 |
|
| 2900 |
@os_helper.skip_unless_symlink |
| 2901 |
@mock_rename |
| 2902 |
def test_move_file_symlink_to_dir(self): |
| 2903 |
filename = "bar" |
| 2904 |
dst = os.path.join(self.src_dir, filename) |
| 2905 |
os.symlink(self.src_file, dst) |
| 2906 |
shutil.move(dst, self.dst_dir) |
| 2907 |
final_link = os.path.join(self.dst_dir, filename) |
| 2908 |
self.assertTrue(os.path.islink(final_link)) |
| 2909 |
self.assertTrue(os.path.samefile(self.src_file, final_link)) |
| 2910 |
|
| 2911 |
@os_helper.skip_unless_symlink |
| 2912 |
@mock_rename |
| 2913 |
def test_move_dangling_symlink(self): |
| 2914 |
src = os.path.join(self.src_dir, 'baz') |
| 2915 |
dst = os.path.join(self.src_dir, 'bar') |
| 2916 |
os.symlink(src, dst) |
| 2917 |
dst_link = os.path.join(self.dst_dir, 'quux') |
| 2918 |
shutil.move(dst, dst_link) |
| 2919 |
self.assertTrue(os.path.islink(dst_link)) |
| 2920 |
self.assertEqual(os.path.realpath(src), os.path.realpath(dst_link)) |
| 2921 |
|
| 2922 |
@os_helper.skip_unless_symlink |
| 2923 |
@mock_rename |
| 2924 |
def test_move_dir_symlink(self): |
| 2925 |
src = os.path.join(self.src_dir, 'baz') |
| 2926 |
dst = os.path.join(self.src_dir, 'bar') |
| 2927 |
os.mkdir(src) |
| 2928 |
os.symlink(src, dst) |
| 2929 |
dst_link = os.path.join(self.dst_dir, 'quux') |
| 2930 |
shutil.move(dst, dst_link) |
| 2931 |
self.assertTrue(os.path.islink(dst_link)) |
| 2932 |
self.assertTrue(os.path.samefile(src, dst_link)) |
| 2933 |
|
| 2934 |
def test_move_return_value(self): |
| 2935 |
rv = shutil.move(self.src_file, self.dst_dir) |
| 2936 |
self.assertEqual(rv, |
| 2937 |
os.path.join(self.dst_dir, os.path.basename(self.src_file))) |
| 2938 |
|
| 2939 |
def test_move_as_rename_return_value(self): |
| 2940 |
rv = shutil.move(self.src_file, os.path.join(self.dst_dir, 'bar')) |
| 2941 |
self.assertEqual(rv, os.path.join(self.dst_dir, 'bar')) |
| 2942 |
|
| 2943 |
@mock_rename |
| 2944 |
def test_move_file_special_function(self): |
| 2945 |
moved = [] |
| 2946 |
def _copy(src, dst): |
| 2947 |
moved.append((src, dst)) |
| 2948 |
shutil.move(self.src_file, self.dst_dir, copy_function=_copy) |
| 2949 |
self.assertEqual(len(moved), 1) |
| 2950 |
|
| 2951 |
@mock_rename |
| 2952 |
def test_move_dir_special_function(self): |
| 2953 |
moved = [] |
| 2954 |
def _copy(src, dst): |
| 2955 |
moved.append((src, dst)) |
| 2956 |
os_helper.create_empty_file(os.path.join(self.src_dir, 'child')) |
| 2957 |
os_helper.create_empty_file(os.path.join(self.src_dir, 'child1')) |
| 2958 |
shutil.move(self.src_dir, self.dst_dir, copy_function=_copy) |
| 2959 |
self.assertEqual(len(moved), 3) |
| 2960 |
|
| 2961 |
def test_move_dir_caseinsensitive(self): |
| 2962 |
# Renames a folder to the same name |
| 2963 |
# but a different case. |
| 2964 |
|
| 2965 |
self.src_dir = self.mkdtemp() |
| 2966 |
dst_dir = os.path.join( |
| 2967 |
os.path.dirname(self.src_dir), |
| 2968 |
os.path.basename(self.src_dir).upper()) |
| 2969 |
self.assertNotEqual(self.src_dir, dst_dir) |
| 2970 |
|
| 2971 |
try: |
| 2972 |
shutil.move(self.src_dir, dst_dir) |
| 2973 |
self.assertTrue(os.path.isdir(dst_dir)) |
| 2974 |
finally: |
| 2975 |
os.rmdir(dst_dir) |
| 2976 |
|
| 2977 |
# bpo-26791: Check that a symlink to a directory can |
| 2978 |
# be moved into that directory. |
| 2979 |
@mock_rename |
| 2980 |
def _test_move_symlink_to_dir_into_dir(self, dst): |
| 2981 |
src = os.path.join(self.src_dir, 'linktodir') |
| 2982 |
dst_link = os.path.join(self.dst_dir, 'linktodir') |
| 2983 |
os.symlink(self.dst_dir, src, target_is_directory=True) |
| 2984 |
shutil.move(src, dst) |
| 2985 |
self.assertTrue(os.path.islink(dst_link)) |
| 2986 |
self.assertTrue(os.path.samefile(self.dst_dir, dst_link)) |
| 2987 |
self.assertFalse(os.path.exists(src)) |
| 2988 |
|
| 2989 |
# Repeat the move operation with the destination |
| 2990 |
# symlink already in place (should raise shutil.Error). |
| 2991 |
os.symlink(self.dst_dir, src, target_is_directory=True) |
| 2992 |
with self.assertRaises(shutil.Error): |
| 2993 |
shutil.move(src, dst) |
| 2994 |
self.assertTrue(os.path.samefile(self.dst_dir, dst_link)) |
| 2995 |
self.assertTrue(os.path.exists(src)) |
| 2996 |
|
| 2997 |
@os_helper.skip_unless_symlink |
| 2998 |
def test_move_symlink_to_dir_into_dir(self): |
| 2999 |
self._test_move_symlink_to_dir_into_dir(self.dst_dir) |
| 3000 |
|
| 3001 |
@os_helper.skip_unless_symlink |
| 3002 |
def test_move_symlink_to_dir_into_symlink_to_dir(self): |
| 3003 |
dst = os.path.join(self.src_dir, 'otherlinktodir') |
| 3004 |
os.symlink(self.dst_dir, dst, target_is_directory=True) |
| 3005 |
self._test_move_symlink_to_dir_into_dir(dst) |
| 3006 |
|
| 3007 |
@os_helper.skip_unless_dac_override |
| 3008 |
@unittest.skipUnless(hasattr(os, 'lchflags') |
| 3009 |
and hasattr(stat, 'SF_IMMUTABLE') |
| 3010 |
and hasattr(stat, 'UF_OPAQUE'), |
| 3011 |
'requires lchflags') |
| 3012 |
def test_move_dir_permission_denied(self): |
| 3013 |
# bpo-42782: shutil.move should not create destination directories |
| 3014 |
# if the source directory cannot be removed. |
| 3015 |
try: |
| 3016 |
os.mkdir(TESTFN_SRC) |
| 3017 |
os.lchflags(TESTFN_SRC, stat.SF_IMMUTABLE) |
| 3018 |
|
| 3019 |
# Testing on an empty immutable directory |
| 3020 |
# TESTFN_DST should not exist if shutil.move failed |
| 3021 |
self.assertRaises(PermissionError, shutil.move, TESTFN_SRC, TESTFN_DST) |
| 3022 |
self.assertFalse(TESTFN_DST in os.listdir()) |
| 3023 |
|
| 3024 |
# Create a file and keep the directory immutable |
| 3025 |
os.lchflags(TESTFN_SRC, stat.UF_OPAQUE) |
| 3026 |
os_helper.create_empty_file(os.path.join(TESTFN_SRC, 'child')) |
| 3027 |
os.lchflags(TESTFN_SRC, stat.SF_IMMUTABLE) |
| 3028 |
|
| 3029 |
# Testing on a non-empty immutable directory |
| 3030 |
# TESTFN_DST should not exist if shutil.move failed |
| 3031 |
self.assertRaises(PermissionError, shutil.move, TESTFN_SRC, TESTFN_DST) |
| 3032 |
self.assertFalse(TESTFN_DST in os.listdir()) |
| 3033 |
finally: |
| 3034 |
if os.path.exists(TESTFN_SRC): |
| 3035 |
os.lchflags(TESTFN_SRC, stat.UF_OPAQUE) |
| 3036 |
os_helper.rmtree(TESTFN_SRC) |
| 3037 |
if os.path.exists(TESTFN_DST): |
| 3038 |
os.lchflags(TESTFN_DST, stat.UF_OPAQUE) |
| 3039 |
os_helper.rmtree(TESTFN_DST) |
| 3040 |
|
| 3041 |
|
| 3042 |
class TestCopyFile(unittest.TestCase): |
| 3043 |
|
| 3044 |
class Faux(object): |
| 3045 |
_entered = False |
| 3046 |
_exited_with = None |
| 3047 |
_raised = False |
| 3048 |
def __init__(self, raise_in_exit=False, suppress_at_exit=True): |
| 3049 |
self._raise_in_exit = raise_in_exit |
| 3050 |
self._suppress_at_exit = suppress_at_exit |
| 3051 |
def read(self, *args): |
| 3052 |
return '' |
| 3053 |
def __enter__(self): |
| 3054 |
self._entered = True |
| 3055 |
def __exit__(self, exc_type, exc_val, exc_tb): |
| 3056 |
self._exited_with = exc_type, exc_val, exc_tb |
| 3057 |
if self._raise_in_exit: |
| 3058 |
self._raised = True |
| 3059 |
raise OSError("Cannot close") |
| 3060 |
return self._suppress_at_exit |
| 3061 |
|
| 3062 |
def test_w_source_open_fails(self): |
| 3063 |
def _open(filename, mode='r'): |
| 3064 |
if filename == 'srcfile': |
| 3065 |
raise OSError('Cannot open "srcfile"') |
| 3066 |
assert 0 # shouldn't reach here. |
| 3067 |
|
| 3068 |
with support.swap_attr(shutil, 'open', _open): |
| 3069 |
with self.assertRaises(OSError): |
| 3070 |
shutil.copyfile('srcfile', 'destfile') |
| 3071 |
|
| 3072 |
@unittest.skipIf(MACOS, "skipped on macOS") |
| 3073 |
def test_w_dest_open_fails(self): |
| 3074 |
srcfile = self.Faux() |
| 3075 |
|
| 3076 |
def _open(filename, mode='r'): |
| 3077 |
if filename == 'srcfile': |
| 3078 |
return srcfile |
| 3079 |
if filename == 'destfile': |
| 3080 |
raise OSError('Cannot open "destfile"') |
| 3081 |
assert 0 # shouldn't reach here. |
| 3082 |
|
| 3083 |
with support.swap_attr(shutil, 'open', _open): |
| 3084 |
shutil.copyfile('srcfile', 'destfile') |
| 3085 |
self.assertTrue(srcfile._entered) |
| 3086 |
self.assertTrue(srcfile._exited_with[0] is OSError) |
| 3087 |
self.assertEqual(srcfile._exited_with[1].args, |
| 3088 |
('Cannot open "destfile"',)) |
| 3089 |
|
| 3090 |
@unittest.skipIf(MACOS, "skipped on macOS") |
| 3091 |
def test_w_dest_close_fails(self): |
| 3092 |
srcfile = self.Faux() |
| 3093 |
destfile = self.Faux(True) |
| 3094 |
|
| 3095 |
def _open(filename, mode='r'): |
| 3096 |
if filename == 'srcfile': |
| 3097 |
return srcfile |
| 3098 |
if filename == 'destfile': |
| 3099 |
return destfile |
| 3100 |
assert 0 # shouldn't reach here. |
| 3101 |
|
| 3102 |
with support.swap_attr(shutil, 'open', _open): |
| 3103 |
shutil.copyfile('srcfile', 'destfile') |
| 3104 |
self.assertTrue(srcfile._entered) |
| 3105 |
self.assertTrue(destfile._entered) |
| 3106 |
self.assertTrue(destfile._raised) |
| 3107 |
self.assertTrue(srcfile._exited_with[0] is OSError) |
| 3108 |
self.assertEqual(srcfile._exited_with[1].args, |
| 3109 |
('Cannot close',)) |
| 3110 |
|
| 3111 |
@unittest.skipIf(MACOS, "skipped on macOS") |
| 3112 |
def test_w_source_close_fails(self): |
| 3113 |
|
| 3114 |
srcfile = self.Faux(True) |
| 3115 |
destfile = self.Faux() |
| 3116 |
|
| 3117 |
def _open(filename, mode='r'): |
| 3118 |
if filename == 'srcfile': |
| 3119 |
return srcfile |
| 3120 |
if filename == 'destfile': |
| 3121 |
return destfile |
| 3122 |
assert 0 # shouldn't reach here. |
| 3123 |
|
| 3124 |
with support.swap_attr(shutil, 'open', _open): |
| 3125 |
with self.assertRaises(OSError): |
| 3126 |
shutil.copyfile('srcfile', 'destfile') |
| 3127 |
self.assertTrue(srcfile._entered) |
| 3128 |
self.assertTrue(destfile._entered) |
| 3129 |
self.assertFalse(destfile._raised) |
| 3130 |
self.assertTrue(srcfile._exited_with[0] is None) |
| 3131 |
self.assertTrue(srcfile._raised) |
| 3132 |
|
| 3133 |
|
| 3134 |
class TestCopyFileObj(unittest.TestCase): |
| 3135 |
FILESIZE = 2 * 1024 * 1024 |
| 3136 |
|
| 3137 |
@classmethod |
| 3138 |
def setUpClass(cls): |
| 3139 |
write_test_file(TESTFN, cls.FILESIZE) |
| 3140 |
|
| 3141 |
@classmethod |
| 3142 |
def tearDownClass(cls): |
| 3143 |
os_helper.unlink(TESTFN) |
| 3144 |
os_helper.unlink(TESTFN2) |
| 3145 |
|
| 3146 |
def tearDown(self): |
| 3147 |
os_helper.unlink(TESTFN2) |
| 3148 |
|
| 3149 |
@contextlib.contextmanager |
| 3150 |
def get_files(self): |
| 3151 |
with open(TESTFN, "rb") as src: |
| 3152 |
with open(TESTFN2, "wb") as dst: |
| 3153 |
yield (src, dst) |
| 3154 |
|
| 3155 |
def assert_files_eq(self, src, dst): |
| 3156 |
with open(src, 'rb') as fsrc: |
| 3157 |
with open(dst, 'rb') as fdst: |
| 3158 |
self.assertEqual(fsrc.read(), fdst.read()) |
| 3159 |
|
| 3160 |
def test_content(self): |
| 3161 |
with self.get_files() as (src, dst): |
| 3162 |
shutil.copyfileobj(src, dst) |
| 3163 |
self.assert_files_eq(TESTFN, TESTFN2) |
| 3164 |
|
| 3165 |
def test_file_not_closed(self): |
| 3166 |
with self.get_files() as (src, dst): |
| 3167 |
shutil.copyfileobj(src, dst) |
| 3168 |
assert not src.closed |
| 3169 |
assert not dst.closed |
| 3170 |
|
| 3171 |
def test_file_offset(self): |
| 3172 |
with self.get_files() as (src, dst): |
| 3173 |
shutil.copyfileobj(src, dst) |
| 3174 |
self.assertEqual(src.tell(), self.FILESIZE) |
| 3175 |
self.assertEqual(dst.tell(), self.FILESIZE) |
| 3176 |
|
| 3177 |
@unittest.skipIf(os.name != 'nt', "Windows only") |
| 3178 |
def test_win_impl(self): |
| 3179 |
# Make sure alternate Windows implementation is called. |
| 3180 |
with unittest.mock.patch("shutil._copyfileobj_readinto") as m: |
| 3181 |
shutil.copyfile(TESTFN, TESTFN2) |
| 3182 |
assert m.called |
| 3183 |
|
| 3184 |
# File size is 2 MiB but max buf size should be 1 MiB. |
| 3185 |
self.assertEqual(m.call_args[0][2], 1 * 1024 * 1024) |
| 3186 |
|
| 3187 |
# If file size < 1 MiB memoryview() length must be equal to |
| 3188 |
# the actual file size. |
| 3189 |
with tempfile.NamedTemporaryFile(dir=os.getcwd(), delete=False) as f: |
| 3190 |
f.write(b'foo') |
| 3191 |
fname = f.name |
| 3192 |
self.addCleanup(os_helper.unlink, fname) |
| 3193 |
with unittest.mock.patch("shutil._copyfileobj_readinto") as m: |
| 3194 |
shutil.copyfile(fname, TESTFN2) |
| 3195 |
self.assertEqual(m.call_args[0][2], 3) |
| 3196 |
|
| 3197 |
# Empty files should not rely on readinto() variant. |
| 3198 |
with tempfile.NamedTemporaryFile(dir=os.getcwd(), delete=False) as f: |
| 3199 |
pass |
| 3200 |
fname = f.name |
| 3201 |
self.addCleanup(os_helper.unlink, fname) |
| 3202 |
with unittest.mock.patch("shutil._copyfileobj_readinto") as m: |
| 3203 |
shutil.copyfile(fname, TESTFN2) |
| 3204 |
assert not m.called |
| 3205 |
self.assert_files_eq(fname, TESTFN2) |
| 3206 |
|
| 3207 |
|
| 3208 |
class _ZeroCopyFileTest(object): |
| 3209 |
"""Tests common to all zero-copy APIs.""" |
| 3210 |
FILESIZE = (10 * 1024 * 1024) # 10 MiB |
| 3211 |
FILEDATA = b"" |
| 3212 |
PATCHPOINT = "" |
| 3213 |
|
| 3214 |
@classmethod |
| 3215 |
def setUpClass(cls): |
| 3216 |
write_test_file(TESTFN, cls.FILESIZE) |
| 3217 |
with open(TESTFN, 'rb') as f: |
| 3218 |
cls.FILEDATA = f.read() |
| 3219 |
assert len(cls.FILEDATA) == cls.FILESIZE |
| 3220 |
|
| 3221 |
@classmethod |
| 3222 |
def tearDownClass(cls): |
| 3223 |
os_helper.unlink(TESTFN) |
| 3224 |
|
| 3225 |
def tearDown(self): |
| 3226 |
os_helper.unlink(TESTFN2) |
| 3227 |
|
| 3228 |
@contextlib.contextmanager |
| 3229 |
def get_files(self): |
| 3230 |
with open(TESTFN, "rb") as src: |
| 3231 |
with open(TESTFN2, "wb") as dst: |
| 3232 |
yield (src, dst) |
| 3233 |
|
| 3234 |
def zerocopy_fun(self, *args, **kwargs): |
| 3235 |
raise NotImplementedError("must be implemented in subclass") |
| 3236 |
|
| 3237 |
def reset(self): |
| 3238 |
self.tearDown() |
| 3239 |
self.tearDownClass() |
| 3240 |
self.setUpClass() |
| 3241 |
self.setUp() |
| 3242 |
|
| 3243 |
# --- |
| 3244 |
|
| 3245 |
def test_regular_copy(self): |
| 3246 |
with self.get_files() as (src, dst): |
| 3247 |
self.zerocopy_fun(src, dst) |
| 3248 |
self.assertEqual(read_file(TESTFN2, binary=True), self.FILEDATA) |
| 3249 |
# Make sure the fallback function is not called. |
| 3250 |
with self.get_files() as (src, dst): |
| 3251 |
with unittest.mock.patch('shutil.copyfileobj') as m: |
| 3252 |
shutil.copyfile(TESTFN, TESTFN2) |
| 3253 |
assert not m.called |
| 3254 |
|
| 3255 |
def test_same_file(self): |
| 3256 |
self.addCleanup(self.reset) |
| 3257 |
with self.get_files() as (src, dst): |
| 3258 |
with self.assertRaises((OSError, _GiveupOnFastCopy)): |
| 3259 |
self.zerocopy_fun(src, src) |
| 3260 |
# Make sure src file is not corrupted. |
| 3261 |
self.assertEqual(read_file(TESTFN, binary=True), self.FILEDATA) |
| 3262 |
|
| 3263 |
def test_non_existent_src(self): |
| 3264 |
name = tempfile.mktemp(dir=os.getcwd()) |
| 3265 |
with self.assertRaises(FileNotFoundError) as cm: |
| 3266 |
shutil.copyfile(name, "new") |
| 3267 |
self.assertEqual(cm.exception.filename, name) |
| 3268 |
|
| 3269 |
def test_empty_file(self): |
| 3270 |
srcname = TESTFN + 'src' |
| 3271 |
dstname = TESTFN + 'dst' |
| 3272 |
self.addCleanup(lambda: os_helper.unlink(srcname)) |
| 3273 |
self.addCleanup(lambda: os_helper.unlink(dstname)) |
| 3274 |
create_file(srcname) |
| 3275 |
|
| 3276 |
with open(srcname, "rb") as src: |
| 3277 |
with open(dstname, "wb") as dst: |
| 3278 |
self.zerocopy_fun(src, dst) |
| 3279 |
|
| 3280 |
self.assertEqual(read_file(dstname, binary=True), b"") |
| 3281 |
|
| 3282 |
def test_unhandled_exception(self): |
| 3283 |
with unittest.mock.patch(self.PATCHPOINT, |
| 3284 |
side_effect=ZeroDivisionError): |
| 3285 |
self.assertRaises(ZeroDivisionError, |
| 3286 |
shutil.copyfile, TESTFN, TESTFN2) |
| 3287 |
|
| 3288 |
def test_exception_on_first_call(self): |
| 3289 |
# Emulate a case where the first call to the zero-copy |
| 3290 |
# function raises an exception in which case the function is |
| 3291 |
# supposed to give up immediately. |
| 3292 |
with unittest.mock.patch(self.PATCHPOINT, |
| 3293 |
side_effect=OSError(errno.EINVAL, "yo")): |
| 3294 |
with self.get_files() as (src, dst): |
| 3295 |
with self.assertRaises(_GiveupOnFastCopy): |
| 3296 |
self.zerocopy_fun(src, dst) |
| 3297 |
|
| 3298 |
def test_filesystem_full(self): |
| 3299 |
# Emulate a case where filesystem is full and sendfile() fails |
| 3300 |
# on first call. |
| 3301 |
with unittest.mock.patch(self.PATCHPOINT, |
| 3302 |
side_effect=OSError(errno.ENOSPC, "yo")): |
| 3303 |
with self.get_files() as (src, dst): |
| 3304 |
self.assertRaises(OSError, self.zerocopy_fun, src, dst) |
| 3305 |
|
| 3306 |
|
| 3307 |
class _ZeroCopyFileLinuxTest(_ZeroCopyFileTest): |
| 3308 |
BLOCKSIZE_INDEX = None |
| 3309 |
|
| 3310 |
def test_non_regular_file_src(self): |
| 3311 |
with io.BytesIO(self.FILEDATA) as src: |
| 3312 |
with open(TESTFN2, "wb") as dst: |
| 3313 |
with self.assertRaises(_GiveupOnFastCopy): |
| 3314 |
self.zerocopy_fun(src, dst) |
| 3315 |
shutil.copyfileobj(src, dst) |
| 3316 |
|
| 3317 |
self.assertEqual(read_file(TESTFN2, binary=True), self.FILEDATA) |
| 3318 |
|
| 3319 |
def test_non_regular_file_dst(self): |
| 3320 |
with open(TESTFN, "rb") as src: |
| 3321 |
with io.BytesIO() as dst: |
| 3322 |
with self.assertRaises(_GiveupOnFastCopy): |
| 3323 |
self.zerocopy_fun(src, dst) |
| 3324 |
shutil.copyfileobj(src, dst) |
| 3325 |
dst.seek(0) |
| 3326 |
self.assertEqual(dst.read(), self.FILEDATA) |
| 3327 |
|
| 3328 |
def test_exception_on_second_call(self): |
| 3329 |
def syscall(*args, **kwargs): |
| 3330 |
if not flag: |
| 3331 |
flag.append(None) |
| 3332 |
return orig_syscall(*args, **kwargs) |
| 3333 |
else: |
| 3334 |
raise OSError(errno.EBADF, "yo") |
| 3335 |
|
| 3336 |
flag = [] |
| 3337 |
orig_syscall = eval(self.PATCHPOINT) |
| 3338 |
with unittest.mock.patch(self.PATCHPOINT, create=True, |
| 3339 |
side_effect=syscall): |
| 3340 |
with self.get_files() as (src, dst): |
| 3341 |
with self.assertRaises(OSError) as cm: |
| 3342 |
self.zerocopy_fun(src, dst) |
| 3343 |
assert flag |
| 3344 |
self.assertEqual(cm.exception.errno, errno.EBADF) |
| 3345 |
|
| 3346 |
def test_cant_get_size(self): |
| 3347 |
# Emulate a case where src file size cannot be determined. |
| 3348 |
# Internally bufsize will be set to a small value and |
| 3349 |
# a system call will be called repeatedly. |
| 3350 |
with unittest.mock.patch('os.fstat', side_effect=OSError) as m: |
| 3351 |
with self.get_files() as (src, dst): |
| 3352 |
self.zerocopy_fun(src, dst) |
| 3353 |
assert m.called |
| 3354 |
self.assertEqual(read_file(TESTFN2, binary=True), self.FILEDATA) |
| 3355 |
|
| 3356 |
def test_small_chunks(self): |
| 3357 |
# Force internal file size detection to be smaller than the |
| 3358 |
# actual file size. We want to force a system call to be called |
| 3359 |
# multiple times, also in order to emulate a src fd which gets |
| 3360 |
# bigger while it is being copied. |
| 3361 |
mock = unittest.mock.Mock() |
| 3362 |
mock.st_size = 65536 + 1 |
| 3363 |
with unittest.mock.patch('os.fstat', return_value=mock) as m: |
| 3364 |
with self.get_files() as (src, dst): |
| 3365 |
self.zerocopy_fun(src, dst) |
| 3366 |
assert m.called |
| 3367 |
self.assertEqual(read_file(TESTFN2, binary=True), self.FILEDATA) |
| 3368 |
|
| 3369 |
def test_big_chunk(self): |
| 3370 |
# Force internal file size detection to be +100MB bigger than |
| 3371 |
# the actual file size. Make sure a system call does not rely on |
| 3372 |
# file size value except for (maybe) a better throughput / |
| 3373 |
# performance. |
| 3374 |
mock = unittest.mock.Mock() |
| 3375 |
mock.st_size = self.FILESIZE + (100 * 1024 * 1024) |
| 3376 |
with unittest.mock.patch('os.fstat', return_value=mock) as m: |
| 3377 |
with self.get_files() as (src, dst): |
| 3378 |
self.zerocopy_fun(src, dst) |
| 3379 |
assert m.called |
| 3380 |
self.assertEqual(read_file(TESTFN2, binary=True), self.FILEDATA) |
| 3381 |
|
| 3382 |
def test_blocksize_arg(self): |
| 3383 |
with unittest.mock.patch(self.PATCHPOINT, |
| 3384 |
side_effect=ZeroDivisionError) as m: |
| 3385 |
self.assertRaises(ZeroDivisionError, |
| 3386 |
shutil.copyfile, TESTFN, TESTFN2) |
| 3387 |
blocksize = m.call_args[0][self.BLOCKSIZE_INDEX] |
| 3388 |
# Make sure file size and the block size arg passed to |
| 3389 |
# sendfile() are the same. |
| 3390 |
self.assertEqual(blocksize, os.path.getsize(TESTFN)) |
| 3391 |
# ...unless we're dealing with a small file. |
| 3392 |
os_helper.unlink(TESTFN2) |
| 3393 |
create_file(TESTFN2, b"hello") |
| 3394 |
self.addCleanup(os_helper.unlink, TESTFN2 + '3') |
| 3395 |
self.assertRaises(ZeroDivisionError, |
| 3396 |
shutil.copyfile, TESTFN2, TESTFN2 + '3') |
| 3397 |
blocksize = m.call_args[0][self.BLOCKSIZE_INDEX] |
| 3398 |
self.assertEqual(blocksize, 2 ** 23) |
| 3399 |
|
| 3400 |
|
| 3401 |
@unittest.skipIf(not SUPPORTS_SENDFILE, 'os.sendfile() not supported') |
| 3402 |
@unittest.mock.patch.object(shutil, "_USE_CP_COPY_FILE_RANGE", False) |
| 3403 |
class TestZeroCopySendfile(_ZeroCopyFileLinuxTest, unittest.TestCase): |
| 3404 |
PATCHPOINT = "os.sendfile" |
| 3405 |
BLOCKSIZE_INDEX = 3 |
| 3406 |
|
| 3407 |
def zerocopy_fun(self, fsrc, fdst): |
| 3408 |
return shutil._fastcopy_sendfile(fsrc, fdst) |
| 3409 |
|
| 3410 |
def test_file2file_not_supported(self): |
| 3411 |
# Emulate a case where sendfile() only support file->socket |
| 3412 |
# fds. In such a case copyfile() is supposed to skip the |
| 3413 |
# fast-copy attempt from then on. |
| 3414 |
assert shutil._USE_CP_SENDFILE |
| 3415 |
try: |
| 3416 |
with unittest.mock.patch( |
| 3417 |
self.PATCHPOINT, |
| 3418 |
side_effect=OSError(errno.ENOTSOCK, "yo")) as m: |
| 3419 |
with self.get_files() as (src, dst): |
| 3420 |
with self.assertRaises(_GiveupOnFastCopy): |
| 3421 |
shutil._fastcopy_sendfile(src, dst) |
| 3422 |
assert m.called |
| 3423 |
assert not shutil._USE_CP_SENDFILE |
| 3424 |
|
| 3425 |
with unittest.mock.patch(self.PATCHPOINT) as m: |
| 3426 |
shutil.copyfile(TESTFN, TESTFN2) |
| 3427 |
assert not m.called |
| 3428 |
finally: |
| 3429 |
shutil._USE_CP_SENDFILE = True |
| 3430 |
|
| 3431 |
|
| 3432 |
@unittest.skipUnless(shutil._USE_CP_COPY_FILE_RANGE, "os.copy_file_range() not supported") |
| 3433 |
class TestZeroCopyCopyFileRange(_ZeroCopyFileLinuxTest, unittest.TestCase): |
| 3434 |
PATCHPOINT = "os.copy_file_range" |
| 3435 |
BLOCKSIZE_INDEX = 2 |
| 3436 |
|
| 3437 |
def zerocopy_fun(self, fsrc, fdst): |
| 3438 |
return shutil._fastcopy_copy_file_range(fsrc, fdst) |
| 3439 |
|
| 3440 |
def test_empty_file(self): |
| 3441 |
srcname = f"{TESTFN}src" |
| 3442 |
dstname = f"{TESTFN}dst" |
| 3443 |
self.addCleanup(lambda: os_helper.unlink(srcname)) |
| 3444 |
self.addCleanup(lambda: os_helper.unlink(dstname)) |
| 3445 |
with open(srcname, "wb"): |
| 3446 |
pass |
| 3447 |
|
| 3448 |
with open(srcname, "rb") as src, open(dstname, "wb") as dst: |
| 3449 |
# _fastcopy_copy_file_range gives up copying empty files due |
| 3450 |
# to a bug in older Linux. |
| 3451 |
with self.assertRaises(shutil._GiveupOnFastCopy): |
| 3452 |
self.zerocopy_fun(src, dst) |
| 3453 |
|
| 3454 |
|
| 3455 |
@unittest.skipIf(not MACOS, 'macOS only') |
| 3456 |
class TestZeroCopyMACOS(_ZeroCopyFileTest, unittest.TestCase): |
| 3457 |
PATCHPOINT = "posix._fcopyfile" |
| 3458 |
|
| 3459 |
def zerocopy_fun(self, src, dst): |
| 3460 |
return shutil._fastcopy_fcopyfile(src, dst, posix._COPYFILE_DATA) |
| 3461 |
|
| 3462 |
|
| 3463 |
class TestGetTerminalSize(unittest.TestCase): |
| 3464 |
def test_does_not_crash(self): |
| 3465 |
"""Check if get_terminal_size() returns a meaningful value. |
| 3466 |
|
| 3467 |
There's no easy portable way to actually check the size of the |
| 3468 |
terminal, so let's check if it returns something sensible instead. |
| 3469 |
""" |
| 3470 |
size = shutil.get_terminal_size() |
| 3471 |
self.assertGreaterEqual(size.columns, 0) |
| 3472 |
self.assertGreaterEqual(size.lines, 0) |
| 3473 |
|
| 3474 |
def test_os_environ_first(self): |
| 3475 |
"Check if environment variables have precedence" |
| 3476 |
|
| 3477 |
with os_helper.EnvironmentVarGuard() as env: |
| 3478 |
env['COLUMNS'] = '777' |
| 3479 |
del env['LINES'] |
| 3480 |
size = shutil.get_terminal_size() |
| 3481 |
self.assertEqual(size.columns, 777) |
| 3482 |
|
| 3483 |
with os_helper.EnvironmentVarGuard() as env: |
| 3484 |
del env['COLUMNS'] |
| 3485 |
env['LINES'] = '888' |
| 3486 |
size = shutil.get_terminal_size() |
| 3487 |
self.assertEqual(size.lines, 888) |
| 3488 |
|
| 3489 |
def test_bad_environ(self): |
| 3490 |
with os_helper.EnvironmentVarGuard() as env: |
| 3491 |
env['COLUMNS'] = 'xxx' |
| 3492 |
env['LINES'] = 'yyy' |
| 3493 |
size = shutil.get_terminal_size() |
| 3494 |
self.assertGreaterEqual(size.columns, 0) |
| 3495 |
self.assertGreaterEqual(size.lines, 0) |
| 3496 |
|
| 3497 |
@unittest.skipUnless(os.isatty(sys.__stdout__.fileno()), "not on tty") |
| 3498 |
@support.requires_subprocess() |
| 3499 |
@unittest.skipUnless(hasattr(os, 'get_terminal_size'), |
| 3500 |
'need os.get_terminal_size()') |
| 3501 |
def test_stty_match(self): |
| 3502 |
"""Check if stty returns the same results ignoring env |
| 3503 |
|
| 3504 |
This test will fail if stdin and stdout are connected to |
| 3505 |
different terminals with different sizes. Nevertheless, such |
| 3506 |
situations should be pretty rare. |
| 3507 |
""" |
| 3508 |
try: |
| 3509 |
size = subprocess.check_output(['stty', 'size']).decode().split() |
| 3510 |
except (FileNotFoundError, PermissionError, |
| 3511 |
subprocess.CalledProcessError): |
| 3512 |
self.skipTest("stty invocation failed") |
| 3513 |
expected = (int(size[1]), int(size[0])) # reversed order |
| 3514 |
|
| 3515 |
with os_helper.EnvironmentVarGuard() as env: |
| 3516 |
env.unset('LINES', 'COLUMNS') |
| 3517 |
actual = shutil.get_terminal_size() |
| 3518 |
|
| 3519 |
self.assertEqual(expected, actual) |
| 3520 |
|
| 3521 |
@unittest.skipIf(support.is_wasi, "WASI has no /dev/null") |
| 3522 |
def test_fallback(self): |
| 3523 |
with os_helper.EnvironmentVarGuard() as env: |
| 3524 |
env.unset('LINES', 'COLUMNS') |
| 3525 |
|
| 3526 |
# sys.__stdout__ has no fileno() |
| 3527 |
with support.swap_attr(sys, '__stdout__', None): |
| 3528 |
size = shutil.get_terminal_size(fallback=(10, 20)) |
| 3529 |
self.assertEqual(size.columns, 10) |
| 3530 |
self.assertEqual(size.lines, 20) |
| 3531 |
|
| 3532 |
# sys.__stdout__ is not a terminal on Unix |
| 3533 |
# or fileno() not in (0, 1, 2) on Windows |
| 3534 |
with open(os.devnull, 'w', encoding='utf-8') as f, \ |
| 3535 |
support.swap_attr(sys, '__stdout__', f): |
| 3536 |
size = shutil.get_terminal_size(fallback=(30, 40)) |
| 3537 |
self.assertEqual(size.columns, 30) |
| 3538 |
self.assertEqual(size.lines, 40) |
| 3539 |
|
| 3540 |
|
| 3541 |
class PublicAPITests(unittest.TestCase): |
| 3542 |
"""Ensures that the correct values are exposed in the public API.""" |
| 3543 |
|
| 3544 |
def test_module_all_attribute(self): |
| 3545 |
self.assertHasAttr(shutil, '__all__') |
| 3546 |
target_api = ['copyfileobj', 'copyfile', 'copymode', 'copystat', |
| 3547 |
'copy', 'copy2', 'copytree', 'move', 'rmtree', 'Error', |
| 3548 |
'SpecialFileError', 'make_archive', |
| 3549 |
'get_archive_formats', 'register_archive_format', |
| 3550 |
'unregister_archive_format', 'get_unpack_formats', |
| 3551 |
'register_unpack_format', 'unregister_unpack_format', |
| 3552 |
'unpack_archive', 'ignore_patterns', 'chown', 'which', |
| 3553 |
'get_terminal_size', 'SameFileError'] |
| 3554 |
if hasattr(os, 'statvfs') or os.name == 'nt': |
| 3555 |
target_api.append('disk_usage') |
| 3556 |
self.assertEqual(set(shutil.__all__), set(target_api)) |
| 3557 |
with self.assertWarns(DeprecationWarning): |
| 3558 |
from shutil import ExecError |
| 3559 |
|
| 3560 |
|
| 3561 |
if __name__ == '__main__': |
| 3562 |
unittest.main() |