Update patch_files to work with Python 2 and 3

Signed-off-by: Roberto Rosario <roberto.rosario@mayan-edms.com>
This commit is contained in:
Roberto Rosario
2019-11-21 01:52:57 -04:00
parent b5c4c61b3f
commit b0729ea714

View File

@@ -7,6 +7,7 @@ import tempfile
from pathlib2 import Path
from django.utils.six import PY3
from django.utils.module_loading import import_string
from .settings import setting_temporary_directory
@@ -68,13 +69,18 @@ def patch_files(path=None, replace_list=None):
}
]
"""
if PY3:
file_open_mode = 'r+'
else:
file_open_mode = 'rb+'
path_object = Path(path)
for replace_entry in replace_list or []:
for path_entry in path_object.glob('**/{}'.format(replace_entry['filename_pattern'])):
if path_entry.is_file():
for pattern in replace_entry['content_patterns']:
with path_entry.open(mode='r+') as source_file_object:
with tempfile.TemporaryFile(mode='r+') as temporary_file_object:
with path_entry.open(mode=file_open_mode) as source_file_object:
with tempfile.TemporaryFile(mode=file_open_mode) as temporary_file_object:
source_position = 0
destination_position = 0