From 393c268e725f5b229ecb554f3fac02cfc250d2df Mon Sep 17 00:00:00 2001 From: Matt Robenolt Date: Thu, 31 Jan 2013 18:36:34 -0800 Subject: [PATCH] Fixed #19715 -- Simplified findstatic output when verbosity set to 0 --- AUTHORS | 1 + .../management/commands/findstatic.py | 13 ++++++++----- docs/ref/contrib/staticfiles.txt | 18 ++++++++++++++---- .../regressiontests/staticfiles_tests/tests.py | 18 +++++++++++++++--- 4 files changed, 38 insertions(+), 12 deletions(-) diff --git a/AUTHORS b/AUTHORS index 16bfa574c92..8eb500d3e18 100644 --- a/AUTHORS +++ b/AUTHORS @@ -464,6 +464,7 @@ answer newbie questions, and generally made Django that much better: Mike Richardson Matt Riggott Alex Robbins + Matt Robenolt Henrique Romano Armin Ronacher Daniel Roseman diff --git a/django/contrib/staticfiles/management/commands/findstatic.py b/django/contrib/staticfiles/management/commands/findstatic.py index dc1e88d7780..eaf63f7179d 100644 --- a/django/contrib/staticfiles/management/commands/findstatic.py +++ b/django/contrib/staticfiles/management/commands/findstatic.py @@ -3,7 +3,7 @@ from __future__ import unicode_literals import os from optparse import make_option from django.core.management.base import LabelCommand -from django.utils.encoding import smart_text +from django.utils.encoding import force_text from django.contrib.staticfiles import finders @@ -19,13 +19,16 @@ class Command(LabelCommand): def handle_label(self, path, **options): verbosity = int(options.get('verbosity', 1)) result = finders.find(path, all=options['all']) - path = smart_text(path) + path = force_text(path) if result: if not isinstance(result, (list, tuple)): result = [result] - output = '\n '.join( - (smart_text(os.path.realpath(path)) for path in result)) - self.stdout.write("Found '%s' here:\n %s" % (path, output)) + result = (force_text(os.path.realpath(path)) for path in result) + if verbosity >= 1: + output = '\n '.join(result) + return "Found '%s' here:\n %s" % (path, output) + else: + return '\n'.join(result) else: if verbosity >= 1: self.stderr.write("No matching file found for '%s'." % path) diff --git a/docs/ref/contrib/staticfiles.txt b/docs/ref/contrib/staticfiles.txt index a7540388bc8..fa740f4e2c1 100644 --- a/docs/ref/contrib/staticfiles.txt +++ b/docs/ref/contrib/staticfiles.txt @@ -112,19 +112,29 @@ Searches for one or more relative paths with the enabled finders. For example:: $ python manage.py findstatic css/base.css admin/js/core.js - /home/special.polls.com/core/static/css/base.css - /home/polls.com/core/static/css/base.css - /home/polls.com/src/django/contrib/admin/media/js/core.js + Found 'css/base.css' here: + /home/special.polls.com/core/static/css/base.css + /home/polls.com/core/static/css/base.css + Found 'admin/js/core.js' here: + /home/polls.com/src/django/contrib/admin/media/js/core.js By default, all matching locations are found. To only return the first match for each relative path, use the ``--first`` option:: $ python manage.py findstatic css/base.css --first - /home/special.polls.com/core/static/css/base.css + Found 'css/base.css' here: + /home/special.polls.com/core/static/css/base.css This is a debugging aid; it'll show you exactly which static file will be collected for a given path. +By setting the :djadminopt:`--verbosity` flag to 0, you can suppress the extra +output and just get the path names:: + + $ python manage.py findstatic css/base.css --verbosity 0 + /home/special.polls.com/core/static/css/base.css + /home/polls.com/core/static/css/base.css + .. _staticfiles-runserver: runserver diff --git a/tests/regressiontests/staticfiles_tests/tests.py b/tests/regressiontests/staticfiles_tests/tests.py index 90c8621d0b5..35d5683a0cd 100644 --- a/tests/regressiontests/staticfiles_tests/tests.py +++ b/tests/regressiontests/staticfiles_tests/tests.py @@ -195,21 +195,33 @@ class TestFindStatic(CollectionTestCase, TestDefaults): call_command('findstatic', filepath, all=False, verbosity=0, stdout=out) out.seek(0) lines = [l.strip() for l in out.readlines()] - with codecs.open(force_text(lines[1].strip()), "r", "utf-8") as f: + with codecs.open(force_text(lines[0].strip()), "r", "utf-8") as f: return f.read() def test_all_files(self): """ - Test that findstatic returns all candidate files if run without --first. + Test that findstatic returns all candidate files if run without --first and -v1. """ out = six.StringIO() - call_command('findstatic', 'test/file.txt', verbosity=0, stdout=out) + call_command('findstatic', 'test/file.txt', verbosity=1, stdout=out) out.seek(0) lines = [l.strip() for l in out.readlines()] self.assertEqual(len(lines), 3) # three because there is also the "Found here" line self.assertIn('project', force_text(lines[1])) self.assertIn('apps', force_text(lines[2])) + def test_all_files_less_verbose(self): + """ + Test that findstatic returns all candidate files if run without --first and -v0. + """ + out = six.StringIO() + call_command('findstatic', 'test/file.txt', verbosity=0, stdout=out) + out.seek(0) + lines = [l.strip() for l in out.readlines()] + self.assertEqual(len(lines), 2) + self.assertIn('project', force_text(lines[0])) + self.assertIn('apps', force_text(lines[1])) + class TestCollection(CollectionTestCase, TestDefaults): """