myslice.ini possible themes = onelab, fed4fire, fibre, smartfire
[myslice.git] / unfold / collectstatic.py
1 import os
2 from django.conf import settings
3 from django.utils.datastructures import SortedDict
4 from django.contrib.staticfiles.finders import BaseFinder, FileSystemFinder
5 from django.core.files.storage import FileSystemStorage
6
7 # DEPRECATED # # http://nicks-liquid-soapbox.blogspot.fr/2011/03/splitting-path-to-list-in-python.html
8 # DEPRECATED # def splitpath(path, maxdepth=20):
9 # DEPRECATED #      ( head, tail ) = os.path.split(path)
10 # DEPRECATED #      return splitpath(head, maxdepth - 1) + [ tail ] \
11 # DEPRECATED #          if maxdepth and head and head != path \
12 # DEPRECATED #          else [ head or tail ]
13
14 # The plugin finder is responsible for collecting JS, CSS and PNG files from
15 # the plugins, which are not declared in the myslice.settings file unlike
16 # applications.
17 class PluginFinder(FileSystemFinder):
18     """
19     A static files finder that looks in the directory of each plugin as
20     specified in the source_dir attribute of the given storage class.
21     """
22     def __init__(self, *args, **kwargs):
23         # The list of plugins that are handled
24         self.locations = []
25         # Mapping of plugin module paths to storage instances
26         self.storages = SortedDict()
27         plugins_dir = self.get_immediate_subdirs(settings.PLUGIN_DIR)
28         for root in plugins_dir:
29             if not os.path.exists(root) or not os.path.isdir(root):
30                 continue
31             if ('', root) not in self.locations:
32                 self.locations.append(('', root))
33         for _, root in self.locations:
34             filesystem_storage = FileSystemStorage(location=root)
35             filesystem_storage.prefix = ''
36             self.storages[root] = filesystem_storage
37
38     def get_immediate_subdirs(self, dir):
39         return [os.path.join(dir, name, 'static') for name in os.listdir(dir) 
40                 if os.path.isdir(os.path.join(dir, name))]
41
42 # as these are a django-specific notion
43 class ThirdPartyFinder(BaseFinder):
44     """
45     A static files finder that looks in the directory of each third-party
46     resources and tries to preserve the location of each file
47     """
48     # third-party/MODULE/path/to/js
49     extensions = {
50         # PREFIX : EXTENSIONS
51 # third party stuff is not expected to provide templates,
52 #        ''   : ('.html',),
53         'js' : ('.js',),
54         'css': ('.css',),
55         'img': ('.png', '.ico',),
56         'fonts' : ('.svg', '.eot', '.ttf', '.woff'),
57     }
58
59     def find(self, search_path, all=False):
60         """
61         Given a relative file path this ought to find an
62         absolute file path.
63
64         If the ``all`` parameter is ``False`` (default) only
65         the first found file path will be returned; if set
66         to ``True`` a list of all found files paths is returned.
67         """
68         matches = []
69         #all_extensions = reduce(lambda x,y : x + y, extensions.values())
70
71         for (path, dirs, files) in os.walk(settings.THIRDPARTY_DIR):
72             for file in files:
73                 name, extension = os.path.splitext(file)
74
75                 for type, extensions in self.extensions.items():
76                     if not extension in extensions:
77                         continue
78                     if search_path == os.path.join(type, file):
79                         matched_path = os.path.join(path, file) 
80                         if not all:
81                             return matched_path
82                         print 'ThirdPartyFinder, adding',matched_path
83                         matches.append(matched_path)
84         return matches
85
86     def list(self, ignore_patterns):
87         """
88         Given an optional list of paths to ignore, this should return
89         a two item iterable consisting of the relative path and storage
90         instance.
91         """
92
93         for component in os.listdir(settings.THIRDPARTY_DIR):
94             # We are looking forward symlinks only
95             component_path = os.path.join(settings.THIRDPARTY_DIR, component)
96             if not os.path.islink(component_path) or not os.path.isdir(component_path):
97                 continue
98                 
99             for (path, dirs, files) in os.walk(component_path):
100                 for file in files:
101                     name, extension = os.path.splitext(file)
102
103                     for type, extensions in self.extensions.items():
104                         if not extension in extensions:
105                             continue
106                         filesystem_storage = FileSystemStorage(location=path)
107                         filesystem_storage.prefix = type
108                         yield file, filesystem_storage
109             
110 # DEPRECATED #         for (path, dirs, files) in os.walk(settings.THIRDPARTY_DIR):
111 # DEPRECATED #             component_path = path[len(settings.THIRDPARTY_DIR)+1:]
112 # DEPRECATED #             component_name = splitpath(component_path)[0]
113 # DEPRECATED #             print "PATH", path, " -- COMPONENT", component_name
114 # DEPRECATED #             if not os.path.islink(os.path.join(path, component_name)):
115 # DEPRECATED #                 print "IGNORED", component_name
116 # DEPRECATED #                 continue
117 # DEPRECATED #             print "COMPONENT ADDED: ", component_name
118 # DEPRECATED #             print "=="
119 # DEPRECATED # 
120 # DEPRECATED # 
121 # DEPRECATED #             for file in files:
122 # DEPRECATED #                 name, extension = os.path.splitext(file)
123 # DEPRECATED # 
124 # DEPRECATED #                 for type, extensions in self.extensions.items():
125 # DEPRECATED #                     if not extension in extensions:
126 # DEPRECATED #                         continue
127 # DEPRECATED #                     filesystem_storage = FileSystemStorage(location=path)
128 # DEPRECATED #                     filesystem_storage.prefix = type
129 # DEPRECATED #                     yield file, filesystem_storage
130
131 # XXX I commented this since it should be imported from django.contrib.staticfiles.finders -- jordan
132  
133 #class BaseFinder(object):
134 #    """
135 #    A base file finder to be used for custom staticfiles finder classes.
136 #    """
137 #
138 #    def list(self, ignore_patterns):
139 #        """
140 #        Given an optional list of paths to ignore, this should return
141 #        a two item iterable consisting of the relative path and storage
142 #        instance.
143 #        """
144 #        raise NotImplementedError()
145