diff --git a/CHANGES.rst b/CHANGES.rst index a2c447e..98d28fd 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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 === diff --git a/static_precompiler/compilers/base.py b/static_precompiler/compilers/base.py index 5d6b54b..51461cc 100644 --- a/static_precompiler/compilers/base.py +++ b/static_precompiler/compilers/base.py @@ -161,7 +161,7 @@ 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. @@ -169,8 +169,8 @@ def compile(self, source_path, from_management=False): :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): @@ -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 @@ -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) diff --git a/static_precompiler/management/commands/compilestatic.py b/static_precompiler/management/commands/compilestatic.py index a9cabee..00abb0b 100644 --- a/static_precompiler/management/commands/compilestatic.py +++ b/static_precompiler/management/commands/compilestatic.py @@ -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(): @@ -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 diff --git a/static_precompiler/tests/compilestatic/coffee/test.coffee b/static_precompiler/tests/compilestatic/coffee/test.coffee new file mode 100644 index 0000000..fe8d71b --- /dev/null +++ b/static_precompiler/tests/compilestatic/coffee/test.coffee @@ -0,0 +1 @@ +console.log "Hello, World!" \ No newline at end of file diff --git a/static_precompiler/tests/compilestatic/less/test.less b/static_precompiler/tests/compilestatic/less/test.less new file mode 100644 index 0000000..6e8ce42 --- /dev/null +++ b/static_precompiler/tests/compilestatic/less/test.less @@ -0,0 +1,6 @@ +p { + font-size: 15px; + a { + color: red; + } +} diff --git a/static_precompiler/tests/compilestatic/scss/_imported.scss b/static_precompiler/tests/compilestatic/scss/_imported.scss new file mode 100644 index 0000000..adc68fa --- /dev/null +++ b/static_precompiler/tests/compilestatic/scss/_imported.scss @@ -0,0 +1,3 @@ +h1 { + color: red; +} diff --git a/static_precompiler/tests/compilestatic/scss/test.scss b/static_precompiler/tests/compilestatic/scss/test.scss new file mode 100644 index 0000000..1bc1848 --- /dev/null +++ b/static_precompiler/tests/compilestatic/scss/test.scss @@ -0,0 +1,8 @@ +@import "imported"; + +p { + font-size: 15px; + a { + color: red; + } +} diff --git a/static_precompiler/tests/django_settings.py b/static_precompiler/tests/django_settings.py index a61dfd1..bdc15b0 100644 --- a/static_precompiler/tests/django_settings.py +++ b/static_precompiler/tests/django_settings.py @@ -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')), ) diff --git a/static_precompiler/tests/static/COMPILED/scripts/test.js b/static_precompiler/tests/static/COMPILED/scripts/test.js new file mode 100644 index 0000000..4acd66d --- /dev/null +++ b/static_precompiler/tests/static/COMPILED/scripts/test.js @@ -0,0 +1,5 @@ +// Generated by CoffeeScript 1.7.1 +(function() { + console.log("Hello, World!"); + +}).call(this); diff --git a/static_precompiler/tests/test_management.py b/static_precompiler/tests/test_management.py index 861b590..6009eb8 100644 --- a/static_precompiler/tests/test_management.py +++ b/static_precompiler/tests/test_management.py @@ -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): @@ -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 == ""