diff --git a/README.rst b/README.rst index e66caed..9855f54 100644 --- a/README.rst +++ b/README.rst @@ -15,6 +15,7 @@ A faster collectstatic command. - ``storages.backends.s3boto.S3BotoStorage`` (deprecated) - ``storages.backends.s3boto3.S3Boto3Storage`` - ``storages.backends.gcloud.GoogleCloudStorage`` +- ``django.core.files.storage.FileSystemStorage`` Running Django's ``collectstatic`` command can become painfully slow as more and more files are added to a project, especially when heavy libraries such as @@ -54,15 +55,17 @@ Make sure you have this in your settings file and add ``'collectfast'`` to your Supported Strategies ~~~~~~~~~~~~~~~~~~~~ -+-----------------------------------------------------+-----------------------------------------------+ -|Strategy class |Storage class | -+=====================================================+===============================================+ -|``collectfast.strategies.boto.BotoStrategy`` |``storages.backends.s3boto.S3BotoStorage`` | -+-----------------------------------------------------+-----------------------------------------------+ -|``collectfast.strategies.boto3.Boto3Strategy`` |``storages.backends.s3boto3.S3Boto3Storage`` | -+-----------------------------------------------------+-----------------------------------------------+ -|``collectfast.strategies.gcloud.GoogleCloudStrategy``|``storages.backends.gcloud.GoogleCloudStorage``| -+-----------------------------------------------------+-----------------------------------------------+ ++--------------------------------------------------------+-----------------------------------------------+ +|Strategy class |Storage class | ++========================================================+===============================================+ +|``collectfast.strategies.boto.BotoStrategy`` |``storages.backends.s3boto.S3BotoStorage`` | ++--------------------------------------------------------+-----------------------------------------------+ +|``collectfast.strategies.boto3.Boto3Strategy`` |``storages.backends.s3boto3.S3Boto3Storage`` | ++--------------------------------------------------------+-----------------------------------------------+ +|``collectfast.strategies.gcloud.GoogleCloudStrategy`` |``storages.backends.gcloud.GoogleCloudStorage``| ++--------------------------------------------------------+-----------------------------------------------+ +|``collectfast.strategies.filesystem.FileSystemStrategy``|``django.core.files.storage.FileSystemStorage``| ++--------------------------------------------------------+-----------------------------------------------+ Usage diff --git a/collectfast/__init__.py b/collectfast/__init__.py index c68196d..67bc602 100644 --- a/collectfast/__init__.py +++ b/collectfast/__init__.py @@ -1 +1 @@ -__version__ = "1.2.0" +__version__ = "1.3.0" diff --git a/collectfast/strategies/filesystem.py b/collectfast/strategies/filesystem.py new file mode 100644 index 0000000..349f1fe --- /dev/null +++ b/collectfast/strategies/filesystem.py @@ -0,0 +1,14 @@ +from typing import Optional + +from django.core.files.storage import FileSystemStorage + +from .base import HashStrategy + + +class FileSystemStrategy(HashStrategy[FileSystemStorage]): + def get_remote_file_hash(self, prefixed_path): + # type: (str) -> Optional[str] + try: + return self.get_local_file_hash(prefixed_path, self.remote_storage) + except FileNotFoundError: + return None diff --git a/collectfast/tests/settings.py b/collectfast/tests/settings.py index af5781e..30f012f 100644 --- a/collectfast/tests/settings.py +++ b/collectfast/tests/settings.py @@ -25,6 +25,7 @@ INSTALLED_APPS = ("collectfast", "django.contrib.staticfiles") STATIC_URL = "/staticfiles/" STATIC_ROOT = str(base_path / "static_root") +MEDIA_ROOT = str(base_path / "fs_remote") STATICFILES_DIRS = [str(base_path / "static")] STATICFILES_STORAGE = "storages.backends.s3boto3.S3Boto3Storage" COLLECTFAST_STRATEGY = "collectfast.strategies.boto3.Boto3Strategy" diff --git a/collectfast/tests/test_command.py b/collectfast/tests/test_command.py index 9b8098f..f61573a 100644 --- a/collectfast/tests/test_command.py +++ b/collectfast/tests/test_command.py @@ -28,10 +28,14 @@ all_backend_confs = aws_backend_confs.copy() all_backend_confs.update( { - "google": override_django_settings( + "gcloud": override_django_settings( STATICFILES_STORAGE="storages.backends.gcloud.GoogleCloudStorage", COLLECTFAST_STRATEGY="collectfast.strategies.gcloud.GoogleCloudStrategy", - ) + ), + "filesystem": override_django_settings( + STATICFILES_STORAGE="django.core.files.storage.FileSystemStorage", + COLLECTFAST_STRATEGY="collectfast.strategies.filesystem.FileSystemStrategy", + ), } ) diff --git a/collectfast/tests/test_storages/filesystem_subtype.py b/collectfast/tests/test_storages/filesystem_subtype.py new file mode 100644 index 0000000..1292c89 --- /dev/null +++ b/collectfast/tests/test_storages/filesystem_subtype.py @@ -0,0 +1,10 @@ +import pathlib + +from django.core.files.storage import FileSystemStorage + + +class TestFileSystemStorage(FileSystemStorage): + def __init__(self): + # type: () -> None + output = str(pathlib.Path(__file__).parent / "fs-remote") + super().__init__(location=output) diff --git a/runtests.py b/runtests.py index d17ee0d..a3253b2 100755 --- a/runtests.py +++ b/runtests.py @@ -33,13 +33,16 @@ def main(): parent_dir, app_name = os.path.split(app_path) sys.path.insert(0, parent_dir) - # create static dir + # create static dirs staticfiles_dir = test_settings.STATICFILES_DIRS[0] staticroot_dir = test_settings.STATIC_ROOT + fs_remote = test_settings.MEDIA_ROOT if not os.path.exists(staticfiles_dir): os.makedirs(staticfiles_dir) if not os.path.exists(staticroot_dir): os.makedirs(staticroot_dir) + if not os.path.exists(fs_remote): + os.makedirs(fs_remote) if options.target is not None: test_arg = "%s.%s" % (app_name, options.target) @@ -54,6 +57,7 @@ def main(): # delete static dirs shutil.rmtree(staticfiles_dir) shutil.rmtree(staticroot_dir) + shutil.rmtree(fs_remote) if __name__ == "__main__":