Commit 5c99beab by Peter Weidenbach

fail-safe symlink function added

parent c03949db
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 .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__ = [
'get_directory_for_filename',
......@@ -15,6 +15,7 @@ __all__ = [
'write_binary_to_file',
'get_safe_name',
'delete_file',
'create_symlink',
'get_files_in_dir',
'get_dirs_in_dir',
]
import os
import sys
import logging
import os
import re
import sys
from builtins import FileExistsError
from .file_functions import create_dir_for_file
......@@ -103,6 +104,25 @@ def delete_file(file_path):
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-_+. '):
"""
removes all problematic characters from a file name
......
......@@ -5,7 +5,8 @@ import unittest
from common_helper_files import get_binary_from_file, \
write_binary_to_file, delete_file, get_safe_name, \
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
......@@ -71,6 +72,13 @@ class Test_FailSafeFileOperations(unittest.TestCase):
# Test delete none existing file
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):
a = "/()=Hello%&World!? Foo"
self.assertEqual(get_safe_name(a), "HelloWorld_Foo", "result not correct")
......
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