From b0729ea714d9eff0b6d62a05cb6fe10939868270 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Thu, 21 Nov 2019 01:52:57 -0400 Subject: [PATCH] Update patch_files to work with Python 2 and 3 Signed-off-by: Roberto Rosario --- mayan/apps/storage/utils.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/mayan/apps/storage/utils.py b/mayan/apps/storage/utils.py index 27175ba45c..379c5605d0 100644 --- a/mayan/apps/storage/utils.py +++ b/mayan/apps/storage/utils.py @@ -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