mirror of
https://github.com/django/django.git
synced 2024-12-01 15:42:04 +01:00
359be1c870
File operations always raise a ENOENT error when a file doesn't exist. Checking the file exists before the operation adds a race condition condition where the file could be removed between operations. As the operation already raises an error on a missing file, avoid this race and avoid checking the file exists twice. Instead only check a file exists by catching the ENOENT error.
79 lines
2.0 KiB
Python
79 lines
2.0 KiB
Python
import errno
|
|
import os
|
|
from datetime import datetime
|
|
|
|
from django.conf import settings
|
|
from django.contrib.staticfiles.storage import CachedStaticFilesStorage
|
|
from django.core.files import storage
|
|
from django.utils import timezone
|
|
|
|
|
|
class DummyStorage(storage.Storage):
|
|
"""
|
|
A storage class that implements get_modified_time().
|
|
"""
|
|
def _save(self, name, content):
|
|
return 'dummy'
|
|
|
|
def delete(self, name):
|
|
pass
|
|
|
|
def exists(self, name):
|
|
pass
|
|
|
|
def get_modified_time(self, name):
|
|
return datetime.datetime(1970, 1, 1, tzinfo=timezone.utc)
|
|
|
|
|
|
class PathNotImplementedStorage(storage.Storage):
|
|
|
|
def _save(self, name, content):
|
|
return 'dummy'
|
|
|
|
def _path(self, name):
|
|
return os.path.join(settings.STATIC_ROOT, name)
|
|
|
|
def exists(self, name):
|
|
return os.path.exists(self._path(name))
|
|
|
|
def listdir(self, path):
|
|
path = self._path(path)
|
|
directories, files = [], []
|
|
for entry in os.listdir(path):
|
|
if os.path.isdir(os.path.join(path, entry)):
|
|
directories.append(entry)
|
|
else:
|
|
files.append(entry)
|
|
return directories, files
|
|
|
|
def delete(self, name):
|
|
name = self._path(name)
|
|
try:
|
|
os.remove(name)
|
|
except OSError as e:
|
|
if e.errno != errno.ENOENT:
|
|
raise
|
|
|
|
def path(self, name):
|
|
raise NotImplementedError
|
|
|
|
|
|
class SimpleCachedStaticFilesStorage(CachedStaticFilesStorage):
|
|
|
|
def file_hash(self, name, content=None):
|
|
return 'deploy12345'
|
|
|
|
|
|
class ExtraPatternsCachedStaticFilesStorage(CachedStaticFilesStorage):
|
|
"""
|
|
A storage class to test pattern substitutions with more than one pattern
|
|
entry. The added pattern rewrites strings like "url(...)" to JS_URL("...").
|
|
"""
|
|
patterns = tuple(CachedStaticFilesStorage.patterns) + (
|
|
(
|
|
"*.js", (
|
|
(r"""(url\(['"]{0,1}\s*(.*?)["']{0,1}\))""", 'JS_URL("%s")'),
|
|
),
|
|
),
|
|
)
|