2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2010-10-20 09:33:24 +08:00
|
|
|
import os
|
|
|
|
|
|
|
|
from django.contrib.staticfiles import finders
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.core.management.base import LabelCommand
|
|
|
|
from django.utils.encoding import force_text
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2010-10-20 09:33:24 +08:00
|
|
|
class Command(LabelCommand):
|
|
|
|
help = "Finds the absolute paths for the given static file(s)."
|
|
|
|
label = 'static file'
|
2014-06-07 04:39:33 +08:00
|
|
|
|
|
|
|
def add_arguments(self, parser):
|
|
|
|
super(Command, self).add_arguments(parser)
|
|
|
|
parser.add_argument('--first', action='store_false', dest='all',
|
|
|
|
default=True,
|
|
|
|
help="Only return the first match for each static file.")
|
2010-10-20 09:33:24 +08:00
|
|
|
|
|
|
|
def handle_label(self, path, **options):
|
2014-06-07 04:39:33 +08:00
|
|
|
verbosity = options['verbosity']
|
2010-10-20 09:33:24 +08:00
|
|
|
result = finders.find(path, all=options['all'])
|
2013-02-01 10:36:34 +08:00
|
|
|
path = force_text(path)
|
2013-11-16 22:55:50 +08:00
|
|
|
if verbosity >= 2:
|
|
|
|
searched_locations = ("Looking in the following locations:\n %s" %
|
|
|
|
"\n ".join(force_text(location)
|
|
|
|
for location in finders.searched_locations))
|
|
|
|
else:
|
|
|
|
searched_locations = ''
|
2010-10-20 09:33:24 +08:00
|
|
|
if result:
|
2010-10-23 22:32:54 +08:00
|
|
|
if not isinstance(result, (list, tuple)):
|
|
|
|
result = [result]
|
2013-02-01 10:36:34 +08:00
|
|
|
result = (force_text(os.path.realpath(path)) for path in result)
|
|
|
|
if verbosity >= 1:
|
2013-11-16 22:55:50 +08:00
|
|
|
file_list = '\n '.join(result)
|
|
|
|
return ("Found '%s' here:\n %s\n%s" %
|
|
|
|
(path, file_list, searched_locations))
|
2013-02-01 10:36:34 +08:00
|
|
|
else:
|
|
|
|
return '\n'.join(result)
|
2010-10-20 09:33:24 +08:00
|
|
|
else:
|
2013-11-16 22:55:50 +08:00
|
|
|
message = ["No matching file found for '%s'." % path]
|
|
|
|
if verbosity >= 2:
|
|
|
|
message.append(searched_locations)
|
2010-10-20 09:33:24 +08:00
|
|
|
if verbosity >= 1:
|
2013-11-16 22:55:50 +08:00
|
|
|
self.stderr.write('\n'.join(message))
|