def test_copytree_copies_unreadable_files(self): src = self.get_test_loc('fileutils/exec', copy=True) dst = self.get_temp_dir() src_file1 = join(src, 'a.bat') src_file2 = join(src, 'subtxt', 'a.txt') try: # make some unreadable source files make_non_readable(src_file1) if on_posix: assert not filetype.is_readable(src_file1) make_non_readable(src_file2) if on_posix: assert not filetype.is_readable(src_file2) # copy proper dest_dir = join(dst, 'dest') fileutils.copytree(src, dest_dir) dest_file1 = join(dest_dir, 'a.bat') assert os.path.exists(dest_file1) assert filetype.is_readable(dest_file1) dest_file2 = join(dest_dir, 'subtxt', 'a.txt') assert os.path.exists(dest_file2) assert filetype.is_readable(dest_file2) finally: fileutils.chmod(src, fileutils.RW, recurse=True) fileutils.chmod(dst, fileutils.RW, recurse=True)
def copytree(src, dst): """ Copy recursively the `src` directory to the `dst` directory. If `dst` is an existing directory, files in `dst` may be overwritten during the copy. Preserve timestamps. Ignores: -`src` permissions: `dst` files are created with the default permissions. - all special files such as FIFO or character devices and symlinks. Raise an shutil.Error with a list of reasons. This function is similar to and derived from the Python shutil.copytree function. See fileutils.py.ABOUT for details. """ if on_linux: src = path_to_bytes(src) dst = path_to_bytes(dst) if not filetype.is_readable(src): chmod(src, R, recurse=False) names = os.listdir(src) if not os.path.exists(dst): os.makedirs(dst) errors = [] errors.extend(copytime(src, dst)) for name in names: srcname = os.path.join(src, name) dstname = os.path.join(dst, name) # skip anything that is not a regular file, dir or link if not filetype.is_regular(srcname): continue if not filetype.is_readable(srcname): chmod(srcname, R, recurse=False) try: if os.path.isdir(srcname): copytree(srcname, dstname) elif filetype.is_file(srcname): copyfile(srcname, dstname) # catch the Error from the recursive copytree so that we can # continue with other files except shutil.Error, err: errors.extend(err.args[0]) except EnvironmentError, why: errors.append((srcname, dstname, str(why)))
def check_files(test_dir, expected): """ Walk test_dir. Check that all dirs are readable. Check that all files are: * non-special, * readable, * have a posix path that ends with one of the expected tuple paths. """ result = [] locs = [] if filetype.is_file(test_dir): test_dir = fileutils.parent_directory(test_dir) test_dir_path = fileutils.as_posixpath(test_dir) for top, _, files in os.walk(test_dir): for f in files: location = os.path.join(top, f) locs.append(location) path = fileutils.as_posixpath(location) path = path.replace(test_dir_path, '').strip('/') result.append(path) assert sorted(expected) == sorted(result) for location in locs: assert filetype.is_file(location) assert not filetype.is_special(location) assert filetype.is_readable(location)
def test_chmod_read_write_recursively_on_dir(self): test_dir = self.get_test_loc('fileutils/executable', copy=True) test_file = join(test_dir, 'deep1', 'deep2', 'ctags') test_dir2 = join(test_dir, 'deep1', 'deep2') parent = join(test_dir, 'deep1') try: make_non_writable(test_file) assert not filetype.is_writable(test_file) if on_posix: make_non_executable(test_file) assert not filetype.is_executable(test_file) if on_posix: make_non_executable(test_dir2) assert not filetype.is_executable(test_dir2) make_non_writable(test_dir) if on_posix: assert not filetype.is_writable(test_dir2) fileutils.chmod(parent, fileutils.RW, recurse=True) assert filetype.is_readable(test_dir2) is True assert filetype.is_writable(test_dir2) if on_posix: assert filetype.is_executable(test_dir2) finally: fileutils.chmod(test_dir, fileutils.RW, recurse=True)
def test_copyfile_does_not_keep_permissions(self): src_file = self.get_temp_file() dest = self.get_temp_dir() with open(src_file, 'wb') as f: f.write('') try: make_non_readable(src_file) if on_posix: assert not filetype.is_readable(src_file) fileutils.copyfile(src_file, dest) dest_file = join(dest, os.listdir(dest)[0]) assert filetype.is_readable(dest_file) finally: fileutils.chmod(src_file, fileutils.RW, recurse=True) fileutils.chmod(dest, fileutils.RW, recurse=True)
def test_is_readable_is_writeable_file(self): base_dir = self.get_test_loc('filetype/readwrite', copy=True) test_file = os.path.join(os.path.join(base_dir, 'sub'), 'file') try: assert filetype.is_readable(test_file) assert filetype.is_writable(test_file) make_non_readable(test_file) if on_posix: assert not filetype.is_readable(test_file) make_non_writable(test_file) assert not filetype.is_writable(test_file) finally: fileutils.chmod(base_dir, fileutils.RW, recurse=True)
def test_chmod_read_write_file(self): test_dir = self.get_test_loc('fileutils/executable', copy=True) test_file = join(test_dir, 'deep1', 'deep2', 'ctags') try: make_non_writable(test_file) assert not filetype.is_writable(test_file) fileutils.chmod(test_file, fileutils.RW) assert filetype.is_readable(test_file) assert filetype.is_writable(test_file) finally: fileutils.chmod(test_dir, fileutils.RW, recurse=True)
def test_is_readable_is_writeable_dir(self): base_dir = self.get_test_loc('filetype/readwrite', copy=True) test_dir = os.path.join(base_dir, 'sub') try: assert filetype.is_readable(test_dir) assert filetype.is_writable(test_dir) make_non_readable(test_dir) if on_posix: assert not filetype.is_readable(test_dir) else: # dirs are always RW on windows assert filetype.is_readable(test_dir) make_non_writable(test_dir) if on_posix: assert not filetype.is_writable(test_dir) else: # dirs are always RW on windows assert filetype.is_writable(test_dir) # finally finally: fileutils.chmod(base_dir, fileutils.RW, recurse=True)
def copyfile(src, dst): """ Copy src file to dst file preserving timestamps. Ignore permissions and special files. Similar to and derived from Python shutil module. See fileutils.py.ABOUT for details. """ if not filetype.is_regular(src): return if not filetype.is_readable(src): chmod(src, R, recurse=False) if os.path.isdir(dst): dst = os.path.join(dst, os.path.basename(src)) shutil.copyfile(src, dst) copytime(src, dst)
def test_chmod_read_write_exec_dir(self): test_dir = self.get_test_loc('fileutils/executable', copy=True) test_file = join(test_dir, 'deep1', 'deep2', 'ctags') try: if on_posix: make_non_executable(test_dir) assert not filetype.is_executable(test_file) make_non_writable(test_dir) fileutils.chmod(test_dir, fileutils.RWX, recurse=True) assert filetype.is_readable(test_file) assert filetype.is_writable(test_file) if on_posix: assert filetype.is_executable(test_file) finally: fileutils.chmod(test_dir, fileutils.RW, recurse=True)
def test_chmod_read_write_exec_dir(self): test_dir = self.get_test_loc("fileutils/executable", copy=True) test_file = join(test_dir, "deep1", "deep2", "ctags") try: if on_posix: make_non_executable(test_dir) assert not filetype.is_executable(test_file) make_non_writable(test_dir) fileutils.chmod(test_dir, fileutils.RWX, recurse=True) assert filetype.is_readable(test_file) assert filetype.is_writable(test_file) if on_posix: assert filetype.is_executable(test_file) finally: fileutils.chmod(test_dir, fileutils.RW, recurse=True)
def test_is_rwx_with_none(self): assert not filetype.is_writable(None) assert not filetype.is_readable(None) assert not filetype.is_executable(None)