Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/webassets/bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ def __init__(self, *contents, **options):
self.contents = contents
self.output = options.pop('output', None)
self.filters = options.pop('filters', None)
self.sort = options.pop('sort', None)
self.depends = options.pop('depends', [])
self.version = options.pop('version', [])
self.extra = options.pop('extra', {})
Expand Down Expand Up @@ -250,7 +251,13 @@ def resolve_contents(self, ctx=None, force=False):

resolved.extend(map(lambda r: (item, r), result))

# A hook for sorting the bundle contents in a certain order
if self.sort:
resolved = self.sort(ctx, resolved)

self._resolved_contents = resolved


return self._resolved_contents

def _get_depends(self):
Expand Down
99 changes: 99 additions & 0 deletions src/webassets/filter/jsdeps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import os
from pprint import pprint
from webassets.filter import Filter

class JSDepSort:

def __init__(self, *args, **kwargs):
self.search_path = kwargs.pop('search_path', [])
if not isinstance(self.search_path, (list, tuple)):
self.search_path = [self.search_path]

if not '' in self.search_path:
self.search_path.append('')

def sort(self, ctx, contents):
deps = map(self.parse_deps, map(lambda c: c[1], contents))

content_map = {}
for d, c in zip(deps, contents):
content_map[c[1]] = {'deps':d, 'content':c}

ordered_contents = []
pre_visits = set()
post_visits = set()

for content in contents:
path = content[1]
deps = content_map[path]['deps']
self.append_content(content, deps, ctx.directory, content_map, ordered_contents, pre_visits, post_visits)

return ordered_contents

def read_contents(self, path):
return open(path).read()

def parse_deps(self, path):
hunk = self.read_contents(path)

dependencies = []

lines = hunk.splitlines()
for line in lines:
line = line.strip()

if line.startswith('//='):
directive = line[len('//='):].split()
if directive[0] == 'require':
dependencies.append(directive[1])

return dependencies

def append_content(self, content, deps, base_dir, content_map, ordered_contents, pre_visits, post_visits):
path = content[1]

if path in post_visits:
return

pre_visits.add(path)

for dep in deps:
dep_path = self.resolve_dep(dep, base_dir, content_map, path)
if not dep_path:
raise Exception(
'Could not resolve dependency "{}" of source'
' file "{}"'.format(dep, path))

if dep_path in pre_visits and not dep_path in post_visits:
raise Exception(
'Circular dependency found: "{}" depends on {}'
.format(path, dep))


self.append_content(content_map[dep_path]['content'], content_map[dep_path]['deps'],
base_dir, content_map, ordered_contents, pre_visits, post_visits)


ordered_contents.append(content)

post_visits.add(path)

def resolve_dep(self, dep, base_dir, content_map, included_by):
if dep.startswith("/"):
#We are an absolute path. Why would you do this???
if dep in content_map:
return dep
else:
raise Exception('Dependency "{}" not in this bundle. (Included by {})'
.format(dep, included_by))

for search_path in self.search_path:
path = os.path.join(base_dir, os.path.join(search_path, dep))
# print 'searching for dep {} at {}'.format(dep, path)
if path in content_map:
return path

raise Exception('Dependency "{}" not found in this bundle. (Included by {})'
.format(dep, included_by))


11 changes: 11 additions & 0 deletions tests/test_bundle_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from nose import SkipTest

from webassets import Bundle
from webassets.env import Resolver
from webassets.exceptions import BundleError
from webassets.test import TempEnvironmentHelper, TempDirHelper

Expand Down Expand Up @@ -80,6 +81,16 @@ def test_pass_down_env(self):
# Does no longer raise an "unconnected env" exception
assert root.urls() == ['/1', '/2']

def test_sorting(self):
"""Specifying a sorting function should sort the resolved list of files
"""
def sortFunc(cxt, files):
return sorted(files, key=(lambda f: f[1]))

self.env.debug = True
bundle = self.MockBundle('c', 'a', '1', 'b', sort=sortFunc, output='out')
assert bundle.urls() == ['/1', '/a', '/b', '/c']

def test_invalid_source_file(self):
"""If a source file is missing, an error is raised even when rendering
the urls (as opposed to just outputting the url to the missing file
Expand Down