Move setting to the storage app
The setting COMMON_TEMPORARY_DIRECTORY is now STORAGE_TEMPORARY_DIRECTORY. Move file related utilities to the storage app. Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
@@ -11,8 +11,8 @@ from django.core.management.base import CommandError
|
||||
from django.utils.encoding import force_text
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from mayan.apps.common.utils import fs_cleanup
|
||||
from mayan.apps.documents.models import DocumentType
|
||||
from mayan.apps.storage.utils import fs_cleanup
|
||||
|
||||
CONVERTDB_FOLDER = 'convertdb'
|
||||
CONVERTDB_OUTPUT_FILENAME = 'migrate.json'
|
||||
|
||||
@@ -80,14 +80,6 @@ setting_shared_storage_arguments = namespace.add_setting(
|
||||
os.path.join(settings.MEDIA_ROOT, 'shared_files')
|
||||
), quoted=True
|
||||
)
|
||||
setting_temporary_directory = namespace.add_setting(
|
||||
global_name='COMMON_TEMPORARY_DIRECTORY', default=tempfile.gettempdir(),
|
||||
help_text=_(
|
||||
'Temporary directory used site wide to store thumbnails, previews '
|
||||
'and temporary files.'
|
||||
),
|
||||
is_path=True
|
||||
)
|
||||
|
||||
namespace = Namespace(name='django', label=_('Django'))
|
||||
|
||||
|
||||
@@ -18,9 +18,10 @@ from django.template import Context, Template
|
||||
from django.test.utils import ContextList
|
||||
from django.urls import clear_url_caches, reverse
|
||||
|
||||
from mayan.apps.storage.settings import setting_temporary_directory
|
||||
|
||||
from .literals import TEST_VIEW_NAME, TEST_VIEW_URL
|
||||
|
||||
from ..settings import setting_temporary_directory
|
||||
|
||||
if getattr(settings, 'COMMON_TEST_FILE_HANDLES', False):
|
||||
import psutil
|
||||
|
||||
@@ -21,7 +21,6 @@ from mayan.apps.common.compat import dict_type, dictionary_type
|
||||
|
||||
from .exceptions import NotLatestVersion, UnknownLatestVersion
|
||||
from .literals import DJANGO_SQLITE_BACKEND, MAYAN_PYPI_NAME, PYPI_URL
|
||||
from .settings import setting_temporary_directory
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -40,27 +39,6 @@ def check_version():
|
||||
raise NotLatestVersion(upstream_version=versions[0])
|
||||
|
||||
|
||||
# http://stackoverflow.com/questions/123198/how-do-i-copy-a-file-in-python
|
||||
def copyfile(source, destination, buffer_size=1024 * 1024):
|
||||
"""
|
||||
Copy a file from source to dest. source and dest
|
||||
can either be strings or any object with a read or
|
||||
write method, like StringIO for example.
|
||||
"""
|
||||
source_descriptor = get_descriptor(source)
|
||||
destination_descriptor = get_descriptor(destination, read=False)
|
||||
|
||||
while True:
|
||||
copy_buffer = source_descriptor.read(buffer_size)
|
||||
if copy_buffer:
|
||||
destination_descriptor.write(copy_buffer)
|
||||
else:
|
||||
break
|
||||
|
||||
source_descriptor.close()
|
||||
destination_descriptor.close()
|
||||
|
||||
|
||||
def encapsulate(function):
|
||||
# Workaround Django ticket 15791
|
||||
# Changeset 16045
|
||||
@@ -76,54 +54,6 @@ def get_user_label_text(context):
|
||||
return context['request'].user.get_full_name() or context['request'].user
|
||||
|
||||
|
||||
def fs_cleanup(filename, file_descriptor=None, suppress_exceptions=True):
|
||||
"""
|
||||
Tries to remove the given filename. Ignores non-existent files
|
||||
"""
|
||||
if file_descriptor:
|
||||
os.close(file_descriptor)
|
||||
|
||||
try:
|
||||
os.remove(filename)
|
||||
except OSError:
|
||||
try:
|
||||
shutil.rmtree(filename)
|
||||
except OSError:
|
||||
if suppress_exceptions:
|
||||
pass
|
||||
else:
|
||||
raise
|
||||
|
||||
|
||||
def get_descriptor(file_input, read=True):
|
||||
try:
|
||||
# Is it a file like object?
|
||||
file_input.seek(0)
|
||||
except AttributeError:
|
||||
# If not, try open it.
|
||||
if read:
|
||||
return open(file_input, mode='rb')
|
||||
else:
|
||||
return open(file_input, mode='wb')
|
||||
else:
|
||||
return file_input
|
||||
|
||||
|
||||
def TemporaryFile(*args, **kwargs):
|
||||
kwargs.update({'dir': setting_temporary_directory.value})
|
||||
return tempfile.TemporaryFile(*args, **kwargs)
|
||||
|
||||
|
||||
def mkdtemp(*args, **kwargs):
|
||||
kwargs.update({'dir': setting_temporary_directory.value})
|
||||
return tempfile.mkdtemp(*args, **kwargs)
|
||||
|
||||
|
||||
def mkstemp(*args, **kwargs):
|
||||
kwargs.update({'dir': setting_temporary_directory.value})
|
||||
return tempfile.mkstemp(*args, **kwargs)
|
||||
|
||||
|
||||
def resolve(path, urlconf=None):
|
||||
path = '/{}'.format(path.replace(get_script_prefix(), '', 1))
|
||||
return django_resolve(path=path, urlconf=urlconf)
|
||||
@@ -201,24 +131,3 @@ def urlquote(link=None, get=None):
|
||||
return '%s%s' % (link, django_urlencode(get, doseq=True))
|
||||
else:
|
||||
return django_urlquote(link)
|
||||
|
||||
|
||||
def validate_path(path):
|
||||
if not os.path.exists(path):
|
||||
# If doesn't exist try to create it
|
||||
try:
|
||||
os.mkdir(path)
|
||||
except Exception as exception:
|
||||
logger.debug('unhandled exception: %s', exception)
|
||||
return False
|
||||
|
||||
# Check if it is writable
|
||||
try:
|
||||
fd, test_filepath = tempfile.mkstemp(dir=path)
|
||||
os.close(fd)
|
||||
os.unlink(test_filepath)
|
||||
except Exception as exception:
|
||||
logger.debug('unhandled exception: %s', exception)
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
Reference in New Issue
Block a user