Revision a442c81d snf-webproject/synnefo/webproject/management/commands/link_static.py
b/snf-webproject/synnefo/webproject/management/commands/link_static.py | ||
---|---|---|
29 | 29 |
""" |
30 | 30 |
Collect static files required by synnefo to a specific location |
31 | 31 |
""" |
32 |
import os, shutil
|
|
32 |
import os |
|
33 | 33 |
|
34 | 34 |
from django.utils.importlib import import_module |
35 | 35 |
from optparse import make_option |
36 | 36 |
|
37 |
from django.core.management.base import BaseCommand, CommandError
|
|
37 |
from django.core.management.base import BaseCommand |
|
38 | 38 |
from django.conf import settings |
39 | 39 |
|
40 | 40 |
STATIC_FILES = getattr(settings, "STATIC_FILES", {}) |
41 | 41 |
|
42 |
|
|
42 | 43 |
class Command(BaseCommand): |
43 | 44 |
|
44 | 45 |
help = 'Symlink static files to directory specified' |
45 | 46 |
|
46 | 47 |
option_list = BaseCommand.option_list + ( |
47 |
make_option('--static-root', |
|
48 |
make_option( |
|
49 |
'--static-root', |
|
48 | 50 |
action='store', |
49 | 51 |
dest='static_root', |
50 | 52 |
default=settings.MEDIA_ROOT, |
51 |
help='Path to place symlinks (default: `%s`)' % settings.MEDIA_ROOT), |
|
52 |
make_option('--dry-run', |
|
53 |
help='Path to place symlinks (default: `%s`)' |
|
54 |
% settings.MEDIA_ROOT), |
|
55 |
make_option( |
|
56 |
'--dry-run', |
|
53 | 57 |
action='store_true', |
54 | 58 |
dest='dry', |
55 | 59 |
default=False, |
... | ... | |
58 | 62 |
|
59 | 63 |
def collect_files(self, target): |
60 | 64 |
symlinks = [] |
61 |
dirs_to_create = set() |
|
62 | 65 |
for module, ns in STATIC_FILES.iteritems(): |
63 | 66 |
module = import_module(module) |
64 |
static_root = os.path.join(os.path.dirname(module.__file__), 'static') |
|
67 |
static_root = os.path.join(os.path.dirname(module.__file__), |
|
68 |
'static') |
|
65 | 69 |
|
66 | 70 |
# no nested dir exists for the app |
67 | 71 |
if ns == '': |
68 | 72 |
for f in os.listdir(static_root): |
69 |
symlinks.append((os.path.join(static_root, f), os.path.join(target, ns, f))) |
|
73 |
symlinks.append((os.path.join(static_root, f), |
|
74 |
os.path.join(target, ns, f))) |
|
70 | 75 |
|
71 | 76 |
# symlink whole app directory |
72 | 77 |
else: |
73 |
symlinks.append((os.path.join(static_root), os.path.join(target, ns))) |
|
78 |
symlinks.append((os.path.join(static_root), |
|
79 |
os.path.join(target, ns))) |
|
74 | 80 |
|
75 | 81 |
return symlinks |
76 | 82 |
|
77 | 83 |
def handle(self, *args, **options): |
78 | 84 |
|
79 |
print "The following synlinks will get created"
|
|
85 |
self.stderr.write("The following synlinks will get created")
|
|
80 | 86 |
|
81 | 87 |
symlinks = self.collect_files(options['static_root']) |
82 | 88 |
for linkfrom, linkto in symlinks: |
83 |
print "Symlink '%s' to '%s' will get created." % (linkfrom, linkto) |
|
89 |
self.stderr.write("Symlink '%s' to '%s' will get created." |
|
90 |
% (linkfrom, linkto)) |
|
84 | 91 |
|
85 | 92 |
if not options['dry']: |
86 | 93 |
confirm = raw_input(""" |
... | ... | |
89 | 96 |
|
90 | 97 |
if confirm == "yes": |
91 | 98 |
for linkfrom, linkto in symlinks: |
92 |
print "Creating link from %s to %s" % (linkfrom, linkto) |
|
99 |
self.stderr.write("Creating link from %s to %s" |
|
100 |
% (linkfrom, linkto)) |
|
93 | 101 |
if os.path.exists(linkto): |
94 |
print "Skippig %s" % linkto
|
|
102 |
self.stderr.write("Skippig %s" % linkto)
|
|
95 | 103 |
continue |
96 | 104 |
|
97 | 105 |
os.symlink(linkfrom, linkto) |
98 |
|
Also available in: Unified diff