Skip to content
This repository has been archived by the owner on Jan 16, 2023. It is now read-only.

Commit

Permalink
fix #159: don't load strategy when disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
antonagestam committed Oct 27, 2019
1 parent 7608d4f commit e979787
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 47 deletions.
2 changes: 1 addition & 1 deletion collectfast/__init__.py
@@ -1 +1 @@
__version__ = "1.3.0"
__version__ = "1.3.1"
25 changes: 17 additions & 8 deletions collectfast/management/commands/collectstatic.py
Expand Up @@ -2,6 +2,7 @@
from typing import Any
from typing import Dict
from typing import List
from typing import Optional
from typing import Tuple
from typing import Type

Expand All @@ -13,6 +14,7 @@

from collectfast import __version__
from collectfast import settings
from collectfast.strategies import DisabledStrategy
from collectfast.strategies import guess_strategy
from collectfast.strategies import load_strategy
from collectfast.strategies import Strategy
Expand All @@ -27,7 +29,7 @@ def __init__(self, *args, **kwargs):
self.num_copied_files = 0
self.tasks = [] # type: List[Task]
self.collectfast_enabled = settings.enabled
self.strategy = self._load_strategy()(self.storage)
self.strategy = DisabledStrategy(Storage()) # type: Strategy

@staticmethod
def _load_strategy():
Expand Down Expand Up @@ -62,8 +64,11 @@ def add_arguments(self, parser):
def set_options(self, **options):
# type: (Any) -> None
"""Set options and handle deprecation."""
disable = options.pop("disable_collectfast", False)
self.collectfast_enabled = not disable
self.collectfast_enabled = self.collectfast_enabled and not options.pop(
"disable_collectfast"
)
if self.collectfast_enabled:
self.strategy = self._load_strategy()(self.storage)
super().set_options(**options)

def collect(self):
Expand All @@ -73,14 +78,18 @@ def collect(self):
Command.copy_file() which is called by super().collect().
"""
ret = super().collect()
if not self.collectfast_enabled:
return ret
if settings.threads:
Pool(settings.threads).map(self.maybe_copy_file, self.tasks)
return ret

def handle(self, *args, **options):
# type: (Any, Any) -> str
# type: (Any, Any) -> Optional[str]
"""Override handle to suppress summary output."""
super().handle(**options)
ret = super().handle(**options)
if not self.collectfast_enabled:
return ret
return "{} static file{} copied.".format(
self.num_copied_files, "" if self.num_copied_files == 1 else "s"
)
Expand All @@ -90,9 +99,9 @@ def maybe_copy_file(self, args):
"""Determine if file should be copied or not and handle exceptions."""
path, prefixed_path, source_storage = args

self.strategy.pre_should_copy_hook()

if self.collectfast_enabled and not self.dry_run:
self.strategy.pre_should_copy_hook()

if not self.strategy.should_copy_file(path, prefixed_path, source_storage):
self.log("Skipping '%s'" % path)
return
Expand All @@ -107,7 +116,7 @@ def copy_file(self, path, prefixed_path, source_storage):
file with a blocking call.
"""
args = (path, prefixed_path, source_storage)
if settings.threads:
if settings.threads and self.collectfast_enabled:
self.tasks.append(args)
else:
self.maybe_copy_file(args)
Expand Down
3 changes: 2 additions & 1 deletion collectfast/strategies/__init__.py
@@ -1,5 +1,6 @@
from .base import DisabledStrategy
from .base import guess_strategy
from .base import load_strategy
from .base import Strategy

__all__ = ("load_strategy", "guess_strategy", "Strategy")
__all__ = ("load_strategy", "guess_strategy", "Strategy", "DisabledStrategy")
11 changes: 11 additions & 0 deletions collectfast/strategies/base.py
Expand Up @@ -9,6 +9,7 @@
from io import BytesIO
from typing import ClassVar
from typing import Generic
from typing import NoReturn
from typing import Optional
from typing import Tuple
from typing import Type
Expand Down Expand Up @@ -138,6 +139,16 @@ def get_gzipped_local_file_hash(self, uncompressed_file_hash, path, contents):
return str(file_hash)


class DisabledStrategy(Strategy):
def should_copy_file(self, path, prefixed_path, local_storage):
# type: (str, str, Storage) -> NoReturn
raise NotImplementedError

def pre_should_copy_hook(self):
# type: () -> NoReturn
raise NotImplementedError


def load_strategy(klass: Union[str, type, object]) -> Type[Strategy[Storage]]:
if isinstance(klass, str):
klass = pydoc.locate(klass)
Expand Down
Empty file.
@@ -1,18 +1,17 @@
from io import StringIO
from typing import Any
from unittest import TestCase

from django.core.exceptions import ImproperlyConfigured
from django.core.management import call_command
from django.test import override_settings as override_django_settings

from .utils import clean_static_dir
from .utils import create_static_file
from .utils import make_test
from .utils import override_setting
from .utils import override_storage_attr
from .utils import test_many
from .utils import call_collectstatic
from collectfast.management.commands.collectstatic import Command
from collectfast.tests.utils import clean_static_dir
from collectfast.tests.utils import create_static_file
from collectfast.tests.utils import make_test
from collectfast.tests.utils import override_setting
from collectfast.tests.utils import override_storage_attr
from collectfast.tests.utils import test_many


aws_backend_confs = {
"boto3": override_django_settings(
Expand Down Expand Up @@ -43,15 +42,6 @@
make_test_all_backends = test_many(**all_backend_confs)


def call_collectstatic(*args, **kwargs):
# type: (Any, Any) -> str
out = StringIO()
call_command(
"collectstatic", *args, verbosity=3, interactive=False, stdout=out, **kwargs
)
return out.getvalue()


@make_test_all_backends
def test_basics(case):
# type: (TestCase) -> None
Expand All @@ -73,25 +63,6 @@ def test_threads(case):
case.assertIn("0 static files copied.", call_collectstatic())


@make_test
@override_django_settings(
STATICFILES_STORAGE="django.contrib.staticfiles.storage.StaticFilesStorage"
)
def test_disable_collectfast_with_default_storage(case):
# type: (TestCase) -> None
clean_static_dir()
create_static_file()
case.assertIn("1 static file copied.", call_collectstatic(disable_collectfast=True))


@make_test
def test_disable_collectfast(case):
# type: (TestCase) -> None
clean_static_dir()
create_static_file()
case.assertIn("1 static file copied.", call_collectstatic(disable_collectfast=True))


@make_test
def test_dry_run(case):
# type: (TestCase) -> None
Expand Down
46 changes: 46 additions & 0 deletions collectfast/tests/command/test_disable.py
@@ -0,0 +1,46 @@
from unittest import mock
from unittest import TestCase

from django.test import override_settings as override_django_settings

from .utils import call_collectstatic
from collectfast.tests.utils import clean_static_dir
from collectfast.tests.utils import create_static_file
from collectfast.tests.utils import make_test
from collectfast.tests.utils import override_setting


@make_test
@override_django_settings(
STATICFILES_STORAGE="django.contrib.staticfiles.storage.StaticFilesStorage"
)
def test_disable_collectfast_with_default_storage(case):
# type: (TestCase) -> None
clean_static_dir()
create_static_file()
case.assertIn("1 static file copied", call_collectstatic(disable_collectfast=True))


@make_test
def test_disable_collectfast(case):
# type: (TestCase) -> None
clean_static_dir()
create_static_file()
case.assertIn("1 static file copied.", call_collectstatic(disable_collectfast=True))


@override_setting("enabled", False)
@mock.patch("collectfast.management.commands.collectstatic.Command._load_strategy")
def test_no_load_with_disable_setting(mocked_load_strategy):
# type: (mock.MagicMock) -> None
clean_static_dir()
call_collectstatic()
mocked_load_strategy.assert_not_called()


@mock.patch("collectfast.management.commands.collectstatic.Command._load_strategy")
def test_no_load_with_disable_flag(mocked_load_strategy):
# type: (mock.MagicMock) -> None
clean_static_dir()
call_collectstatic(disable_collectfast=True)
mocked_load_strategy.assert_not_called()
13 changes: 13 additions & 0 deletions collectfast/tests/command/utils.py
@@ -0,0 +1,13 @@
from io import StringIO
from typing import Any

from django.core.management import call_command


def call_collectstatic(*args, **kwargs):
# type: (Any, Any) -> str
out = StringIO()
call_command(
"collectstatic", *args, verbosity=3, interactive=False, stdout=out, **kwargs
)
return out.getvalue()

0 comments on commit e979787

Please sign in to comment.