django.contrib.staticfiles.storage: 160 total statements, 0.0% covered

Generated: Wed 2013-03-13 10:33 CET

Source file: /media/Envs/Envs/filer-gallery/lib/python2.7/site-packages/django/contrib/staticfiles/storage.py

Stats: 0 executed, 143 missed, 17 excluded, 121 ignored

  1. from __future__ import with_statement
  2. import hashlib
  3. import os
  4. import posixpath
  5. import re
  6. from urllib import unquote
  7. from urlparse import urlsplit, urlunsplit, urldefrag
  8. from django.conf import settings
  9. from django.core.cache import (get_cache, InvalidCacheBackendError,
  10. cache as default_cache)
  11. from django.core.exceptions import ImproperlyConfigured
  12. from django.core.files.base import ContentFile
  13. from django.core.files.storage import FileSystemStorage, get_storage_class
  14. from django.utils.datastructures import SortedDict
  15. from django.utils.encoding import force_unicode, smart_str
  16. from django.utils.functional import LazyObject
  17. from django.utils.importlib import import_module
  18. from django.contrib.staticfiles.utils import check_settings, matches_patterns
  19. class StaticFilesStorage(FileSystemStorage):
  20. """
  21. Standard file system storage for static files.
  22. The defaults for ``location`` and ``base_url`` are
  23. ``STATIC_ROOT`` and ``STATIC_URL``.
  24. """
  25. def __init__(self, location=None, base_url=None, *args, **kwargs):
  26. if location is None:
  27. location = settings.STATIC_ROOT
  28. if base_url is None:
  29. base_url = settings.STATIC_URL
  30. check_settings(base_url)
  31. super(StaticFilesStorage, self).__init__(location, base_url,
  32. *args, **kwargs)
  33. def path(self, name):
  34. if not self.location:
  35. raise ImproperlyConfigured("You're using the staticfiles app "
  36. "without having set the STATIC_ROOT "
  37. "setting to a filesystem path.")
  38. return super(StaticFilesStorage, self).path(name)
  39. class CachedFilesMixin(object):
  40. patterns = (
  41. ("*.css", (
  42. r"""(url\(['"]{0,1}\s*(.*?)["']{0,1}\))""",
  43. r"""(@import\s*["']\s*(.*?)["'])""",
  44. )),
  45. )
  46. def __init__(self, *args, **kwargs):
  47. super(CachedFilesMixin, self).__init__(*args, **kwargs)
  48. try:
  49. self.cache = get_cache('staticfiles')
  50. except InvalidCacheBackendError:
  51. # Use the default backend
  52. self.cache = default_cache
  53. self._patterns = SortedDict()
  54. for extension, patterns in self.patterns:
  55. for pattern in patterns:
  56. compiled = re.compile(pattern)
  57. self._patterns.setdefault(extension, []).append(compiled)
  58. def hashed_name(self, name, content=None):
  59. parsed_name = urlsplit(unquote(name))
  60. clean_name = parsed_name.path.strip()
  61. if content is None:
  62. if not self.exists(clean_name):
  63. raise ValueError("The file '%s' could not be found with %r." %
  64. (clean_name, self))
  65. try:
  66. content = self.open(clean_name)
  67. except IOError:
  68. # Handle directory paths and fragments
  69. return name
  70. path, filename = os.path.split(clean_name)
  71. root, ext = os.path.splitext(filename)
  72. # Get the MD5 hash of the file
  73. md5 = hashlib.md5()
  74. for chunk in content.chunks():
  75. md5.update(chunk)
  76. md5sum = md5.hexdigest()[:12]
  77. hashed_name = os.path.join(path, u"%s.%s%s" %
  78. (root, md5sum, ext))
  79. unparsed_name = list(parsed_name)
  80. unparsed_name[2] = hashed_name
  81. # Special casing for a @font-face hack, like url(myfont.eot?#iefix")
  82. # http://www.fontspring.com/blog/the-new-bulletproof-font-face-syntax
  83. if '?#' in name and not unparsed_name[3]:
  84. unparsed_name[2] += '?'
  85. return urlunsplit(unparsed_name)
  86. def cache_key(self, name):
  87. return u'staticfiles:%s' % hashlib.md5(smart_str(name)).hexdigest()
  88. def url(self, name, force=False):
  89. """
  90. Returns the real URL in DEBUG mode.
  91. """
  92. if settings.DEBUG and not force:
  93. hashed_name, fragment = name, ''
  94. else:
  95. clean_name, fragment = urldefrag(name)
  96. if urlsplit(clean_name).path.endswith('/'): # don't hash paths
  97. hashed_name = name
  98. else:
  99. cache_key = self.cache_key(name)
  100. hashed_name = self.cache.get(cache_key)
  101. if hashed_name is None:
  102. hashed_name = self.hashed_name(clean_name).replace('\\', '/')
  103. # set the cache if there was a miss
  104. # (e.g. if cache server goes down)
  105. self.cache.set(cache_key, hashed_name)
  106. final_url = super(CachedFilesMixin, self).url(hashed_name)
  107. # Special casing for a @font-face hack, like url(myfont.eot?#iefix")
  108. # http://www.fontspring.com/blog/the-new-bulletproof-font-face-syntax
  109. query_fragment = '?#' in name # [sic!]
  110. if fragment or query_fragment:
  111. urlparts = list(urlsplit(final_url))
  112. if fragment and not urlparts[4]:
  113. urlparts[4] = fragment
  114. if query_fragment and not urlparts[3]:
  115. urlparts[2] += '?'
  116. final_url = urlunsplit(urlparts)
  117. return unquote(final_url)
  118. def url_converter(self, name):
  119. """
  120. Returns the custom URL converter for the given file name.
  121. """
  122. def converter(matchobj):
  123. """
  124. Converts the matched URL depending on the parent level (`..`)
  125. and returns the normalized and hashed URL using the url method
  126. of the storage.
  127. """
  128. matched, url = matchobj.groups()
  129. # Completely ignore http(s) prefixed URLs,
  130. # fragments and data-uri URLs
  131. if url.startswith(('#', 'http:', 'https:', 'data:')):
  132. return matched
  133. name_parts = name.split(os.sep)
  134. # Using posix normpath here to remove duplicates
  135. url = posixpath.normpath(url)
  136. url_parts = url.split('/')
  137. parent_level, sub_level = url.count('..'), url.count('/')
  138. if url.startswith('/'):
  139. sub_level -= 1
  140. url_parts = url_parts[1:]
  141. if parent_level or not url.startswith('/'):
  142. start, end = parent_level + 1, parent_level
  143. else:
  144. if sub_level:
  145. if sub_level == 1:
  146. parent_level -= 1
  147. start, end = parent_level, 1
  148. else:
  149. start, end = 1, sub_level - 1
  150. joined_result = '/'.join(name_parts[:-start] + url_parts[end:])
  151. hashed_url = self.url(unquote(joined_result), force=True)
  152. file_name = hashed_url.split('/')[-1:]
  153. relative_url = '/'.join(url.split('/')[:-1] + file_name)
  154. # Return the hashed version to the file
  155. return 'url("%s")' % unquote(relative_url)
  156. return converter
  157. def post_process(self, paths, dry_run=False, **options):
  158. """
  159. Post process the given list of files (called from collectstatic).
  160. Processing is actually two separate operations:
  161. 1. renaming files to include a hash of their content for cache-busting,
  162. and copying those files to the target storage.
  163. 2. adjusting files which contain references to other files so they
  164. refer to the cache-busting filenames.
  165. If either of these are performed on a file, then that file is considered
  166. post-processed.
  167. """
  168. # don't even dare to process the files if we're in dry run mode
  169. if dry_run:
  170. return
  171. # where to store the new paths
  172. hashed_paths = {}
  173. # build a list of adjustable files
  174. matches = lambda path: matches_patterns(path, self._patterns.keys())
  175. adjustable_paths = [path for path in paths if matches(path)]
  176. # then sort the files by the directory level
  177. path_level = lambda name: len(name.split(os.sep))
  178. for name in sorted(paths.keys(), key=path_level, reverse=True):
  179. # use the original, local file, not the copied-but-unprocessed
  180. # file, which might be somewhere far away, like S3
  181. storage, path = paths[name]
  182. with storage.open(path) as original_file:
  183. # generate the hash with the original content, even for
  184. # adjustable files.
  185. hashed_name = self.hashed_name(name, original_file)
  186. # then get the original's file content..
  187. if hasattr(original_file, 'seek'):
  188. original_file.seek(0)
  189. hashed_file_exists = self.exists(hashed_name)
  190. processed = False
  191. # ..to apply each replacement pattern to the content
  192. if name in adjustable_paths:
  193. content = original_file.read()
  194. converter = self.url_converter(name)
  195. for patterns in self._patterns.values():
  196. for pattern in patterns:
  197. content = pattern.sub(converter, content)
  198. if hashed_file_exists:
  199. self.delete(hashed_name)
  200. # then save the processed result
  201. content_file = ContentFile(smart_str(content))
  202. saved_name = self._save(hashed_name, content_file)
  203. hashed_name = force_unicode(saved_name.replace('\\', '/'))
  204. processed = True
  205. else:
  206. # or handle the case in which neither processing nor
  207. # a change to the original file happened
  208. if not hashed_file_exists:
  209. processed = True
  210. saved_name = self._save(hashed_name, original_file)
  211. hashed_name = force_unicode(saved_name.replace('\\', '/'))
  212. # and then set the cache accordingly
  213. hashed_paths[self.cache_key(name)] = hashed_name
  214. yield name, hashed_name, processed
  215. # Finally set the cache
  216. self.cache.set_many(hashed_paths)
  217. class CachedStaticFilesStorage(CachedFilesMixin, StaticFilesStorage):
  218. """
  219. A static file system storage backend which also saves
  220. hashed copies of the files it saves.
  221. """
  222. pass
  223. class AppStaticStorage(FileSystemStorage):
  224. """
  225. A file system storage backend that takes an app module and works
  226. for the ``static`` directory of it.
  227. """
  228. prefix = None
  229. source_dir = 'static'
  230. def __init__(self, app, *args, **kwargs):
  231. """
  232. Returns a static file storage if available in the given app.
  233. """
  234. # app is the actual app module
  235. mod = import_module(app)
  236. mod_path = os.path.dirname(mod.__file__)
  237. location = os.path.join(mod_path, self.source_dir)
  238. super(AppStaticStorage, self).__init__(location, *args, **kwargs)
  239. class ConfiguredStorage(LazyObject):
  240. def _setup(self):
  241. self._wrapped = get_storage_class(settings.STATICFILES_STORAGE)()
  242. staticfiles_storage = ConfiguredStorage()