Skip to content

Commit

Permalink
Enable verbose output for compilestatic management command
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Fedoseev committed Feb 21, 2016
1 parent 76436bb commit d25761a
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 28 deletions.
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Dev
- Fix Stylus compiler to actually enable support for detecting changes in imported files
- Add ``precision`` option to SASS / SCSS / LibSass compilers. Set it to 8 or more if you compile Bootstrap.
- Add ``output_style`` option to SASS / SCSS / LibSass compilers.

- Enable verbose output for ``compilestatic`` management command

1.2
===
Expand Down
21 changes: 13 additions & 8 deletions static_precompiler/compilers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,16 @@ def get_source(self, source_path):
with open(self.get_full_source_path(source_path)) as source:
return source.read()

def compile(self, source_path, from_management=False):
def compile(self, source_path, from_management=False, verbosity=0):
""" Compile the given source path and return relative path to the compiled file.
Raise ValueError is the source file type is not supported.
May raise a StaticCompilationError if something goes wrong with compilation.
:param source_path: relative path to a source file
:type source_path: str
:param from_management: whether the method was invoked from management command
:type from_management: bool
:returns: str
:type verbosity: int
:rtype: str
"""
if not self.is_supported(source_path):
Expand All @@ -187,7 +187,12 @@ def compile(self, source_path, from_management=False):
if self.supports_dependencies:
self.update_dependencies(source_path, self.find_dependencies(source_path))

logging.info("Compiled: '{0}'".format(source_path))
message = "Compiled '{0}' to '{1}'".format(source_path, compiled_path)

if from_management and verbosity >= 1:
print(message)
else:
logging.info(message)

return compiled_path

Expand Down Expand Up @@ -298,13 +303,13 @@ def update_dependencies(self, source_path, dependencies):
depends_on=dependency,
)

def handle_changed_file(self, source_path):
def handle_changed_file(self, source_path, verbosity=0):
""" Handle the modification of the source file.
:param source_path: relative path to a source file
:type source_path: str
:type verbosity: int
"""
self.compile(source_path, from_management=True)
self.compile(source_path, from_management=True, verbosity=verbosity)
for dependent in self.get_dependents(source_path):
self.compile(dependent, from_management=True)
self.compile(dependent, from_management=True, verbosity=verbosity)
6 changes: 4 additions & 2 deletions static_precompiler/management/commands/compilestatic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@


def get_scanned_dirs():
dirs = set([settings.STATIC_ROOT])
dirs = set()
if settings.STATIC_ROOT:
dirs.add(settings.STATIC_ROOT)
for finder in django.contrib.staticfiles.finders.get_finders():
if hasattr(finder, "storages"):
for storage in finder.storages.values():
Expand Down Expand Up @@ -60,7 +62,7 @@ def handle_noargs(self, **options):
for compiler in compilers:
if compiler.is_supported(path):
try:
compiler.handle_changed_file(path)
compiler.handle_changed_file(path, verbosity=options["verbosity"])
except (exceptions.StaticCompilationError, ValueError) as e:
print(e)
break
Expand Down
1 change: 1 addition & 0 deletions static_precompiler/tests/compilestatic/coffee/test.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log "Hello, World!"
6 changes: 6 additions & 0 deletions static_precompiler/tests/compilestatic/less/test.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
p {
font-size: 15px;
a {
color: red;
}
}
3 changes: 3 additions & 0 deletions static_precompiler/tests/compilestatic/scss/_imported.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
h1 {
color: red;
}
8 changes: 8 additions & 0 deletions static_precompiler/tests/compilestatic/scss/test.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@import "imported";

p {
font-size: 15px;
a {
color: red;
}
}
1 change: 1 addition & 0 deletions static_precompiler/tests/django_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

# noinspection PyUnresolvedReferences
STATICFILES_DIRS = (
os.path.join(os.path.dirname(__file__), 'compilestatic'),
os.path.join(os.path.dirname(__file__), 'staticfiles_dir'),
("prefix", os.path.join(os.path.dirname(__file__), 'staticfiles_dir_with_prefix')),
)
Expand Down
5 changes: 5 additions & 0 deletions static_precompiler/tests/static/COMPILED/scripts/test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 24 additions & 17 deletions static_precompiler/tests/test_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,32 @@
import pytest
from django.core import management

from static_precompiler import settings
import static_precompiler.settings
from static_precompiler.management.commands import compilestatic


def test_get_scanned_dirs():

assert compilestatic.get_scanned_dirs() == sorted([
os.path.join(os.path.dirname(__file__), "compilestatic"),
os.path.join(os.path.dirname(__file__), "staticfiles_dir"),
os.path.join(os.path.dirname(__file__), "staticfiles_dir_with_prefix"),
settings.STATIC_ROOT
static_precompiler.settings.STATIC_ROOT,
])


@pytest.mark.django_db
def test_compilestatic_command(monkeypatch, tmpdir):
@pytest.mark.parametrize("verbosity", (0, 1, ))
def test_compilestatic_command(verbosity, capsys, monkeypatch, tmpdir):

monkeypatch.setattr("static_precompiler.management.commands.compilestatic.get_scanned_dirs", lambda: (
os.path.join(os.path.dirname(__file__), "compilestatic"),
))
monkeypatch.setattr("static_precompiler.settings.ROOT", tmpdir.strpath)

management.call_command("compilestatic")
management.call_command("compilestatic", verbosity=verbosity)

output_path = os.path.join(tmpdir.strpath, settings.OUTPUT_DIR)
output_path = os.path.join(tmpdir.strpath, static_precompiler.settings.OUTPUT_DIR)

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

assert compiled_files == [
"another_test.js",
"scripts/test.js",
"styles/less/imported.css",
"styles/less/test.css",
"styles/sass/precision.css",
"styles/sass/test.css",
"styles/stylus/A.css",
"styles/stylus/B/C.css",
"styles/stylus/D.css",
"styles/stylus/E/F.css",
"styles/stylus/E/index.css",
"test-compass.css",
"coffee/test.js",
"less/test.css",
"scss/test.css",
]

stdout, _ = capsys.readouterr()

if verbosity >= 1:
assert stdout == (
"Compiled 'coffee/test.coffee' to 'COMPILED/coffee/test.js'\n"
"Compiled 'less/test.less' to 'COMPILED/less/test.css'\n"
"Compiled 'scss/test.scss' to 'COMPILED/scss/test.css'\n"
)
else:
assert stdout == ""

0 comments on commit d25761a

Please sign in to comment.