2010-10-20 09:33:24 +08:00
|
|
|
import fnmatch
|
2015-01-28 20:35:27 +08:00
|
|
|
import os
|
|
|
|
|
2010-11-12 05:43:49 +08:00
|
|
|
from django.conf import settings
|
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2011-08-11 22:07:39 +08:00
|
|
|
def matches_patterns(path, patterns=None):
|
2010-10-20 09:33:24 +08:00
|
|
|
"""
|
2011-01-16 07:38:00 +08:00
|
|
|
Return True or False depending on whether the ``path`` should be
|
|
|
|
ignored (if it matches any pattern in ``ignore_patterns``).
|
2010-10-20 09:33:24 +08:00
|
|
|
"""
|
2017-12-27 02:44:12 +08:00
|
|
|
return any(fnmatch.fnmatchcase(path, pattern) for pattern in (patterns or []))
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2011-08-11 22:07:39 +08:00
|
|
|
def get_files(storage, ignore_patterns=None, location=''):
|
2011-01-16 07:38:00 +08:00
|
|
|
"""
|
|
|
|
Recursively walk the storage directories yielding the paths
|
|
|
|
of all files that should be copied.
|
|
|
|
"""
|
2011-08-11 22:07:39 +08:00
|
|
|
if ignore_patterns is None:
|
|
|
|
ignore_patterns = []
|
2010-10-20 09:33:24 +08:00
|
|
|
directories, files = storage.listdir(location)
|
2011-01-16 07:38:00 +08:00
|
|
|
for fn in files:
|
2018-07-14 04:48:19 +08:00
|
|
|
# Match only the basename.
|
2011-08-11 22:07:39 +08:00
|
|
|
if matches_patterns(fn, ignore_patterns):
|
2011-01-16 07:38:00 +08:00
|
|
|
continue
|
|
|
|
if location:
|
|
|
|
fn = os.path.join(location, fn)
|
2018-07-14 04:48:19 +08:00
|
|
|
# Match the full file path.
|
|
|
|
if matches_patterns(fn, ignore_patterns):
|
|
|
|
continue
|
2011-01-16 07:38:00 +08:00
|
|
|
yield fn
|
2010-10-20 09:33:24 +08:00
|
|
|
for dir in directories:
|
2011-08-11 22:07:39 +08:00
|
|
|
if matches_patterns(dir, ignore_patterns):
|
2010-10-20 09:33:24 +08:00
|
|
|
continue
|
|
|
|
if location:
|
2011-01-02 09:32:17 +08:00
|
|
|
dir = os.path.join(location, dir)
|
2017-02-24 09:06:01 +08:00
|
|
|
yield from get_files(storage, ignore_patterns, dir)
|
2010-11-12 05:43:49 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2011-08-15 02:15:04 +08:00
|
|
|
def check_settings(base_url=None):
|
2010-11-12 05:43:49 +08:00
|
|
|
"""
|
2017-01-25 04:31:57 +08:00
|
|
|
Check if the staticfiles settings have sane values.
|
2010-11-12 05:43:49 +08:00
|
|
|
"""
|
2011-08-15 16:22:22 +08:00
|
|
|
if base_url is None:
|
2011-08-15 02:15:04 +08:00
|
|
|
base_url = settings.STATIC_URL
|
|
|
|
if not base_url:
|
2011-02-01 22:57:10 +08:00
|
|
|
raise ImproperlyConfigured(
|
|
|
|
"You're using the staticfiles app "
|
|
|
|
"without having set the required STATIC_URL setting.")
|
2011-08-15 02:15:04 +08:00
|
|
|
if settings.MEDIA_URL == base_url:
|
2010-11-17 23:36:26 +08:00
|
|
|
raise ImproperlyConfigured("The MEDIA_URL and STATIC_URL "
|
|
|
|
"settings must have different values")
|
|
|
|
if ((settings.MEDIA_ROOT and settings.STATIC_ROOT) and
|
|
|
|
(settings.MEDIA_ROOT == settings.STATIC_ROOT)):
|
|
|
|
raise ImproperlyConfigured("The MEDIA_ROOT and STATIC_ROOT "
|
|
|
|
"settings must have different values")
|