Statistics
| Branch: | Tag: | Revision:

root / snf-webproject / synnefo / webproject / management / commands / link_static.py @ d63a86d6

History | View | Annotate | Download (3.8 kB)

1 e868d384 Kostas Papadimitriou
# Copyright 2011 GRNET S.A. All rights reserved.
2 e868d384 Kostas Papadimitriou
#
3 e868d384 Kostas Papadimitriou
# Redistribution and use in source and binary forms, with or without
4 e868d384 Kostas Papadimitriou
# modification, are permitted provided that the following conditions
5 e868d384 Kostas Papadimitriou
# are met:
6 e868d384 Kostas Papadimitriou
#
7 e868d384 Kostas Papadimitriou
#   1. Redistributions of source code must retain the above copyright
8 e868d384 Kostas Papadimitriou
#      notice, this list of conditions and the following disclaimer.
9 e868d384 Kostas Papadimitriou
#
10 e868d384 Kostas Papadimitriou
#  2. Redistributions in binary form must reproduce the above copyright
11 e868d384 Kostas Papadimitriou
#     notice, this list of conditions and the following disclaimer in the
12 e868d384 Kostas Papadimitriou
#     documentation and/or other materials provided with the distribution.
13 e868d384 Kostas Papadimitriou
#
14 e868d384 Kostas Papadimitriou
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15 e868d384 Kostas Papadimitriou
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 e868d384 Kostas Papadimitriou
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 e868d384 Kostas Papadimitriou
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18 e868d384 Kostas Papadimitriou
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 e868d384 Kostas Papadimitriou
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 e868d384 Kostas Papadimitriou
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 e868d384 Kostas Papadimitriou
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 e868d384 Kostas Papadimitriou
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 e868d384 Kostas Papadimitriou
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 e868d384 Kostas Papadimitriou
# SUCH DAMAGE.
25 e868d384 Kostas Papadimitriou
#
26 e868d384 Kostas Papadimitriou
# The views and conclusions contained in the software and documentation are
27 e868d384 Kostas Papadimitriou
# those of the authors and should not be interpreted as representing official
28 e868d384 Kostas Papadimitriou
# policies, either expressed or implied, of GRNET S.A.
29 e868d384 Kostas Papadimitriou
"""
30 e868d384 Kostas Papadimitriou
Collect static files required by synnefo to a specific location
31 e868d384 Kostas Papadimitriou
"""
32 e868d384 Kostas Papadimitriou
import os, shutil
33 e868d384 Kostas Papadimitriou
34 e868d384 Kostas Papadimitriou
from django.utils.importlib import import_module
35 e868d384 Kostas Papadimitriou
from optparse import make_option
36 e868d384 Kostas Papadimitriou
37 e868d384 Kostas Papadimitriou
from django.core.management.base import BaseCommand, CommandError
38 e868d384 Kostas Papadimitriou
from django.conf import settings
39 e868d384 Kostas Papadimitriou
40 0b9b9cd2 Kostas Papadimitriou
STATIC_FILES = getattr(settings, "STATIC_FILES", {})
41 e868d384 Kostas Papadimitriou
42 e868d384 Kostas Papadimitriou
class Command(BaseCommand):
43 e868d384 Kostas Papadimitriou
44 e868d384 Kostas Papadimitriou
    help = 'Symlink static files to directory specified'
45 e868d384 Kostas Papadimitriou
46 e868d384 Kostas Papadimitriou
    option_list = BaseCommand.option_list + (
47 e868d384 Kostas Papadimitriou
        make_option('--static-root',
48 e868d384 Kostas Papadimitriou
            action='store',
49 e868d384 Kostas Papadimitriou
            dest='static_root',
50 e868d384 Kostas Papadimitriou
            default=settings.MEDIA_ROOT,
51 e868d384 Kostas Papadimitriou
            help='Path to place symlinks (default: `%s`)' % settings.MEDIA_ROOT),
52 e868d384 Kostas Papadimitriou
        make_option('--dry-run',
53 e868d384 Kostas Papadimitriou
            action='store_true',
54 e868d384 Kostas Papadimitriou
            dest='dry',
55 e868d384 Kostas Papadimitriou
            default=False,
56 e868d384 Kostas Papadimitriou
            help='Do not actually create symlinks'),
57 e868d384 Kostas Papadimitriou
        )
58 e868d384 Kostas Papadimitriou
59 e868d384 Kostas Papadimitriou
    def collect_files(self, target):
60 e868d384 Kostas Papadimitriou
        symlinks = []
61 e868d384 Kostas Papadimitriou
        dirs_to_create = set()
62 e868d384 Kostas Papadimitriou
        for module, ns in STATIC_FILES.iteritems():
63 e868d384 Kostas Papadimitriou
            module = import_module(module)
64 e868d384 Kostas Papadimitriou
            static_root = os.path.join(os.path.dirname(module.__file__), 'static')
65 e868d384 Kostas Papadimitriou
66 8f39ec0d Kostas Papadimitriou
            # no nested dir exists for the app
67 8f39ec0d Kostas Papadimitriou
            if ns == '':
68 8f39ec0d Kostas Papadimitriou
                for f in os.listdir(static_root):
69 8f39ec0d Kostas Papadimitriou
                    symlinks.append((os.path.join(static_root, f), os.path.join(target, ns, f)))
70 8f39ec0d Kostas Papadimitriou
71 8f39ec0d Kostas Papadimitriou
            # symlink whole app directory
72 8f39ec0d Kostas Papadimitriou
            else:
73 8f39ec0d Kostas Papadimitriou
                symlinks.append((os.path.join(static_root), os.path.join(target, ns)))
74 8f39ec0d Kostas Papadimitriou
75 8f39ec0d Kostas Papadimitriou
        return symlinks
76 e868d384 Kostas Papadimitriou
77 e868d384 Kostas Papadimitriou
    def handle(self, *args, **options):
78 e868d384 Kostas Papadimitriou
79 e868d384 Kostas Papadimitriou
        print "The following synlinks will get created"
80 e868d384 Kostas Papadimitriou
81 8f39ec0d Kostas Papadimitriou
        symlinks = self.collect_files(options['static_root'])
82 e868d384 Kostas Papadimitriou
        for linkfrom, linkto in symlinks:
83 e868d384 Kostas Papadimitriou
            print "Symlink '%s' to '%s' will get created." % (linkfrom, linkto)
84 e868d384 Kostas Papadimitriou
85 e868d384 Kostas Papadimitriou
        if not options['dry']:
86 e868d384 Kostas Papadimitriou
            confirm = raw_input("""
87 e868d384 Kostas Papadimitriou
Are you soure you want to continue ?
88 e868d384 Kostas Papadimitriou
Type 'yes' to continue, or 'no' to cancel: """)
89 e868d384 Kostas Papadimitriou
90 e868d384 Kostas Papadimitriou
            if confirm == "yes":
91 e868d384 Kostas Papadimitriou
                for linkfrom, linkto in symlinks:
92 e868d384 Kostas Papadimitriou
                    print "Creating link from %s to %s" % (linkfrom, linkto)
93 8f39ec0d Kostas Papadimitriou
                    if os.path.exists(linkto):
94 8f39ec0d Kostas Papadimitriou
                        print "Skippig %s" % linkto
95 8f39ec0d Kostas Papadimitriou
                        continue
96 8f39ec0d Kostas Papadimitriou
97 e868d384 Kostas Papadimitriou
                    os.symlink(linkfrom, linkto)