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
dd46ab43
Commit
dd46ab43
authored
Dec 03, 2021
by
Marten Ringwelski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve logging and avoid one-letter variables
parent
493bcedb
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
23 additions
and
23 deletions
+23
-23
fail_safe_file_operations.py
common_helper_files/fail_safe_file_operations.py
+23
-23
No files found.
common_helper_files/fail_safe_file_operations.py
View file @
dd46ab43
...
...
@@ -17,14 +17,14 @@ def get_binary_from_file(file_path: Union[str, Path]) -> Union[str, bytes]:
:return: file's binary as bytes; returns empty byte string on error
'''
try
:
p
=
Path
(
file_path
)
if
p
.
is_symlink
():
p
ath
=
Path
(
file_path
)
if
p
ath
.
is_symlink
():
# We need to wait for python 3.9 for Path.readlink
binary
=
f
'symbolic link -> {os.readlink(p)}'
binary
=
f
'symbolic link -> {os.readlink(p
ath
)}'
else
:
binary
=
p
.
read_bytes
()
binary
=
p
ath
.
read_bytes
()
except
Exception
as
e
:
logging
.
error
(
f
'Could not read file: {
sys.exc_info()[0].__name__} {e}'
)
logging
.
error
(
f
'Could not read file: {
e}'
,
exc_info
=
True
)
binary
=
b
''
return
binary
...
...
@@ -66,8 +66,8 @@ def write_binary_to_file(file_binary: Union[str, bytes], file_path: Union[str, P
if
file_path
.
exists
()
and
(
not
overwrite
or
file_copy
):
file_path
=
Path
(
_get_counted_file_path
(
str
(
file_path
)))
file_path
.
write_bytes
(
file_binary
)
except
Exception
as
e
:
logging
.
error
(
'Could not write file: {} {}'
.
format
(
sys
.
exc_info
()[
0
]
.
__name__
,
e
)
)
except
Exception
as
e
xc
:
logging
.
error
(
f
'Could not write file: {exc}'
,
exc_info
=
True
)
def
_get_counted_file_path
(
original_path
):
...
...
@@ -89,8 +89,8 @@ def delete_file(file_path: Union[str, Path]) -> None:
'''
try
:
Path
(
file_path
)
.
unlink
()
except
Exception
as
e
:
logging
.
error
(
'Could not delete file: {} {}'
.
format
(
sys
.
exc_info
()[
0
]
.
__name__
,
e
)
)
except
Exception
as
e
xc
:
logging
.
error
(
f
'Could not delete file: {exc}'
,
exc_info
=
True
)
def
create_symlink
(
src_path
:
Union
[
str
,
Path
],
dst_path
:
Union
[
str
,
Path
])
->
None
:
...
...
@@ -104,10 +104,10 @@ def create_symlink(src_path: Union[str, Path], dst_path: Union[str, Path]) -> No
try
:
create_dir_for_file
(
dst_path
)
Path
(
dst_path
)
.
symlink_to
(
src_path
)
except
FileExistsError
as
e
:
logging
.
debug
(
'Could not create Link: File exists: {}'
.
format
(
e
)
)
except
Exception
as
e
:
logging
.
error
(
'Could not create link: {} {}'
.
format
(
sys
.
exc_info
()[
0
]
.
__name__
,
e
)
)
except
FileExistsError
as
e
xc
:
logging
.
debug
(
f
'Could not create Link: File exists: {exc}'
)
except
Exception
as
e
xc
:
logging
.
error
(
f
'Could not create link: {exc}'
,
exc_info
=
True
)
def
get_safe_name
(
file_name
:
str
,
max_size
:
int
=
200
,
valid_characters
:
str
=
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_+. '
)
->
str
:
...
...
@@ -141,8 +141,8 @@ def get_files_in_dir(directory_path: Union[str, Path]) -> List[str]:
for
file_path
,
_
,
files
in
os
.
walk
(
directory_path
):
for
file_
in
files
:
result
.
append
(
str
(
Path
(
file_path
,
file_
)
.
absolute
()))
except
Exception
as
e
:
logging
.
error
(
'Could not get files: {} {}'
.
format
(
sys
.
exc_info
()[
0
]
.
__name__
,
e
)
)
except
Exception
as
e
xc
:
logging
.
error
(
f
'Could not get files: {exc}'
,
exc_info
=
True
)
return
result
...
...
@@ -154,13 +154,13 @@ def get_dirs_in_dir(directory_path: Union[str, Path]) -> List[str]:
'''
result
=
[]
try
:
p
=
Path
(
directory_path
)
for
item
in
p
.
iterdir
():
p
ath
=
Path
(
directory_path
)
for
item
in
p
ath
.
iterdir
():
if
not
Path
(
item
)
.
is_dir
():
continue
result
.
append
(
str
(
item
.
resolve
()))
except
Exception
as
e
:
logging
.
error
(
'Could not get directories: {} {}'
.
format
(
sys
.
exc_info
()[
0
]
.
__name__
,
e
)
)
except
Exception
as
e
xc
:
logging
.
error
(
f
'Could not get directories: {exc}'
,
exc_info
=
True
)
return
result
...
...
@@ -173,8 +173,8 @@ def get_dir_of_file(file_path: Union[str, Path]) -> str:
'''
try
:
return
str
(
Path
(
file_path
)
.
resolve
()
.
parent
)
except
Exception
as
e
:
logging
.
error
(
'Could not get directory path: {} {}'
.
format
(
sys
.
exc_info
()[
0
]
.
__name__
,
e
)
)
except
Exception
as
e
xc
:
logging
.
error
(
f
'Could not get directory path: {exc}'
,
exc_info
=
True
)
return
'/'
...
...
@@ -202,7 +202,7 @@ def _iterate_path_recursively(path: Path, include_symlinks: bool = True, include
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
())
)
logging
.
error
(
f
'Permission Error: could not access path {path.absolute()}'
)
except
OSError
:
logging
.
warning
(
'possible broken symlink: {path}'
.
format
(
path
=
path
.
absolute
())
)
logging
.
warning
(
f
'possible broken symlink: {path.absolute()}'
)
yield
from
[]
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