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-gitdep
common_helper_files
Commits
5c99beab
Commit
5c99beab
authored
Jul 21, 2017
by
Peter Weidenbach
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fail-safe symlink function added
parent
c03949db
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
34 additions
and
5 deletions
+34
-5
__init__.py
common_helper_files/__init__.py
+3
-2
fail_safe_file_operations.py
common_helper_files/fail_safe_file_operations.py
+22
-2
test_fail_safe_file_operations.py
tests/test_fail_safe_file_operations.py
+9
-1
No files found.
common_helper_files/__init__.py
View file @
5c99beab
from
.file_functions
import
read_in_chunks
,
get_directory_for_filename
,
create_dir_for_file
,
human_readable_file_size
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
from
.git_functions
import
get_version_string_from_git
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
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
__version__
=
'0.1.
7
'
__version__
=
'0.1.
8
'
__all__
=
[
__all__
=
[
'get_directory_for_filename'
,
'get_directory_for_filename'
,
...
@@ -15,6 +15,7 @@ __all__ = [
...
@@ -15,6 +15,7 @@ __all__ = [
'write_binary_to_file'
,
'write_binary_to_file'
,
'get_safe_name'
,
'get_safe_name'
,
'delete_file'
,
'delete_file'
,
'create_symlink'
,
'get_files_in_dir'
,
'get_files_in_dir'
,
'get_dirs_in_dir'
,
'get_dirs_in_dir'
,
]
]
common_helper_files/fail_safe_file_operations.py
View file @
5c99beab
import
os
import
sys
import
logging
import
logging
import
os
import
re
import
re
import
sys
from
builtins
import
FileExistsError
from
.file_functions
import
create_dir_for_file
from
.file_functions
import
create_dir_for_file
...
@@ -103,6 +104,25 @@ def delete_file(file_path):
...
@@ -103,6 +104,25 @@ def delete_file(file_path):
logging
.
error
(
"Could not delete file: {} {}"
.
format
(
sys
.
exc_info
()[
0
]
.
__name__
,
e
))
logging
.
error
(
"Could not delete file: {} {}"
.
format
(
sys
.
exc_info
()[
0
]
.
__name__
,
e
))
def
create_symlink
(
src_path
,
dst_path
):
"""
Fail-safe symlink operation. Symlinks a file if dest does not exist.
Errors are logged. No exception raised.
:param src_path: src file
:type src_path: str
:param dst_path: link location
:type dst_path: str
:return: None
"""
try
:
os
.
symlink
(
src_path
,
dst_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
))
def
get_safe_name
(
file_name
,
max_size
=
200
,
valid_characters
=
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_+. '
):
def
get_safe_name
(
file_name
,
max_size
=
200
,
valid_characters
=
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_+. '
):
"""
"""
removes all problematic characters from a file name
removes all problematic characters from a file name
...
...
tests/test_fail_safe_file_operations.py
View file @
5c99beab
...
@@ -5,7 +5,8 @@ import unittest
...
@@ -5,7 +5,8 @@ import unittest
from
common_helper_files
import
get_binary_from_file
,
\
from
common_helper_files
import
get_binary_from_file
,
\
write_binary_to_file
,
delete_file
,
get_safe_name
,
\
write_binary_to_file
,
delete_file
,
get_safe_name
,
\
get_files_in_dir
,
get_string_list_from_file
,
\
get_files_in_dir
,
get_string_list_from_file
,
\
get_dirs_in_dir
,
get_directory_for_filename
get_dirs_in_dir
,
get_directory_for_filename
,
\
create_symlink
from
common_helper_files.fail_safe_file_operations
import
_get_counted_file_path
from
common_helper_files.fail_safe_file_operations
import
_get_counted_file_path
...
@@ -71,6 +72,13 @@ class Test_FailSafeFileOperations(unittest.TestCase):
...
@@ -71,6 +72,13 @@ class Test_FailSafeFileOperations(unittest.TestCase):
# Test delete none existing file
# Test delete none existing file
delete_file
(
file_path
)
delete_file
(
file_path
)
def
test_create_symlink
(
self
):
test_file_path
=
os
.
path
.
join
(
self
.
tmp_dir
.
name
,
'test_folder'
,
'test_file'
)
symlink_path
=
os
.
path
.
join
(
self
.
tmp_dir
.
name
,
'test_symlink'
)
create_symlink
(
test_file_path
,
symlink_path
)
self
.
assertEqual
(
os
.
readlink
(
symlink_path
),
test_file_path
)
create_symlink
(
test_file_path
,
symlink_path
)
def
test_get_safe_name
(
self
):
def
test_get_safe_name
(
self
):
a
=
"/()=Hello
%
&World!? Foo"
a
=
"/()=Hello
%
&World!? Foo"
self
.
assertEqual
(
get_safe_name
(
a
),
"HelloWorld_Foo"
,
"result not correct"
)
self
.
assertEqual
(
get_safe_name
(
a
),
"HelloWorld_Foo"
,
"result not correct"
)
...
...
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