utils.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import os
  2. import fnmatch
  3. from django.conf import settings
  4. from django.core.exceptions import ImproperlyConfigured
  5. def matches_patterns(path, patterns=None):
  6. """
  7. Return True or False depending on whether the ``path`` should be
  8. ignored (if it matches any pattern in ``ignore_patterns``).
  9. """
  10. if patterns is None:
  11. patterns = []
  12. for pattern in patterns:
  13. if fnmatch.fnmatchcase(path, pattern):
  14. return True
  15. return False
  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. if matches_patterns(fn, ignore_patterns):
  26. continue
  27. if location:
  28. fn = os.path.join(location, fn)
  29. yield fn
  30. for dir in directories:
  31. if matches_patterns(dir, ignore_patterns):
  32. continue
  33. if location:
  34. dir = os.path.join(location, dir)
  35. for fn in get_files(storage, ignore_patterns, dir):
  36. yield fn
  37. def check_settings(base_url=None):
  38. """
  39. Checks if the staticfiles settings have sane values.
  40. """
  41. if base_url is None:
  42. base_url = settings.STATIC_URL
  43. if not base_url:
  44. raise ImproperlyConfigured(
  45. "You're using the staticfiles app "
  46. "without having set the required STATIC_URL setting.")
  47. if settings.MEDIA_URL == base_url:
  48. raise ImproperlyConfigured("The MEDIA_URL and STATIC_URL "
  49. "settings must have different values")
  50. if ((settings.MEDIA_ROOT and settings.STATIC_ROOT) and
  51. (settings.MEDIA_ROOT == settings.STATIC_ROOT)):
  52. raise ImproperlyConfigured("The MEDIA_ROOT and STATIC_ROOT "
  53. "settings must have different values")