From a36c2a6590d6411fca80396290fa71a647f66224 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Wed, 20 Nov 2019 17:33:42 -0400 Subject: [PATCH] Move the file patching code Moved from the Dependency class to a generalized utility of the storages app. Signed-off-by: Roberto Rosario --- HISTORY.rst | 2 ++ mayan/apps/dependencies/classes.py | 30 ++----------------------- mayan/apps/storage/utils.py | 36 ++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 28 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 58a592f38a..a2cc419254 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -5,6 +5,8 @@ - Update FAQ entry about the LDAP file. - Automate documentation building dependencies. - Add sphinx sitemap extension. +- Move the file patching code from the Dependency class to a + generalized utility of the storages app. 3.2.10 (2019-11-19) =================== diff --git a/mayan/apps/dependencies/classes.py b/mayan/apps/dependencies/classes.py index cc62c77935..e91859ca64 100644 --- a/mayan/apps/dependencies/classes.py +++ b/mayan/apps/dependencies/classes.py @@ -1,6 +1,5 @@ from __future__ import print_function, unicode_literals -import fileinput import json from importlib import import_module import logging @@ -25,7 +24,7 @@ from django.utils.translation import ugettext_lazy as _, ugettext from mayan.apps.common.compat import FileNotFoundErrorException from mayan.apps.common.utils import resolve_attribute -from mayan.apps.storage.utils import mkdtemp +from mayan.apps.storage.utils import mkdtemp, patch_files as storage_patch_files from .algorithms import HashAlgorithm from .exceptions import DependenciesException @@ -410,20 +409,6 @@ class Dependency(object): return self.version_string or _('Not specified') def patch_files(self, path=None, replace_list=None): - """ - Search and replace content from a list of file based on a pattern - replace_list[ - { - 'filename_pattern': '*.css', - 'content_patterns': [ - { - 'search': '', - 'replace': '', - } - ] - } - ] - """ print(_('Patching files... '), end='') try: @@ -437,18 +422,7 @@ class Dependency(object): if not replace_list: replace_list = self.replace_list - 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(): - # PY3 - # Don't use context processor to allow working on Python 2.7 - # Update on Mayan EDMS version >= 4.0 - file_object = fileinput.FileInput(force_text(path_entry), inplace=True) - for line in file_object: - for pattern in replace_entry['content_patterns']: - print(line.replace(pattern['search'], pattern['replace']), end='') - file_object.close() + storage_patch_files(path=path, replace_list=replace_list) def verify(self): """ diff --git a/mayan/apps/storage/utils.py b/mayan/apps/storage/utils.py index 90eab5bae5..e288734c01 100644 --- a/mayan/apps/storage/utils.py +++ b/mayan/apps/storage/utils.py @@ -1,10 +1,16 @@ from __future__ import unicode_literals +import fileinput import logging import os import shutil import tempfile +from pathlib2 import Path + +from django.utils.encoding import force_text +from django.utils.module_loading import import_string + from .settings import setting_temporary_directory logger = logging.getLogger(__name__) @@ -49,6 +55,36 @@ def mkstemp(*args, **kwargs): return tempfile.mkstemp(*args, **kwargs) +def patch_files(path=None, replace_list=None): + """ + Search and replace content from a list of file based on a pattern + replace_list[ + { + 'filename_pattern': '*.css', + 'content_patterns': [ + { + 'search': '', + 'replace': '', + } + ] + } + ] + """ + 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(): + # PY3 + # Don't use context processor to allow working on Python 2.7 + # Update on Mayan EDMS version >= 4.0 + file_object = fileinput.FileInput(force_text(path_entry), inplace=True) + for line in file_object: + for pattern in replace_entry['content_patterns']: + line = line.replace(pattern['search'], pattern['replace']) + print(line, end='') + file_object.close() + + def validate_path(path): if not os.path.exists(path): # If doesn't exist try to create it