Statistics
| Branch: | Tag: | Revision:

root / snf-app / synnefo / ui / management / commands / link_static.py @ 483c9197

History | View | Annotate | Download (3.6 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 = {
41
    'synnefo.ui': '',
42
    'synnefo.admin': 'admin',
43
}
44

    
45
class Command(BaseCommand):
46

    
47
    help = 'Symlink static files to directory specified'
48

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

    
62
    def collect_files(self, target):
63
        symlinks = []
64
        dirs_to_create = set()
65
        for module, ns in STATIC_FILES.iteritems():
66
            module = import_module(module)
67
            static_root = os.path.join(os.path.dirname(module.__file__), 'static')
68
            for f in os.listdir(static_root):
69
                symlinks.append((os.path.join(static_root, f), os.path.join(target, ns, f)))
70
                if not os.path.exists(os.path.join(target, ns)):
71
                    dirs_to_create.add(os.path.join(target, ns))
72

    
73
        return symlinks, dirs_to_create
74

    
75
    def handle(self, *args, **options):
76

    
77
        print "The following synlinks will get created"
78

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

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

    
88
            if confirm == "yes":
89
                for d in dirs_to_create:
90
                    os.mkdir(d)
91

    
92
                for linkfrom, linkto in symlinks:
93
                    print "Creating link from %s to %s" % (linkfrom, linkto)
94
                    os.symlink(linkfrom, linkto)
95