You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

finders.py 10KB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. import functools
  2. import os
  3. from collections import OrderedDict
  4. from django.apps import apps
  5. from django.conf import settings
  6. from django.contrib.staticfiles import utils
  7. from django.core.checks import Error
  8. from django.core.exceptions import ImproperlyConfigured
  9. from django.core.files.storage import (
  10. FileSystemStorage, Storage, default_storage,
  11. )
  12. from django.utils._os import safe_join
  13. from django.utils.functional import LazyObject, empty
  14. from django.utils.module_loading import import_string
  15. # To keep track on which directories the finder has searched the static files.
  16. searched_locations = []
  17. class BaseFinder:
  18. """
  19. A base file finder to be used for custom staticfiles finder classes.
  20. """
  21. def check(self, **kwargs):
  22. raise NotImplementedError(
  23. 'subclasses may provide a check() method to verify the finder is '
  24. 'configured correctly.'
  25. )
  26. def find(self, path, all=False):
  27. """
  28. Given a relative file path, find an absolute file path.
  29. If the ``all`` parameter is False (default) return only the first found
  30. file path; if True, return a list of all found files paths.
  31. """
  32. raise NotImplementedError('subclasses of BaseFinder must provide a find() method')
  33. def list(self, ignore_patterns):
  34. """
  35. Given an optional list of paths to ignore, return a two item iterable
  36. consisting of the relative path and storage instance.
  37. """
  38. raise NotImplementedError('subclasses of BaseFinder must provide a list() method')
  39. class FileSystemFinder(BaseFinder):
  40. """
  41. A static files finder that uses the ``STATICFILES_DIRS`` setting
  42. to locate files.
  43. """
  44. def __init__(self, app_names=None, *args, **kwargs):
  45. # List of locations with static files
  46. self.locations = []
  47. # Maps dir paths to an appropriate storage instance
  48. self.storages = OrderedDict()
  49. for root in settings.STATICFILES_DIRS:
  50. if isinstance(root, (list, tuple)):
  51. prefix, root = root
  52. else:
  53. prefix = ''
  54. if (prefix, root) not in self.locations:
  55. self.locations.append((prefix, root))
  56. for prefix, root in self.locations:
  57. filesystem_storage = FileSystemStorage(location=root)
  58. filesystem_storage.prefix = prefix
  59. self.storages[root] = filesystem_storage
  60. super().__init__(*args, **kwargs)
  61. def check(self, **kwargs):
  62. errors = []
  63. if not isinstance(settings.STATICFILES_DIRS, (list, tuple)):
  64. errors.append(Error(
  65. 'The STATICFILES_DIRS setting is not a tuple or list.',
  66. hint='Perhaps you forgot a trailing comma?',
  67. id='staticfiles.E001',
  68. ))
  69. for root in settings.STATICFILES_DIRS:
  70. if isinstance(root, (list, tuple)):
  71. prefix, root = root
  72. if prefix.endswith('/'):
  73. errors.append(Error(
  74. 'The prefix %r in the STATICFILES_DIRS setting must '
  75. 'not end with a slash.' % prefix,
  76. id='staticfiles.E003',
  77. ))
  78. if settings.STATIC_ROOT and os.path.abspath(settings.STATIC_ROOT) == os.path.abspath(root):
  79. errors.append(Error(
  80. 'The STATICFILES_DIRS setting should not contain the '
  81. 'STATIC_ROOT setting.',
  82. id='staticfiles.E002',
  83. ))
  84. return errors
  85. def find(self, path, all=False):
  86. """
  87. Look for files in the extra locations as defined in STATICFILES_DIRS.
  88. """
  89. matches = []
  90. for prefix, root in self.locations:
  91. if root not in searched_locations:
  92. searched_locations.append(root)
  93. matched_path = self.find_location(root, path, prefix)
  94. if matched_path:
  95. if not all:
  96. return matched_path
  97. matches.append(matched_path)
  98. return matches
  99. def find_location(self, root, path, prefix=None):
  100. """
  101. Find a requested static file in a location and return the found
  102. absolute path (or ``None`` if no match).
  103. """
  104. if prefix:
  105. prefix = '%s%s' % (prefix, os.sep)
  106. if not path.startswith(prefix):
  107. return None
  108. path = path[len(prefix):]
  109. path = safe_join(root, path)
  110. if os.path.exists(path):
  111. return path
  112. def list(self, ignore_patterns):
  113. """
  114. List all files in all locations.
  115. """
  116. for prefix, root in self.locations:
  117. storage = self.storages[root]
  118. for path in utils.get_files(storage, ignore_patterns):
  119. yield path, storage
  120. class AppDirectoriesFinder(BaseFinder):
  121. """
  122. A static files finder that looks in the directory of each app as
  123. specified in the source_dir attribute.
  124. """
  125. storage_class = FileSystemStorage
  126. source_dir = 'static'
  127. def __init__(self, app_names=None, *args, **kwargs):
  128. # The list of apps that are handled
  129. self.apps = []
  130. # Mapping of app names to storage instances
  131. self.storages = OrderedDict()
  132. app_configs = apps.get_app_configs()
  133. if app_names:
  134. app_names = set(app_names)
  135. app_configs = [ac for ac in app_configs if ac.name in app_names]
  136. for app_config in app_configs:
  137. app_storage = self.storage_class(
  138. os.path.join(app_config.path, self.source_dir))
  139. if os.path.isdir(app_storage.location):
  140. self.storages[app_config.name] = app_storage
  141. if app_config.name not in self.apps:
  142. self.apps.append(app_config.name)
  143. super().__init__(*args, **kwargs)
  144. def list(self, ignore_patterns):
  145. """
  146. List all files in all app storages.
  147. """
  148. for storage in self.storages.values():
  149. if storage.exists(''): # check if storage location exists
  150. for path in utils.get_files(storage, ignore_patterns):
  151. yield path, storage
  152. def find(self, path, all=False):
  153. """
  154. Look for files in the app directories.
  155. """
  156. matches = []
  157. for app in self.apps:
  158. app_location = self.storages[app].location
  159. if app_location not in searched_locations:
  160. searched_locations.append(app_location)
  161. match = self.find_in_app(app, path)
  162. if match:
  163. if not all:
  164. return match
  165. matches.append(match)
  166. return matches
  167. def find_in_app(self, app, path):
  168. """
  169. Find a requested static file in an app's static locations.
  170. """
  171. storage = self.storages.get(app)
  172. if storage:
  173. # only try to find a file if the source dir actually exists
  174. if storage.exists(path):
  175. matched_path = storage.path(path)
  176. if matched_path:
  177. return matched_path
  178. class BaseStorageFinder(BaseFinder):
  179. """
  180. A base static files finder to be used to extended
  181. with an own storage class.
  182. """
  183. storage = None
  184. def __init__(self, storage=None, *args, **kwargs):
  185. if storage is not None:
  186. self.storage = storage
  187. if self.storage is None:
  188. raise ImproperlyConfigured("The staticfiles storage finder %r "
  189. "doesn't have a storage class "
  190. "assigned." % self.__class__)
  191. # Make sure we have a storage instance here.
  192. if not isinstance(self.storage, (Storage, LazyObject)):
  193. self.storage = self.storage()
  194. super().__init__(*args, **kwargs)
  195. def find(self, path, all=False):
  196. """
  197. Look for files in the default file storage, if it's local.
  198. """
  199. try:
  200. self.storage.path('')
  201. except NotImplementedError:
  202. pass
  203. else:
  204. if self.storage.location not in searched_locations:
  205. searched_locations.append(self.storage.location)
  206. if self.storage.exists(path):
  207. match = self.storage.path(path)
  208. if all:
  209. match = [match]
  210. return match
  211. return []
  212. def list(self, ignore_patterns):
  213. """
  214. List all files of the storage.
  215. """
  216. for path in utils.get_files(self.storage, ignore_patterns):
  217. yield path, self.storage
  218. class DefaultStorageFinder(BaseStorageFinder):
  219. """
  220. A static files finder that uses the default storage backend.
  221. """
  222. storage = default_storage
  223. def __init__(self, *args, **kwargs):
  224. super().__init__(*args, **kwargs)
  225. base_location = getattr(self.storage, 'base_location', empty)
  226. if not base_location:
  227. raise ImproperlyConfigured("The storage backend of the "
  228. "staticfiles finder %r doesn't have "
  229. "a valid location." % self.__class__)
  230. def find(path, all=False):
  231. """
  232. Find a static file with the given path using all enabled finders.
  233. If ``all`` is ``False`` (default), return the first matching
  234. absolute path (or ``None`` if no match). Otherwise return a list.
  235. """
  236. searched_locations[:] = []
  237. matches = []
  238. for finder in get_finders():
  239. result = finder.find(path, all=all)
  240. if not all and result:
  241. return result
  242. if not isinstance(result, (list, tuple)):
  243. result = [result]
  244. matches.extend(result)
  245. if matches:
  246. return matches
  247. # No match.
  248. return [] if all else None
  249. def get_finders():
  250. for finder_path in settings.STATICFILES_FINDERS:
  251. yield get_finder(finder_path)
  252. @functools.lru_cache(maxsize=None)
  253. def get_finder(import_path):
  254. """
  255. Import the staticfiles finder class described by import_path, where
  256. import_path is the full Python path to the class.
  257. """
  258. Finder = import_string(import_path)
  259. if not issubclass(Finder, BaseFinder):
  260. raise ImproperlyConfigured('Finder "%s" is not a subclass of "%s"' %
  261. (Finder, BaseFinder))
  262. return Finder()