Commit 55eb11f3 by Jörg Stucke

added safe rglob

parent fc40851b
from .fail_safe_file_operations import get_binary_from_file, get_string_list_from_file, write_binary_to_file, get_safe_name, delete_file, get_files_in_dir, get_dirs_in_dir, create_symlink, get_dir_of_file
from .fail_safe_file_operations import (
get_binary_from_file, get_string_list_from_file, write_binary_to_file, get_safe_name, delete_file, get_files_in_dir,
get_dirs_in_dir, create_symlink, get_dir_of_file, safe_rglob
)
from .file_functions import read_in_chunks, get_directory_for_filename, create_dir_for_file, human_readable_file_size
from .git_functions import get_version_string_from_git
__all__ = [
'get_directory_for_filename',
'create_dir_for_file',
'human_readable_file_size',
'read_in_chunks',
'get_version_string_from_git',
'create_symlink',
'delete_file',
'get_binary_from_file',
'get_dir_of_file',
'get_directory_for_filename',
'get_dirs_in_dir',
'get_files_in_dir',
'get_safe_name',
'get_string_list_from_file',
'get_version_string_from_git',
'human_readable_file_size',
'read_in_chunks',
'safe_rglob',
'write_binary_to_file',
'get_safe_name',
'delete_file',
'create_symlink',
'get_files_in_dir',
'get_dirs_in_dir',
'get_dir_of_file'
]
......@@ -2,6 +2,8 @@ import logging
import os
import re
import sys
from pathlib import Path
from typing import Iterable
from .file_functions import create_dir_for_file
......@@ -194,8 +196,8 @@ def get_dir_of_file(file_path):
'''
Returns absolute path of the directory including file
:param file_path: Paht of the file
:type: paht-like object
:param file_path: Path of the file
:type: path-like object
:return: string
'''
try:
......@@ -203,3 +205,36 @@ def get_dir_of_file(file_path):
except Exception as e:
logging.error('Could not get directory path: {} {}'.format(sys.exc_info()[0].__name__, e))
return '/'
def safe_rglob(path: Path, include_symlinks: bool = True, include_directories: bool = True) -> Iterable[Path]:
'''
alternative to pathlib.rglob which tries to follow symlinks and crashes if it encounters certain broken ones
'''
if not path.is_symlink() and path.is_dir():
for child_path in path.iterdir():
yield from _iterate_path_recursively(child_path, include_symlinks, include_directories)
else:
yield from []
def _iterate_path_recursively(path: Path, include_symlinks: bool = True, include_directories: bool = True):
try:
if path.is_symlink():
if include_symlinks and path.is_file():
yield path
else:
yield from []
elif path.is_file():
yield path
elif path.is_dir():
if include_directories:
yield path
for child_path in path.iterdir():
yield from _iterate_path_recursively(child_path, include_symlinks, include_directories)
except PermissionError:
logging.error('Permission Error: could not access path {path}'.format(path=path.absolute()))
yield from []
except OSError:
logging.warning('possible broken symlink: {path}'.format(path=path.absolute()))
yield from []
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment