Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (3.8 kB)

1
# Copyright 2011 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or without
4
# modification, are permitted provided that the following conditions
5
# are met:
6
#
7
#   1. Redistributions of source code must retain the above copyright
8
#      notice, this list of conditions and the following disclaimer.
9
#
10
#  2. Redistributions in binary form must reproduce the above copyright
11
#     notice, this list of conditions and the following disclaimer in the
12
#     documentation and/or other materials provided with the distribution.
13
#
14
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
# SUCH DAMAGE.
25
#
26
# The views and conclusions contained in the software and documentation are
27
# those of the authors and should not be interpreted as representing official
28
# policies, either expressed or implied, of GRNET S.A.
29
"""
30
Collect static files required by synnefo to a specific location
31
"""
32
import os, shutil
33

    
34
from django.utils.importlib import import_module
35
from optparse import make_option
36

    
37
from django.core.management.base import BaseCommand, CommandError
38
from django.conf import settings
39

    
40
STATIC_FILES = getattr(settings, "STATIC_FILES", {})
41

    
42
class Command(BaseCommand):
43

    
44
    help = 'Symlink static files to directory specified'
45

    
46
    option_list = BaseCommand.option_list + (
47
        make_option('--static-root',
48
            action='store',
49
            dest='static_root',
50
            default=settings.MEDIA_ROOT,
51
            help='Path to place symlinks (default: `%s`)' % settings.MEDIA_ROOT),
52
        make_option('--dry-run',
53
            action='store_true',
54
            dest='dry',
55
            default=False,
56
            help='Do not actually create symlinks'),
57
        )
58

    
59
    def collect_files(self, target):
60
        symlinks = []
61
        dirs_to_create = set()
62
        for module, ns in STATIC_FILES.iteritems():
63
            module = import_module(module)
64
            static_root = os.path.join(os.path.dirname(module.__file__), 'static')
65

    
66
            # no nested dir exists for the app
67
            if ns == '':
68
                for f in os.listdir(static_root):
69
                    symlinks.append((os.path.join(static_root, f), os.path.join(target, ns, f)))
70

    
71
            # symlink whole app directory
72
            else:
73
                symlinks.append((os.path.join(static_root), os.path.join(target, ns)))
74

    
75
        return symlinks
76

    
77
    def handle(self, *args, **options):
78

    
79
        print "The following synlinks will get created"
80

    
81
        symlinks = self.collect_files(options['static_root'])
82
        for linkfrom, linkto in symlinks:
83
            print "Symlink '%s' to '%s' will get created." % (linkfrom, linkto)
84

    
85
        if not options['dry']:
86
            confirm = raw_input("""
87
Are you soure you want to continue ?
88
Type 'yes' to continue, or 'no' to cancel: """)
89

    
90
            if confirm == "yes":
91
                for linkfrom, linkto in symlinks:
92
                    print "Creating link from %s to %s" % (linkfrom, linkto)
93
                    if os.path.exists(linkto):
94
                        print "Skippig %s" % linkto
95
                        continue
96

    
97
                    os.symlink(linkfrom, linkto)
98