Skip to content

Commit

Permalink
Added --no-cache option to build command.
Browse files Browse the repository at this point in the history
  • Loading branch information
miracle2k committed Feb 11, 2012
1 parent 661a0f8 commit c9a2e7b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
2 changes: 0 additions & 2 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
There is currently no locking going during auto-rebuilds, that is multiple
requests could potentially cause multiple builds!

Add a switch to the rebuild() command to ignore the cache.

Should bundle names be namespacable, similar to Django urls? At the least,
we probably want to encourage a convention of prefixing your bundle names
with an id. Everything else would just intend to make this easier or more
Expand Down
12 changes: 7 additions & 5 deletions src/webassets/bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,8 @@ def _merge_and_apply(self, env, output_path, force, parent_debug=None,
return apply_filters(final, filters, 'output',
env.cache, actually_skip_cache_here)

def _build(self, env, extra_filters=[], force=None, output=None):
def _build(self, env, extra_filters=[], force=None, output=None,
disable_cache=None):
"""Internal bundle build function.
This actually tries to build this very bundle instance, as
Expand Down Expand Up @@ -384,7 +385,6 @@ def _build(self, env, extra_filters=[], force=None, output=None):

# Determine if we really need to build, or if the output file
# already exists and nothing has changed.
disable_cache = None
if force:
update_needed = True
elif not env.updater:
Expand All @@ -402,7 +402,8 @@ def _build(self, env, extra_filters=[], force=None, output=None):
# _merge_and_apply() is now smart enough to do without
# this disable_cache hint, but for now, keep passing it
# along if we get the info from the updater.
disable_cache = update_needed==SKIP_CACHE
if update_needed==SKIP_CACHE:
disable_cache = True
else:
update_needed = False

Expand Down Expand Up @@ -432,7 +433,7 @@ def _build(self, env, extra_filters=[], force=None, output=None):

return hunk

def build(self, env=None, force=None, output=None):
def build(self, env=None, force=None, output=None, disable_cache=None):
"""Build this bundle, meaning create the file given by the
``output`` attribute, applying the configured filters etc.
Expand All @@ -454,7 +455,8 @@ def build(self, env=None, force=None, output=None):
hunks = []
for bundle, extra_filters in self.iterbuild(env):
hunks.append(bundle._build(
env, extra_filters, force=force, output=output))
env, extra_filters, force=force, output=output,
disable_cache=disable_cache))
return hunks

def iterbuild(self, env=None):
Expand Down
16 changes: 12 additions & 4 deletions src/webassets/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def rebuild(self):
ImminentDeprecationWarning)
return self.build()

def build(self, bundles=None, output=None, directory=None):
def build(self, bundles=None, output=None, directory=None, no_cache=None):
"""Build/Rebuild assets.
``bundles``
Expand All @@ -73,6 +73,9 @@ def build(self, bundles=None, output=None, directory=None):
used. If the ``output`` of the bundles are pointing to different
directories, they will be offset by their common prefix.
Cannot be used with ``output``.
``no_cache``
If set, a cache (if one is configured) will not be used.
"""
if self.environment.debug != False:
self.log.warning(
Expand Down Expand Up @@ -151,7 +154,8 @@ def build(self, bundles=None, output=None, directory=None):

try:
if not overwrite_filename:
bundle.build(force=True, env=self.environment)
bundle.build(force=True, env=self.environment,
disable_cache=no_cache)
else:
# TODO: Rethink how we deal with container bundles here.
# As it currently stands, we write all child bundles
Expand All @@ -160,7 +164,8 @@ def build(self, bundles=None, output=None, directory=None):
# using the ``Hunk`` objects that build() would return
# anyway.
output = StringIO()
bundle.build(force=True, env=self.environment, output=output)
bundle.build(force=True, env=self.environment, output=output,
disable_cache=no_cache)
if directory:
# Only auto-create directories in this mode.
output_dir = os.path.dirname(overwrite_filename)
Expand Down Expand Up @@ -215,7 +220,7 @@ def check_for_changes():
pass

def clean(self):
""" Delete generated assets.
"""Delete generated assets.
TODO: Clean the cache?
"""
Expand Down Expand Up @@ -326,6 +331,9 @@ def make_build_parser(parser):
'basename defined by the bundle. Will offset '
'the original bundle output paths on their common '
'prefix. Cannot be used with --output.')
parser.add_argument(
'--no-cache', action='store_true',
help='Do not use a cache that might be configured.')

def run_with_argv(self, argv):
try:
Expand Down
12 changes: 11 additions & 1 deletion tests/test_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class MockBundle(Bundle):
build_called = False
on_build = None
def _build(self, *a, **kw):
self.build_called = True
self.build_called = (True, a, kw)
self.on_build(*a, **kw) if self.on_build else None


Expand Down Expand Up @@ -107,3 +107,13 @@ def test_custom_directory_not_supported_for_container_bundles(self):
self.assets_env.add(b)
assert_raises(CommandError, self.cmd_env.build,
directory=self.path('some/path'))

def test_no_cache(self):
"""Test the no_cache option."""
a = MockBundle(output='a')
self.assets_env.register('a', a)
self.cmd_env.build(no_cache=True)
assert a.build_called[2].get('disable_cache') == True

self.cmd_env.build(no_cache=False)
assert a.build_called[2].get('disable_cache') == False

0 comments on commit c9a2e7b

Please sign in to comment.