Skip to content

Commit d25761a

Browse files
author
Andrey Fedoseev
committed
Enable verbose output for compilestatic management command
1 parent 76436bb commit d25761a

File tree

10 files changed

+66
-28
lines changed

10 files changed

+66
-28
lines changed

CHANGES.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Dev
88
- Fix Stylus compiler to actually enable support for detecting changes in imported files
99
- Add ``precision`` option to SASS / SCSS / LibSass compilers. Set it to 8 or more if you compile Bootstrap.
1010
- Add ``output_style`` option to SASS / SCSS / LibSass compilers.
11-
11+
- Enable verbose output for ``compilestatic`` management command
1212

1313
1.2
1414
===

static_precompiler/compilers/base.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,16 @@ def get_source(self, source_path):
161161
with open(self.get_full_source_path(source_path)) as source:
162162
return source.read()
163163

164-
def compile(self, source_path, from_management=False):
164+
def compile(self, source_path, from_management=False, verbosity=0):
165165
""" Compile the given source path and return relative path to the compiled file.
166166
Raise ValueError is the source file type is not supported.
167167
May raise a StaticCompilationError if something goes wrong with compilation.
168168
:param source_path: relative path to a source file
169169
:type source_path: str
170170
:param from_management: whether the method was invoked from management command
171171
:type from_management: bool
172-
173-
:returns: str
172+
:type verbosity: int
173+
:rtype: str
174174
175175
"""
176176
if not self.is_supported(source_path):
@@ -187,7 +187,12 @@ def compile(self, source_path, from_management=False):
187187
if self.supports_dependencies:
188188
self.update_dependencies(source_path, self.find_dependencies(source_path))
189189

190-
logging.info("Compiled: '{0}'".format(source_path))
190+
message = "Compiled '{0}' to '{1}'".format(source_path, compiled_path)
191+
192+
if from_management and verbosity >= 1:
193+
print(message)
194+
else:
195+
logging.info(message)
191196

192197
return compiled_path
193198

@@ -298,13 +303,13 @@ def update_dependencies(self, source_path, dependencies):
298303
depends_on=dependency,
299304
)
300305

301-
def handle_changed_file(self, source_path):
306+
def handle_changed_file(self, source_path, verbosity=0):
302307
""" Handle the modification of the source file.
303308
304309
:param source_path: relative path to a source file
305310
:type source_path: str
306-
311+
:type verbosity: int
307312
"""
308-
self.compile(source_path, from_management=True)
313+
self.compile(source_path, from_management=True, verbosity=verbosity)
309314
for dependent in self.get_dependents(source_path):
310-
self.compile(dependent, from_management=True)
315+
self.compile(dependent, from_management=True, verbosity=verbosity)

static_precompiler/management/commands/compilestatic.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111

1212
def get_scanned_dirs():
13-
dirs = set([settings.STATIC_ROOT])
13+
dirs = set()
14+
if settings.STATIC_ROOT:
15+
dirs.add(settings.STATIC_ROOT)
1416
for finder in django.contrib.staticfiles.finders.get_finders():
1517
if hasattr(finder, "storages"):
1618
for storage in finder.storages.values():
@@ -60,7 +62,7 @@ def handle_noargs(self, **options):
6062
for compiler in compilers:
6163
if compiler.is_supported(path):
6264
try:
63-
compiler.handle_changed_file(path)
65+
compiler.handle_changed_file(path, verbosity=options["verbosity"])
6466
except (exceptions.StaticCompilationError, ValueError) as e:
6567
print(e)
6668
break
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log "Hello, World!"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
p {
2+
font-size: 15px;
3+
a {
4+
color: red;
5+
}
6+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
h1 {
2+
color: red;
3+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@import "imported";
2+
3+
p {
4+
font-size: 15px;
5+
a {
6+
color: red;
7+
}
8+
}

static_precompiler/tests/django_settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
# noinspection PyUnresolvedReferences
1717
STATICFILES_DIRS = (
18+
os.path.join(os.path.dirname(__file__), 'compilestatic'),
1819
os.path.join(os.path.dirname(__file__), 'staticfiles_dir'),
1920
("prefix", os.path.join(os.path.dirname(__file__), 'staticfiles_dir_with_prefix')),
2021
)

static_precompiler/tests/static/COMPILED/scripts/test.js

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

static_precompiler/tests/test_management.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,32 @@
33
import pytest
44
from django.core import management
55

6-
from static_precompiler import settings
6+
import static_precompiler.settings
77
from static_precompiler.management.commands import compilestatic
88

99

1010
def test_get_scanned_dirs():
1111

1212
assert compilestatic.get_scanned_dirs() == sorted([
13+
os.path.join(os.path.dirname(__file__), "compilestatic"),
1314
os.path.join(os.path.dirname(__file__), "staticfiles_dir"),
1415
os.path.join(os.path.dirname(__file__), "staticfiles_dir_with_prefix"),
15-
settings.STATIC_ROOT
16+
static_precompiler.settings.STATIC_ROOT,
1617
])
1718

1819

1920
@pytest.mark.django_db
20-
def test_compilestatic_command(monkeypatch, tmpdir):
21+
@pytest.mark.parametrize("verbosity", (0, 1, ))
22+
def test_compilestatic_command(verbosity, capsys, monkeypatch, tmpdir):
2123

24+
monkeypatch.setattr("static_precompiler.management.commands.compilestatic.get_scanned_dirs", lambda: (
25+
os.path.join(os.path.dirname(__file__), "compilestatic"),
26+
))
2227
monkeypatch.setattr("static_precompiler.settings.ROOT", tmpdir.strpath)
2328

24-
management.call_command("compilestatic")
29+
management.call_command("compilestatic", verbosity=verbosity)
2530

26-
output_path = os.path.join(tmpdir.strpath, settings.OUTPUT_DIR)
31+
output_path = os.path.join(tmpdir.strpath, static_precompiler.settings.OUTPUT_DIR)
2732

2833
compiled_files = []
2934
for root, dirs, files in os.walk(output_path):
@@ -33,16 +38,18 @@ def test_compilestatic_command(monkeypatch, tmpdir):
3338
compiled_files.sort()
3439

3540
assert compiled_files == [
36-
"another_test.js",
37-
"scripts/test.js",
38-
"styles/less/imported.css",
39-
"styles/less/test.css",
40-
"styles/sass/precision.css",
41-
"styles/sass/test.css",
42-
"styles/stylus/A.css",
43-
"styles/stylus/B/C.css",
44-
"styles/stylus/D.css",
45-
"styles/stylus/E/F.css",
46-
"styles/stylus/E/index.css",
47-
"test-compass.css",
41+
"coffee/test.js",
42+
"less/test.css",
43+
"scss/test.css",
4844
]
45+
46+
stdout, _ = capsys.readouterr()
47+
48+
if verbosity >= 1:
49+
assert stdout == (
50+
"Compiled 'coffee/test.coffee' to 'COMPILED/coffee/test.js'\n"
51+
"Compiled 'less/test.less' to 'COMPILED/less/test.css'\n"
52+
"Compiled 'scss/test.scss' to 'COMPILED/scss/test.css'\n"
53+
)
54+
else:
55+
assert stdout == ""

0 commit comments

Comments
 (0)