Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (4 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
33

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

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

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

    
42

    
43
class Command(BaseCommand):
44

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

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

    
63
    def collect_files(self, target):
64
        symlinks = []
65
        for module, ns in STATIC_FILES.iteritems():
66
            module = import_module(module)
67
            static_root = os.path.join(os.path.dirname(module.__file__),
68
                                       'static')
69

    
70
            # no nested dir exists for the app
71
            if ns == '':
72
                for f in os.listdir(static_root):
73
                    symlinks.append((os.path.join(static_root, f),
74
                                     os.path.join(target, ns, f)))
75

    
76
            # symlink whole app directory
77
            else:
78
                symlinks.append((os.path.join(static_root),
79
                                 os.path.join(target, ns)))
80

    
81
        return symlinks
82

    
83
    def handle(self, *args, **options):
84

    
85
        self.stderr.write("The following synlinks will get created")
86

    
87
        symlinks = self.collect_files(options['static_root'])
88
        for linkfrom, linkto in symlinks:
89
            self.stderr.write("Symlink '%s' to '%s' will get created."
90
                              % (linkfrom, linkto))
91

    
92
        if not options['dry']:
93
            confirm = raw_input("""
94
Are you soure you want to continue ?
95
Type 'yes' to continue, or 'no' to cancel: """)
96

    
97
            if confirm == "yes":
98
                for linkfrom, linkto in symlinks:
99
                    self.stderr.write("Creating link from %s to %s"
100
                                      % (linkfrom, linkto))
101
                    if os.path.exists(linkto):
102
                        self.stderr.write("Skippig %s" % linkto)
103
                        continue
104

    
105
                    os.symlink(linkfrom, linkto)