| 1 |
import fnmatch |
| 2 |
import os |
| 3 |
|
| 4 |
from django.conf import settings |
| 5 |
from django.core.exceptions import ImproperlyConfigured |
| 6 |
|
| 7 |
|
| 8 |
def matches_patterns(path, patterns): |
| 9 |
""" |
| 10 |
Return True or False depending on whether the ``path`` should be |
| 11 |
ignored (if it matches any pattern in ``ignore_patterns``). |
| 12 |
""" |
| 13 |
return any(fnmatch.fnmatchcase(path, pattern) for pattern in patterns) |
| 14 |
|
| 15 |
|
| 16 |
def get_files(storage, ignore_patterns=None, location=""): |
| 17 |
""" |
| 18 |
Recursively walk the storage directories yielding the paths |
| 19 |
of all files that should be copied. |
| 20 |
""" |
| 21 |
if ignore_patterns is None: |
| 22 |
ignore_patterns = [] |
| 23 |
directories, files = storage.listdir(location) |
| 24 |
for fn in files: |
| 25 |
# Match only the basename. |
| 26 |
if matches_patterns(fn, ignore_patterns): |
| 27 |
continue |
| 28 |
if location: |
| 29 |
fn = os.path.join(location, fn) |
| 30 |
# Match the full file path. |
| 31 |
if matches_patterns(fn, ignore_patterns): |
| 32 |
continue |
| 33 |
yield fn |
| 34 |
for dir in directories: |
| 35 |
if matches_patterns(dir, ignore_patterns): |
| 36 |
continue |
| 37 |
if location: |
| 38 |
dir = os.path.join(location, dir) |
| 39 |
yield from get_files(storage, ignore_patterns, dir) |
| 40 |
|
| 41 |
|
| 42 |
def check_settings(base_url=None): |
| 43 |
""" |
| 44 |
Check if the staticfiles settings have sane values. |
| 45 |
""" |
| 46 |
if base_url is None: |
| 47 |
base_url = settings.STATIC_URL |
| 48 |
if not base_url: |
| 49 |
raise ImproperlyConfigured( |
| 50 |
"You're using the staticfiles app " |
| 51 |
"without having set the required STATIC_URL setting." |
| 52 |
) |
| 53 |
if settings.MEDIA_URL == base_url: |
| 54 |
raise ImproperlyConfigured( |
| 55 |
"The MEDIA_URL and STATIC_URL settings must have different values" |
| 56 |
) |
| 57 |
if ( |
| 58 |
settings.DEBUG |
| 59 |
and settings.MEDIA_URL |
| 60 |
and settings.STATIC_URL |
| 61 |
and settings.MEDIA_URL.startswith(settings.STATIC_URL) |
| 62 |
): |
| 63 |
raise ImproperlyConfigured( |
| 64 |
"runserver can't serve media if MEDIA_URL is within STATIC_URL." |
| 65 |
) |
| 66 |
if (settings.MEDIA_ROOT and settings.STATIC_ROOT) and ( |
| 67 |
settings.MEDIA_ROOT == settings.STATIC_ROOT |
| 68 |
): |
| 69 |
raise ImproperlyConfigured( |
| 70 |
"The MEDIA_ROOT and STATIC_ROOT settings must have different values" |
| 71 |
) |