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
|
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)."
|
2016-05-12 22:41:53 +08:00
|
|
|
label = 'staticfile'
|
2014-06-07 04:39:33 +08:00
|
|
|
|
|
|
|
def add_arguments(self, parser):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().add_arguments(parser)
|
2016-03-29 06:33:29 +08:00
|
|
|
parser.add_argument(
|
|
|
|
'--first', action='store_false', dest='all',
|
|
|
|
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-11-16 22:55:50 +08:00
|
|
|
if verbosity >= 2:
|
2016-03-29 06:33:29 +08:00
|
|
|
searched_locations = (
|
|
|
|
"\nLooking in the following locations:\n %s" %
|
2019-11-07 17:26:22 +08:00
|
|
|
"\n ".join([str(loc) for loc in finders.searched_locations])
|
2016-03-29 06:33:29 +08:00
|
|
|
)
|
2013-11-16 22:55:50 +08:00
|
|
|
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]
|
2017-03-04 22:47:49 +08:00
|
|
|
result = (os.path.realpath(path) for path in result)
|
2013-02-01 10:36:34 +08:00
|
|
|
if verbosity >= 1:
|
2013-11-16 22:55:50 +08:00
|
|
|
file_list = '\n '.join(result)
|
2016-02-14 01:20:29 +08:00
|
|
|
return ("Found '%s' here:\n %s%s" %
|
2013-11-16 22:55:50 +08:00
|
|
|
(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))
|