wcs/wcs/ctl/collectstatic.py

70 lines
2.9 KiB
Python

# w.c.s. - web application for online forms
# Copyright (C) 2005-2016 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 qommon.ctl import Command, make_option
class CmdCollectStatic(Command):
name = 'collectstatic'
def __init__(self):
Command.__init__(self, [
make_option('-c', '--clear', action='store_true',
dest='clear', default=False),
make_option('-l', '--link', action='store_true',
dest='link', default=False),
])
def execute(self, base_options, sub_options, args):
import publisher
publisher.WcsPublisher.configure(self.config)
pub = publisher.WcsPublisher.create_publisher(
register_cron=False, register_tld_names=False)
return self.collectstatic(pub,
clear=sub_options.clear, link=sub_options.link)
@classmethod
def collectstatic(cls, pub, clear=False, link=False):
root_directory_class = pub.root_directory_class
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)
CmdCollectStatic.register()