Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
common_helper_files
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
fact-depend
common_helper_files
Commits
55eb11f3
Commit
55eb11f3
authored
6 years ago
by
Jörg Stucke
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added safe rglob
parent
fc40851b
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
52 additions
and
13 deletions
+52
-13
__init__.py
common_helper_files/__init__.py
+15
-11
fail_safe_file_operations.py
common_helper_files/fail_safe_file_operations.py
+37
-2
No files found.
common_helper_files/__init__.py
View file @
55eb11f3
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'
]
This diff is collapsed.
Click to expand it.
common_helper_files/fail_safe_file_operations.py
View file @
55eb11f3
...
...
@@ -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: Pa
ht
of the file
:type: pa
ht
-like object
:param file_path: Pa
th
of the file
:type: pa
th
-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
[]
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment