Skip to content

Commit 5abfe31

Browse files
author
Andrey Fedoseev
committed
Add --ignore-dependencies option to compilestatic command
1 parent 01518e1 commit 5abfe31

File tree

6 files changed

+55
-38
lines changed

6 files changed

+55
-38
lines changed

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Dev
66
===
77

88
- Remove deprecated settings: ``COFFEESCRIPT_EXECUTABLE``, ``SCSS_EXECUTABLE``, ``SCSS_USE_COMPASS``, ``LESS_EXECUTABLE``
9+
- Add ``--ignore-dependencies`` option to ``compilestatic`` command
910

1011
1.7.1
1112
=====

docs/compilestatic-command.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ You can use this command in conjunction with ``STATIC_PRECOMPILER_DISABLE_AUTO_C
99
``STATICFILES_STORAGE`` such as S3 or some CDN. In that case you can should run ``compilestatic`` every time when your
1010
source files change and then run ``collectstatic``.
1111

12+
Sometimes it may be useful to prevent dependency tracking when running ``compilestatic``, for example when you don't
13+
have access to a database (building a Docker image). Use ``--ignore-dependencies`` option to disable the dependency
14+
tracking.
15+
1216
You can run ``compilestatic`` in watch mode (``--watch`` option). In watch mode it will monitor the changes in your
1317
source files and re-compile them on the fly. It can be handy if you use tools such as
1418
`LiveReload <http://livereload.com/>`_.

static_precompiler/compilers/base.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -301,14 +301,3 @@ def update_dependencies(self, source_path, dependencies):
301301
source=source_path,
302302
depends_on=dependency,
303303
)
304-
305-
def handle_changed_file(self, source_path, verbosity=0):
306-
""" Handle the modification of the source file.
307-
308-
:param source_path: relative path to a source file
309-
:type source_path: str
310-
:type verbosity: int
311-
"""
312-
self.compile(source_path, from_management=True, verbosity=verbosity)
313-
for dependent in self.get_dependents(source_path):
314-
self.compile(dependent, from_management=True, verbosity=verbosity)

static_precompiler/management/commands/compilestatic.py

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,27 +31,37 @@ def list_files(scanned_dirs):
3131
yield path
3232

3333

34+
ARGUMENTS = (
35+
("--ignore-dependencies", dict(
36+
action="store_true",
37+
dest="ignore_dependencies",
38+
default=False,
39+
help="Disable dependency tracking, this prevents any database access.",
40+
)),
41+
("--watch", dict(
42+
action="store_true",
43+
dest="watch",
44+
default=False,
45+
help="Watch for changes and recompile if necessary."
46+
)),
47+
("--no-initial-scan", dict(
48+
action="store_false",
49+
dest="initial_scan",
50+
default=True,
51+
help="Skip the initial scan of watched directories in --watch mode."
52+
)),
53+
)
54+
55+
3456
class Command(django.core.management.base.BaseCommand):
3557

3658
help = "Compile static files."
3759

3860
requires_system_checks = False
3961

4062
def add_arguments(self, parser):
41-
parser.add_argument(
42-
"--watch",
43-
action="store_true",
44-
dest="watch",
45-
default=False,
46-
help="Watch for changes and recompile if necessary."
47-
)
48-
parser.add_argument(
49-
"--no-initial-scan",
50-
action="store_false",
51-
dest="initial_scan",
52-
default=True,
53-
help="Skip the initial scan of watched directories in --watch mode."
54-
)
63+
for argument, parameters in ARGUMENTS:
64+
parser.add_argument(argument, **parameters)
5565

5666
def handle(self, **options):
5767

@@ -62,6 +72,10 @@ def handle(self, **options):
6272
verbosity = int(options["verbosity"])
6373
compilers = registry.get_compilers().values()
6474

75+
if options["ignore_dependencies"]:
76+
for compiler in compilers:
77+
compiler.supports_dependencies = False
78+
6579
if not options["watch"] or options["initial_scan"]:
6680
# Scan the watched directories and compile everything
6781
for path in sorted(set(list_files(scanned_dirs))):
@@ -75,7 +89,7 @@ def handle(self, **options):
7589
continue
7690

7791
try:
78-
compiler.handle_changed_file(path, verbosity=verbosity)
92+
compiler.compile(path, from_management=True, verbosity=verbosity)
7993
except (exceptions.StaticCompilationError, ValueError) as e:
8094
print(e)
8195

@@ -86,15 +100,6 @@ def handle(self, **options):
86100

87101
if django.VERSION < (1, 8):
88102
import optparse
89-
Command.option_list = django.core.management.base.NoArgsCommand.option_list + (
90-
optparse.make_option("--watch",
91-
action="store_true",
92-
dest="watch",
93-
default=False,
94-
help="Watch for changes and recompile if necessary."),
95-
optparse.make_option("--no-initial-scan",
96-
action="store_false",
97-
dest="initial_scan",
98-
default=True,
99-
help="Skip the initial scan of watched directories in --watch mode.")
103+
Command.option_list = django.core.management.base.NoArgsCommand.option_list + tuple(
104+
optparse.make_option(argument, **argument_parameters) for argument, argument_parameters in ARGUMENTS
100105
)

static_precompiler/tests/test_management.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,13 @@ def test_compilestatic_command(verbosity, capsys, monkeypatch, tmpdir):
5353
)
5454
else:
5555
assert stdout == ""
56+
57+
58+
@pytest.mark.skip("Re-enable when pytest-django>3.1.2 is released")
59+
@pytest.mark.django_db
60+
def test_ignore_dependencies_option(django_assert_num_queries, monkeypatch, tmpdir):
61+
62+
monkeypatch.setattr("static_precompiler.settings.ROOT", tmpdir.strpath)
63+
64+
with django_assert_num_queries(0):
65+
management.call_command("compilestatic", ignore_dependencies=True)

static_precompiler/watch.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import time
22

3+
from typing import * # noqa
34
from watchdog import events, observers
45

56
from . import exceptions, registry
7+
from .compilers import BaseCompiler # noqa
68

79

810
class EventHandler(events.FileSystemEventHandler):
911

12+
# noinspection PyShadowingNames
1013
def __init__(self, scanned_dir, verbosity, compilers):
14+
# type: (str, int, List[BaseCompiler]) -> None
1115
self.scanned_dir = scanned_dir
1216
self.verbosity = verbosity
1317
self.compilers = compilers
@@ -24,7 +28,11 @@ def on_any_event(self, e):
2428
if self.verbosity > 1:
2529
print("Modified: '{0}'".format(path))
2630
try:
27-
compiler.handle_changed_file(path)
31+
compiler.compile(path, from_management=True, verbosity=self.verbosity)
32+
if compiler.supports_dependencies:
33+
for dependent in compiler.get_dependents(path):
34+
compiler.compile(path, from_management=True, verbosity=self.verbosity)
35+
self.compile(dependent, from_management=True, verbosity=self.verbosity)
2836
except (exceptions.StaticCompilationError, ValueError) as e:
2937
print(e)
3038
break

0 commit comments

Comments
 (0)