wcs/wcs/qommon/management/commands/collectstatic.py

68 lines
3.0 KiB
Python

# w.c.s. - web application for online forms
# Copyright (C) 2005-2017 Entr'ouvert
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
import os
import shutil
from django.core.management.base import BaseCommand
from qommon.publisher import get_publisher_class
class Command(BaseCommand):
help = "Collect static files in a single location."
def add_arguments(self, parser):
parser.add_argument('-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.")
parser.add_argument('-l', '--link',
action='store_true', dest='link', default=False,
help="Create a symbolic link to each file instead of copying.")
def handle(self, **options):
Publisher = get_publisher_class()
Publisher.ERROR_LOG = None
pub = Publisher.create_publisher()
return self.collectstatic(pub,
clear=options['clear'], link=options['link'])
@classmethod
def collectstatic(cls, pub, clear=False, link=False):
root_directory_class = pub.root_directory_class.static
static_dir = os.path.join(pub.app_dir, 'collectstatic')
if clear and os.path.exists(static_dir):
shutil.rmtree(static_dir)
if not os.path.exists(static_dir):
os.mkdir(static_dir)
for prefix in root_directory_class.static_directories:
for directory in root_directory_class.resolve_static_directories(prefix):
if not os.path.exists(directory):
continue
real_prefix = prefix.replace('_', '/') # xstatic hack
dst_base = os.path.join(static_dir, real_prefix)
for basedir, dirnames, filenames in os.walk(directory):
for filename in filenames:
dst_path = os.path.join(dst_base, basedir[len(directory)+1:])
dst_filename = os.path.join(dst_path, filename)
if not os.path.exists(dst_path):
os.makedirs(dst_path)
if os.path.exists(dst_filename):
os.unlink(dst_filename)
if link:
os.symlink(os.path.join(basedir, filename), dst_filename)
else:
shutil.copy(os.path.join(basedir, filename), dst_filename)