2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2010-10-20 09:33:24 +08:00
|
|
|
import os
|
|
|
|
import sys
|
2013-08-03 13:41:15 +08:00
|
|
|
from collections import OrderedDict
|
2010-10-20 09:33:24 +08:00
|
|
|
from optparse import make_option
|
|
|
|
|
2011-08-11 22:07:39 +08:00
|
|
|
from django.core.files.storage import FileSystemStorage
|
2010-10-20 09:33:24 +08:00
|
|
|
from django.core.management.base import CommandError, NoArgsCommand
|
2012-07-21 16:00:10 +08:00
|
|
|
from django.utils.encoding import smart_text
|
2012-08-09 01:08:05 +08:00
|
|
|
from django.utils.six.moves import input
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2011-08-11 22:07:39 +08:00
|
|
|
from django.contrib.staticfiles import finders, storage
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2011-07-08 17:39:56 +08:00
|
|
|
|
2010-10-20 09:33:24 +08:00
|
|
|
class Command(NoArgsCommand):
|
|
|
|
"""
|
2012-02-13 18:51:17 +08:00
|
|
|
Command that allows to copy or symlink static files from different
|
2010-11-17 23:36:26 +08:00
|
|
|
locations to the settings.STATIC_ROOT.
|
2010-10-20 09:33:24 +08:00
|
|
|
"""
|
|
|
|
option_list = NoArgsCommand.option_list + (
|
2011-08-11 22:07:39 +08:00
|
|
|
make_option('--noinput',
|
|
|
|
action='store_false', dest='interactive', default=True,
|
|
|
|
help="Do NOT prompt the user for input of any kind."),
|
|
|
|
make_option('--no-post-process',
|
|
|
|
action='store_false', dest='post_process', default=True,
|
|
|
|
help="Do NOT post process collected files."),
|
2010-10-20 09:33:24 +08:00
|
|
|
make_option('-i', '--ignore', action='append', default=[],
|
|
|
|
dest='ignore_patterns', metavar='PATTERN',
|
|
|
|
help="Ignore files or directories matching this glob-style "
|
|
|
|
"pattern. Use multiple times to ignore more."),
|
2011-08-11 22:07:39 +08:00
|
|
|
make_option('-n', '--dry-run',
|
|
|
|
action='store_true', dest='dry_run', default=False,
|
|
|
|
help="Do everything except modify the filesystem."),
|
|
|
|
make_option('-c', '--clear',
|
|
|
|
action='store_true', dest='clear', default=False,
|
|
|
|
help="Clear the existing files using the storage "
|
|
|
|
"before trying to copy or link the original file."),
|
|
|
|
make_option('-l', '--link',
|
|
|
|
action='store_true', dest='link', default=False,
|
|
|
|
help="Create a symbolic link to each file instead of copying."),
|
2010-10-20 09:33:24 +08:00
|
|
|
make_option('--no-default-ignore', action='store_false',
|
|
|
|
dest='use_default_ignore_patterns', default=True,
|
|
|
|
help="Don't ignore the common private glob-style patterns 'CVS', "
|
|
|
|
"'.*' and '*~'."),
|
|
|
|
)
|
2011-08-11 22:07:39 +08:00
|
|
|
help = "Collect static files in a single location."
|
2012-02-12 22:49:42 +08:00
|
|
|
requires_model_validation = False
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2011-01-08 18:03:27 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
2011-01-20 00:36:20 +08:00
|
|
|
super(NoArgsCommand, self).__init__(*args, **kwargs)
|
2011-02-02 03:19:52 +08:00
|
|
|
self.copied_files = []
|
|
|
|
self.symlinked_files = []
|
|
|
|
self.unmodified_files = []
|
2012-02-13 18:51:17 +08:00
|
|
|
self.post_processed_files = []
|
2011-08-11 22:07:39 +08:00
|
|
|
self.storage = storage.staticfiles_storage
|
2010-10-20 09:33:24 +08:00
|
|
|
try:
|
2011-02-02 03:19:52 +08:00
|
|
|
self.storage.path('')
|
2010-10-20 09:33:24 +08:00
|
|
|
except NotImplementedError:
|
2011-02-02 03:19:52 +08:00
|
|
|
self.local = False
|
2010-10-20 09:33:24 +08:00
|
|
|
else:
|
2011-02-02 03:19:52 +08:00
|
|
|
self.local = True
|
2011-01-08 18:03:27 +08:00
|
|
|
|
2012-02-13 18:51:17 +08:00
|
|
|
def set_options(self, **options):
|
|
|
|
"""
|
|
|
|
Set instance variables based on an options dict
|
|
|
|
"""
|
|
|
|
self.interactive = options['interactive']
|
|
|
|
self.verbosity = int(options.get('verbosity', 1))
|
|
|
|
self.symlink = options['link']
|
2011-07-05 05:34:29 +08:00
|
|
|
self.clear = options['clear']
|
|
|
|
self.dry_run = options['dry_run']
|
2011-01-08 18:03:27 +08:00
|
|
|
ignore_patterns = options['ignore_patterns']
|
|
|
|
if options['use_default_ignore_patterns']:
|
|
|
|
ignore_patterns += ['CVS', '.*', '*~']
|
2011-07-05 05:34:29 +08:00
|
|
|
self.ignore_patterns = list(set(ignore_patterns))
|
2011-08-11 22:07:39 +08:00
|
|
|
self.post_process = options['post_process']
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2012-02-13 18:51:17 +08:00
|
|
|
def collect(self):
|
|
|
|
"""
|
|
|
|
Perform the bulk of the work of collectstatic.
|
|
|
|
|
|
|
|
Split off from handle_noargs() to facilitate testing.
|
|
|
|
"""
|
2011-07-05 05:34:29 +08:00
|
|
|
if self.symlink:
|
2010-10-20 09:33:24 +08:00
|
|
|
if sys.platform == 'win32':
|
|
|
|
raise CommandError("Symlinking is not supported by this "
|
|
|
|
"platform (%s)." % sys.platform)
|
2011-02-02 03:19:52 +08:00
|
|
|
if not self.local:
|
2010-10-20 09:33:24 +08:00
|
|
|
raise CommandError("Can't symlink to a remote destination.")
|
|
|
|
|
2012-02-13 18:51:17 +08:00
|
|
|
if self.clear:
|
|
|
|
self.clear_dir('')
|
|
|
|
|
|
|
|
if self.symlink:
|
|
|
|
handler = self.link_file
|
|
|
|
else:
|
|
|
|
handler = self.copy_file
|
|
|
|
|
2013-08-03 13:41:15 +08:00
|
|
|
found_files = OrderedDict()
|
2012-02-13 18:51:17 +08:00
|
|
|
for finder in finders.get_finders():
|
|
|
|
for path, storage in finder.list(self.ignore_patterns):
|
|
|
|
# Prefix the relative path if the source storage contains it
|
|
|
|
if getattr(storage, 'prefix', None):
|
|
|
|
prefixed_path = os.path.join(storage.prefix, path)
|
|
|
|
else:
|
|
|
|
prefixed_path = path
|
2012-03-02 07:02:38 +08:00
|
|
|
|
|
|
|
if prefixed_path not in found_files:
|
|
|
|
found_files[prefixed_path] = (storage, path)
|
|
|
|
handler(path, prefixed_path, storage)
|
2012-02-13 18:51:17 +08:00
|
|
|
|
|
|
|
# Here we check if the storage backend has a post_process
|
|
|
|
# method and pass it the list of modified files.
|
|
|
|
if self.post_process and hasattr(self.storage, 'post_process'):
|
|
|
|
processor = self.storage.post_process(found_files,
|
|
|
|
dry_run=self.dry_run)
|
|
|
|
for original_path, processed_path, processed in processor:
|
2013-04-28 22:45:05 +08:00
|
|
|
if isinstance(processed, Exception):
|
|
|
|
self.stderr.write("Post-processing '%s' failed!" % original_path)
|
|
|
|
# Add a blank line before the traceback, otherwise it's
|
|
|
|
# too easy to miss the relevant part of the error message.
|
|
|
|
self.stderr.write("")
|
|
|
|
raise processed
|
2012-02-13 18:51:17 +08:00
|
|
|
if processed:
|
2012-12-28 03:37:12 +08:00
|
|
|
self.log("Post-processed '%s' as '%s'" %
|
2012-02-13 18:51:17 +08:00
|
|
|
(original_path, processed_path), level=1)
|
|
|
|
self.post_processed_files.append(original_path)
|
|
|
|
else:
|
2012-06-08 00:08:47 +08:00
|
|
|
self.log("Skipped post-processing '%s'" % original_path)
|
2012-02-13 18:51:17 +08:00
|
|
|
|
|
|
|
return {
|
|
|
|
'modified': self.copied_files + self.symlinked_files,
|
|
|
|
'unmodified': self.unmodified_files,
|
|
|
|
'post_processed': self.post_processed_files,
|
|
|
|
}
|
|
|
|
|
|
|
|
def handle_noargs(self, **options):
|
|
|
|
self.set_options(**options)
|
2013-12-10 01:29:39 +08:00
|
|
|
|
|
|
|
message = ['\n']
|
|
|
|
if self.dry_run:
|
|
|
|
message.append(
|
|
|
|
'You have activated the --dry-run option so no files will be modified.\n\n'
|
|
|
|
)
|
|
|
|
|
|
|
|
message.append(
|
|
|
|
'You have requested to collect static files at the destination\n'
|
|
|
|
'location as specified in your settings'
|
|
|
|
)
|
|
|
|
|
|
|
|
if (isinstance(self.storage._wrapped, FileSystemStorage) and
|
2011-07-05 05:34:29 +08:00
|
|
|
self.storage.location):
|
|
|
|
destination_path = self.storage.location
|
2013-12-10 01:29:39 +08:00
|
|
|
message.append(':\n\n %s\n\n' % destination_path)
|
2011-07-05 05:34:29 +08:00
|
|
|
else:
|
|
|
|
destination_path = None
|
2013-12-10 01:29:39 +08:00
|
|
|
message.append('.\n\n')
|
2011-07-05 05:34:29 +08:00
|
|
|
|
|
|
|
if self.clear:
|
2013-12-10 01:29:39 +08:00
|
|
|
message.append('This will DELETE EXISTING FILES!\n')
|
2011-07-05 05:34:29 +08:00
|
|
|
else:
|
2013-12-10 01:29:39 +08:00
|
|
|
message.append('This will overwrite existing files!\n')
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2013-12-10 01:29:39 +08:00
|
|
|
message.append(
|
|
|
|
'Are you sure you want to do this?\n\n'
|
|
|
|
"Type 'yes' to continue, or 'no' to cancel: "
|
|
|
|
)
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2013-12-10 01:29:39 +08:00
|
|
|
if self.interactive and input(''.join(message)) != 'yes':
|
|
|
|
raise CommandError("Collecting static files cancelled.")
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2012-02-13 18:51:17 +08:00
|
|
|
collected = self.collect()
|
|
|
|
modified_count = len(collected['modified'])
|
|
|
|
unmodified_count = len(collected['unmodified'])
|
|
|
|
post_processed_count = len(collected['post_processed'])
|
2011-08-11 22:07:39 +08:00
|
|
|
|
2011-01-08 18:03:27 +08:00
|
|
|
if self.verbosity >= 1:
|
2012-02-13 18:51:17 +08:00
|
|
|
template = ("\n%(modified_count)s %(identifier)s %(action)s"
|
|
|
|
"%(destination)s%(unmodified)s%(post_processed)s.\n")
|
2011-08-11 22:07:39 +08:00
|
|
|
summary = template % {
|
2012-02-13 18:51:17 +08:00
|
|
|
'modified_count': modified_count,
|
2013-05-17 22:33:36 +08:00
|
|
|
'identifier': 'static file' + ('' if modified_count == 1 else 's'),
|
2013-05-27 10:47:50 +08:00
|
|
|
'action': 'symlinked' if self.symlink else 'copied',
|
|
|
|
'destination': (" to '%s'" % destination_path if destination_path else ''),
|
|
|
|
'unmodified': (', %s unmodified' % unmodified_count if collected['unmodified'] else ''),
|
2012-02-13 18:51:17 +08:00
|
|
|
'post_processed': (collected['post_processed'] and
|
|
|
|
', %s post-processed'
|
|
|
|
% post_processed_count or ''),
|
2011-08-11 22:07:39 +08:00
|
|
|
}
|
2012-05-19 19:51:54 +08:00
|
|
|
self.stdout.write(summary)
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2011-01-08 18:03:27 +08:00
|
|
|
def log(self, msg, level=2):
|
|
|
|
"""
|
|
|
|
Small log helper
|
|
|
|
"""
|
|
|
|
if self.verbosity >= level:
|
|
|
|
self.stdout.write(msg)
|
|
|
|
|
2011-07-05 05:34:29 +08:00
|
|
|
def clear_dir(self, path):
|
|
|
|
"""
|
2012-08-30 08:09:47 +08:00
|
|
|
Deletes the given relative path using the destination storage backend.
|
2011-07-05 05:34:29 +08:00
|
|
|
"""
|
|
|
|
dirs, files = self.storage.listdir(path)
|
|
|
|
for f in files:
|
|
|
|
fpath = os.path.join(path, f)
|
|
|
|
if self.dry_run:
|
2012-06-08 00:08:47 +08:00
|
|
|
self.log("Pretending to delete '%s'" %
|
2012-07-21 16:00:10 +08:00
|
|
|
smart_text(fpath), level=1)
|
2011-07-05 05:34:29 +08:00
|
|
|
else:
|
2012-07-21 16:00:10 +08:00
|
|
|
self.log("Deleting '%s'" % smart_text(fpath), level=1)
|
2011-07-05 05:34:29 +08:00
|
|
|
self.storage.delete(fpath)
|
|
|
|
for d in dirs:
|
|
|
|
self.clear_dir(os.path.join(path, d))
|
|
|
|
|
|
|
|
def delete_file(self, path, prefixed_path, source_storage):
|
2012-03-02 07:02:38 +08:00
|
|
|
"""
|
|
|
|
Checks if the target file should be deleted if it already exists
|
|
|
|
"""
|
2011-02-02 03:19:52 +08:00
|
|
|
if self.storage.exists(prefixed_path):
|
2010-10-20 09:33:24 +08:00
|
|
|
try:
|
2011-02-02 03:19:52 +08:00
|
|
|
# When was the target file modified last time?
|
2011-08-11 22:07:39 +08:00
|
|
|
target_last_modified = \
|
|
|
|
self.storage.modified_time(prefixed_path)
|
2012-02-13 18:51:17 +08:00
|
|
|
except (OSError, NotImplementedError, AttributeError):
|
2011-02-02 03:19:52 +08:00
|
|
|
# The storage doesn't support ``modified_time`` or failed
|
2010-10-20 09:33:24 +08:00
|
|
|
pass
|
|
|
|
else:
|
2011-02-02 03:19:52 +08:00
|
|
|
try:
|
|
|
|
# When was the source file modified last time?
|
|
|
|
source_last_modified = source_storage.modified_time(path)
|
2012-02-13 18:51:17 +08:00
|
|
|
except (OSError, NotImplementedError, AttributeError):
|
2011-02-02 03:19:52 +08:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
# The full path of the target file
|
|
|
|
if self.local:
|
|
|
|
full_path = self.storage.path(prefixed_path)
|
|
|
|
else:
|
|
|
|
full_path = None
|
|
|
|
# Skip the file if the source file is younger
|
2013-01-26 20:28:44 +08:00
|
|
|
# Avoid sub-second precision (see #14665, #19540)
|
|
|
|
if (target_last_modified.replace(microsecond=0)
|
|
|
|
>= source_last_modified.replace(microsecond=0)):
|
2011-08-11 22:07:39 +08:00
|
|
|
if not ((self.symlink and full_path
|
|
|
|
and not os.path.islink(full_path)) or
|
|
|
|
(not self.symlink and full_path
|
|
|
|
and os.path.islink(full_path))):
|
2011-02-02 03:19:52 +08:00
|
|
|
if prefixed_path not in self.unmodified_files:
|
|
|
|
self.unmodified_files.append(prefixed_path)
|
2012-06-08 00:08:47 +08:00
|
|
|
self.log("Skipping '%s' (not modified)" % path)
|
2011-02-02 03:19:52 +08:00
|
|
|
return False
|
|
|
|
# Then delete the existing file if really needed
|
2011-07-05 05:34:29 +08:00
|
|
|
if self.dry_run:
|
2012-06-08 00:08:47 +08:00
|
|
|
self.log("Pretending to delete '%s'" % path)
|
2010-10-20 09:33:24 +08:00
|
|
|
else:
|
2012-06-08 00:08:47 +08:00
|
|
|
self.log("Deleting '%s'" % path)
|
2011-02-02 03:19:52 +08:00
|
|
|
self.storage.delete(prefixed_path)
|
|
|
|
return True
|
2010-10-20 09:33:24 +08:00
|
|
|
|
2011-07-05 05:34:29 +08:00
|
|
|
def link_file(self, path, prefixed_path, source_storage):
|
2011-02-02 03:19:52 +08:00
|
|
|
"""
|
|
|
|
Attempt to link ``path``
|
|
|
|
"""
|
|
|
|
# Skip this file if it was already copied earlier
|
|
|
|
if prefixed_path in self.symlinked_files:
|
2012-06-08 00:08:47 +08:00
|
|
|
return self.log("Skipping '%s' (already linked earlier)" % path)
|
2011-02-02 03:19:52 +08:00
|
|
|
# Delete the target file if needed or break
|
2011-07-05 05:34:29 +08:00
|
|
|
if not self.delete_file(path, prefixed_path, source_storage):
|
2011-02-02 03:19:52 +08:00
|
|
|
return
|
|
|
|
# The full path of the source file
|
|
|
|
source_path = source_storage.path(path)
|
|
|
|
# Finally link the file
|
2011-07-05 05:34:29 +08:00
|
|
|
if self.dry_run:
|
2012-06-08 00:08:47 +08:00
|
|
|
self.log("Pretending to link '%s'" % source_path, level=1)
|
2011-02-02 03:19:52 +08:00
|
|
|
else:
|
2012-06-08 00:08:47 +08:00
|
|
|
self.log("Linking '%s'" % source_path, level=1)
|
2011-02-02 03:19:52 +08:00
|
|
|
full_path = self.storage.path(prefixed_path)
|
|
|
|
try:
|
|
|
|
os.makedirs(os.path.dirname(full_path))
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
os.symlink(source_path, full_path)
|
|
|
|
if prefixed_path not in self.symlinked_files:
|
|
|
|
self.symlinked_files.append(prefixed_path)
|
|
|
|
|
2011-07-05 05:34:29 +08:00
|
|
|
def copy_file(self, path, prefixed_path, source_storage):
|
2011-02-02 03:19:52 +08:00
|
|
|
"""
|
|
|
|
Attempt to copy ``path`` with storage
|
|
|
|
"""
|
|
|
|
# Skip this file if it was already copied earlier
|
|
|
|
if prefixed_path in self.copied_files:
|
2012-06-08 00:08:47 +08:00
|
|
|
return self.log("Skipping '%s' (already copied earlier)" % path)
|
2011-02-02 03:19:52 +08:00
|
|
|
# Delete the target file if needed or break
|
2011-07-05 05:34:29 +08:00
|
|
|
if not self.delete_file(path, prefixed_path, source_storage):
|
2011-02-02 03:19:52 +08:00
|
|
|
return
|
|
|
|
# The full path of the source file
|
|
|
|
source_path = source_storage.path(path)
|
|
|
|
# Finally start copying
|
2011-07-05 05:34:29 +08:00
|
|
|
if self.dry_run:
|
2012-06-08 00:08:47 +08:00
|
|
|
self.log("Pretending to copy '%s'" % source_path, level=1)
|
2011-02-02 03:19:52 +08:00
|
|
|
else:
|
2012-06-08 00:08:47 +08:00
|
|
|
self.log("Copying '%s'" % source_path, level=1)
|
2011-07-08 17:39:56 +08:00
|
|
|
with source_storage.open(path) as source_file:
|
|
|
|
self.storage.save(prefixed_path, source_file)
|
2011-02-02 03:19:52 +08:00
|
|
|
if not prefixed_path in self.copied_files:
|
|
|
|
self.copied_files.append(prefixed_path)
|